1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-12-18 07:34:20 +00:00
royalnet/royalpack/stars/api_cvstats_avg.py

165 lines
8.3 KiB
Python
Raw Normal View History

2020-06-26 14:13:11 +00:00
import royalnet.constellation.api as rca
2020-05-13 23:08:36 +00:00
import royalnet.utils as ru
2020-06-26 14:13:11 +00:00
class ApiCvstatsAvgStar(rca.ApiStar):
2020-05-13 23:08:36 +00:00
path = "/api/cvstats/avg/v1"
tags = ["cvstats"]
2020-06-26 14:13:11 +00:00
@rca.magic
async def get(self, data: rca.ApiData) -> ru.JSON:
2020-06-22 17:27:11 +00:00
"""Get some averages on the cvstats."""
2020-05-13 23:08:36 +00:00
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
2020-06-14 15:55:25 +00:00
LEFT JOIN
2020-05-13 23:08:36 +00:00
(
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
2020-06-04 16:01:47 +00:00
WHERE c.timestamp > current_timestamp - interval '7 day'
2020-05-13 23:08:36 +00:00
GROUP BY h
) c
GROUP BY ph
) last_week ON last_week.ph = all_time.ph
2020-06-14 15:55:25 +00:00
LEFT JOIN
2020-05-13 23:08:36 +00:00
(
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
2020-06-04 16:01:47 +00:00
WHERE c.timestamp > current_timestamp - interval '30 day'
2020-05-13 23:08:36 +00:00
GROUP BY h
) c
GROUP BY ph
) last_month ON last_month.ph = all_time.ph
2020-06-14 15:55:25 +00:00
LEFT JOIN
2020-05-13 23:08:36 +00:00
(
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
2020-06-04 16:01:47 +00:00
WHERE c.timestamp > current_timestamp - interval '1 day'
2020-05-13 23:08:36 +00:00
GROUP BY h
) c
GROUP BY ph
) last_day ON last_day.ph = all_time.ph;
""")
return [{
"h": r[0],
"all_time": {
2020-06-14 16:15:11 +00:00
"members_connected": float(r[1]) if r[1] is not None else None,
"users_connected": float(r[2]) if r[2] is not None else None,
"members_online": float(r[3]) if r[3] is not None else None,
"users_online": float(r[4]) if r[4] is not None else None,
"members_playing": float(r[5]) if r[5] is not None else None,
"users_playing": float(r[6]) if r[6] is not None else None,
"members_total": float(r[7]) if r[7] is not None else None,
"users_total": float(r[8]) if r[8] is not None else None,
2020-05-13 23:08:36 +00:00
},
"last_week": {
2020-06-14 16:15:11 +00:00
"members_connected": float(r[10]) if r[10] is not None else None,
"users_connected": float(r[11]) if r[11] is not None else None,
"members_online": float(r[12]) if r[12] is not None else None,
"users_online": float(r[13]) if r[13] is not None else None,
"members_playing": float(r[14]) if r[14] is not None else None,
"users_playing": float(r[15]) if r[15] is not None else None,
"members_total": float(r[16]) if r[16] is not None else None,
"users_total": float(r[17]) if r[17] is not None else None,
2020-05-13 23:08:36 +00:00
},
"last_month": {
2020-06-14 16:15:11 +00:00
"members_connected": float(r[19]) if r[19] is not None else None,
"users_connected": float(r[20]) if r[20] is not None else None,
"members_online": float(r[21]) if r[21] is not None else None,
"users_online": float(r[22]) if r[22] is not None else None,
"members_playing": float(r[23]) if r[23] is not None else None,
"users_playing": float(r[24]) if r[24] is not None else None,
"members_total": float(r[25]) if r[25] is not None else None,
"users_total": float(r[26]) if r[26] is not None else None,
2020-05-13 23:08:36 +00:00
},
"last_day": {
2020-06-14 16:15:11 +00:00
"members_connected": float(r[28]) if r[28] is not None else None,
"users_connected": float(r[29]) if r[29] is not None else None,
"members_online": float(r[30]) if r[30] is not None else None,
"users_online": float(r[31]) if r[31] is not None else None,
"members_playing": float(r[32]) if r[32] is not None else None,
"users_playing": float(r[33]) if r[33] is not None else None,
"members_total": float(r[34]) if r[34] is not None else None,
2020-05-13 23:08:36 +00:00
},
} for r in sorted(results.fetchall(), key=lambda s: s[0])]