+@cache_region('hourly_term')
+def get_summary_stats(scope="all"):
+ """
+ Gets the following aggregate statistics according to the provided 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.
+ """
+ if scope not in ["all", "day"]:
+ scope = "all"
+
+ 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()
+
+ i = 1
+ total_games = 0
+ other_games = 0
+ for row in ss:
+ # the number of players is constant in each row
+ total_players = row.num_players
+
+ total_games += row.num_games
+
+ # we can't show all game types on the single summary line, so any
+ # past the fifth one will get bundled in to an "other" count
+ if i > 5:
+ other_games += row.num_games
+
+ i += 1
+
+ # don't send anything if we don't have any activity
+ if total_games == 0:
+ stat_line = None
+ else:
+ # This is ugly because we're doing template-like stuff within the
+ # view code. The alternative isn't any better, though: we would
+ # have to assemble the string inside the template by using a Python
+ # code block. For now I'll leave it like this since it is the lesser
+ # of two evils IMO.
+ # Also we need to hard-code the URL structure in here to allow caching,
+ # which also sucks.
+ in_paren = "; ".join(["{:2,d} {}".format(
+ g.num_games,
+ "<a href='/games?type={0}'>{0}</a>".format(g.game_type_cd)
+ ) for g in ss[:5]])
+
+ if other_games > 0:
+ in_paren += "; {:2,d} other".format(other_games)
+
+ stat_line = "{:2,d} players and {:2,d} games ({})".format(
+ total_players,
+ total_games,
+ in_paren
+ )
+
+ except Exception as e:
+ stat_line = None
+ raise e
+
+ return stat_line
+
+
+@cache_region('hourly_term')
+def get_ranks(game_type_cd):
+ """
+ Gets a set number of the top-ranked people for the specified game_type_cd.
+
+ The game_type_cd parameter is the type to fetch. Currently limited to
+ duel, dm, ctf, and tdm.
+ """
+ # how many ranks we want to fetch