Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: poll #622

Merged
merged 4 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions backend/apps/api/v1/migrations/0034_poll.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# -*- coding: utf-8 -*-
# Generated by Django 4.2.13 on 2024-06-26 11:05

import uuid

import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("v1", "0033_remove_dataset_db_slug_remove_table_db_slug"),
]

operations = [
migrations.CreateModel(
name="Poll",
fields=[
("id", models.UUIDField(default=uuid.uuid4, primary_key=True, serialize=False)),
("frequency", models.IntegerField()),
("latest", models.DateTimeField(blank=True, null=True)),
(
"entity",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="polls",
to="v1.entity",
),
),
(
"information_request",
models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.CASCADE,
related_name="polls",
to="v1.informationrequest",
),
),
(
"raw_data_source",
models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.CASCADE,
related_name="polls",
to="v1.rawdatasource",
),
),
],
options={
"verbose_name": "Poll",
"verbose_name_plural": "Polls",
"db_table": "poll",
"ordering": ["frequency"],
},
),
]
55 changes: 55 additions & 0 deletions backend/apps/api/v1/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,14 @@ def table_last_updated_at(self):
] # fmt: skip
return max(updates) if updates else None

@property
def raw_data_source_last_polled_at(self):
polls = [
u.last_polled_at for u in self.raw_data_sources.all()
if u.last_polled_at
] # fmt: skip
return max(polls) if polls else None
rdahis marked this conversation as resolved.
Show resolved Hide resolved

@property
def raw_data_source_last_updated_at(self):
updates = [
Expand Down Expand Up @@ -717,7 +725,49 @@ def clean(self) -> None:
raise ValidationError("Entity's category is not in category.slug = `datetime`.")
return super().clean()

class Poll(BaseModel):
id = models.UUIDField(primary_key=True, default=uuid4)
entity = models.ForeignKey("Entity", on_delete=models.CASCADE, related_name="polls")
frequency = models.IntegerField()
latest = models.DateTimeField(blank=True, null=True)
raw_data_source = models.ForeignKey(
"RawDataSource",
blank=True,
null=True,
on_delete=models.CASCADE,
related_name="polls",
)
information_request = models.ForeignKey(
"InformationRequest",
blank=True,
null=True,
on_delete=models.CASCADE,
related_name="polls",
)

graphql_nested_filter_fields_whitelist = ["id"]

def __str__(self):
return f"{str(self.frequency)} {str(self.entity)}"

class Meta:
"""Meta definition for Poll."""

db_table = "poll"
verbose_name = "Poll"
verbose_name_plural = "Polls"
ordering = ["frequency"]

def clean(self) -> None:
"""Assert that only one of "raw_data_source", "information_request" is set"""
if bool(self.raw_data_source) == bool(self.information_request):
raise ValidationError(
"One and only one of 'raw_data_source',"
" or 'information_request' must be set."
)
if self.entity.category.slug != "datetime":
raise ValidationError("Entity's category is not in category.slug = `datetime`.")
return super().clean()
class Table(BaseModel, OrderedModel):
"""Table model"""

Expand Down Expand Up @@ -1493,6 +1543,11 @@ class Meta:
def __str__(self):
return f"{self.name} ({self.dataset.name})"

@property
def last_polled_at(self):
polls = [u.latest for u in self.polls.all() if u.latest]
return max(polls) if polls else None

@property
def last_updated_at(self):
updates = [u.latest for u in self.updates.all() if u.latest]
Expand Down
Loading