diff --git a/pyproject.toml b/pyproject.toml index d5b2ad74..6a91cb49 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -2,7 +2,7 @@ [tool.poetry] name = "royalpack" - version = "5.15.8" + version = "5.15.9" description = "A Royalnet command pack for the Royal Games community" authors = ["Stefano Pigozzi "] license = "AGPL-3.0+" diff --git a/royalpack/stars/__init__.py b/royalpack/stars/__init__.py index 745d5023..43eeaffc 100644 --- a/royalpack/stars/__init__.py +++ b/royalpack/stars/__init__.py @@ -16,6 +16,7 @@ from .api_auth_login_osu import ApiAuthLoginOsuStar from .api_diario_range import ApiDiarioRangeStar from .api_diario_latest import ApiDiarioLatestStar from ..halloween2020.api_user_trionfi import ApiUserTrionfiStar +from .api_cvstats_months import ApiCvstatsMonthsStar # Enter the PageStars of your Pack here! available_page_stars = [ @@ -36,6 +37,7 @@ available_page_stars = [ ApiDiarioRangeStar, ApiDiarioLatestStar, ApiUserTrionfiStar, + ApiCvstatsMonthsStar, ] # Don't change this, it should automatically generate __all__ diff --git a/royalpack/stars/api_cvstats_months.py b/royalpack/stars/api_cvstats_months.py new file mode 100644 index 00000000..ba2564f4 --- /dev/null +++ b/royalpack/stars/api_cvstats_months.py @@ -0,0 +1,54 @@ +import royalnet.constellation.api as rca +import royalnet.utils as ru + + +class ApiCvstatsMonthsStar(rca.ApiStar): + path = "/api/cvstats/months/v1" + + tags = ["cvstats"] + + @rca.magic + async def get(self, data: rca.ApiData) -> ru.JSON: + """Get some averages on the cvstats.""" + results = data.session.execute(""" +SELECT date_part('month', c.m) pm, + date_part('hour', c.h) ph, + AVG(c.members_connected) members_connected, + AVG(c.users_connected) users_connected, + AVG(c.members_online) members_online, + AVG(c.users_online) users_online, + AVG(c.members_playing) members_playing, + AVG(c.users_playing) users_playing, + AVG(c.members_total) members_total, + AVG(c.users_total) users_total +FROM ( + SELECT date_trunc('month', c.timestamp) m, + date_trunc('hour', c.timestamp) h, + AVG(c.members_connected) members_connected, + AVG(c.users_connected) users_connected, + AVG(c.members_online) members_online, + AVG(c.users_online) users_online, + AVG(c.members_playing) members_playing, + AVG(c.users_playing) users_playing, + AVG(c.members_total) members_total, + AVG(c.users_total) users_total + FROM cvstats c + GROUP BY m, h + ) c +GROUP BY pm, ph; + """) + + return [{ + "m": r[0], + "h": r[1], + "all_time": { + "members_connected": float(r[2]) if r[2] is not None else None, + "users_connected": float(r[3]) if r[3] is not None else None, + "members_online": float(r[4]) if r[4] is not None else None, + "users_online": float(r[5]) if r[5] is not None else None, + "members_playing": float(r[6]) if r[6] is not None else None, + "users_playing": float(r[7]) if r[7] is not None else None, + "members_total": float(r[8]) if r[8] is not None else None, + "users_total": float(r[9]) if r[9] is not None else None, + } + } for r in sorted(results.fetchall(), key=lambda s: (s[0], s[1]))]