jamessewell / django-timescaledb

A Django database backend and tooling for Timescaledb.
Apache License 2.0
184 stars 47 forks source link

Incorrect function location #42

Open llirrikk opened 1 year ago

llirrikk commented 1 year ago

According to the official documentation, the functions should be after the keyword "SELECT".

I wrote:

ranges = (datetime.now() - timedelta(days=3), datetime.now())
res = Metric.timescale\
    .filter(pool=pool, time__range=ranges)\
    .values('time', 'value')

And I've got this SQL:

SELECT "graphs_volume24hoursmetric"."time", "graphs_volume24hoursmetric"."value"
FROM "graphs_volume24hoursmetric"
WHERE ("graphs_volume24hoursmetric"."pool_id" = c6e6b7bd-1f0e-4766-89ed-56bc592742ed AND "graphs_volume24hoursmetric"."time" BETWEEN 2023-03-02 10:58:03.333117+00:00 AND 2023-03-05 10:58:03.333130+00:00)

This query outputs all the points that were created after 3 days.


Then I've used time_bucket_gapfill according to the documentation in the README. Wrote:

ranges = (datetime.now() - timedelta(days=3), datetime.now())
res = Volume24HoursMetric.timescale\
    .filter(pool=pool, time__range=ranges)\
    .time_bucket_gapfill('time', '1 day', ranges[0], ranges[1])\
    .annotate(value=Avg('value'))\
    .values('time', 'value'

Got:

SELECT "graphs_volume24hoursmetric"."time", AVG("graphs_volume24hoursmetric"."value") AS "value"
FROM "graphs_volume24hoursmetric" WHERE ("graphs_volume24hoursmetric"."pool_id" = c6e6b7bd-1f0e-4766-89ed-56bc592742ed AND "graphs_volume24hoursmetric"."time" BETWEEN 2023-03-02 11:07:24.915758+00:00 AND 2023-03-05 11:07:24.915784+00:00) 
GROUP BY time_bucket_gapfill(INTERVAL 1 day, "graphs_volume24hoursmetric"."time", 2023-03-02 11:07:24.915758+00:00, 2023-03-05 11:07:24.915784+00:00), "graphs_volume24hoursmetric"."time"

Hoping to get 3 points, I get some bullshit out of 40 points. According to the official documentation, you need to use the function after the 'SELECT" keyword, and not after "GROUP BY". In my case, aggregation should occur in the interval of 1 day, not 40.

Time buckets are usually used together with GROUP BY to aggregate data. But you can also run time_bucket on a single time value.