From 9d033acffff319e4f38b98a09ff2519f3f36dc88 Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Thu, 9 Jul 2020 01:26:01 +0100 Subject: [PATCH] Separate rolling average downloads from score --- app/blueprints/packages/releases.py | 1 + app/models.py | 9 +++++++- app/tasks/pkgtasks.py | 7 ++++++- migrations/versions/dd27f1311a90_.py | 31 ++++++++++++++++++++++++++++ 4 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 migrations/versions/dd27f1311a90_.py diff --git a/app/blueprints/packages/releases.py b/app/blueprints/packages/releases.py index 81365219..12be8f65 100644 --- a/app/blueprints/packages/releases.py +++ b/app/blueprints/packages/releases.py @@ -141,6 +141,7 @@ def download_release(package, id): Package.query.filter_by(id=package.id).update({ "downloads": Package.downloads + 1, + "score_downloads": Package.score_downloads + bonus, "score": Package.score + bonus }) diff --git a/app/models.py b/app/models.py index 79b70cf2..80f6fea7 100644 --- a/app/models.py +++ b/app/models.py @@ -431,7 +431,8 @@ class Package(db.Model): approved = db.Column(db.Boolean, nullable=False, default=False) soft_deleted = db.Column(db.Boolean, nullable=False, default=False) - score = db.Column(db.Float, nullable=False, default=0) + score = db.Column(db.Float, nullable=False, default=0) + score_downloads = db.Column(db.Float, nullable=False, default=0) downloads = db.Column(db.Integer, nullable=False, default=0) review_thread_id = db.Column(db.Integer, db.ForeignKey("thread.id"), nullable=True, default=None) @@ -720,6 +721,12 @@ class Package(db.Model): if self.getMainScreenshotURL() is None: self.score *= 0.8 + self.recalcScore() + + def recalcScore(self): + self.score_downloads = self.score + + class MetaPackage(db.Model): id = db.Column(db.Integer, primary_key=True) diff --git a/app/tasks/pkgtasks.py b/app/tasks/pkgtasks.py index c864ad04..639f61e3 100644 --- a/app/tasks/pkgtasks.py +++ b/app/tasks/pkgtasks.py @@ -20,5 +20,10 @@ from app.tasks import celery @celery.task() def updatePackageScores(): - Package.query.update({ "score": Package.score * 0.95 }) + Package.query.update({ "score_downloads": Package.score_downloads * 0.95 }) + db.session.commit() + + for package in Package.query.all(): + package.recalcScore() + db.session.commit() diff --git a/migrations/versions/dd27f1311a90_.py b/migrations/versions/dd27f1311a90_.py new file mode 100644 index 00000000..19867fbf --- /dev/null +++ b/migrations/versions/dd27f1311a90_.py @@ -0,0 +1,31 @@ +"""empty message + +Revision ID: dd27f1311a90 +Revises: c141a63b2487 +Create Date: 2020-07-09 00:20:39.501355 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = 'dd27f1311a90' +down_revision = 'c141a63b2487' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.add_column('package', sa.Column('score_downloads', sa.Float(), nullable=False, server_default="0")) + op.execute(""" + UPDATE "package" SET "score_downloads"="score"; + """) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_column('package', 'score_downloads') + # ### end Alembic commands ###