Skip to content

Commit

Permalink
feat: add table neighbors (#552)
Browse files Browse the repository at this point in the history
  • Loading branch information
vncsna authored Feb 1, 2024
1 parent 78bf03a commit 0b86a65
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 21 deletions.
74 changes: 53 additions & 21 deletions bd_api/apps/api/v1/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,6 @@ def has_picture(self):
has_picture.short_description = "Has Picture"
has_picture.boolean = True

@property
def get_graphql_has_picture(self):
"""Get the has_picture property for graphql"""
return self.has_picture()
Expand All @@ -485,7 +484,6 @@ def full_slug(self):

return f"{self.slug}"

@property
def get_graphql_full_slug(self):
"""Get the full slug or Organization for graphql"""
return self.full_slug
Expand Down Expand Up @@ -573,7 +571,6 @@ def full_slug(self):
return f"{self.organization.area.slug}_{self.organization.slug}_{self.slug}"
return f"{self.organization.slug}_{self.slug}"

@property
def get_graphql_full_slug(self):
"""Get the full slug or Dataset for graphql"""
return self.full_slug
Expand Down Expand Up @@ -697,7 +694,6 @@ def coverage(self):

return coverage_str

@property
def get_graphql_coverage(self):
"""
Returns the temporal coverage of the dataset in the format
Expand Down Expand Up @@ -726,7 +722,6 @@ def full_coverage(self) -> str:
]
return json.dumps(full_coverage_dict)

@property
def get_graphql_full_coverage(self):
return self.full_coverage

Expand All @@ -735,7 +730,6 @@ def contains_tables(self):
"""Returns true if there are tables in the dataset"""
return len(self.tables.all()) > 0

@property
def get_graphql_contains_tables(self):
"""Returns true if there are tables in the dataset for graphql"""
return self.contains_tables
Expand All @@ -757,7 +751,6 @@ def contains_closed_data(self):

return closed_data

@property
def get_graphql_contains_closed_data(self):
"""Returns true if there are tables or columns with closed coverages for graphql"""
return self.contains_closed_data
Expand All @@ -775,7 +768,6 @@ def contains_open_data(self):

return open_data

@property
def get_graphql_contains_open_data(self):
"""Returns true if there are tables or columns with open coverages for graphql"""
return self.contains_open_data
Expand All @@ -786,7 +778,6 @@ def contains_closed_tables(self):
closed_tables = self.tables.all().filter(is_closed=True)
return len(closed_tables) > 0

@property
def get_graphql_contains_closed_tables(self):
"""Returns true if there are tables with closed coverages for graphql (DEPRECATED)"""
return self.contains_closed_tables
Expand All @@ -797,7 +788,6 @@ def contains_open_tables(self):
open_tables = self.tables.all().filter(is_closed=False)
return len(open_tables) > 0

@property
def get_graphql_contains_open_tables(self):
"""Returns true if there are tables with open coverages for graphql (DEPRECATED)"""
return self.contains_open_tables
Expand All @@ -807,7 +797,6 @@ def contains_raw_data_sources(self):
"""Returns true if there are raw data sources in the dataset"""
return len(self.raw_data_sources.all()) > 0

@property
def get_graphql_contains_raw_data_sources(self):
"""Returns true if there are raw data sources in the dataset for graphql"""
return self.contains_raw_data_sources
Expand All @@ -817,7 +806,6 @@ def contains_information_requests(self):
"""Returns true if there are information requests in the dataset"""
return len(self.information_requests.all()) > 0

@property
def get_graphql_contains_information_requests(self):
"""Returns true if there are information requests in the dataset for graphql"""
return self.contains_information_requests
Expand Down Expand Up @@ -981,6 +969,9 @@ def gbq_slug(self):
table = cloud_table.gcp_table_id
return f"basedosdados.{dataset}.{table}"

def get_graphql_gbq_slug(self):
return self.gbq_slug

@property
def gcs_slug(self):
"""Get the slug used in Google Cloud Storage"""
Expand All @@ -989,11 +980,6 @@ def gcs_slug(self):
table = cloud_table.gcp_table_id
return f"staging/{dataset}/{table}"

@property
def get_graphql_gbq_slug(self):
return self.gbq_slug

@property
def get_graphql_gcs_slug(self):
return self.gcs_slug

Expand All @@ -1003,7 +989,6 @@ def partitions(self):
partitions_list = [p.name for p in self.columns.all().filter(is_partition=True)]
return ", ".join(partitions_list)

@property
def get_graphql_partitions(self):
"""Returns a list of columns used to partition the table"""
return self.partitions
Expand All @@ -1022,7 +1007,6 @@ def contains_closed_data(self):

return closed_data

@property
def get_graphql_contains_closed_data(self):
"""Returns true if there are columns with closed coverages to be used in graphql"""
return self.contains_closed_data
Expand Down Expand Up @@ -1105,10 +1089,34 @@ def full_coverage(self) -> str:

return json.dumps(full_coverage)

@property
def get_graphql_full_coverage(self):
return self.full_coverage

@property
def neighbors(self):
"""Similiar tables and columns
- Tables and columns with similar directories
- Tables and columns with similar coverages or tags (WIP)
"""
all_neighbors = []
for column in self.columns.all():
for neighbor in column.neighbors:
all_neighbors.append((neighbor.table, neighbor))
return all_neighbors

def get_graphql_neighbors(self) -> list[dict]:
all_neighbors = []
for table, column in self.neighbors:
all_neighbors.append(
{
"table_id": str(table.id),
"table_name": table.name,
"column_id": str(column.id),
"column_name": column.name,
}
)
return all_neighbors

def clean(self):
"""
Clean method for Table model
Expand Down Expand Up @@ -1298,10 +1306,34 @@ def full_coverage(self) -> str:

return json.dumps(column_full_coverage)

@property
def get_graphql_full_coverage(self):
return self.full_coverage

@property
def neighbors(self):
"""Similiar tables and columns
- Columns with similar directories
- Columns with similar coverages or tags (WIP)
"""
if not self.directory_primary_key:
return []
return (
Column.objects.filter(directory_primary_key=self.directory_primary_key)
.exclude(id=self.id)
.all()
)

def get_graphql_neighbors(self) -> list[dict]:
all_neighbors = []
for column in self.neighbors:
all_neighbors.append(
{
"column_id": str(column.id),
"column_name": column.name,
}
)
return all_neighbors


class ColumnOriginalName(BaseModel):
"""Model definition for ColumnOriginalName."""
Expand Down
3 changes: 3 additions & 0 deletions bd_api/custom/graphql_auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
Schema,
String,
)
from graphene.types.generic import GenericScalar
from graphene.types.utils import yank_fields_from_attrs
from graphene_django import DjangoObjectType
from graphene_django.converter import convert_django_field
Expand Down Expand Up @@ -391,6 +392,8 @@ def match_type(model, attr):
return partial(List, of_type=Int)
if "str" in get_type(model, attr):
return partial(List, of_type=String)
if "dict" in get_type(model, attr):
return GenericScalar
return String

def build_custom_attrs(model, attrs):
Expand Down

0 comments on commit 0b86a65

Please sign in to comment.