horrible title, but let me explain: i've got this django model containing a timestamp (date) and the attribute to log - f.e. the number of users consuming some ressource - (value).
class Viewers(models.Model):
date = models.DateTimeField()
value = models.IntegerField()
for each 10seconds the table contains the number of users. something like this:
| date | value |
|------|-------|
| t1 | 15 |
| t2 | 18 |
| t3 | 27 |
| t4 | 25 |
| .. | .. |
| t30 | 38 |
| t31 | 36 |
| .. | .. |
now i want to generate different statistics from this data, each with another resolution. f.e. for a chart of the last day i don't need the 10second resolution, so i want 5 minute steps (that are build by averaging the values (and maybe also the date) of the rows from t1 to t29, t30 to t59, ...), so that i'll get:
| date | value |
|------|-------|
| t15 | 21 |
| t45 | 32 |
| .. | .. |
the attributes to keep variable are start & end timestamp and the resolution (like 5 minutes). is there a way using the django orm/queryset api and if not, how to reach this with custom sql?
I've been trying to solve this problem in the most 'django' way possible. I've settled for the following. It averages the values for 15minute time slots between start_date and end_date where the column name is'date':
It returns a dictionary:
The core of the idea comes from this answer to the same question for PHP/SQL. The code passed to extra is for a Postgres DB.
have you looked at the range filter?
https://docs.djangoproject.com/en/dev/ref/models/querysets/#range
The example given in the doc's seems similar to your situation.
That will get you the average of all the
values
betweenstart_time
andend_time
, returned as a dictionary in the form of{ 'average': <the average> }
.start_time
andend_time
need to be Python datetime objects. So if you have a timestamp, or something, you'll need to convert it first. You can also usedatetime.timedelta
to calculate theend_time
based on the start_time. For a five minute resolution, something like this:After long trying i made it as SQL-statement:
with
in the end - not really hard - and indeed independent from the time resolution of the samplings in the origin table.