<commit_before>"""
Manage loggers for the api.
"""

import logging, logging.handlers, time
import api

from bson import json_util

from flask import request, has_request_context
from flask import logging as flask_logging

from sys import stdout
from datetime import datetime

critical_error_timeout = 600
log = logging.getLogger(__name__)

class StatsHandler(logging.StreamHandler):
    """
    Logs statistical information into the mongodb.
    """

    time_format = "%H:%M:%S %Y-%m-%d"

    action_parsers = {
        "api.user.create_user_request":
            lambda params, result=None: {
                "username": params["username"],
                "new_team": params["create-new-team"]
            },
        "api.achievement.process_achievement":
            lambda aid, data, result=None: {
                "aid": aid,
                "success": result[0]
            },
        "api.autogen.grade_problem_instance":
            lambda pid, tid, key, result=None: {
                "pid": pid,
                "key": key,
                "correct": result["correct"]
            },
        "api.game.get_game_problem":
            lambda etcid, result=None: {
                "etcid": etcid
            },
        "api.group.create_group":
            lambda uid, group_name, result=None: {
                "name": group_name,
                "owner": uid
            },
        "api.group.join_group":
            lambda tid, gid, result=None: {
                "gid": gid
            },
        "api.group.leave_group":
            lambda tid, gid, result=None: {
                "gid": gid
            },
        "api.group.delete_group":
            lambda gid, result=None: {
                "gid": gid
            },
        "api.problem.submit_key":
            lambda tid, pid, key, uid=None, ip=None, result=None: {
                "pid": pid,
                "key": key,
                "success": result["correct"]
            },
        "api.problem_feedback.add_problem_feedback":
            lambda pid, uid, feedback, result=None: {
                "pid": pid,
                "feedback": feedback
            },
        "api.user.update_password_request":
            lambda params, uid=None, check_current=False, result=None: {},
        "api.utilities.request_password_reset":
            lambda username, result=None: {},
        "api.team.create_team":
            lambda params, result=None: params,
        "api.team.assign_shell_account":
            lambda tid, result=None: {},
        "api.app.hint":
            lambda pid, source, result=None: {"pid": pid, "source": source}
    }

    def __init__(self):

        logging.StreamHandler.__init__(self)

    def emit(self, record):
        """
        Store record into the db.
        """

        information = get_request_information()

        result = record.msg

        if type(result) == dict:

            information.update({
                "event": result["name"],
                "time": datetime.now()
            })

            information["pass"] = True
            information["action"] = {}

            if "exception" in result:

                information["action"]["exception"] = result["exception"]
                information["pass"] = False

            elif result["name"] in self.action_parsers:
                action_parser = self.action_parsers[result["name"]]

                result["kwargs"]["result"] = result["result"]
                action_result = action_parser(*result["args"], **result["kwargs"])

                information["action"].update(action_result)

            api.common.get_conn().statistics.insert(information)

class ExceptionHandler(logging.StreamHandler):
    """
    Logs exceptions into mongodb.
    """

    time_format = "%H:%M:%S %Y-%m-%d"

    def __init__(self):

        logging.StreamHandler.__init__(self)

    def emit(self, record):
        """
        Store record into the db.
        """

        information = get_request_information()

        information.update({
            "event": "exception",
            "time": datetime.now().strftime(self.time_format),
            "trace": record.msg
        })

        api.common.get_conn().exceptions.insert(information)<commit_msg>Exceptions logs stored with ISODates. Closes #18<commit_after>"""
Manage loggers for the api.
"""

import logging, logging.handlers, time
import api

from bson import json_util

from flask import request, has_request_context
from flask import logging as flask_logging

from sys import stdout
from datetime import datetime

critical_error_timeout = 600
log = logging.getLogger(__name__)

class StatsHandler(logging.StreamHandler):
    """
    Logs statistical information into the mongodb.
    """

    time_format = "%H:%M:%S %Y-%m-%d"

    action_parsers = {
        "api.user.create_user_request":
            lambda params, result=None: {
                "username": params["username"],
                "new_team": params["create-new-team"]
            },
        "api.achievement.process_achievement":
            lambda aid, data, result=None: {
                "aid": aid,
                "success": result[0]
            },
        "api.autogen.grade_problem_instance":
            lambda pid, tid, key, result=None: {
                "pid": pid,
                "key": key,
                "correct": result["correct"]
            },
        "api.game.get_game_problem":
            lambda etcid, result=None: {
                "etcid": etcid
            },
        "api.group.create_group":
            lambda uid, group_name, result=None: {
                "name": group_name,
                "owner": uid
            },
        "api.group.join_group":
            lambda tid, gid, result=None: {
                "gid": gid
            },
        "api.group.leave_group":
            lambda tid, gid, result=None: {
                "gid": gid
            },
        "api.group.delete_group":
            lambda gid, result=None: {
                "gid": gid
            },
        "api.problem.submit_key":
            lambda tid, pid, key, uid=None, ip=None, result=None: {
                "pid": pid,
                "key": key,
                "success": result["correct"]
            },
        "api.problem_feedback.add_problem_feedback":
            lambda pid, uid, feedback, result=None: {
                "pid": pid,
                "feedback": feedback
            },
        "api.user.update_password_request":
            lambda params, uid=None, check_current=False, result=None: {},
        "api.utilities.request_password_reset":
            lambda username, result=None: {},
        "api.team.create_team":
            lambda params, result=None: params,
        "api.team.assign_shell_account":
            lambda tid, result=None: {},
        "api.app.hint":
            lambda pid, source, result=None: {"pid": pid, "source": source}
    }

    def __init__(self):

        logging.StreamHandler.__init__(self)

    def emit(self, record):
        """
        Store record into the db.
        """

        information = get_request_information()

        result = record.msg

        if type(result) == dict:

            information.update({
                "event": result["name"],
                "time": datetime.now()
            })

            information["pass"] = True
            information["action"] = {}

            if "exception" in result:

                information["action"]["exception"] = result["exception"]
                information["pass"] = False

            elif result["name"] in self.action_parsers:
                action_parser = self.action_parsers[result["name"]]

                result["kwargs"]["result"] = result["result"]
                action_result = action_parser(*result["args"], **result["kwargs"])

                information["action"].update(action_result)

            api.common.get_conn().statistics.insert(information)

class ExceptionHandler(logging.StreamHandler):
    """
    Logs exceptions into mongodb.
    """

    def __init__(self):

        logging.StreamHandler.__init__(self)

    def emit(self, record):
        """
        Store record into the db.
        """

        information = get_request_information()

        information.update({
            "event": "exception",
            "time": datetime.now(),
            "trace": record.msg
        })

        api.common.get_conn().exceptions.insert(information)
{'fraction_non_alphanumeric': 0.12409795338932923, 'fraction_numerical': 0.001183011948420679, 'mean_word_length': 1.7745323268788973, 'pattern_counts': {'":': 80, '<': 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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3900875', 'n_tokens_mistral': 2346, 'n_tokens_neox': 2211, 'n_words': 490}