Let the db perform the stat calculations.

This commit is contained in:
A. Svensson 2015-03-08 15:12:08 +01:00
parent 9ac1078e46
commit fa49c0b829
2 changed files with 17 additions and 15 deletions

View File

@ -35,9 +35,11 @@ class Server(models.Model):
def calc_player_stats(self, days=7): def calc_player_stats(self, days=7):
history = self.get_history_stats(days=days) history = self.get_history_stats(days=days)
stats = [tmp.players for tmp in history] return history.aggregate(
average = sum(stats) / float(len(stats)) # Moving average models.Avg('players'),
return average, min(stats), max(stats) models.Min('players'),
models.Max('players'),
)
class ServerHistory(models.Model): class ServerHistory(models.Model):

View File

@ -15,19 +15,19 @@ class ServerDetailView(generic.DetailView):
server = context['server'] server = context['server']
context['weekly_history'] = server.get_history_stats(days=7) context['weekly_history'] = server.get_history_stats(days=7)
avg, min, max = server.calc_player_stats(days=1) stats = server.calc_player_stats(days=1)
context['daily_average'] = avg context['daily_average'] = stats['players__avg']
context['daily_min'] = min context['daily_min'] = stats['players__min']
context['daily_max'] = max context['daily_max'] = stats['players__max']
avg, min, max = server.calc_player_stats(days=7) stats = server.calc_player_stats(days=7)
context['weekly_average'] = avg context['weekly_average'] = stats['players__avg']
context['weekly_min'] = min context['weekly_min'] = stats['players__min']
context['weekly_max'] = max context['weekly_max'] = stats['players__max']
avg, min, max = server.calc_player_stats(days=31) stats = server.calc_player_stats(days=31)
context['monthly_average'] = avg context['monthly_average'] = stats['players__avg']
context['monthly_min'] = min context['monthly_min'] = stats['players__min']
context['monthly_max'] = max context['monthly_max'] = stats['players__max']
return context return context