mirror of
https://github.com/daniviga/bite.git
synced 2024-11-22 21:16:12 +01:00
Hyperscale (#25)
* Fix a regression in the hyperscale creation * Clean the jupyter notebook [skip ci] * Fix typos [skip ci] * Improve date args in API
This commit is contained in:
parent
5cff8b9c2c
commit
665b3e87d0
|
@ -1,4 +1,4 @@
|
||||||
# Generated by Django 3.1.3 on 2021-03-19 08:08
|
# Generated by Django 3.1.7 on 2021-03-25 10:55
|
||||||
|
|
||||||
import django.core.validators
|
import django.core.validators
|
||||||
from django.db import migrations, models
|
from django.db import migrations, models
|
||||||
|
@ -18,7 +18,7 @@ class Migration(migrations.Migration):
|
||||||
migrations.CreateModel(
|
migrations.CreateModel(
|
||||||
name='Telemetry',
|
name='Telemetry',
|
||||||
fields=[
|
fields=[
|
||||||
('id', models.AutoField(primary_key=True, serialize=False)),
|
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||||
('time', models.DateTimeField(auto_now_add=True, db_index=True)),
|
('time', models.DateTimeField(auto_now_add=True, db_index=True)),
|
||||||
('transport', models.CharField(choices=[('http', 'http'), ('mqtt', 'mqtt')], default='http', max_length=4)),
|
('transport', models.CharField(choices=[('http', 'http'), ('mqtt', 'mqtt')], default='http', max_length=4)),
|
||||||
('clock', models.IntegerField(null=True, validators=[django.core.validators.MinValueValidator(0)])),
|
('clock', models.IntegerField(null=True, validators=[django.core.validators.MinValueValidator(0)])),
|
||||||
|
@ -28,6 +28,7 @@ class Migration(migrations.Migration):
|
||||||
options={
|
options={
|
||||||
'verbose_name_plural': 'Telemetry',
|
'verbose_name_plural': 'Telemetry',
|
||||||
'ordering': ['-time', 'device'],
|
'ordering': ['-time', 'device'],
|
||||||
|
'unique_together': {('time', 'device')},
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
]
|
]
|
||||||
|
|
24
bite/telemetry/migrations/0002_timescale.py
Normal file
24
bite/telemetry/migrations/0002_timescale.py
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
# Generated by Django 3.1.7 on 2021-03-25 10:55
|
||||||
|
|
||||||
|
from django.db import migrations
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('telemetry', '0001_initial'),
|
||||||
|
]
|
||||||
|
|
||||||
|
# Timescale requires an hyperscale table to have the field used for the
|
||||||
|
# partitioning ('time') to be in the any UNIQUE constraint.
|
||||||
|
# Because of that we have a unique_together on 'time' and 'device',
|
||||||
|
# however Django always adds an 'id' as PRIMARY_KEY.
|
||||||
|
# Django's 'id' isn't used as a foreign key, so we are dropping
|
||||||
|
# the contraint and simply adding an index to the 'id' column.
|
||||||
|
# We can now create the hypertable.
|
||||||
|
operations = [
|
||||||
|
migrations.RunSQL(
|
||||||
|
"ALTER TABLE telemetry_telemetry DROP CONSTRAINT telemetry_telemetry_pkey ;" # noqa: E501
|
||||||
|
"CREATE INDEX telemetry_telemetry_id_idx ON telemetry_telemetry(id);" # noqa: E501
|
||||||
|
"SELECT create_hypertable('telemetry_telemetry', 'time');"),
|
||||||
|
]
|
|
@ -30,7 +30,6 @@ def telemetry_validation(value):
|
||||||
|
|
||||||
|
|
||||||
class Telemetry(models.Model):
|
class Telemetry(models.Model):
|
||||||
id = models.AutoField(primary_key=True)
|
|
||||||
device = models.ForeignKey(Device, on_delete=models.CASCADE)
|
device = models.ForeignKey(Device, on_delete=models.CASCADE)
|
||||||
time = models.DateTimeField(db_index=True, auto_now_add=True)
|
time = models.DateTimeField(db_index=True, auto_now_add=True)
|
||||||
transport = models.CharField(max_length=4,
|
transport = models.CharField(max_length=4,
|
||||||
|
@ -43,6 +42,7 @@ class Telemetry(models.Model):
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
ordering = ['-time', 'device']
|
ordering = ['-time', 'device']
|
||||||
|
unique_together = ['time', 'device']
|
||||||
verbose_name_plural = "Telemetry"
|
verbose_name_plural = "Telemetry"
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
|
|
|
@ -17,9 +17,10 @@
|
||||||
# You should have received a copy of the GNU Affero General Public License
|
# You should have received a copy of the GNU Affero General Public License
|
||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
from datetime import datetime
|
||||||
from django.http import Http404
|
from django.http import Http404
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
from django.utils.dateparse import parse_datetime
|
from django.utils.dateparse import parse_date, parse_datetime
|
||||||
from rest_framework.views import APIView
|
from rest_framework.views import APIView
|
||||||
from rest_framework.viewsets import ModelViewSet
|
from rest_framework.viewsets import ModelViewSet
|
||||||
|
|
||||||
|
@ -69,12 +70,16 @@ class TelemetryRange(ModelViewSet):
|
||||||
lookup_field = 'device'
|
lookup_field = 'device'
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def datetime_validation(datetime):
|
def datetime_validation(time):
|
||||||
parsed_datetime = parse_datetime(datetime)
|
parsed_datetime = parse_datetime(time)
|
||||||
if parsed_datetime is None:
|
if parsed_datetime is None:
|
||||||
raise ParseError({
|
parsed_datetime = parse_date(time)
|
||||||
datetime: 'Invalid date format'
|
if parsed_datetime is None:
|
||||||
})
|
raise ParseError({
|
||||||
|
datetime: 'Invalid date format'
|
||||||
|
})
|
||||||
|
parsed_datetime = datetime.combine(
|
||||||
|
parsed_datetime, datetime.min.time())
|
||||||
return timezone.make_aware(parsed_datetime)
|
return timezone.make_aware(parsed_datetime)
|
||||||
|
|
||||||
def list(self, request, device, time_from, time_to=None):
|
def list(self, request, device, time_from, time_to=None):
|
||||||
|
|
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user