]> git.lizzy.rs Git - cheatdb.git/commitdiff
Improve package scoring
authorrubenwardy <rw@rubenwardy.com>
Thu, 21 Nov 2019 22:16:35 +0000 (22:16 +0000)
committerGitHub <noreply@github.com>
Thu, 21 Nov 2019 22:16:35 +0000 (22:16 +0000)
app/blueprints/admin/admin.py
app/blueprints/packages/releases.py
app/models.py
app/tasks/__init__.py
app/tasks/forumtasks.py
app/tasks/pkgtasks.py [new file with mode: 0644]

index 2a2bace74441f9d762b975da09b5887bdb6e07da..c3591f533710a5669b30358c04cf5ba0d7193471 100644 (file)
@@ -75,7 +75,7 @@ def admin_page():
                        return redirect(url_for("admin.admin_page"))
                elif action == "recalcscores":
                        for p in Package.query.all():
-                               p.recalcScore()
+                               p.setStartScore()
 
                        db.session.commit()
                        return redirect(url_for("admin.admin_page"))
index 60a54bddf62299690ce7769717361bf1bb838538..a7f161e8aee7ae0653bf5faa97674832441b3f37 100644 (file)
@@ -130,9 +130,18 @@ def download_release(package, id):
                if not has_key(key):
                        set_key(key, "true")
 
+                       bonus = 1
+                       if not package.getIsFOSS():
+                               bonus *= 0.1
+
                        PackageRelease.query.filter_by(id=release.id).update({
                                        "downloads": PackageRelease.downloads + 1
                                })
+
+                       Package.query.filter_by(id=package.id).update({
+                                       "score": Package.score + bonus
+                               })
+
                        db.session.commit()
 
        return redirect(release.url, code=300)
index 3632ebcd921e886f67abe39e94b4c4979c19b56e..9a80873ecc5a025fe60e20b971941dada7106fe3 100644 (file)
@@ -24,6 +24,7 @@ from flask import Flask, url_for
 from flask_sqlalchemy import SQLAlchemy, BaseQuery
 from flask_migrate import Migrate
 from flask_user import login_required, UserManager, UserMixin, SQLAlchemyAdapter
+from sqlalchemy import func
 from sqlalchemy.orm import validates
 from sqlalchemy_searchable import SearchQueryMixin
 from sqlalchemy_utils.types import TSVectorType
@@ -425,6 +426,9 @@ class Package(db.Model):
                for e in PackagePropertyKey:
                        setattr(self, e.name, getattr(package, e.name))
 
+       def getIsFOSS(self):
+               return self.license.is_foss and self.media_license.is_foss
+
        def getState(self):
                if self.approved:
                        return "approved"
@@ -602,16 +606,20 @@ class Package(db.Model):
                else:
                        raise Exception("Permission {} is not related to packages".format(perm.name))
 
-       def recalcScore(self):
-               self.score = 10
+       def setStartScore(self):
+               downloads = db.session.query(func.sum(PackageRelease.downloads)). \
+                               filter(PackageRelease.package_id == self.id).scalar() or 0
+
+               forum_score = 0
+               forum_bonus = 0
+               topic = self.forums and ForumTopic.query.get(self.forums)
+               if topic:
+                       months = (datetime.datetime.now() - topic.created_at).days / 30
+                       years  = months / 12
+                       forum_score = topic.views / max(years, 0.0416) + 80*min(max(months, 0.5), 6)
+                       forum_bonus = topic.views + topic.posts
 
-               if self.forums is not None:
-                       topic = ForumTopic.query.get(self.forums)
-                       if topic:
-                               days   = (datetime.datetime.now() - topic.created_at).days
-                               months = days / 30
-                               years  = days / 365
-                               self.score = topic.views / max(years, 0.0416) + 80*min(max(months, 0.5), 6)
+               self.score = max(downloads, forum_score * 0.6) + forum_bonus
 
                if self.getMainScreenshotURL() is None:
                        self.score *= 0.8
@@ -619,6 +627,7 @@ class Package(db.Model):
                if not self.license.is_foss or not self.media_license.is_foss:
                        self.score *= 0.1
 
+
 class MetaPackage(db.Model):
        id           = db.Column(db.Integer, primary_key=True)
        name         = db.Column(db.String(100), unique=True, nullable=False)
index fb62cb08398af0461f081c867facba315cd2d785..1f3df02b2aeb3bf16da7e84a660c79a4d1181f48 100644 (file)
@@ -69,6 +69,10 @@ CELERYBEAT_SCHEDULE = {
        'topic_list_import': {
                'task': 'app.tasks.forumtasks.importTopicList',
                'schedule': crontab(minute=1, hour=1),
+       },
+       'package_score_update': {
+               'task': 'app.tasks.pkgtasks.updatePackageScores',
+               'schedule': crontab(minute=10, hour=1),
        }
 }
 celery.conf.beat_schedule = CELERYBEAT_SCHEDULE
index 9b200fc1958ad50150890ee068d9e4f3515cc5ad..4a4e27f0c1cd325e893ae15795640f927aca31fa 100644 (file)
@@ -171,7 +171,4 @@ def importTopicList():
                topic.views      = int(info["views"])
                topic.created_at = info["date"]
 
-       for p in Package.query.all():
-               p.recalcScore()
-
        db.session.commit()
diff --git a/app/tasks/pkgtasks.py b/app/tasks/pkgtasks.py
new file mode 100644 (file)
index 0000000..8ff5836
--- /dev/null
@@ -0,0 +1,23 @@
+# Content DB
+# Copyright (C) 2018  rubenwardy
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <https://www.gnu.org/licenses/>.
+
+
+from app.models import Package, PackageRelease
+from app.tasks import celery
+
+@celery.task()
+def updatePackageScores():
+       Package.query.update({ "score": PackageRelease.score * 0.8 })