Skip to content

Commit

Permalink
adding logic to table full_coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
mfagundes committed Aug 11, 2023
1 parent 1e5d865 commit b6448d5
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 16 deletions.
86 changes: 76 additions & 10 deletions basedosdados_api/api/v1/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -729,9 +729,9 @@ def full_coverage(self) -> str:
str: json string representing the full coverage
"""
full_coverage_dict = [
{"year": 2021, "month": 6, "type": "open"},
{"year": 2023, "month": 6, "type": "open"},
{"year": 2026, "month": 6, "type": "closed"},
# {"year": 2021, "month": 6, "type": "open"},
# {"year": 2023, "month": 6, "type": "open"},
# {"year": 2026, "month": 6, "type": "closed"},
]
return json.dumps(full_coverage_dict)

Expand Down Expand Up @@ -1021,7 +1021,7 @@ def get_graphql_contains_closed_data(self):
@property
def full_coverage(self) -> str:
"""
Returns the full temporal coverage of the dataset as a json string
Returns the full temporal coverage of the table as a json string
representing an object with the 3 initial points of the coverage
The first point is the start of the open coverage, the second point is the
end of the open coverage and the third point is the end of closed coverage
Expand All @@ -1032,12 +1032,78 @@ def full_coverage(self) -> str:
Returns:
str: json string representing the full coverage
"""
full_coverage_dict = [
{"year": 2021, "month": 6, "type": "open"},
{"year": 2023, "month": 6, "type": "open"},
{"year": 2026, "month": 6, "type": "closed"},
]
return json.dumps(full_coverage_dict)
# First area of all coverages - thus must be changed to get all areas
try:
first_area = self.coverages.first().area
except AttributeError:
return ""
# First open coverage of a table - it's an open coverage for now
try:
first_open_datetime_range = (
self.coverages.filter(area=first_area, is_closed=False)
.first()
.datetime_ranges.order_by("start_year", "start_month", "start_day")
.first()
)
except AttributeError:
first_open_datetime_range = None
# First closed coverage of a table - it's a closed coverage for now
try:
first_closed_datetime_range = (
self.coverages.filter(area=first_area, is_closed=True)
.first()
.datetime_ranges.order_by("start_year", "start_month", "start_day")
.first()
)
except AttributeError:
first_closed_datetime_range = None
full_coverage = []
if first_open_datetime_range:
full_coverage.append(
{
"year": str(first_open_datetime_range.start_year)
if first_open_datetime_range.start_year
else None,
"month": str(first_open_datetime_range.start_month).zfill(2)
if first_open_datetime_range.end_month
else None,
"day": str(first_open_datetime_range.start_day).zfill(2)
if first_open_datetime_range.start_day
else None,
"type": "open",
}
)
full_coverage.append(
{
"year": str(first_open_datetime_range.end_year)
if first_open_datetime_range.end_year
else None,
"month": str(first_open_datetime_range.end_month).zfill(2)
if first_open_datetime_range.end_month
else None,
"day": str(first_open_datetime_range.end_day).zfill(2)
if first_open_datetime_range.end_day
else None,
"type": "open",
}
)
if first_closed_datetime_range:
full_coverage.append(
{
"year": str(first_closed_datetime_range.end_year)
if first_closed_datetime_range.end_year
else None,
"month": str(first_closed_datetime_range.end_month).zfill(2)
if first_closed_datetime_range.end_month
else None,
"day": str(first_closed_datetime_range.end_day).zfill(2)
if first_closed_datetime_range.end_day
else None,
"type": "closed",
}
)

return json.dumps(full_coverage)

@property
def get_graphql_full_coverage(self):
Expand Down
12 changes: 6 additions & 6 deletions basedosdados_api/api/v1/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ def test_cloud_table_create(tabela_bairros):


@pytest.mark.django_db
def test_dataset_multiple_coverages(
def test_table_with_multiple_coverages(
tabela_bairros,
coverage_tabela_open,
coverage_tabela_closed,
Expand All @@ -270,10 +270,10 @@ def test_dataset_multiple_coverages(
tabela_bairros.coverages.add(coverage_tabela_open, coverage_tabela_closed)
tabela_bairros.save()

expected_coverage = [
{"year": 2021, "month": 6, "type": "open"},
{"year": 2023, "month": 6, "type": "open"},
{"year": 2026, "month": 6, "type": "closed"},
table_expected_coverage = [
{"year": "2021", "month": "06", "day": None, "type": "open"},
{"year": "2023", "month": "06", "day": None, "type": "open"},
{"year": "2026", "month": "06", "day": None, "type": "closed"},
]

assert tabela_bairros.dataset.full_coverage == json.dumps(expected_coverage)
assert tabela_bairros.full_coverage == json.dumps(table_expected_coverage)

0 comments on commit b6448d5

Please sign in to comment.