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: turn one big table query into endpoint #591

Merged
merged 2 commits into from
Apr 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
10 changes: 9 additions & 1 deletion bd_api/apps/api/v1/graphql.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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)
Original file line number Diff line number Diff line change
@@ -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",
),
]
5 changes: 3 additions & 2 deletions bd_api/apps/api/v1/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down Expand Up @@ -982,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):
Expand Down Expand Up @@ -1026,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"""
Expand Down
7 changes: 0 additions & 7 deletions bd_api/apps/api/v1/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
Loading