From: Ant Zucaro Date: Fri, 4 Apr 2014 21:58:45 +0000 (-0400) Subject: Add a daily statline to the home page. X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=84afc66f897de1d4e470ee0a315fd72754f7da86;p=xonotic%2Fxonstat.git Add a daily statline to the home page. Gets the following aggregate statistics about the past 24 hours: - the number of active players (day_active_players) - the number of games (day_games) - the total number of dm games (day_dm_games) - the total number of duel games (day_duel_games) - the total number of ctf games (day_ctf_games) Cached by the hour, although it is not a terribly expensive query.. --- diff --git a/xonstat/templates/main_index.mako b/xonstat/templates/main_index.mako index 8e14481..175d478 100644 --- a/xonstat/templates/main_index.mako +++ b/xonstat/templates/main_index.mako @@ -17,6 +17,10 @@ Leaderboard % else:

Tracking ${'{:2,d}'.format(summary_stats.total_players)} players, ${'{:2,d}'.format(summary_stats.total_games)} games (${'{:2,d}'.format(summary_stats.duel_games)} duel; ${'{:2,d}'.format(summary_stats.ctf_games)} ctf; ${'{:2,d}'.format(summary_stats.dm_games)} dm) and ${'{:2,d}'.format(summary_stats.total_servers)} servers since October 2011.

% endif + + % if day_stats is not None: +

${day_stats.day_active_players} active players and ${day_stats.day_games} games (${day_stats.day_duel_games} duel, ${day_stats.day_ctf_games} ctf, ${day_stats.day_dm_games} dm) in the past 24 hours.

+ % endif diff --git a/xonstat/views/main.py b/xonstat/views/main.py index 76eeb07..9b0128f 100644 --- a/xonstat/views/main.py +++ b/xonstat/views/main.py @@ -66,6 +66,55 @@ def get_summary_stats(): return summary_stats +@cache_region('hourly_term') +def get_day_summary_stats(): + """ + Gets the following aggregate statistics about the past 24 hours: + - the number of active players (day_active_players) + - the number of games (day_games) + - the total number of dm games (day_dm_games) + - the total number of duel games (day_duel_games) + - the total number of ctf games (day_ctf_games) + """ + try: + day_stats = DBSession.query("day_active_players", + "day_games", "day_dm_games", "day_duel_games", "day_ctf_games").\ + from_statement( + """ + with day_games as ( + select game_type_cd, count(*) day_games + from games + where game_type_cd in ('duel', 'dm', 'ctf') + and create_dt > now() - interval '1 day' + group by game_type_cd + ), + day_active_players as ( + select count(distinct player_id) day_active_players + from player_game_stats + where create_dt > now() - interval '1 day' + ) + select tap.day_active_players, dm.day_games+ + duel.day_games+ctf.day_games day_games, + dm.day_games day_dm_games, duel.day_games day_duel_games, + ctf.day_games day_ctf_games + from day_games dm, day_games duel, day_games ctf, + day_active_players tap + where dm.game_type_cd = 'dm' + and ctf.game_type_cd = 'ctf' + and duel.game_type_cd = 'duel' + """ + ).one() + + # don't show anything if we don't have any activity + if day_stats.day_active_players is None or \ + day_stats.day_active_players == 0: + day_stats = None + + except Exception as e: + day_stats = None + + return day_stats + @cache_region('hourly_term') def get_ranks(game_type_cd): """ @@ -223,8 +272,11 @@ def _main_index_data(request): # summary statistics for the tagline try: summary_stats = get_summary_stats() + day_stats = get_day_summary_stats() + except: summary_stats = None + day_stats = None # the three top ranks tables ranks = [] @@ -255,6 +307,7 @@ def _main_index_data(request): 'recent_games':recent_games, 'ranks':ranks, 'summary_stats':summary_stats, + 'day_stats':day_stats, }