Skip to content

Commit

Permalink
feat: add a new parameter to get_table_one_big_table_query (#629)
Browse files Browse the repository at this point in the history
  • Loading branch information
jhonylucas74 authored Jul 3, 2024
1 parent c4c856c commit 00db381
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
7 changes: 5 additions & 2 deletions backend/apps/api/v1/graphql.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,14 @@ class Query(ObjectType):
get_table_one_big_table_query = String(
table_id=UUID(required=True),
columns=List(String),
include_table_translation=Boolean(),
)

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):
def resolve_get_table_one_big_table_query(
root, info, table_id, columns=None, include_table_translation=True, **kwargs
):
if table := Table.objects.filter(pk=table_id).first():
return table.gen_one_big_table_query(columns)
return table.gen_one_big_table_query(columns, include_table_translation)
18 changes: 11 additions & 7 deletions backend/apps/api/v1/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,7 @@ 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")
Expand Down Expand Up @@ -760,14 +761,15 @@ class Meta:

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):
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."
"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 @@ -1040,7 +1042,9 @@ def gen_neighbors(self) -> list[dict]:
)
return all_neighbors

def gen_one_big_table_query(self, columns: list[str] = None):
def gen_one_big_table_query(
self, columns: list[str] = None, include_table_translation: bool = True
):
"""Get a denormalized sql query, similar to a one big table"""

def has_directory(column: Column):
Expand Down Expand Up @@ -1086,7 +1090,7 @@ def get_components():
for column in self.columns.order_by("order").all():
if columns and column.name not in columns:
continue
if column.covered_by_dictionary:
if column.covered_by_dictionary and include_table_translation:
sql_cte_table = f"dicionario_{column.name}"
sql_cte = f"""
{sql_cte_table} AS (
Expand All @@ -1106,7 +1110,7 @@ def get_components():
sql_ctes.append(dedent(sql_cte))
sql_joins.append(dedent(sql_join))
sql_selects.append(dedent(sql_select))
elif has_directory(column):
elif has_directory(column) and include_table_translation:
sql_cte_table = f"diretorio_{column.name}"
sql_cte_column = get_column(column, column.dir_cloud_table)
sql_select_id = f"dados.{column.name} AS {column.name}"
Expand Down Expand Up @@ -1547,7 +1551,7 @@ def __str__(self):
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

0 comments on commit 00db381

Please sign in to comment.