import logging
-import sqlalchemy as sa
-import sqlalchemy.sql.functions as func
-import sqlalchemy.sql.expression as expr
-from beaker.cache import cache_regions, cache_region
-from collections import namedtuple
from datetime import datetime, timedelta
-from pyramid.response import Response
-from xonstat.models import *
-from xonstat.util import *
-from xonstat.views.helpers import RecentGame, recent_games_q
-from webhelpers.paginate import Page
+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
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
+ :param scope: The scope to fetch from the table. May be "all" or "day".
+ :return: list[tuple]
+ """
+ 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 ")
- Scope can be "all" or "day".
+ try:
+ ss = DBSession.query("num_players", "game_type_cd", "num_games", "refresh_dt").\
+ from_statement(sql).params(scope=scope).all()
- The fetched information is summarized into a string which is passed
- directly to the template.
- """
+ 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,
+ }
+
+
+@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
)
except Exception as e:
- raise e
stat_line = None
+ raise e
return stat_line
@cache_region('hourly_term')
-def get_top_servers_by_games(limit=None, start=None):
+def get_top_servers_by_play_time(limit=None, start=None):
"""
- The top servers by the number of games played during a date range.
+ The top servers by the cumulative amount of time played on them during a given interval.
"""
q = DBSession.query(ActiveServer)
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
top_players = get_top_players_by_time(10)
# top servers by number of games
- top_servers = get_top_servers_by_games(10)
+ top_servers = get_top_servers_by_play_time(10)
# top maps by total times played
top_maps = get_top_maps_by_games(10)
except:
start = None
- top_servers = get_top_servers_by_games(20, start)
+ top_servers = get_top_servers_by_play_time(20, start)
# building a query string
query = {}