<commit_before>"""Queue item for basic analysis by irwin"""
from default_imports import *

from modules.auth.Auth import AuthID
from modules.game.Game import PlayerID, GameID
from modules.queue.Origin import Origin, OriginReport, OriginModerator, OriginRandom, maxOrigin

from datetime import datetime, timedelta

import pymongo
from pymongo.collection import Collection

import numpy as np
from math import ceil

EngineQueueID = NewType('EngineQueueID', str)
Precedence = NewType('Precedence', int)

class EngineQueue(NamedTuple('EngineQueue', [
        ('id', EngineQueueID), # same as player ID
        ('origin', Origin),
        ('requiredGameIds', List[GameID]), # games that must be analysed
        ('precedence', Precedence),
        ('completed', bool),
        ('owner', AuthID),
        ('date', datetime)
    ])):
    @staticmethod
    def new(playerId: PlayerID, origin: Origin, gamePredictions):
        if len(gamePredictions) > 0:
            activations = sorted([(a**2) for a in gamePredictions], reverse=True)
            top30avg = ceil(np.average(activations[:ceil(0.3*len(activations))]))
        else:
            top30avg = 0
        
        # set the precedence to the top30avg
        precedence = top30avg

        # then modify it depending on where it came from
        if origin == OriginReport:
            precedence += 5000
        elif origin == OriginModerator:
            precedence = 100000

        return EngineQueue(
            id=playerId,
            origin=origin,
            requiredGameIds=[], # we'll add this as it becomes available
            precedence=precedence,
            owner=None,
            completed=False,
            date=datetime.now())

    def complete(self):
        return EngineQueue(
            id=self.id,
            origin=self.origin,
            requiredGameIds=self.requiredGameIds,
            precedence=self.precedence,
            completed=True,
            owner=self.owner,
            date=self.date)

    @staticmethod
    def merge(engineQueueA, engineQueueB):
        return EngineQueue(
            id=engineQueueA.id,
            origin=maxOrigin(engineQueueA.origin, engineQueueB.origin),
            requiredGameIds=engineQueueA.requiredGameIds + engineQueueB.requiredGameIds,
            precedence=max(engineQueueA.precedence, engineQueueB.precedence),
            completed=min(engineQueueA.completed, engineQueueB.completed),
            owner=engineQueueA.owner if engineQueueA.owner is not None else (engineQueueB.owner if engineQueueB.owner is not None else None),
            date=min(engineQueueA.date, engineQueueB.date)) # retain the oldest datetime so the sorting doesn't mess up

class EngineQueueBSONHandler:
    @staticmethod
    def reads(bson: Dict) -> EngineQueue:
        return EngineQueue(
            id=bson['_id'],
            origin=bson['origin'],
            precedence=bson['precedence'],
            requiredGameIds=bson.get('requiredGameIds', []),
            completed=bson.get('complete', False),
            owner=bson.get('owner'),<commit_msg>if either queue item is completed, return the other<commit_after>"""Queue item for basic analysis by irwin"""
from default_imports import *

from modules.auth.Auth import AuthID
from modules.game.Game import PlayerID, GameID
from modules.queue.Origin import Origin, OriginReport, OriginModerator, OriginRandom, maxOrigin

from datetime import datetime, timedelta

import pymongo
from pymongo.collection import Collection

import numpy as np
from math import ceil

EngineQueueID = NewType('EngineQueueID', str)
Precedence = NewType('Precedence', int)

class EngineQueue(NamedTuple('EngineQueue', [
        ('id', EngineQueueID), # same as player ID
        ('origin', Origin),
        ('requiredGameIds', List[GameID]), # games that must be analysed
        ('precedence', Precedence),
        ('completed', bool),
        ('owner', AuthID),
        ('date', datetime)
    ])):
    @staticmethod
    def new(playerId: PlayerID, origin: Origin, gamePredictions):
        if len(gamePredictions) > 0:
            activations = sorted([(a**2) for a in gamePredictions], reverse=True)
            top30avg = ceil(np.average(activations[:ceil(0.3*len(activations))]))
        else:
            top30avg = 0
        
        # set the precedence to the top30avg
        precedence = top30avg

        # then modify it depending on where it came from
        if origin == OriginReport:
            precedence += 5000
        elif origin == OriginModerator:
            precedence = 100000

        return EngineQueue(
            id=playerId,
            origin=origin,
            requiredGameIds=[], # we'll add this as it becomes available
            precedence=precedence,
            owner=None,
            completed=False,
            date=datetime.now())

    def complete(self):
        return EngineQueue(
            id=self.id,
            origin=self.origin,
            requiredGameIds=self.requiredGameIds,
            precedence=self.precedence,
            completed=True,
            owner=self.owner,
            date=self.date)

    @staticmethod
    def merge(engineQueueA, engineQueueB):
        if engineQueueA.completed:
            return engineQueueB
        elif engineQueueB.completed:
            return engineQueueA
        return EngineQueue(
            id=engineQueueA.id,
            origin=maxOrigin(engineQueueA.origin, engineQueueB.origin),
            requiredGameIds=engineQueueA.requiredGameIds + engineQueueB.requiredGameIds,
            precedence=max(engineQueueA.precedence, engineQueueB.precedence),
            completed=min(engineQueueA.completed, engineQueueB.completed),
            owner=engineQueueA.owner if engineQueueA.owner is not None else (engineQueueB.owner if engineQueueB.owner is not None else None),
            date=min(engineQueueA.date, engineQueueB.date)) # retain the oldest datetime so the sorting doesn't mess up

class EngineQueueBSONHandler:
    @staticmethod
    def reads(bson: Dict) -> EngineQueue:
        return EngineQueue(
            id=bson['_id'],
            origin=bson['origin'],
            precedence=bson['precedence'],
            requiredGameIds=bson.get('requiredGameIds', []),
            completed=bson.get('complete', False),
            owner=bson.get('owner'),
{'fraction_non_alphanumeric': 0.09316671939259728, 'fraction_numerical': 0.007276178424549193, 'mean_word_length': 2.602849002849003, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '1755079', 'n_tokens_mistral': 1734, 'n_tokens_neox': 1655, 'n_words': 437}