- summary_stats = DBSession.query("total_players", "total_servers",
- "total_games", "dm_games", "duel_games", "ctf_games").\
- from_statement(
- """
- with total_games as (
- select game_type_cd, count(*) total_games
- from games
- where game_type_cd in ('duel', 'dm', 'ctf')
- group by game_type_cd
- ),
- total_players as (
- select count(*) total_players
- from players
- where active_ind = true
- ),
- total_servers as (
- select count(*) total_servers
- from servers
- where active_ind = true
- )
- select tp.total_players, ts.total_servers, dm.total_games+
- duel.total_games+ctf.total_games total_games,
- dm.total_games dm_games, duel.total_games duel_games,
- ctf.total_games ctf_games
- from total_games dm, total_games duel, total_games ctf,
- total_players tp, total_servers ts
- where dm.game_type_cd = 'dm'
- and ctf.game_type_cd = 'ctf'
- and duel.game_type_cd = 'duel'
- """
- ).one()
-
- return summary_stats
+ sql = text("SELECT num_players, game_type_cd, num_games, create_dt refresh_dt "
+ "FROM summary_stats_mv "
+ "WHERE scope = :scope "
+ "ORDER BY sort_order ")
+
+ try:
+ ss = DBSession.query("num_players", "game_type_cd", "num_games", "refresh_dt").\
+ from_statement(sql).params(scope=scope).all()
+
+ return ss
+ except Exception as e:
+ log.error(e)
+ return []
+
+
+def summary_stats_json(request):
+ scope = request.params.get("scope", "all")
+ if scope not in ["all", "day"]:
+ scope = "all"
+
+ ss = summary_stats_data(scope)
+
+ # default values
+ players = 0
+ last_refreshed = "unknown"
+ games = []
+
+ if len(ss) > 0:
+ players = ss[0].num_players
+ last_refreshed = ss[0].refresh_dt.isoformat()
+ games = [{"game_type_cd": r.game_type_cd, "num_games": r.num_games} for r in ss]
+
+ return {
+ "players": players,
+ "scope": scope,
+ "last_refreshed": last_refreshed,
+ "games": games,
+ }