Use the new model in the views.
--HG-- branch : history_model
This commit is contained in:
parent
afa8e74b6f
commit
65d1b043bf
@ -1,8 +1,11 @@
|
|||||||
|
|
||||||
|
from datetime import timedelta
|
||||||
|
|
||||||
from django.shortcuts import render
|
from django.shortcuts import render
|
||||||
from django.views import generic
|
from django.views import generic
|
||||||
|
from django.utils import timezone
|
||||||
|
|
||||||
from .models import Server, PlayerHistory
|
from .models import Server, ServerHistory
|
||||||
|
|
||||||
class ServerListView(generic.ListView):
|
class ServerListView(generic.ListView):
|
||||||
model = Server
|
model = Server
|
||||||
@ -13,25 +16,24 @@ class ServerDetailView(generic.DetailView):
|
|||||||
def get_context_data(self, **kwargs):
|
def get_context_data(self, **kwargs):
|
||||||
context = super(ServerDetailView, self).get_context_data(**kwargs)
|
context = super(ServerDetailView, self).get_context_data(**kwargs)
|
||||||
server = context['server']
|
server = context['server']
|
||||||
history = PlayerHistory()
|
|
||||||
items = history.get_history(server)
|
weekly_history = ServerHistory.objects.filter(
|
||||||
context['player_history'] = items
|
server=server,
|
||||||
|
created__gte=timezone.now() - timedelta(days=7),
|
||||||
|
)
|
||||||
|
context['weekly_history'] = weekly_history
|
||||||
|
|
||||||
# Moving average for the last day
|
# Moving average for the last day
|
||||||
# TODO: remove the hardcoded value
|
tmp = [tmp.players for tmp in ServerHistory.objects.filter(
|
||||||
tmp = [players for time, players in items[-96:]]
|
server=server,
|
||||||
|
created__gte=timezone.now() - timedelta(days=1))]
|
||||||
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 items[-96*7:]]
|
tmp = [tmp.players for tmp in weekly_history]
|
||||||
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 items]
|
|
||||||
context['total_average'] = sum(tmp) / float(len(tmp))
|
|
||||||
context['total_min'] = min(tmp)
|
|
||||||
context['total_max'] = max(tmp)
|
|
||||||
return context
|
return context
|
||||||
|
|
||||||
|
|||||||
@ -45,12 +45,6 @@
|
|||||||
<th>{{weekly_min}}</th>
|
<th>{{weekly_min}}</th>
|
||||||
<th>{{weekly_max}}</th>
|
<th>{{weekly_max}}</th>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
|
||||||
<th>Total</th>
|
|
||||||
<th>{{total_average|floatformat}}</th>
|
|
||||||
<th>{{total_min}}</th>
|
|
||||||
<th>{{total_max}}</th>
|
|
||||||
</tr>
|
|
||||||
</table>
|
</table>
|
||||||
|
|
||||||
<div id="chart"></div>
|
<div id="chart"></div>
|
||||||
@ -62,8 +56,8 @@
|
|||||||
$(function() {
|
$(function() {
|
||||||
var series = [
|
var series = [
|
||||||
{# convert timestamp to ms, because javascript... #}
|
{# convert timestamp to ms, because javascript... #}
|
||||||
{% for timestamp, players in player_history %}
|
{% for item in weekly_history %}
|
||||||
[{{timestamp}} * 1000, {{players}}],
|
[{{item.created|date:'U'}} * 1000, {{item.players}}],
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user