Skip to content

Commit

Permalink
fix: update temporal coverages
Browse files Browse the repository at this point in the history
  • Loading branch information
vncsna committed Feb 20, 2024
1 parent 5d6ae51 commit 7953b33
Showing 1 changed file with 55 additions and 123 deletions.
178 changes: 55 additions & 123 deletions bd_api/apps/api/v1/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -573,128 +573,25 @@ def get_graphql_full_slug(self):

@property
def coverage(self):
"""Get the temporal coverage of the dataset in the format YYYY-MM-DD - YYYY-MM-DD"""
tables = self.tables.all()
raw_data_sources = self.raw_data_sources.all()
information_requests = self.information_requests.all()
start_year, start_month, start_day = False, False, False
end_year, end_month, end_day = False, False, False

start_date = datetime(3000, 12, 31, 0, 0, 0)
end_date = datetime(1, 1, 1, 0, 0, 0)

# This must be refactored to avoid code duplication
for table in tables:
for coverage in table.coverages.all():
date_times = DateTimeRange.objects.filter(coverage=coverage.pk)
if len(date_times) == 0:
continue
date_time = get_date_time(date_times)

start_year = date_time.start_year if date_time.start_year else start_year
start_month = date_time.start_month if date_time.start_month else start_month
start_day = date_time.start_day if date_time.start_day else start_day
end_year = date_time.end_year if date_time.end_year else end_year
end_month = date_time.end_month if date_time.end_month else end_month
end_day = date_time.end_day if date_time.end_day else end_day

new_start_date = datetime(
date_time.start_year or 3000,
date_time.start_month or 1,
date_time.start_day or 1,
)
start_date = new_start_date if new_start_date < start_date else start_date
new_end_date = datetime(
date_time.end_year or 1,
date_time.end_month or 1,
date_time.end_day or 1,
)
end_date = new_end_date if new_end_date > end_date else end_date

for raw_data_source in raw_data_sources:
for coverage in raw_data_source.coverages.all():
date_times = DateTimeRange.objects.filter(coverage=coverage.pk)
if len(date_times) == 0:
continue
date_time = get_date_time(date_times)

start_year = date_time.start_year if date_time.start_year else start_year
start_month = date_time.start_month if date_time.start_month else start_month
start_day = date_time.start_day if date_time.start_day else start_day
end_year = date_time.end_year if date_time.end_year else end_year
end_month = date_time.end_month if date_time.end_month else end_month
end_day = date_time.end_day if date_time.end_day else end_day

new_start_date = datetime(
date_time.start_year or 3000,
date_time.start_month or 1,
date_time.start_day or 1,
)
start_date = new_start_date if new_start_date < start_date else start_date
new_end_date = datetime(
date_time.end_year or 1,
date_time.end_month or 1,
date_time.end_day or 1,
)
end_date = new_end_date if new_end_date > end_date else end_date

for information_request in information_requests:
for coverage in information_request.coverages.all():
date_times = DateTimeRange.objects.filter(coverage=coverage.pk)
if len(date_times) == 0:
continue
date_time = get_date_time(date_times)

start_year = date_time.start_year if date_time.start_year else start_year
start_month = date_time.start_month if date_time.start_month else start_month
start_day = date_time.start_day if date_time.start_day else start_day
end_year = date_time.end_year if date_time.end_year else end_year
end_month = date_time.end_month if date_time.end_month else end_month
end_day = date_time.end_day if date_time.end_day else end_day

new_start_date = datetime(
date_time.start_year or 3000,
date_time.start_month or 1,
date_time.start_day or 1,
)
start_date = new_start_date if new_start_date < start_date else start_date
new_end_date = datetime(
date_time.end_year or 1,
date_time.end_month or 1,
date_time.end_day or 1,
)
end_date = new_end_date if new_end_date > end_date else end_date

start = []
end = []

if start_year and start_year < 3000 and start_date.year:
start.append(str(start_date.year))
if start_month and start_date.month:
start.append(str(start_date.month).zfill(2))
if start_day and start_date.day:
start.append(str(start_date.day).zfill(2))

if end_year and end_year > 1 and end_date.year:
end.append(str(end_date.year))
if end_month and end_date.month:
end.append(str(end_date.month).zfill(2))
if end_day and end_date.day:
end.append(str(end_date.day).zfill(2))

coverage_str = ""
if start:
coverage_str += "-".join(start)
if end:
coverage_str += " - " + "-".join(end)

return coverage_str

def get_graphql_coverage(self):
"""
Returns the temporal coverage of the dataset in the format
YYYY-MM-DD - YYYY-MM-DD for graphql
"""
since = datetime.max
until = datetime.min
entities = [
*self.tables.all(),
*self.raw_data_sources.all(),
*self.information_requests.all(),
]
for entity in entities:
for cov in entity.coverages.all():
for dt in cov.datetime_ranges.all():
if dt.since and dt.since < since:
since = dt.since
if dt.until and dt.until > until:
until = dt.until
since = since.strftime("%Y") if since != datetime.max else None
until = until.strftime("%Y") if until != datetime.min else None
return {"since": since, "until": until}

def get_graphql_coverage(self) -> dict:
return self.coverage

@property
Expand Down Expand Up @@ -1027,7 +924,24 @@ def get_graphql_contains_closed_data(self):
return self.contains_closed_data

@property
def full_coverage(self) -> str:
def coverage(self):
since = datetime.max
until = datetime.min
for cov in self.coverages.all():
for dt in cov.datetime_ranges.all():
if dt.since and dt.since < since:
since = dt.since
if dt.until and dt.until > until:
until = dt.until
since = since.strftime("%Y") if since != datetime.max else None
until = until.strftime("%Y") if until != datetime.min else None
return {"since": since, "until": until}

def get_graphql_coverage(self) -> dict:
return self.coverage

@property
def full_coverage(self):
"""
Returns the full temporal coverage of the table as a json string
representing an object with the 3 initial points of the coverage
Expand Down Expand Up @@ -1320,6 +1234,24 @@ class Meta:
verbose_name_plural = "Columns"
ordering = ["name"]

@property
def coverage(self):
since = datetime.max
until = datetime.min
for entity in [self, self.table]:
for cov in entity.coverages.all():
for dt in cov.datetime_ranges.all():
if dt.since and dt.since < since:
since = dt.since
if dt.until and dt.until > until:
until = dt.until
since = since.strftime("%Y") if since != datetime.max else None
until = until.strftime("%Y") if until != datetime.min else None
return {"since": since, "until": until}

def get_graphql_coverage(self) -> dict:
return self.coverage

@property
def full_coverage(self) -> str:
"""
Expand Down

0 comments on commit 7953b33

Please sign in to comment.