Let the db perform the stat calculations.
This commit is contained in:
parent
9ac1078e46
commit
fa49c0b829
@ -35,9 +35,11 @@ class Server(models.Model):
|
||||
|
||||
def calc_player_stats(self, days=7):
|
||||
history = self.get_history_stats(days=days)
|
||||
stats = [tmp.players for tmp in history]
|
||||
average = sum(stats) / float(len(stats)) # Moving average
|
||||
return average, min(stats), max(stats)
|
||||
return history.aggregate(
|
||||
models.Avg('players'),
|
||||
models.Min('players'),
|
||||
models.Max('players'),
|
||||
)
|
||||
|
||||
|
||||
class ServerHistory(models.Model):
|
||||
|
||||
@ -15,19 +15,19 @@ class ServerDetailView(generic.DetailView):
|
||||
server = context['server']
|
||||
context['weekly_history'] = server.get_history_stats(days=7)
|
||||
|
||||
avg, min, max = server.calc_player_stats(days=1)
|
||||
context['daily_average'] = avg
|
||||
context['daily_min'] = min
|
||||
context['daily_max'] = max
|
||||
stats = server.calc_player_stats(days=1)
|
||||
context['daily_average'] = stats['players__avg']
|
||||
context['daily_min'] = stats['players__min']
|
||||
context['daily_max'] = stats['players__max']
|
||||
|
||||
avg, min, max = server.calc_player_stats(days=7)
|
||||
context['weekly_average'] = avg
|
||||
context['weekly_min'] = min
|
||||
context['weekly_max'] = max
|
||||
stats = server.calc_player_stats(days=7)
|
||||
context['weekly_average'] = stats['players__avg']
|
||||
context['weekly_min'] = stats['players__min']
|
||||
context['weekly_max'] = stats['players__max']
|
||||
|
||||
avg, min, max = server.calc_player_stats(days=31)
|
||||
context['monthly_average'] = avg
|
||||
context['monthly_min'] = min
|
||||
context['monthly_max'] = max
|
||||
stats = server.calc_player_stats(days=31)
|
||||
context['monthly_average'] = stats['players__avg']
|
||||
context['monthly_min'] = stats['players__min']
|
||||
context['monthly_max'] = stats['players__max']
|
||||
return context
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user