perf(organizations): speed up filtered API lists
This commit is contained in:
@@ -6,6 +6,7 @@ from django.db import models
|
||||
from django.db.models import Q
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from organizations.data_sources import snapshot_data_source_summary
|
||||
from organizations.name_normalization import normalize_organization_name
|
||||
|
||||
|
||||
@@ -116,6 +117,11 @@ class OrganizationDataSnapshot(models.Model):
|
||||
default=list,
|
||||
help_text=_("Готовый JSON registries для API v2"),
|
||||
)
|
||||
data_source_counts = models.JSONField(
|
||||
_("счетчики источников"),
|
||||
default=list,
|
||||
help_text=_("Готовый JSON data_sources для API v2"),
|
||||
)
|
||||
updated_at = models.DateTimeField(
|
||||
_("дата обновления"),
|
||||
auto_now=True,
|
||||
@@ -129,3 +135,14 @@ class OrganizationDataSnapshot(models.Model):
|
||||
|
||||
def __str__(self) -> str:
|
||||
return f"Snapshot for {self.organization_id}"
|
||||
|
||||
def save(self, *args, **kwargs) -> None:
|
||||
update_fields = kwargs.get("update_fields")
|
||||
if update_fields is None or "data" in update_fields:
|
||||
self.data_source_counts = snapshot_data_source_summary(self.data)
|
||||
if update_fields is not None:
|
||||
kwargs["update_fields"] = list(
|
||||
dict.fromkeys([*update_fields, "data_source_counts"])
|
||||
)
|
||||
|
||||
super().save(*args, **kwargs)
|
||||
|
||||
Reference in New Issue
Block a user