Did some changes on how to get the player history for a server.

This commit is contained in:
A. Svensson 2015-03-08 11:31:26 +01:00
parent 53f3d4e3d5
commit 2630afa14d
3 changed files with 20 additions and 20 deletions

View File

@ -1,6 +1,4 @@
import datetime
from django.db import models from django.db import models
from django.utils import timezone from django.utils import timezone
@ -34,8 +32,8 @@ class PlayerHistory(object):
def __init__(self, redis_settings=dict(host='localhost', port=6379, db=0)): def __init__(self, redis_settings=dict(host='localhost', port=6379, db=0)):
self.redis = redis.StrictRedis(**redis_settings) self.redis = redis.StrictRedis(**redis_settings)
# 2688 = 4 times per hour * 24 hours * 7 days * 4 weeks # 32256 = 4 times per hour * 24 hours * 7 days * 4 weeks * 12 months
self.max_points = 2688 self.max_items = 32256
def add_point(self, server, time, players): def add_point(self, server, time, players):
'''Add a new point in the player history.''' '''Add a new point in the player history.'''
@ -43,17 +41,19 @@ class PlayerHistory(object):
def trim_points(self, server): def trim_points(self, server):
'''Trim away too old points and servers in the player history.''' '''Trim away too old points and servers in the player history.'''
self.redis.ltrim(server, 0, self.max_points) self.redis.ltrim(server, 0, self.max_items)
# let the list expire after a week without updates # let the list expire after a week without updates
self.redis.expire(server, 604800) self.redis.expire(server, 604800)
def get_points(self, server): def get_history(self, server, days=7):
'''Get a range of points from the player history.''' '''Get a range of days in a server's player history.'''
# TODO: not memory efficient, turn into a generator instead? # 96 = 4 times per hour * 24 hours
points = [] max_items = days * 96
for tmp in self.redis.lrange(server, 0, self.max_points):
time, players = tmp.split(',') items = []
time, players = datetime.datetime.fromtimestamp(float(time)), int(players) for tmp in self.redis.lrange(server, 0, max_items):
points.append((time, players)) time, players = tmp.split(',')
return points time, players = float(time), int(players)
items.append((time, players))
return items

View File

@ -14,22 +14,22 @@ class ServerDetailView(generic.DetailView):
context = super(ServerDetailView, self).get_context_data(**kwargs) context = super(ServerDetailView, self).get_context_data(**kwargs)
server = context['server'] server = context['server']
history = PlayerHistory() history = PlayerHistory()
points = history.get_points(server) items = history.get_history(server)
context['player_history'] = points context['player_history'] = items
# Moving average for the last day # Moving average for the last day
# TODO: remove the hardcoded value # TODO: remove the hardcoded value
tmp = [players for time, players in points[-96:]] tmp = [players for time, players in items[-96:]]
context['daily_average'] = sum(tmp) / float(len(tmp)) context['daily_average'] = sum(tmp) / float(len(tmp))
context['daily_min'] = min(tmp) context['daily_min'] = min(tmp)
context['daily_max'] = max(tmp) context['daily_max'] = max(tmp)
tmp = [players for time, players in points[-96*7:]] tmp = [players for time, players in items[-96*7:]]
context['weekly_average'] = sum(tmp) / float(len(tmp)) context['weekly_average'] = sum(tmp) / float(len(tmp))
context['weekly_min'] = min(tmp) context['weekly_min'] = min(tmp)
context['weekly_max'] = max(tmp) context['weekly_max'] = max(tmp)
tmp = [players for time, players in points] tmp = [players for time, players in items]
context['total_average'] = sum(tmp) / float(len(tmp)) context['total_average'] = sum(tmp) / float(len(tmp))
context['total_min'] = min(tmp) context['total_min'] = min(tmp)
context['total_max'] = max(tmp) context['total_max'] = max(tmp)

View File

@ -63,7 +63,7 @@
var series = [ var series = [
{# convert timestamp to ms, because javascript... #} {# convert timestamp to ms, because javascript... #}
{% for timestamp, players in player_history %} {% for timestamp, players in player_history %}
[{{timestamp|date:'U'}} * 1000, {{players}}], [{{timestamp}} * 1000, {{players}}],
{% endfor %} {% endfor %}
]; ];