<commit_before>import math
from django.db import models
from django.utils import timezone

from mydefs import MyGenericIPAddressField
from .fields import UnixDateTimeField


class StatElem(models.Model):
    cur_time = UnixDateTimeField(primary_key=True)
    ip = MyGenericIPAddressField()
    octets = models.PositiveIntegerField(default=0)
    packets = models.PositiveIntegerField(default=0)

    @staticmethod
    def percentile(N, percent, key=lambda x:x):
        """
        Find the percentile of a list of values.
<commit_msg>добавил манагер для получения трафа по ip<commit_after>import math
from datetime import datetime, timedelta
from django.db import models
from django.utils import timezone
from mydefs import MyGenericIPAddressField
from .fields import UnixDateTimeField


class StatManager(models.Manager):

    def traffic_by_ip(self, ip):
        # ip = IPv4Address(ip)
        traf = self.filter(ip=ip, octets__gt=524288)[0]
        now = datetime.now()
        if traf.cur_time < now - timedelta(minutes=5):
            # значит трафа небыло больше 5 минут
            return False, traf
        else:
            return True, traf


class StatElem(models.Model):
    cur_time = UnixDateTimeField(primary_key=True)
    ip = MyGenericIPAddressField()
    octets = models.PositiveIntegerField(default=0)
    packets = models.PositiveIntegerField(default=0)

    objects = StatManager()

    @staticmethod
    def percentile(N, percent, key=lambda x:x):
        """
        Find the percentile of a list of values.

{'fraction_non_alphanumeric': 0.07697325505544683, 'fraction_numerical': 0.0091324200913242, 'mean_word_length': 3.5250737463126844, 'pattern_counts': {'":': 0, '<': 4, '<?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': '2841602', 'n_tokens_mistral': 467, 'n_tokens_neox': 448, 'n_words': 126}