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")
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
@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
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