From b58a37c56e2a4908d222908660d1c58a0c9d3969 Mon Sep 17 00:00:00 2001 From: Vinicius Date: Fri, 26 Apr 2024 08:05:01 -0300 Subject: [PATCH 1/2] chore: remove one big table query field --- .../0030_remove_table_one_big_table_query.py | 17 +++++++++++++++++ bd_api/apps/api/v1/models.py | 1 - bd_api/apps/api/v1/tasks.py | 7 ------- 3 files changed, 17 insertions(+), 8 deletions(-) create mode 100644 bd_api/apps/api/v1/migrations/0030_remove_table_one_big_table_query.py diff --git a/bd_api/apps/api/v1/migrations/0030_remove_table_one_big_table_query.py b/bd_api/apps/api/v1/migrations/0030_remove_table_one_big_table_query.py new file mode 100644 index 00000000..f9b031ca --- /dev/null +++ b/bd_api/apps/api/v1/migrations/0030_remove_table_one_big_table_query.py @@ -0,0 +1,17 @@ +# -*- coding: utf-8 -*- +# Generated by Django 4.2.10 on 2024-04-26 11:05 + +from django.db import migrations + + +class Migration(migrations.Migration): + dependencies = [ + ("v1", "0029_table_one_big_table_query"), + ] + + operations = [ + migrations.RemoveField( + model_name="table", + name="one_big_table_query", + ), + ] diff --git a/bd_api/apps/api/v1/models.py b/bd_api/apps/api/v1/models.py index 132439ce..a4fbaeb5 100644 --- a/bd_api/apps/api/v1/models.py +++ b/bd_api/apps/api/v1/models.py @@ -785,7 +785,6 @@ class Table(BaseModel, OrderedModel): default=0, help_text="Number of page views by Google Analytics", ) - one_big_table_query = models.TextField(blank=True, null=True) order_with_respect_to = ("dataset",) graphql_nested_filter_fields_whitelist = ["id", "dataset"] diff --git a/bd_api/apps/api/v1/tasks.py b/bd_api/apps/api/v1/tasks.py index 65421611..f75f8fe8 100644 --- a/bd_api/apps/api/v1/tasks.py +++ b/bd_api/apps/api/v1/tasks.py @@ -124,13 +124,6 @@ def update_table_neighbors_task(): TableNeighbor.objects.update_or_create(**neighbor) -@periodic_task(crontab(day_of_week="0", hour="6", minute="20")) -def update_table_one_big_table_query_task(): - for table in Table.objects.all(): - table.one_big_table_query = table.gen_one_big_table_query() - table.save() - - @periodic_task(crontab(day_of_week="1-5", hour="7", minute="0")) @production_task def update_page_views_task(backfill: bool = False): From b748e964f0448a3f79a92543b77b68328721d7d2 Mon Sep 17 00:00:00 2001 From: Vinicius Date: Fri, 26 Apr 2024 08:22:56 -0300 Subject: [PATCH 2/2] feat: turn one big query table into endpoint --- bd_api/apps/api/v1/graphql.py | 10 +++++++++- bd_api/apps/api/v1/models.py | 4 +++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/bd_api/apps/api/v1/graphql.py b/bd_api/apps/api/v1/graphql.py index 0ec02ff3..384881eb 100644 --- a/bd_api/apps/api/v1/graphql.py +++ b/bd_api/apps/api/v1/graphql.py @@ -3,7 +3,7 @@ from graphene import UUID, Boolean, Float, List, ObjectType, String from graphene_django import DjangoObjectType -from bd_api.apps.api.v1.models import TableNeighbor +from bd_api.apps.api.v1.models import Table, TableNeighbor from bd_api.custom.graphql_base import PlainTextNode @@ -45,6 +45,14 @@ class Query(ObjectType): theme=String(), share_theme=Boolean(), ) + get_table_one_big_table_query = String( + table_id=UUID(required=True), + columns=List(String), + ) def resolve_get_table_neighbor(root, info, table_id, **kwargs): return TableNeighbor.objects.filter(table_a__pk=table_id).all() + + def resolve_get_table_one_big_table_query(root, info, table_id, columns=None, **kwargs): + if table := Table.objects.filter(pk=table_id).first(): + return table.gen_one_big_table_query(columns) diff --git a/bd_api/apps/api/v1/models.py b/bd_api/apps/api/v1/models.py index a4fbaeb5..e1f39f22 100644 --- a/bd_api/apps/api/v1/models.py +++ b/bd_api/apps/api/v1/models.py @@ -981,7 +981,7 @@ def gen_neighbors(self) -> list[dict]: ) return all_neighbors - def gen_one_big_table_query(self): + def gen_one_big_table_query(self, columns: list[str] = None): """Get a denormalized sql query, similar to a one big table""" def has_directory(column: Column): @@ -1025,6 +1025,8 @@ def get_components(): column: Column for column in self.columns.order_by("order").all(): + if columns and column.name not in columns: + continue if column.covered_by_dictionary: sql_cte_table = f"dicionario_{column.name}" sql_cte = f"""