<commit_before># -*- coding: utf-8 -*-
from __future__ import unicode_literals

from datetime import date, timedelta

from udata.models import db
from . import metric_catalog


__all__ = ('Metrics', 'WithMetrics')


class MetricsQuerySet(db.BaseQuerySet):
    def _today(self):
        return date.today()

    def get_for(self, obj, days=1):
        object_id = obj.id if hasattr(obj, 'id') else obj
        days = max(days or 1, 1)
        first_day = (date.today() - timedelta(days)).isoformat()
        qs = self(object_id=object_id, level='daily', date__gt=first_day)
        return qs.order_by('-date')

    def last_for(self, obj):
        return self.get_for(obj=obj).first()

    def update_daily(self, obj, date=None, **kwargs):
        oid = obj.id if hasattr(obj, 'id') else obj
        if isinstance(date, basestring):
            day = date
        else:
            day = (date or self._today()).isoformat()
        commands = dict(('set__values__{0}'.format(k), v)
                        for k, v in kwargs.items())
        return (Metrics.objects(object_id=oid, level='daily', date=day)
                       .update_one(upsert=True, **commands))


class WithMetrics(object):
    metrics = db.DictField()

    def clean(self):
        '''Fill metrics with defaults on create'''
        if not self.metrics:
            self.metrics = dict(
                (name, spec.default)
                for name, spec in (metric_catalog.get(self.__class__, {})
                                                 .items()))
        return super(WithMetrics, self).clean()


class Metrics(db.Document):
    object_id = db.DynamicField(required=True)
    date = db.StringField(required=True)
    level = db.StringField(<commit_msg>Prevent storing metrics with object_id=None<commit_after># -*- coding: utf-8 -*-
from __future__ import unicode_literals

from datetime import date, timedelta

from udata.models import db
from . import metric_catalog


__all__ = ('Metrics', 'WithMetrics')


class MetricsQuerySet(db.BaseQuerySet):
    def _today(self):
        return date.today()

    def get_for(self, obj, days=1):
        object_id = obj.id if hasattr(obj, 'id') else obj
        days = max(days or 1, 1)
        first_day = (date.today() - timedelta(days)).isoformat()
        qs = self(object_id=object_id, level='daily', date__gt=first_day)
        return qs.order_by('-date')

    def last_for(self, obj):
        return self.get_for(obj=obj).first()

    def update_daily(self, obj, date=None, **kwargs):
        oid = obj.id if hasattr(obj, 'id') else obj
        if not oid:
            raise ValueError('Unable to get identifier for {0}'.format(obj))
        if isinstance(date, basestring):
            day = date
        else:
            day = (date or self._today()).isoformat()
        commands = dict(('set__values__{0}'.format(k), v)
                        for k, v in kwargs.items())
        return (Metrics.objects(object_id=oid, level='daily', date=day)
                       .update_one(upsert=True, **commands))


class WithMetrics(object):
    metrics = db.DictField()

    def clean(self):
        '''Fill metrics with defaults on create'''
        if not self.metrics:
            self.metrics = dict(
                (name, spec.default)
                for name, spec in (metric_catalog.get(self.__class__, {})
                                                 .items()))
        return super(WithMetrics, self).clean()


class Metrics(db.Document):
    object_id = db.DynamicField(required=True, null=False, unique_with='date')
    date = db.StringField(required=True)
    level = db.StringField(
{'fraction_non_alphanumeric': 0.12482777624689997, 'fraction_numerical': 0.003031138054560485, 'mean_word_length': 2.3798882681564244, 'pattern_counts': {'":': 0, '<': 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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6932912', 'n_tokens_mistral': 1108, 'n_tokens_neox': 1058, 'n_words': 275}