11import re
22import statistics
33from collections import defaultdict
4- from typing import Any , DefaultDict , Dict , List , Optional , Union , cast
4+ from typing import DefaultDict , Dict , List , Optional , Union , cast
55
66from dateutil import tz
77
1616 TestResultSchema ,
1717 TestResultSummarySchema ,
1818 TestRunSchema ,
19+ TestSchema ,
20+ )
21+ from elementary .monitor .api .tests .utils import (
22+ get_display_name ,
23+ get_normalized_full_path ,
24+ get_table_full_name ,
25+ get_test_configuration ,
1926)
2027from elementary .monitor .api .totals_schema import TotalsSchema
2128from elementary .monitor .data_monitoring .schema import SelectorFilterSchema
2229from elementary .monitor .fetchers .tests .schema import (
2330 NormalizedTestSchema ,
31+ TestDBRowSchema ,
2432 TestResultDBRowSchema ,
2533)
2634from elementary .monitor .fetchers .tests .tests import TestsFetcher
@@ -130,6 +138,13 @@ def _get_test_subscribers(test_meta: dict, model_meta: dict) -> List[str]:
130138 def get_singular_tests (self ) -> List [NormalizedTestSchema ]:
131139 return self .tests_fetcher .get_singular_tests ()
132140
141+ def get_tests (self ) -> Dict [str , TestSchema ]:
142+ tests_db_rows = self .tests_fetcher .get_tests ()
143+ return {
144+ test_db_row .unique_id : self ._parse_test_db_row (test_db_row )
145+ for test_db_row in tests_db_rows
146+ }
147+
133148 def get_test_results (
134149 self ,
135150 invocation_id : Optional [str ],
@@ -304,26 +319,17 @@ def _parse_affected_row(results_description: str) -> Optional[int]:
304319 def _get_test_metadata_from_test_result_db_row (
305320 test_result_db_row : TestResultDBRowSchema ,
306321 ) -> TestMetadataSchema :
307- test_display_name = (
308- test_result_db_row .test_name .replace ("_" , " " ).title ()
309- if test_result_db_row .test_name
310- else ""
311- )
322+ test_display_name = get_display_name (test_result_db_row .test_name )
312323 detected_at_datetime = convert_utc_iso_format_to_datetime (
313324 test_result_db_row .detected_at
314325 )
315326 detected_at_utc = detected_at_datetime
316327 detected_at = detected_at_datetime .astimezone (tz .tzlocal ())
317- table_full_name_parts = [
318- name
319- for name in [
320- test_result_db_row .database_name ,
321- test_result_db_row .schema_name ,
322- test_result_db_row .table_name ,
323- ]
324- if name
325- ]
326- table_full_name = "." .join (table_full_name_parts ).lower ()
328+ table_full_name = get_table_full_name (
329+ test_result_db_row .database_name ,
330+ test_result_db_row .schema_name ,
331+ test_result_db_row .table_name ,
332+ )
327333 test_params = test_result_db_row .test_params
328334 test_query = (
329335 test_result_db_row .test_results_query .strip ()
@@ -336,23 +342,11 @@ def _get_test_metadata_from_test_result_db_row(
336342 result_query = test_query ,
337343 )
338344
339- configuration : Dict [str , Any ]
340- if test_result_db_row .test_type == "dbt_test" :
341- configuration = dict (
342- test_name = test_result_db_row .test_name ,
343- test_params = test_params ,
344- )
345- else :
346- time_bucket_configuration = test_params .get ("time_bucket" , {})
347- time_bucket_count = time_bucket_configuration .get ("count" , 1 )
348- time_bucket_period = time_bucket_configuration .get ("period" , "day" )
349- configuration = dict (
350- test_name = test_result_db_row .test_name ,
351- timestamp_column = test_params .get ("timestamp_column" ),
352- testing_timeframe = f"{ time_bucket_count } { time_bucket_period } { 's' if time_bucket_count > 1 else '' } " ,
353- anomaly_threshold = test_params .get ("sensitivity" )
354- or test_params .get ("anomaly_sensitivity" ),
355- )
345+ configuration = get_test_configuration (
346+ test_type = test_result_db_row .test_type ,
347+ name = test_result_db_row .test_name ,
348+ test_params = test_params ,
349+ )
356350
357351 return TestMetadataSchema (
358352 test_unique_id = test_result_db_row .test_unique_id ,
@@ -378,7 +372,56 @@ def _get_test_metadata_from_test_result_db_row(
378372 result = result ,
379373 configuration = configuration ,
380374 test_tags = test_result_db_row .test_tags ,
381- normalized_full_path = test_result_db_row .normalized_full_path ,
375+ normalized_full_path = get_normalized_full_path (
376+ test_result_db_row .package_name , test_result_db_row .original_path
377+ ),
378+ )
379+
380+ @classmethod
381+ def _parse_test_db_row (cls , test_db_row : TestDBRowSchema ) -> TestSchema :
382+ latest_run_datetime = (
383+ convert_utc_iso_format_to_datetime (test_db_row .latest_run_time )
384+ if test_db_row .latest_run_time
385+ else None
386+ )
387+
388+ return TestSchema (
389+ unique_id = test_db_row .unique_id ,
390+ model_unique_id = test_db_row .model_unique_id ,
391+ table_unique_id = get_table_full_name (
392+ test_db_row .database_name ,
393+ test_db_row .schema_name ,
394+ test_db_row .table_name ,
395+ ),
396+ database_name = test_db_row .database_name ,
397+ schema_name = test_db_row .schema_name ,
398+ table_name = test_db_row .table_name ,
399+ column_name = test_db_row .column_name ,
400+ name = test_db_row .name ,
401+ display_name = get_display_name (test_db_row .name ),
402+ original_path = test_db_row .original_path ,
403+ type = test_db_row .type ,
404+ test_type = test_db_row .test_type ,
405+ test_sub_type = test_db_row .test_sub_type ,
406+ test_params = test_db_row .test_params ,
407+ description = test_db_row .meta .get ("description" ),
408+ configuration = get_test_configuration (
409+ test_db_row .test_type , test_db_row .name , test_db_row .test_params
410+ ),
411+ tags = list (set (test_db_row .tags + test_db_row .model_tags )),
412+ normalized_full_path = get_normalized_full_path (
413+ test_db_row .package_name , test_db_row .original_path
414+ ),
415+ created_at = test_db_row .created_at if test_db_row .created_at else None ,
416+ latest_run_time = latest_run_datetime .isoformat ()
417+ if latest_run_datetime
418+ else None ,
419+ latest_run_time_utc = latest_run_datetime .astimezone (tz .tzlocal ()).isoformat ()
420+ if latest_run_datetime
421+ else None ,
422+ latest_run_status = test_db_row .latest_run_status
423+ if test_db_row .latest_run_status
424+ else None ,
382425 )
383426
384427 @staticmethod
0 commit comments