1111from testgen .common .models import get_current_session
1212from testgen .common .models .custom_types import NullIfEmptyString , YNString
1313from testgen .common .models .entity import ENTITY_HASH_FUNCS , Entity , EntityMinimal
14+ from testgen .common .models .scheduler import RUN_TESTS_JOB_KEY , JobSchedule
1415from testgen .common .models .scores import ScoreDefinition
1516from testgen .common .models .test_suite import TestSuite
1617
@@ -52,6 +53,11 @@ class TableGroup(Entity):
5253 id : UUID = Column (postgresql .UUID (as_uuid = True ), primary_key = True , default = uuid4 )
5354 project_code : str = Column (String , ForeignKey ("projects.project_code" ))
5455 connection_id : int = Column (BigInteger , ForeignKey ("connections.connection_id" ))
56+ monitor_test_suite_id : UUID | None = Column (
57+ postgresql .UUID (as_uuid = True ),
58+ ForeignKey ("test_suites.id" ),
59+ default = None ,
60+ )
5561 table_groups_name : str = Column (String )
5662 table_group_schema : str = Column (String )
5763 profiling_table_set : str = Column (NullIfEmptyString )
@@ -260,7 +266,12 @@ def clear_cache(cls) -> bool:
260266 cls .select_minimal_where .clear ()
261267 cls .select_summary .clear ()
262268
263- def save (self , add_scorecard_definition : bool = False ) -> None :
269+ def save (
270+ self ,
271+ add_scorecard_definition : bool = False ,
272+ add_monitor_test_suite : bool = False ,
273+ monitor_schedule_timezone : str = "UTC" ,
274+ ) -> None :
264275 if self .id :
265276 values = {
266277 column .key : getattr (self , column .key , None )
@@ -273,7 +284,38 @@ def save(self, add_scorecard_definition: bool = False) -> None:
273284 db_session .commit ()
274285 else :
275286 super ().save ()
287+ db_session = get_current_session ()
288+
276289 if add_scorecard_definition :
277290 ScoreDefinition .from_table_group (self ).save ()
278291
292+ if add_monitor_test_suite :
293+ test_suite = TestSuite (
294+ project_code = self .project_code ,
295+ test_suite = f"{ self .table_groups_name } Monitor" ,
296+ connection_id = self .connection_id ,
297+ table_groups_id = self .id ,
298+ export_to_observability = False ,
299+ dq_score_exclude = True ,
300+ view_mode = "Monitor" ,
301+ )
302+ test_suite .save ()
303+
304+ schedule_job = JobSchedule (
305+ project_code = self .project_code ,
306+ key = RUN_TESTS_JOB_KEY ,
307+ cron_expr = "0 * * * *" ,
308+ cron_tz = monitor_schedule_timezone ,
309+ args = [],
310+ kwargs = {"project_key" : self .project_code , "test_suite_key" : test_suite .test_suite },
311+ )
312+ db_session .add (schedule_job )
313+
314+ self .monitor_test_suite_id = test_suite .id
315+ db_session .execute (
316+ update (TableGroup )
317+ .where (TableGroup .id == self .id ).values (monitor_test_suite_id = test_suite .id )
318+ )
319+ db_session .commit ()
320+
279321 TableGroup .clear_cache ()
0 commit comments