From b5afae16cc370d39facbe16880537b4790e3a9dc Mon Sep 17 00:00:00 2001 From: Ant Zucaro Date: Tue, 13 Mar 2018 22:10:48 -0400 Subject: [PATCH] Add a separate JSON view for summary stats. --- xonstat/__init__.py | 4 +++ xonstat/views/__init__.py | 2 +- xonstat/views/main.py | 59 +++++++++++++++++++++++++++------------ 3 files changed, 46 insertions(+), 19 deletions(-) diff --git a/xonstat/__init__.py b/xonstat/__init__.py index 5055661..877e4b8 100644 --- a/xonstat/__init__.py +++ b/xonstat/__init__.py @@ -44,6 +44,10 @@ def main(global_config, **settings): config.add_route("main_index", "/") config.add_view(main_index, route_name="main_index", renderer="main_index.mako") + config.add_route("summary_stats_json", "/summary") + config.add_view(view=summary_stats_json, route_name="summary_stats_json", renderer="json", + accept="application/json") + # MAIN SUBMISSION ROUTE config.add_route("submit_stats", "stats/submit") config.add_view(submit_stats, route_name="submit_stats", renderer="submit_stats.mako") diff --git a/xonstat/views/__init__.py b/xonstat/views/__init__.py index 830eabd..2b726d0 100644 --- a/xonstat/views/__init__.py +++ b/xonstat/views/__init__.py @@ -26,7 +26,7 @@ from xonstat.views.search import search_json from xonstat.views.exceptions import notfound from xonstat.views.main import main_index, top_players_index, top_servers_index -from xonstat.views.main import top_maps_index +from xonstat.views.main import top_maps_index, summary_stats_json from xonstat.views.admin import forbidden, login, merge diff --git a/xonstat/views/main.py b/xonstat/views/main.py index c002f51..9f3f11d 100644 --- a/xonstat/views/main.py +++ b/xonstat/views/main.py @@ -2,6 +2,7 @@ import logging from datetime import datetime, timedelta from beaker.cache import cache_region +from sqlalchemy import text from xonstat.models import DBSession, PlayerRank, ActivePlayer, ActiveServer, ActiveMap from xonstat.views.helpers import RecentGame, recent_games_q @@ -9,29 +10,51 @@ log = logging.getLogger(__name__) @cache_region('hourly_term') -def get_summary_stats(scope="all"): +def summary_stats_data(scope="all"): """ - Gets the following aggregate statistics according to the provided scope: + Gets the summary stats (number of active players, the game type, and the number of games) + for a given scope. - - the number of active players - - the number of games per game type - - Scope can be "all" or "day". - - The fetched information is summarized into a string which is passed - directly to the template. + :param scope: The scope to fetch from the table. May be "all" or "day". + :return: list[tuple] """ if scope not in ["all", "day"]: scope = "all" + 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): + ss = summary_stats_data(request.params.get("scope", "all")) + return [ + { + "players": r.num_players, + "game_type_cd": r.game_type_cd, + "games": r.num_games, + "refresh_dt": r.refresh_dt.isoformat(), + } + for r in ss] + + +@cache_region('hourly_term') +def summary_stats_string(scope="all"): + """ + Assembles the summary stats data into a readable line for direct inclusion in templates. + """ try: - ss = DBSession.query("num_players", "game_type_cd", "num_games").\ - from_statement( - "SELECT num_players, game_type_cd, num_games " - "FROM summary_stats_mv " - "WHERE scope = :scope " - "ORDER BY sort_order " - ).params(scope=scope).all() + ss = summary_stats_data(scope) i = 1 total_games = 0 @@ -169,8 +192,8 @@ def _main_index_data(request): recent_games_count = 20 # summary statistics for the tagline - stat_line = get_summary_stats("all") - day_stat_line = get_summary_stats("day") + stat_line = summary_stats_string("all") + day_stat_line = summary_stats_string("day") # the three top ranks tables -- 2.39.2