Browse Source

When statistics is empty then message about it instead of redirect

devel
bashmak 8 years ago
parent
commit
1ee6706951
  1. 26
      abonapp/templates/abonapp/charts.html
  2. 27
      abonapp/views.py
  3. 43
      statistics/models.py
  4. 1
      templates/site_base.html

26
abonapp/templates/abonapp/charts.html

@ -11,14 +11,6 @@
<div class="panel-body">
{% if charts_data %}
<div id="chrt"></div>
<form action="{% url 'abonapp:charts' group.pk abon.pk %}" method="get" class="input-group">
<span class="input-group-btn">
<button class="btn btn-default" type="submit">
<span class="glyphicon glyphicon-calendar"></span> {% trans 'Show graph by date' %}
</button>
</span>
<input type="text" class="form-control" placeholder="{% trans 'Choose a date' %}" id="date_choose" name="wantdate" value="{{ wantdate|date:'dmY' }}">
</form>
<script type="text/javascript">
$(document).ready(function ($) {
new Chartist.Line('#chrt', {
@ -46,14 +38,26 @@
tension: 0
})
});
$('#date_choose').datetimepicker({
format: 'DDMMYYYY'
});
});
</script>
{% else %}
<h2>{% trans 'Static info was Not found' %}</h2>
{% endif %}
<form action="{% url 'abonapp:charts' group.pk abon.pk %}" method="get" class="input-group">
<span class="input-group-btn">
<button class="btn btn-default" type="submit">
<span class="glyphicon glyphicon-calendar"></span> {% trans 'Show graph by date' %}
</button>
</span>
<input type="text" class="form-control" placeholder="{% trans 'Choose a date' %}" id="date_choose" name="wantdate" value="{{ wantdate|date:'dmY' }}">
</form>
<script type="text/javascript">
$(document).ready(function ($) {
$('#date_choose').datetimepicker({
format: 'DDMMYYYY'
});
});
</script>
</div>
</div>
</div>

27
abonapp/views.py

@ -579,21 +579,18 @@ def charts(request, gid, uid):
abon.group = Group.objects.get(pk=gid)
abon.save(update_fields=['group'])
if abon.ip_address is None:
charts_data = None
else:
charts_data = StatElem.objects.chart(
abon.username,
count_of_parts=30,
want_date=wandate
)
abontariff = abon.active_tariff()
if abontariff is not None:
trf = abontariff.tariff
high = trf.speedIn + trf.speedOut
if high > 100:
high = 100
charts_data = StatElem.objects.chart(
abon.username,
count_of_parts=30,
want_date=wandate
)
abontariff = abon.active_tariff()
if abontariff is not None:
trf = abontariff.tariff
high = trf.speedIn + trf.speedOut
if high > 100:
high = 100
except models.Abon.DoesNotExist:
messages.error(request, _('Abon does not exist'))

43
statistics/models.py

@ -1,6 +1,6 @@
import math
from datetime import datetime, timedelta, date, time
from django.db import models, connection
from django.db import models, connection, ProgrammingError
from django.utils.timezone import now
from mydefs import MyGenericIPAddressField
@ -28,24 +28,29 @@ class StatManager(models.Manager):
def avarage(elements):
return sum(elements) / len(elements)
charts_data = self.filter(uname=username)
charts_times = [cd.cur_time.timestamp()*1000 for cd in charts_data]
charts_octets = [cd.octets for cd in charts_data]
if len(charts_octets) > 0 and len(charts_octets) == len(charts_times):
charts_octets = split_list(charts_octets, count_of_parts)
charts_octets = [byte_to_mbit(avarage(c)) for c in charts_octets]
charts_times = split_list(charts_times, count_of_parts)
charts_times = [avarage(t) for t in charts_times]
charts_data = zip(charts_times, charts_octets)
charts_data = ["{x: new Date(%d), y: %.2f}" % (cd[0], cd[1]) for cd in charts_data]
midnight = datetime.combine(want_date, time.min)
charts_data.append("{x:new Date(%d),y:0}" % (int(charts_times[-1:][0]) + 1))
charts_data.append("{x:new Date(%d),y:0}" % (int((midnight + timedelta(days=1)).timestamp()) * 1000))
return charts_data
else:
return
try:
charts_data = self.filter(uname=username)
charts_times = [cd.cur_time.timestamp()*1000 for cd in charts_data]
charts_octets = [cd.octets for cd in charts_data]
if len(charts_octets) > 0 and len(charts_octets) == len(charts_times):
charts_octets = split_list(charts_octets, count_of_parts)
charts_octets = [byte_to_mbit(avarage(c)) for c in charts_octets]
charts_times = split_list(charts_times, count_of_parts)
charts_times = [avarage(t) for t in charts_times]
charts_data = zip(charts_times, charts_octets)
charts_data = ["{x: new Date(%d), y: %.2f}" % (cd[0], cd[1]) for cd in charts_data]
midnight = datetime.combine(want_date, time.min)
charts_data.append("{x:new Date(%d),y:0}" % (int(charts_times[-1:][0]) + 1))
charts_data.append("{x:new Date(%d),y:0}" % (int((midnight + timedelta(days=1)).timestamp()) * 1000))
return charts_data
else:
return
except ProgrammingError as e:
if "Table 'djing_db_n.flowstat" in str(e):
return
class StatElem(models.Model):

1
templates/site_base.html

@ -0,0 +1 @@
{% include 'all_base.html' %}
Loading…
Cancel
Save