<commit_before>"""
Note: we're using the model_utils TimeStampedModel to record the date the record in the
*local* DB was created/modified, not the one in Phabricator's primary DB.

Where we also store created/modified fields corresponding to data in the Phabricator DB,
we give those fields different names (e.g. "date_opened" and "date_updated" for PullRequest)
"""

from django.db import models
from dj_phab.queryset import DateGroupingQuerySet
from model_utils import Choices
from model_utils.models import TimeStampedModel


class PhabModel(models.Model):
    phid = models.CharField(max_length=30, unique=True)

    class Meta:
        abstract = True


class PhabUser(TimeStampedModel, PhabModel):
    user_name = models.CharField(max_length=255, unique=True)
    real_name = models.CharField(max_length=255)

    class Meta:
        ordering = ['real_name',]


class Project(TimeStampedModel, PhabModel):
    phab_id = models.IntegerField()
    name = models.CharField(max_length=255)

    class Meta:
        ordering = ['name',]


class Repository(TimeStampedModel, PhabModel):
    phab_id = models.IntegerField()
    name = models.CharField(max_length=255)
    callsign = models.CharField(max_length=16, help_text=u"Abbreviated, unique name")
    monogram = models.CharField(max_length=16, help_text=u"Version of callsign used for references in markup")
    uri = models.URLField(help_text=u"URL to access on Phabricator")
    remote_uri = models.CharField(max_length=255, help_text=u"URI of origin repo")
    is_active = models.BooleanField(default=True)

    class Meta:
        verbose_name_plural = u"Repositories"
        ordering=['callsign',]


class PullRequestBaseManager(models.Manager):
    # @TODO:
    # Frequency by granularity
    # Average size by granularity
    pass


class PullRequest(TimeStampedModel, PhabModel):
    STATUS = Choices(
        (0, 'needs_review', u"Needs Review"),
        (1, 'needs_revision', u"Needs Revision"),
        (2, 'accepted', u"Accepted"),
        (3, 'closed', u"Closed"),
        (4, 'abandoned', u"Abandoned"),
        (5, 'changes_planned', u"Changes Planned"),
        (0, 'in_preparation', u"In Preparation"),
    )

    phab_id = models.IntegerField()
    repository = models.ForeignKey(Repository, null=True, blank=True)
    title = models.CharField(max_length=255)
    author = models.ForeignKey(PhabUser)
    reviewers = models.ManyToManyField(PhabUser, related_name='reviews')
    line_count = models.PositiveIntegerField()
    status = models.SmallIntegerField(choices=STATUS)
    uri = models.URLField()
    diff_count = models.PositiveSmallIntegerField()
    commit_count = models.PositiveSmallIntegerField()
    date_opened = models.DateTimeField()
    date_updated = models.DateTimeField()

    objects = PullRequestBaseManager.from_queryset(DateGroupingQuerySet)()

    def d_id(self):
        return u"D%s" % self.phab_id

    def abbrev_title(self):
        return self.title[:32]

    class Meta:
        ordering = ['date_opened',]


class LastImportTracker(models.Model):
    """
    This is a singleton -- only one row permitted.
    """
    last_import_time = models.DateTimeField()
<commit_msg>Query shortcut on manager for simplest data case<commit_after>"""
Note: we're using the model_utils TimeStampedModel to record the date the record in the
*local* DB was created/modified, not the one in Phabricator's primary DB.

Where we also store created/modified fields corresponding to data in the Phabricator DB,
we give those fields different names (e.g. "date_opened" and "date_updated" for PullRequest)
"""

from django.db import models
from dj_phab.queryset import DateGroupingQuerySet
from model_utils import Choices
from model_utils.models import TimeStampedModel


class PhabModel(models.Model):
    phid = models.CharField(max_length=30, unique=True)

    class Meta:
        abstract = True


class PhabUser(TimeStampedModel, PhabModel):
    user_name = models.CharField(max_length=255, unique=True)
    real_name = models.CharField(max_length=255)

    class Meta:
        ordering = ['real_name',]


class Project(TimeStampedModel, PhabModel):
    phab_id = models.IntegerField()
    name = models.CharField(max_length=255)

    class Meta:
        ordering = ['name',]


class Repository(TimeStampedModel, PhabModel):
    phab_id = models.IntegerField()
    name = models.CharField(max_length=255)
    callsign = models.CharField(max_length=16, help_text=u"Abbreviated, unique name")
    monogram = models.CharField(max_length=16, help_text=u"Version of callsign used for references in markup")
    uri = models.URLField(help_text=u"URL to access on Phabricator")
    remote_uri = models.CharField(max_length=255, help_text=u"URI of origin repo")
    is_active = models.BooleanField(default=True)

    class Meta:
        verbose_name_plural = u"Repositories"
        ordering=['callsign',]


class PullRequestQuerySet(DateGroupingQuerySet):
    def size_and_frequency_by_granularity(self, granularity):
        return self.filter(status=PullRequest.STATUS.closed)\
                   .group_by_date('date_opened', granularity)\
                   .annotate(**{
                        'avg_lines': models.Avg('line_count'),
                        'review_count': models.Count('id'),
                    })


class PullRequestBaseManager(models.Manager):
    pass


class PullRequest(TimeStampedModel, PhabModel):
    STATUS = Choices(
        (0, 'needs_review', u"Needs Review"),
        (1, 'needs_revision', u"Needs Revision"),
        (2, 'accepted', u"Accepted"),
        (3, 'closed', u"Closed"),
        (4, 'abandoned', u"Abandoned"),
        (5, 'changes_planned', u"Changes Planned"),
        (0, 'in_preparation', u"In Preparation"),
    )

    phab_id = models.IntegerField()
    repository = models.ForeignKey(Repository, null=True, blank=True)
    title = models.CharField(max_length=255)
    author = models.ForeignKey(PhabUser)
    reviewers = models.ManyToManyField(PhabUser, related_name='reviews')
    line_count = models.PositiveIntegerField()
    status = models.SmallIntegerField(choices=STATUS)
    uri = models.URLField()
    diff_count = models.PositiveSmallIntegerField()
    commit_count = models.PositiveSmallIntegerField()
    date_opened = models.DateTimeField()
    date_updated = models.DateTimeField()

    objects = PullRequestBaseManager.from_queryset(PullRequestQuerySet)()

    def d_id(self):
        return u"D%s" % self.phab_id

    def abbrev_title(self):
        return self.title[:32]

    class Meta:
        ordering = ['-date_opened',]


class LastImportTracker(models.Model):
    """
    This is a singleton -- only one row permitted.
    """
    last_import_time = models.DateTimeField()

{'fraction_non_alphanumeric': 0.10451306413301663, 'fraction_numerical': 0.009798099762470308, 'mean_word_length': 3.8397988505747125, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '574473', 'n_tokens_mistral': 2155, 'n_tokens_neox': 2014, 'n_words': 483}