33from sqlalchemy import func
44from sqlalchemy .orm .attributes import flag_modified
55
6+
67from ..business_objects import general
8+ from ..integration_objects import manager as integration_records_bo
79from ..session import session
8- from ..models import CognitionIntegration , CognitionGroup
10+ from ..models import CognitionIntegration , CognitionGroup , EtlTask
911from ..enums import (
1012 CognitionMarkdownFileState ,
1113 CognitionIntegrationType ,
@@ -147,6 +149,49 @@ def get_last_synced_at(
147149 return result [0 ] if result else None
148150
149151
152+ def get_active_etl_tasks (
153+ integration_id : str ,
154+ ) -> List [EtlTask ]:
155+ IntegrationModel = integration_records_bo .integration_model (integration_id )
156+ return (
157+ session .query (EtlTask )
158+ .filter (EtlTask .is_active == True )
159+ .join (
160+ IntegrationModel ,
161+ (EtlTask .id == IntegrationModel .etl_task_id )
162+ & (IntegrationModel .integration_id == integration_id ),
163+ )
164+ .all ()
165+ )
166+
167+
168+ def get_all_etl_tasks (
169+ integration_id : str ,
170+ ) -> List [EtlTask ]:
171+ IntegrationModel = integration_records_bo .integration_model (integration_id )
172+ return (
173+ session .query (EtlTask )
174+ .join (
175+ IntegrationModel ,
176+ (IntegrationModel .etl_task_id == EtlTask .id )
177+ & (IntegrationModel .integration_id == integration_id ),
178+ )
179+ .all ()
180+ )
181+
182+
183+ def get_integration_progress (
184+ integration_id : str ,
185+ ) -> float :
186+ count_all_records = integration_records_bo .count (integration_id )
187+ all_tasks = get_all_etl_tasks (integration_id )
188+ finished_tasks = [task for task in all_tasks if task .state in FINISHED_STATES ]
189+
190+ if count_all_records == 0 :
191+ return 0.0
192+ return round ((len (finished_tasks ) / count_all_records ) * 100.0 , 2 )
193+
194+
150195def count_org_integrations (org_id : str ) -> Dict [str , int ]:
151196 counts = (
152197 session .query (CognitionIntegration .type , func .count (CognitionIntegration .id ))
@@ -201,6 +246,7 @@ def create(
201246
202247def update (
203248 id : str ,
249+ project_id : Optional [str ] = None ,
204250 updated_by : Optional [str ] = None ,
205251 name : Optional [str ] = None ,
206252 description : Optional [str ] = None ,
@@ -220,6 +266,8 @@ def update(
220266 if not integration :
221267 return None
222268
269+ if project_id is not None and integration .project_id is None :
270+ integration .project_id = project_id
223271 if updated_by is not None :
224272 integration .updated_by = updated_by
225273 if name is not None :
@@ -279,6 +327,16 @@ def execution_finished(id: str) -> bool:
279327def delete_many (
280328 ids : List [str ], delete_cognition_groups : bool = True , with_commit : bool = True
281329) -> None :
330+ for id in ids :
331+ integration_records , IntegrationModel = (
332+ integration_records_bo .get_all_by_integration_id (id )
333+ )
334+ integration_records_bo .delete_many (
335+ IntegrationModel ,
336+ ids = [rec .id for rec in integration_records ],
337+ with_commit = True ,
338+ )
339+
282340 (
283341 session .query (CognitionIntegration )
284342 .filter (CognitionIntegration .id .in_ (ids ))
@@ -290,6 +348,7 @@ def delete_many(
290348 .filter (CognitionGroup .meta_data .op ("->>" )("integration_id" ).in_ (ids ))
291349 .delete (synchronize_session = False )
292350 )
351+
293352 general .flush_or_commit (with_commit )
294353
295354
0 commit comments