<commit_before>import datetime
import json

from channels.generic.websocket import JsonWebsocketConsumer
from auth_API.helpers import get_or_create_user_information
from experiment_at.models import ATExperimentAction


class ATExperimentConsumer(JsonWebsocketConsumer):
    ##### WebSocket event handlers
    def connect(self):
        """
        Called when the websocket is handshaking as part of initial connection.
        """
        # Accept the connection
        self.accept()

    def receive_json(self, content, **kwargs):
        """
        Called when we get a text frame. Channels will JSON-decode the payload
        for us and pass it as the first argument.
        """

        # Get an updated session store
        user_info = get_or_create_user_information(self.scope['session'], self.scope['user'], 'AT')

        if hasattr(user_info, 'atexperimentcontext'):
            experiment_context = user_info.atexperimentcontext
            if content.get('msg_type') == 'add_action':
                experiment_stage = experiment_context.atexperimentstage_set.all().order_by("id")[content['stage']]
                ATExperimentAction.objects.create(atexperimentstage=experiment_stage,
                                                  action=json.dumps(content['action']),
                                                  date=datetime.datetime.utcnow())
                self.send_json({
                    'action': content['action'],
                    'date': datetime.datetime.utcnow().isoformat()
                })
            elif content.get('msg_type') == 'update_state':
                experiment_context.current_state = json.dumps(content['state'])
                experiment_context.save()
                self.send_json({
                    "state": json.loads(experiment_context.current_state)
                })

    def disconnect(self, close_code):
        user_info = get_or_create_user_information(self.scope['session'], self.scope['user'], 'AT')
        user_info.atexperimentcontext = False
        user_info.atexperimentcontext.save()<commit_msg>Logout feature.<commit_after>import datetime
import json

from channels.generic.websocket import JsonWebsocketConsumer
from auth_API.helpers import get_or_create_user_information
from experiment_at.models import ATExperimentAction


class ATExperimentConsumer(JsonWebsocketConsumer):
    ##### WebSocket event handlers
    def connect(self):
        """
        Called when the websocket is handshaking as part of initial connection.
        """
        # Accept the connection
        self.accept()

    def receive_json(self, content, **kwargs):
        """
        Called when we get a text frame. Channels will JSON-decode the payload
        for us and pass it as the first argument.
        """

        # Get an updated session store
        user_info = None
        try:
            user_info = get_or_create_user_information(self.scope['session'], self.scope['user'], 'AT')
        except:
            pass
        
        if hasattr(user_info, 'atexperimentcontext'):
            experiment_context = user_info.atexperimentcontext
            if content.get('msg_type') == 'add_action':
                experiment_stage = experiment_context.atexperimentstage_set.all().order_by("id")[content['stage']]
                ATExperimentAction.objects.create(atexperimentstage=experiment_stage,
                                                  action=json.dumps(content['action']),
                                                  date=datetime.datetime.utcnow())
                self.send_json({
                    'action': content['action'],
                    'date': datetime.datetime.utcnow().isoformat()
                })
            elif content.get('msg_type') == 'update_state':
                experiment_context.current_state = json.dumps(content['state'])
                experiment_context.save()
                self.send_json({
                    "state": json.loads(experiment_context.current_state)
                })

    def disconnect(self, close_code):
        user_info = get_or_create_user_information(self.scope['session'], self.scope['user'], 'AT')
        #user_info.atexperimentcontext.is_running = False
        #user_info.atexperimentcontext.save()
{'fraction_non_alphanumeric': 0.09560542309490416, 'fraction_numerical': 0.0, 'mean_word_length': 2.251519756838906, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2457162', 'n_tokens_mistral': 1094, 'n_tokens_neox': 1072, 'n_words': 223}