Skip to content

Commit

Permalink
types cleanup + fix test results totals
Browse files Browse the repository at this point in the history
  • Loading branch information
NoyaArie committed Mar 6, 2024
1 parent 3495da6 commit e52b704
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 48 deletions.
8 changes: 4 additions & 4 deletions elementary/monitor/api/filters/filters.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Dict, List, Optional
from typing import Dict, List

from elementary.clients.api.api_client import APIClient
from elementary.monitor.api.filters.schema import FilterSchema, FiltersSchema
Expand All @@ -20,8 +20,8 @@
class FiltersAPI(APIClient):
def get_filters(
self,
test_results_totals: Dict[Optional[str], TotalsSchema],
test_runs_totals: Dict[Optional[str], TotalsSchema],
test_results_totals: Dict[str, TotalsSchema],
test_runs_totals: Dict[str, TotalsSchema],
models: Dict[str, NormalizedModelSchema],
sources: Dict[str, NormalizedSourceSchema],
models_runs: List[ModelRunsSchema],
Expand All @@ -39,7 +39,7 @@ def get_filters(

@staticmethod
def _get_test_filters(
totals: Dict[Optional[str], TotalsSchema],
totals: Dict[str, TotalsSchema],
models: Dict[str, NormalizedModelSchema],
sources: Dict[str, NormalizedSourceSchema],
) -> List[FilterSchema]:
Expand Down
36 changes: 16 additions & 20 deletions elementary/monitor/api/report/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,11 @@ def get_report_data(

invocations_job_identification = defaultdict(list)
for invocation in invocations:
key = invocation.job_name or invocation.job_id
if key is not None:
invocations_job_identification[key].append(invocation.invocation_id)
invocation_key = invocation.job_name or invocation.job_id
if invocation_key is not None:
invocations_job_identification[invocation_key].append(
invocation.invocation_id
)

report_data = ReportDataSchema(
creation_time=get_now_utc_iso_format(),
Expand Down Expand Up @@ -193,33 +195,27 @@ def _serialize_models_runs(self, models_runs: List[ModelRunsSchema]) -> List[dic
def _serialize_test_results(
self,
test_results: Dict[
Optional[str], List[Union[TestResultSchema, SourceFreshnessResultSchema]]
str, List[Union[TestResultSchema, SourceFreshnessResultSchema]]
],
) -> Dict[Optional[str], List[dict]]:
) -> Dict[str, List[dict]]:
serializable_test_results = defaultdict(list)
for model_unique_id, test_result in test_results.items():
serializable_test_results[model_unique_id].extend(
for key, test_result in test_results.items():
serializable_test_results[key].extend(
[result.dict() for result in test_result]
)
return serializable_test_results

def _serialize_test_runs(
self,
test_runs: Dict[
Optional[str], List[Union[TestRunSchema, SourceFreshnessRunSchema]]
],
) -> Dict[Optional[str], List[dict]]:
test_runs: Dict[str, List[Union[TestRunSchema, SourceFreshnessRunSchema]]],
) -> Dict[str, List[dict]]:
serializable_test_runs = defaultdict(list)
for model_unique_id, test_run in test_runs.items():
serializable_test_runs[model_unique_id].extend(
[run.dict() for run in test_run]
)
for key, test_run in test_runs.items():
serializable_test_runs[key].extend([run.dict() for run in test_run])
return serializable_test_runs

def _serialize_totals(
self, totals: Dict[Optional[str], TotalsSchema]
) -> Dict[Optional[str], dict]:
def _serialize_totals(self, totals: Dict[str, TotalsSchema]) -> Dict[str, dict]:
serialized_totals = dict()
for model_unique_id, total in totals.items():
serialized_totals[model_unique_id] = total.dict()
for key, total in totals.items():
serialized_totals[key] = total.dict()
return serialized_totals
34 changes: 10 additions & 24 deletions elementary/monitor/api/report/totals_utils.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,24 @@
from collections import defaultdict
from typing import Dict, List, Optional, Union
from typing import Dict, List, Union

from elementary.monitor.api.source_freshnesses.schema import (
SourceFreshnessMetadataSchema,
SourceFreshnessResultSchema,
SourceFreshnessRunSchema,
)
from elementary.monitor.api.tests.schema import (
TestMetadataSchema,
TestResultSchema,
TestRunSchema,
)
from elementary.monitor.api.tests.schema import TestResultSchema, TestRunSchema
from elementary.monitor.api.totals_schema import TotalsSchema


def get_total_test_results(
test_results: Dict[
Optional[str], List[Union[TestResultSchema, SourceFreshnessResultSchema]]
],
) -> Dict[Optional[str], TotalsSchema]:
test_metadatas = []
for test_result in test_results.values():
test_metadatas.extend([result.metadata for result in test_result])
test_results: Dict[str, List[Union[TestResultSchema, SourceFreshnessResultSchema]]],
) -> Dict[str, TotalsSchema]:
totals: Dict[str, TotalsSchema] = defaultdict(TotalsSchema)
for key, test_result in test_results.items():
for result in test_result:
# count by the key of the tests_results
totals[key].add_total(result.metadata.latest_run_status)

return _calculate_test_results_totals(test_metadatas)
return totals


def get_total_test_runs(
Expand All @@ -43,12 +38,3 @@ def get_total_test_runs(
# count by the key of the tests_runs
totals[key].add_total(test_invocation.status)
return totals


def _calculate_test_results_totals(
test_metadatas: List[Union[TestMetadataSchema, SourceFreshnessMetadataSchema]],
) -> Dict[Optional[str], TotalsSchema]:
totals: Dict[Optional[str], TotalsSchema] = defaultdict(TotalsSchema)
for test in test_metadatas:
totals[test.model_unique_id].add_total(test.latest_run_status)
return totals

0 comments on commit e52b704

Please sign in to comment.