<commit_before>from .models import *
from googlecharts.collections import Chart
from doomstats.timestuff import daterange_resolution
from datetime import datetime


def general_table():
    batch_count = RefreshBatch.objects.count()
    return Table(
        id="general-table",
        header="All-time stats",
        rows=[
            ("Data collected since:", DateCell(
                _first_batch().date, "%Y-%m-%d %H:%M:%S")),
            ("Collected batches:", batch_count),
            ("Average all-time server count:",
             DecimalCell(
                 Server.objects.count() / float(max(1, batch_count)))),
            ("Average all-time player count:",
             DecimalCell(
                 Player.objects.filter(is_bot=False).count() \
                 / float(max(1, batch_count))))
        ])


def stats_daterange_table(daterange, engine=None):
    batch_count = RefreshBatch.objects.filter(date__range=daterange).count()
    return Table(
        id="stats-daterange-table",
        header="Amounts in date range",
        rows=[
            ("Collected batches:", batch_count),
            ("Average server count:",
             DecimalCell(
                 Server.count_in_daterange(engine, daterange) / \
                 float(max(1, batch_count)))),
            ("Average player count:",
             DecimalCell(
                 Player.count_in_daterange(engine, daterange) / \
                 float(max(1, batch_count))))
        ])


def players_chart(daterange, engine=None):
    resolution = daterange_resolution(daterange)
    if resolution == "day":
        dateformat = "%Y-%m-%d %a"
    elif resolution == "hour":
        dateformat = "%d %a %H:%M"
    dateslices = RefreshBatch.slice(daterange, resolution)
    rows = []
    for dateslice in dateslices:
        batch_filter = {
            "date__year": dateslice.year,
            "date__month": dateslice.month,
            "date__day": dateslice.day
        }
        if resolution == "hour":
            batch_filter["date__hour"] = dateslice.hour
        batches = RefreshBatch.objects.filter(**batch_filter)
        filter = {
            "server__server__refresh_batch__in": batches,
            "is_bot": False
        }
        if engine:
            filter["server__server__engine"] = engine
        count = Player.objects.filter(**filter).count()
        rows.append((dateslice.strftime(dateformat),
                     count / float(max(1, batches.count()))))
    return Chart(
        id="players-chart", kind="LineChart",
        options={'title': 'Players per {0}'.format(resolution),
                 'width': "100%",
                 'height': 300,
                 'legend': 'none'},
        columns=[('string', 'Day'), ('number', 'Players')],
        rows=rows)


def _first_batch():
    return RefreshBatch.objects.order_by("date").first() or \
        RefreshBatch(date=datetime.fromtimestamp(0))


class Table(object):
    def __init__(self, id=None, header=None, rows=None):
        self.id = id
        self.header = header
        self.rows = rows
        if not rows:
            self.rows = []

    @property
    def typeof(self):
        return self.__class__.__name__

    @property
    def num_cols(self):
        if self.rows:
            return len(self.rows[0])
        return 0


class DateCell(object):
    def __init__(self, date, format):
        self.date = date
        self.format = format

    def __str__(self):
        return self.date.strftime(self.format)


class DecimalCell(object):
    def __init__(self, number, digits=2):
        self.number = number
        self.digits = digits

    def __str__(self):
        return str("{0:." + str(self.digits) + "f}").format(self.number)
<commit_msg>Actual UTC time for chart's date slices.<commit_after>from .models import *
from googlecharts.collections import Chart
from doomstats.timestuff import daterange_resolution
from datetime import datetime


def general_table():
    batch_count = RefreshBatch.objects.count()
    return Table(
        id="general-table",
        header="All-time stats",
        rows=[
            ("Data collected since:", DateCell(
                _first_batch().date, "%Y-%m-%d %H:%M:%S")),
            ("Collected batches:", batch_count),
            ("Average all-time server count:",
             DecimalCell(
                 Server.objects.count() / float(max(1, batch_count)))),
            ("Average all-time player count:",
             DecimalCell(
                 Player.objects.filter(is_bot=False).count() \
                 / float(max(1, batch_count))))
        ])


def stats_daterange_table(daterange, engine=None):
    batch_count = RefreshBatch.objects.filter(date__range=daterange).count()
    return Table(
        id="stats-daterange-table",
        header="Amounts in date range",
        rows=[
            ("Collected batches:", batch_count),
            ("Average server count:",
             DecimalCell(
                 Server.count_in_daterange(engine, daterange) / \
                 float(max(1, batch_count)))),
            ("Average player count:",
             DecimalCell(
                 Player.count_in_daterange(engine, daterange) / \
                 float(max(1, batch_count))))
        ])


def players_chart(daterange, engine=None):
    resolution = daterange_resolution(daterange)
    if resolution == "day":
        dateformat = "%Y-%m-%d %a"
    elif resolution == "hour":
        dateformat = "%d %a %H:%M"
    dateslices = RefreshBatch.slice(daterange, resolution)
    rows = []
    for dateslice in dateslices:
        dateslice = timezone.localtime(dateslice, timezone.utc)
        batch_filter = {
            "date__year": dateslice.year,
            "date__month": dateslice.month,
            "date__day": dateslice.day
        }
        if resolution == "hour":
            batch_filter["date__hour"] = dateslice.hour
        batches = RefreshBatch.objects.filter(**batch_filter)
        filter = {
            "server__server__refresh_batch__in": batches,
            "is_bot": False
        }
        if engine:
            filter["server__server__engine"] = engine
        count = Player.objects.filter(**filter).count()
        rows.append((dateslice.strftime(dateformat),
                     count / float(max(1, batches.count()))))
    return Chart(
        id="players-chart", kind="LineChart",
        options={'title': 'Players per {0}'.format(resolution),
                 'width': "100%",
                 'height': 300,
                 'legend': 'none'},
        columns=[('string', 'Day'), ('number', 'Players')],
        rows=rows)


def _first_batch():
    return RefreshBatch.objects.order_by("date").first() or \
        RefreshBatch(date=datetime.fromtimestamp(0))


class Table(object):
    def __init__(self, id=None, header=None, rows=None):
        self.id = id
        self.header = header
        self.rows = rows
        if not rows:
            self.rows = []

    @property
    def typeof(self):
        return self.__class__.__name__

    @property
    def num_cols(self):
        if self.rows:
            return len(self.rows[0])
        return 0


class DateCell(object):
    def __init__(self, date, format):
        self.date = date
        self.format = format

    def __str__(self):
        return self.date.strftime(self.format)


class DecimalCell(object):
    def __init__(self, number, digits=2):
        self.number = number
        self.digits = digits

    def __str__(self):
        return str("{0:." + str(self.digits) + "f}").format(self.number)

{'fraction_non_alphanumeric': 0.12778877887788778, 'fraction_numerical': 0.004488448844884489, 'mean_word_length': 2.154038301415487, 'pattern_counts': {'":': 16, '<': 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': '1162879', 'n_tokens_mistral': 2213, 'n_tokens_neox': 2138, 'n_words': 457}