fix(FastAPI): use popupdate when [un]voting

The `aurweb.scripts.popupdate` script is used to maintain
the NumVotes and Popularity field. We could do the NumVotes
change more simply; however, since this is already a long-term
implementation, we're going to use it until we move scripts
over to ORM.

Signed-off-by: Kevin Morris <kevr@0cost.org>
This commit is contained in:
Kevin Morris 2021-10-07 22:48:31 -07:00
parent 01fb42c5d9
commit 63498f5edd
No known key found for this signature in database
GPG key ID: F7E46DED420788F3
3 changed files with 47 additions and 10 deletions

View file

@ -5,17 +5,44 @@ from datetime import datetime
import aurweb.db
def main():
conn = aurweb.db.Connection()
conn.execute(("UPDATE PackageBases SET NumVotes = ("
"SELECT COUNT(*) FROM PackageVotes "
"WHERE PackageVotes.PackageBaseID = PackageBases.ID)"))
def run_single(conn, pkgbase):
""" A single popupdate. The given pkgbase instance will be
refreshed after the database update is done.
NOTE: This function is compatible only with aurweb FastAPI.
:param conn: db.Connection[Executor]
:param pkgbase: Instance of db.PackageBase
"""
conn.execute("UPDATE PackageBases SET NumVotes = ("
"SELECT COUNT(*) FROM PackageVotes "
"WHERE PackageVotes.PackageBaseID = PackageBases.ID) "
"WHERE PackageBases.ID = ?", [pkgbase.ID])
now = int(datetime.utcnow().timestamp())
conn.execute(("UPDATE PackageBases SET Popularity = ("
"SELECT COALESCE(SUM(POWER(0.98, (? - VoteTS) / 86400)), 0.0) "
"FROM PackageVotes WHERE PackageVotes.PackageBaseID = "
"PackageBases.ID AND NOT VoteTS IS NULL)"), [now])
conn.execute("UPDATE PackageBases SET Popularity = ("
"SELECT COALESCE(SUM(POWER(0.98, (? - VoteTS) / 86400)), 0.0) "
"FROM PackageVotes WHERE PackageVotes.PackageBaseID = "
"PackageBases.ID AND NOT VoteTS IS NULL) WHERE "
"PackageBases.ID = ?", [now, pkgbase.ID])
conn.commit()
conn.close()
aurweb.db.session.refresh(pkgbase)
def main():
conn = aurweb.db.Connection()
conn.execute("UPDATE PackageBases SET NumVotes = ("
"SELECT COUNT(*) FROM PackageVotes "
"WHERE PackageVotes.PackageBaseID = PackageBases.ID)")
now = int(datetime.utcnow().timestamp())
conn.execute("UPDATE PackageBases SET Popularity = ("
"SELECT COALESCE(SUM(POWER(0.98, (? - VoteTS) / 86400)), 0.0) "
"FROM PackageVotes WHERE PackageVotes.PackageBaseID = "
"PackageBases.ID AND NOT VoteTS IS NULL)", [now])
conn.commit()
conn.close()