<commit_before>from django.contrib.auth.models import User
from django.db import models
from tastypie.models import create_api_key
from django.http import QueryDict
from datetime import datetime
from dateutil.relativedelta import relativedelta
from tastypie.utils import dict_strip_unicode_keys

models.signals.post_save.connect(create_api_key, sender=User)

SEVERITY_CHOICES = (
                    (0, 'debug'),
                    (1, 'info'),
                    (2, 'notify'),
                    (3, 'warning'),
                    (4, 'error'),
                    (5, 'critical'),
                    (6, 'alert'),
                    (7, 'emergency')
                   )

class LogMessage(models.Model):
    """
    Model of a log message
    """

    severity = models.IntegerField(choices=SEVERITY_CHOICES)
    datetime = models.DateTimeField()
    add_datetime = models.DateTimeField(auto_now_add=True)
    server_ip = models.GenericIPAddressField()
    application = models.ForeignKey(User)
    short_desc = models.CharField(max_length=100)
    long_desc = models.TextField()

    def __unicode__(self):
        return self.short_desc

    def pretty_severity(self):
        """
        Return the string name of this log message severity.
        """
        return SEVERITY_CHOICES[self.severity][1]

    class Meta:
        ordering = ('-datetime',)



class Filter(models.Model):
    """
    Model of a search filter. This is used in expectations and labels.
    """

    class Meta:
        abstract = True

    query_string = models.TextField()

    def __unicode__(self):
        return self.query_string

    def get_dict(self):
        """
        Creates a ``QueryDict`` from the querystring of this label.
        """
        return QueryDict(self.query_string)

class Label(Filter):
    """
    Model for labels.
    """
    user = models.ForeignKey(User)
    label_name = models.CharField(max_length=20,unique=True)

    def __unicode__(self):
        return self.label_name


class RelativedeltaField(models.Field):

    description = "A relative timedelta"

    __metaclass__ = models.SubfieldBase

    def db_type(self, connection=None):
        """
        Current representation of RelativedeltaField in the database.
        """
        return 'char(30)'

    def to_python(self, value):
        """
        Creates a relativedelta object from what we get from the database.
        """
        if not value:
            return None
        if isinstance(value, (str, unicode)):
            #raises ValueError if split not possible.
            years, months, days, hours, minutes, seconds = [int(v)
                                                            for v
                                                            in value.split("_")]
            return relativedelta(years   = years,
                                 months  = months,
                                 days    = days,
                                 hours   = hours,
                                 minutes = minutes,
                                 seconds = seconds)

        elif isinstance(value, relativedelta):
            return value
        else:
            return None

    def get_db_prep_value(self, value, connection=None, prepared=False):
        """
        Concatenate unit and timedelta into a string.
        """
        return "%i_%i_%i_%i_%i_%i" % (value.years,
                                   value.months,
                                   value.days,
                                   value.hours,
                                   value.minutes,
                                   value.seconds)

    def formfield(self, **kwargs):
        """
        Using a RelativedeltaField to represent this field in a form.
        """
        from monlog.log.forms import RelativedeltaField as RelativedeltaFormField
        defaults = { 'form_class' : RelativedeltaFormField }
        defaults.update(kwargs)
        return super(RelativedeltaField, self).formfield(**defaults)

    def value_to_string(self, obj):
        value = self._get_val_from_obj(obj)
        return self.get_db_prep_value(value)


class Expectation(Filter):
    """
    Model for expectations.
    """

    expectation_name = models.CharField(max_length=20, unique=True)
    user             = models.ForeignKey(User)

    # timestamp for original deadline
    deadline          = models.DateTimeField()
    original_deadline = models.DateTimeField()


    # +- tolerance in relative delta
    # example: '+- 10 minute'
    tolerance = RelativedeltaField()

    # repeat every ``repeat`` relative delta
    # example: 'every 2 month'
    repeat       = RelativedeltaField()
    repeat_count = models.IntegerField()

    least_amount_of_results = models.IntegerField()

    def __unicode__(self):
        return self.expectation_name

    def apply_tolerance(self, querydict):
        """
        Applies startdate and enddate to a querydict.

        Startdate = Current Deadline - Tolerance
        Enddate   = Current Deadline + Tolerance
        """
        if self.deadline is None:
            print ("Error: Deadline must've been set before "<commit_msg>define unique entries in database differently<commit_after>from django.contrib.auth.models import User
from django.db import models
from tastypie.models import create_api_key
from django.http import QueryDict
from datetime import datetime
from dateutil.relativedelta import relativedelta
from tastypie.utils import dict_strip_unicode_keys

models.signals.post_save.connect(create_api_key, sender=User)

SEVERITY_CHOICES = (
                    (0, 'debug'),
                    (1, 'info'),
                    (2, 'notify'),
                    (3, 'warning'),
                    (4, 'error'),
                    (5, 'critical'),
                    (6, 'alert'),
                    (7, 'emergency')
                   )

class LogMessage(models.Model):
    """
    Model of a log message
    """

    severity = models.IntegerField(choices=SEVERITY_CHOICES)
    datetime = models.DateTimeField()
    add_datetime = models.DateTimeField(auto_now_add=True)
    server_ip = models.GenericIPAddressField()
    application = models.ForeignKey(User)
    short_desc = models.CharField(max_length=100)
    long_desc = models.TextField()

    def __unicode__(self):
        return self.short_desc

    def pretty_severity(self):
        """
        Return the string name of this log message severity.
        """
        return SEVERITY_CHOICES[self.severity][1]

    class Meta:
        ordering = ('-datetime',)



class Filter(models.Model):
    """
    Model of a search filter. This is used in expectations and labels.
    """

    class Meta:
        abstract = True

    query_string = models.TextField()

    def __unicode__(self):
        return self.query_string

    def get_dict(self):
        """
        Creates a ``QueryDict`` from the querystring of this label.
        """
        return QueryDict(self.query_string)

class Label(Filter):
    """
    Model for labels.
    """
    user = models.ForeignKey(User)
    label_name = models.CharField(max_length=20)

    def __unicode__(self):
        return self.label_name

    class Meta:
        unique_together = ('user','label_name')


class RelativedeltaField(models.Field):

    description = "A relative timedelta"

    __metaclass__ = models.SubfieldBase

    def db_type(self, connection=None):
        """
        Current representation of RelativedeltaField in the database.
        """
        return 'char(30)'

    def to_python(self, value):
        """
        Creates a relativedelta object from what we get from the database.
        """
        if not value:
            return None
        if isinstance(value, (str, unicode)):
            #raises ValueError if split not possible.
            years, months, days, hours, minutes, seconds = [int(v)
                                                            for v
                                                            in value.split("_")]
            return relativedelta(years   = years,
                                 months  = months,
                                 days    = days,
                                 hours   = hours,
                                 minutes = minutes,
                                 seconds = seconds)

        elif isinstance(value, relativedelta):
            return value
        else:
            return None

    def get_db_prep_value(self, value, connection=None, prepared=False):
        """
        Concatenate unit and timedelta into a string.
        """
        return "%i_%i_%i_%i_%i_%i" % (value.years,
                                   value.months,
                                   value.days,
                                   value.hours,
                                   value.minutes,
                                   value.seconds)

    def formfield(self, **kwargs):
        """
        Using a RelativedeltaField to represent this field in a form.
        """
        from monlog.log.forms import RelativedeltaField as RelativedeltaFormField
        defaults = { 'form_class' : RelativedeltaFormField }
        defaults.update(kwargs)
        return super(RelativedeltaField, self).formfield(**defaults)

    def value_to_string(self, obj):
        value = self._get_val_from_obj(obj)
        return self.get_db_prep_value(value)


class Expectation(Filter):
    """
    Model for expectations.
    """

    expectation_name = models.CharField(max_length=20)
    user             = models.ForeignKey(User)

    # timestamp for original deadline
    deadline          = models.DateTimeField()
    original_deadline = models.DateTimeField()


    # +- tolerance in relative delta
    # example: '+- 10 minute'
    tolerance = RelativedeltaField()

    # repeat every ``repeat`` relative delta
    # example: 'every 2 month'
    repeat       = RelativedeltaField()
    repeat_count = models.IntegerField()

    least_amount_of_results = models.IntegerField()

    class Meta:
        unique_together = ('expectation_name', 'user')

    def __unicode__(self):
        return self.expectation_name

    def apply_tolerance(self, querydict):
        """
        Applies startdate and enddate to a querydict.

        Startdate = Current Deadline - Tolerance
        Enddate   = Current Deadline + Tolerance
        """
        if self.deadline is None:
            print ("Error: Deadline must've been set before "
{'fraction_non_alphanumeric': 0.08655862726406101, 'fraction_numerical': 0.004003813155386082, 'mean_word_length': 1.8201612903225806, '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': 30, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3352910', 'n_tokens_mistral': 2837, 'n_tokens_neox': 2684, 'n_words': 709}