mirror of
https://github.com/minetest/contentdb.git
synced 2025-01-03 11:47:28 +01:00
Add package stats endpoint
This commit is contained in:
parent
de70b21e55
commit
e1f4787fb9
@ -35,6 +35,8 @@ from .support import error, api_create_vcs_release, api_create_zip_release, api_
|
||||
api_order_screenshots, api_edit_package, api_set_cover_image
|
||||
from functools import wraps
|
||||
|
||||
from ...logic.graphs import flatten_data
|
||||
|
||||
|
||||
def cors_allowed(f):
|
||||
@wraps(f)
|
||||
@ -433,6 +435,13 @@ def list_all_reviews():
|
||||
})
|
||||
|
||||
|
||||
@bp.route("/api/packages/<author>/<name>/stats/")
|
||||
@is_package_page
|
||||
@cors_allowed
|
||||
def package_stats(package: Package):
|
||||
return jsonify(flatten_data(package))
|
||||
|
||||
|
||||
@bp.route("/api/scores/")
|
||||
@cors_allowed
|
||||
def package_scores():
|
||||
|
@ -108,6 +108,16 @@ Tokens can be attained by visiting [Settings > API Tokens](/user/tokens/).
|
||||
package dependency (`author/name`).
|
||||
* `optional_depends`: list of optional dependencies
|
||||
* Same as above.
|
||||
* GET `/api/packages/<username>/<name>/stats/`
|
||||
* Returns daily stats for package, or null if there is no data.
|
||||
* EXPERIMENTAL. This API may change without warning.
|
||||
* A table with the following keys:
|
||||
* `dates`: list of dates in isoformat
|
||||
* `platform_minetest`: list of integers per date
|
||||
* `platform_other`: list of integers per date
|
||||
* `reason_new`: list of integers per date
|
||||
* `reason_dependency`: list of integers per date
|
||||
* `reason_update`: list of integers per date
|
||||
|
||||
You can download a package by building one of the two URLs:
|
||||
|
||||
|
17
app/logic/graphs.py
Normal file
17
app/logic/graphs.py
Normal file
@ -0,0 +1,17 @@
|
||||
from app.models import Package, PackageDailyStats, db
|
||||
|
||||
|
||||
def flatten_data(package: Package):
|
||||
stats = package.daily_stats.order_by(db.asc(PackageDailyStats.date)).all()
|
||||
if len(stats) == 0:
|
||||
return None
|
||||
|
||||
result = {
|
||||
"dates": [stat.date.isoformat() for stat in stats],
|
||||
}
|
||||
|
||||
for key in ["platform_minetest", "platform_other", "reason_new",
|
||||
"reason_dependency", "reason_update"]:
|
||||
result[key] = [getattr(stat, key) for stat in stats]
|
||||
|
||||
return result
|
@ -468,6 +468,9 @@ class Package(db.Model):
|
||||
aliases = db.relationship("PackageAlias", foreign_keys="PackageAlias.package_id",
|
||||
back_populates="package", cascade="all, delete, delete-orphan")
|
||||
|
||||
daily_stats = db.relationship("PackageDailyStats", foreign_keys="PackageDailyStats.package_id",
|
||||
back_populates="package", cascade="all, delete, delete-orphan", lazy="dynamic")
|
||||
|
||||
def __init__(self, package=None):
|
||||
if package is None:
|
||||
return
|
||||
@ -1205,7 +1208,7 @@ class PackageAlias(db.Model):
|
||||
|
||||
class PackageDailyStats(db.Model):
|
||||
package_id = db.Column(db.Integer, db.ForeignKey("package.id"), primary_key=True)
|
||||
package = db.relationship("Package", foreign_keys=[package_id])
|
||||
package = db.relationship("Package", back_populates="daily_stats", foreign_keys=[package_id])
|
||||
date = db.Column(db.Date, primary_key=True)
|
||||
|
||||
platform_minetest = db.Column(db.Integer, nullable=False, default=0)
|
||||
|
Loading…
Reference in New Issue
Block a user