From 37445585c5e37d8dd883c93c7dedb4ef941f40a6 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Thu, 14 May 2020 01:08:36 +0200 Subject: [PATCH] Aggiungi un mostro --- royalpack/commands/givefiorygi.py | 8 +- royalpack/stars/__init__.py | 2 + royalpack/stars/api_cvstats_avg.py | 174 +++++++++++++++++++++++++++++ 3 files changed, 178 insertions(+), 6 deletions(-) create mode 100644 royalpack/stars/api_cvstats_avg.py diff --git a/royalpack/commands/givefiorygi.py b/royalpack/commands/givefiorygi.py index 3511381f..18bcda28 100644 --- a/royalpack/commands/givefiorygi.py +++ b/royalpack/commands/givefiorygi.py @@ -17,7 +17,6 @@ class GivefiorygiCommand(rc.Command): user_arg = args[0] qty_arg = args[1] - reason_arg = " ".join(args[2:]) if user_arg is None: raise rc.InvalidInputError("Non hai specificato un destinatario!") @@ -34,11 +33,8 @@ class GivefiorygiCommand(rc.Command): if qty <= 0: raise rc.InvalidInputError("La quantità specificata deve essere almeno 1!") - if reason_arg == "": - raise rc.InvalidInputError("Non hai specificato un motivo!") - if author.fiorygi.fiorygi < qty: raise rc.InvalidInputError("Non hai abbastanza fiorygi per effettuare la transazione!") - await FiorygiTransaction.spawn_fiorygi(data, author, -qty, reason_arg) - await FiorygiTransaction.spawn_fiorygi(data, user, qty, reason_arg) + await FiorygiTransaction.spawn_fiorygi(data, author, -qty, f"aver ceduto fiorygi a {user}") + await FiorygiTransaction.spawn_fiorygi(data, user, qty, f"aver ricevuto fiorygi da {user}") diff --git a/royalpack/stars/__init__.py b/royalpack/stars/__init__.py index dc65f004..6d144ff5 100644 --- a/royalpack/stars/__init__.py +++ b/royalpack/stars/__init__.py @@ -14,6 +14,7 @@ from .api_polls_create import ApiPollsCreate from .api_polls_get import ApiPollsGet from .api_polls_list import ApiPollsList from .api_cvstats_latest import ApiCvstatsLatestStar +from .api_cvstats_avg import ApiCvstatsAvgStar # Enter the PageStars of your Pack here! available_page_stars = [ @@ -32,6 +33,7 @@ available_page_stars = [ ApiPollsGet, ApiPollsList, ApiCvstatsLatestStar, + ApiCvstatsAvgStar, ] # Don't change this, it should automatically generate __all__ diff --git a/royalpack/stars/api_cvstats_avg.py b/royalpack/stars/api_cvstats_avg.py new file mode 100644 index 00000000..0346fb3b --- /dev/null +++ b/royalpack/stars/api_cvstats_avg.py @@ -0,0 +1,174 @@ +import royalnet.utils as ru +from royalnet.backpack.tables import * +from royalnet.constellation.api import * +from ..tables import Cvstats + + +class ApiCvstatsAvgStar(ApiStar): + path = "/api/cvstats/avg/v1" + + methods = ["GET"] + + summary = "Get some averages on the cvstats." + + tags = ["cvstats"] + + async def api(self, data: ApiData) -> ru.JSON: + results = data.session.execute(""" +SELECT * +FROM ( + SELECT 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('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 h + ) c + GROUP BY ph +) all_time +JOIN +( + SELECT 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('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 + WHERE c.timestamp BETWEEN + NOW()::DATE-EXTRACT(DOW FROM NOW())::INTEGER-7 + AND NOW()::DATE-EXTRACT(DOW from NOW())::INTEGER + GROUP BY h + ) c + GROUP BY ph +) last_week ON last_week.ph = all_time.ph +JOIN +( + SELECT 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('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 + WHERE c.timestamp BETWEEN + NOW()::DATE-EXTRACT(DOW FROM NOW())::INTEGER-30 + AND NOW()::DATE-EXTRACT(DOW from NOW())::INTEGER + GROUP BY h + ) c + GROUP BY ph +) last_month ON last_month.ph = all_time.ph +JOIN +( + SELECT 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('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 + WHERE c.timestamp BETWEEN + NOW()::DATE-EXTRACT(DOW FROM NOW())::INTEGER-1 + AND NOW()::DATE-EXTRACT(DOW from NOW())::INTEGER + GROUP BY h + ) c + GROUP BY ph +) last_day ON last_day.ph = all_time.ph; + """) + + return [{ + "h": r[0], + "all_time": { + "members_connected": float(r[1]), + "users_connected": float(r[2]), + "members_online": float(r[3]), + "users_online": float(r[4]), + "members_playing": float(r[5]), + "users_playing": float(r[6]), + "members_total": float(r[7]), + "users_total": float(r[8]) + }, + "last_week": { + "members_connected": float(r[10]), + "users_connected": float(r[11]), + "members_online": float(r[12]), + "users_online": float(r[13]), + "members_playing": float(r[14]), + "users_playing": float(r[15]), + "members_total": float(r[16]), + "users_total": float(r[17]) + }, + "last_month": { + "members_connected": float(r[19]), + "users_connected": float(r[20]), + "members_online": float(r[21]), + "users_online": float(r[22]), + "members_playing": float(r[23]), + "users_playing": float(r[24]), + "members_total": float(r[25]), + "users_total": float(r[26]) + }, + "last_day": { + "members_connected": float(r[28]), + "users_connected": float(r[29]), + "members_online": float(r[30]), + "users_online": float(r[31]), + "members_playing": float(r[32]), + "users_playing": float(r[33]), + "members_total": float(r[34]), + }, + } for r in sorted(results.fetchall(), key=lambda s: s[0])]