1
0
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:
Daniele Viganò 2021-03-26 22:17:53 +01:00 committed by GitHub
parent 5cff8b9c2c
commit 665b3e87d0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 76 additions and 1921 deletions

View File

@ -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
from django.db import migrations, models
@ -18,7 +18,7 @@ class Migration(migrations.Migration):
migrations.CreateModel(
name='Telemetry',
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)),
('transport', models.CharField(choices=[('http', 'http'), ('mqtt', 'mqtt')], default='http', max_length=4)),
('clock', models.IntegerField(null=True, validators=[django.core.validators.MinValueValidator(0)])),
@ -28,6 +28,7 @@ class Migration(migrations.Migration):
options={
'verbose_name_plural': 'Telemetry',
'ordering': ['-time', 'device'],
'unique_together': {('time', 'device')},
},
),
]

View 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');"),
]

View File

@ -30,7 +30,6 @@ def telemetry_validation(value):
class Telemetry(models.Model):
id = models.AutoField(primary_key=True)
device = models.ForeignKey(Device, on_delete=models.CASCADE)
time = models.DateTimeField(db_index=True, auto_now_add=True)
transport = models.CharField(max_length=4,
@ -43,6 +42,7 @@ class Telemetry(models.Model):
class Meta:
ordering = ['-time', 'device']
unique_together = ['time', 'device']
verbose_name_plural = "Telemetry"
def __str__(self):

View File

@ -17,9 +17,10 @@
# 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/>.
from datetime import datetime
from django.http import Http404
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.viewsets import ModelViewSet
@ -69,12 +70,16 @@ class TelemetryRange(ModelViewSet):
lookup_field = 'device'
@staticmethod
def datetime_validation(datetime):
parsed_datetime = parse_datetime(datetime)
def datetime_validation(time):
parsed_datetime = parse_datetime(time)
if parsed_datetime is None:
parsed_datetime = parse_date(time)
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)
def list(self, request, device, time_from, time_to=None):

File diff suppressed because one or more lines are too long