<commit_before>from app import db
import datetime


class Package(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(255), unique=True, nullable=False)
    author = db.Column(db.String(50))
    link = db.Column(db.String(140))
    description = db.Column(db.String())
    downloads = db.relationship('Downloads', backref='package', lazy='dynamic')

    def __repr__(self):
        return 'Package: %s' % self.name

    @classmethod
    def get_count(self):
        return Package.query.count()


class DbFlags(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    date = db.Column(db.DateTime, default=datetime.date.today, nullable=False)
    flag = db.Column(db.Boolean, nullable=False)

    def __repr__(self):
        return 'DbFlags: {} {}'.format(self.date, self.flag)


class Downloads(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    downloads = db.Column(db.Integer, nullable=False)
    date = db.Column(db.DateTime, default=datetime.date.today, nullable=False)
    package_id = db.Column(db.Integer, db.ForeignKey('package.id'), nullable=False)<commit_msg>methods to get downloads count<commit_after>from app import db
import datetime


class Package(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(255), unique=True, nullable=False)
    author = db.Column(db.String(50))
    link = db.Column(db.String(140))
    description = db.Column(db.String())
    downloads = db.relationship('Downloads', backref='package', lazy='dynamic')

    def __repr__(self):
        return 'Package: %s' % self.name

    @classmethod
    def get_count(self):
        return Package.query.count()


class DbFlags(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    date = db.Column(db.DateTime, default=datetime.date.today, nullable=False)
    flag = db.Column(db.Boolean, nullable=False)

    def __repr__(self):
        return 'DbFlags: {} {}'.format(self.date, self.flag)

    @classmethod
    def get_update_time(self):
        return self.query.filter(self.id == 1).first().date


class Downloads(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    downloads = db.Column(db.Integer, nullable=False)
    date = db.Column(db.DateTime, default=datetime.date.today, nullable=False)
    package_id = db.Column(db.Integer, db.ForeignKey('package.id'), nullable=False)

    @classmethod
    def nearest_last_entry(self, time):
        while self.query.filter(self.date == time).count() <= 0:
            time -= datetime.timedelta(days=1)

        return time

    @classmethod
    def __count_downloads(self, entries):
        count = 0
        for entry in entries:
            count += entry.downloads

        return count

    # period should be a datetime.timedelta
    @classmethod
    def get_downloads_count(self, period):
        current_time = DbFlags.get_update_time()
        current_entries = self.query.filter(self.date == current_time).all()
        old_time = self.nearest_last_entry(current_time - period)
        old_entries = self.query.filter(self.date == old_time).all()

        current_downloads = self.__count_downloads(current_entries)
        old_downloads = self.__count_downloads(old_entries)
        print current_downloads, old_downloads
        return current_downloads - old_downloads
{'fraction_non_alphanumeric': 0.1280359820089955, 'fraction_numerical': 0.005997001499250375, 'mean_word_length': 3.5081081081081082, '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': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2720846', 'n_tokens_mistral': 1088, 'n_tokens_neox': 1044, 'n_words': 205}