Use the new model in the views.

--HG--
branch : history_model
This commit is contained in:
A. Svensson 2015-03-08 13:00:39 +01:00
parent afa8e74b6f
commit 65d1b043bf
2 changed files with 16 additions and 20 deletions

View File

@ -1,8 +1,11 @@
from datetime import timedelta
from django.shortcuts import render
from django.views import generic
from django.utils import timezone
from .models import Server, PlayerHistory
from .models import Server, ServerHistory
class ServerListView(generic.ListView):
model = Server
@ -13,25 +16,24 @@ class ServerDetailView(generic.DetailView):
def get_context_data(self, **kwargs):
context = super(ServerDetailView, self).get_context_data(**kwargs)
server = context['server']
history = PlayerHistory()
items = history.get_history(server)
context['player_history'] = items
weekly_history = ServerHistory.objects.filter(
server=server,
created__gte=timezone.now() - timedelta(days=7),
)
context['weekly_history'] = weekly_history
# Moving average for the last day
# TODO: remove the hardcoded value
tmp = [players for time, players in items[-96:]]
tmp = [tmp.players for tmp in ServerHistory.objects.filter(
server=server,
created__gte=timezone.now() - timedelta(days=1))]
context['daily_average'] = sum(tmp) / float(len(tmp))
context['daily_min'] = min(tmp)
context['daily_max'] = max(tmp)
tmp = [players for time, players in items[-96*7:]]
tmp = [tmp.players for tmp in weekly_history]
context['weekly_average'] = sum(tmp) / float(len(tmp))
context['weekly_min'] = min(tmp)
context['weekly_max'] = max(tmp)
tmp = [players for time, players in items]
context['total_average'] = sum(tmp) / float(len(tmp))
context['total_min'] = min(tmp)
context['total_max'] = max(tmp)
return context

View File

@ -45,12 +45,6 @@
<th>{{weekly_min}}</th>
<th>{{weekly_max}}</th>
</tr>
<tr>
<th>Total</th>
<th>{{total_average|floatformat}}</th>
<th>{{total_min}}</th>
<th>{{total_max}}</th>
</tr>
</table>
<div id="chart"></div>
@ -62,8 +56,8 @@
$(function() {
var series = [
{# convert timestamp to ms, because javascript... #}
{% for timestamp, players in player_history %}
[{{timestamp}} * 1000, {{players}}],
{% for item in weekly_history %}
[{{item.created|date:'U'}} * 1000, {{item.players}}],
{% endfor %}
];