jamessewell / django-timescaledb

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

Implement Last Observation Carried Forward #7

Open schlunsen opened 3 years ago

schlunsen commented 3 years ago

https://docs.timescale.com/latest/using-timescaledb/reading-data#locf

Should probably be integrated with time_bucket_gapfill

    metrics.timescale.time_bucket_gapfill('time', '1 day', ranges[0], ranges[1], datapoints=240, locf=True)
schlunsen commented 3 years ago

Currently something like this will do the trick

Metric.
timescale.filter(device=device).filter(time__range=range).values('temperature')
.time_bucket_gapfill('time', '1 day', range[0], range[1], datapoints=1).annotate(Avg('temperature'))
.annotate(locf=Func(F('temperature'), function="locf")).order_by('time')
DoGab commented 3 years ago

This didn't work for me. I tried to run this in a Django shell on Python 3.8.6 and get the following error:

Metrics.timescale.filter(time__range=ranges).time_bucket_gapfill('time', '1 hour', ranges[0], ranges[1], datapoints=30).annotate(Avg('loss')).annotate(locf=Func(F('loss'), function="locf"))
Traceback (most recent call last):
  File "<console>", line 1, in <module>
NameError: name 'Func' is not defined

Any idea?

schlunsen commented 3 years ago
from django.db.models import Func
schlunsen commented 3 years ago

@DoGab As I remember the query above isn't quite there yet, but it's a long time ago since I've played with it.

Really looking forward to hear if you can use it or how to do it :)

DoGab commented 3 years ago

@schlunsen Thank you for the Hint. I didn't find the correct library to include.

It worked great! I have not tested it with data that are gapped but for now the query already works!

jacobsayer commented 2 years ago

Docs are now at https://docs.timescale.com/api/latest/hyperfunctions/gapfilling-interpolation/locf/