22import pandas as pd
33from typing import Optional
44from datetime import datetime
5+ from tqdm .auto import tqdm
56from sqlalchemy .orm import sessionmaker
67from sqlalchemy .exc import SQLAlchemyError
78from sqlalchemy import create_engine , text
@@ -91,7 +92,7 @@ def load_postgres_extension(self):
9192 self .conn .execute ("INSTALL postgres_scanner;" )
9293 self .conn .execute ("LOAD postgres_scanner;" )
9394
94- def create_cohort_definition (self , cohort_definition : CohortDefinition ):
95+ def create_cohort_definition (self , cohort_definition : CohortDefinition , progress_obj = None ):
9596 self .conn .execute ('''
9697 INSERT INTO cohort_definition (name, description, created_date, creation_info, created_by)
9798 VALUES (?, ?, ?, ?, ?)
@@ -102,7 +103,10 @@ def create_cohort_definition(self, cohort_definition: CohortDefinition):
102103 cohort_definition .creation_info ,
103104 cohort_definition .created_by
104105 ))
105- print ("Cohort definition inserted successfully." )
106+ if progress_obj is None :
107+ print ("Cohort definition inserted successfully." )
108+ else :
109+ progress_obj .write ("Cohort definition inserted successfully." )
106110 self .conn .execute ("SELECT id from cohort_definition ORDER BY id DESC LIMIT 1" )
107111 created_cohort_id = self .conn .fetchone ()[0 ]
108112 return created_cohort_id
@@ -407,6 +411,14 @@ def get_concept_hierarchy(self, concept_id: int):
407411 Retrieves the full concept hierarchy (ancestors and descendants) for a given concept_id
408412 and organizes it into a nested dictionary to represent the tree structure.
409413 """
414+ stages = [
415+ "Queried concept hierarchy" ,
416+ "Fetched concept details" ,
417+ "Built hierarchy tree"
418+ ]
419+ progress = tqdm (total = len (stages ), desc = "Concept Hierarchy" , unit = "stage" )
420+
421+ progress .set_postfix_str (stages [0 ])
410422 query = """
411423 WITH RECURSIVE concept_hierarchy AS (
412424 SELECT ancestor_concept_id, descendant_concept_id, min_levels_of_separation
@@ -425,7 +437,9 @@ def get_concept_hierarchy(self, concept_id: int):
425437 """
426438
427439 results = self .execute_query (query , params = {"concept_id" : concept_id })
440+ progress .update (1 )
428441
442+ progress .set_postfix_str (stages [1 ])
429443 # Collect all unique concept IDs involved in the hierarchy using set comprehension
430444 concept_ids = {row ['ancestor_concept_id' ] for row in results } | {row ['descendant_concept_id' ] for row in results }
431445 # Fetch details of each concept
@@ -439,7 +453,9 @@ def get_concept_hierarchy(self, concept_id: int):
439453
440454 result = self .execute_query (query , params = {"concept_ids" : tuple (concept_ids )})
441455 concept_details = {row ['concept_id' ]: row for row in result }
456+ progress .update (1 )
442457
458+ progress .set_postfix_str (stages [2 ])
443459 # Build the hierarchy tree using a dictionary
444460 hierarchy = {}
445461 reverse_hierarchy = {}
@@ -458,6 +474,7 @@ def get_concept_hierarchy(self, concept_id: int):
458474 ancestor_entry_rev = reverse_hierarchy .setdefault (
459475 ancestor_id , {"details" : concept_details [ancestor_id ], "parents" : []})
460476 desc_entry_rev ["parents" ].append (ancestor_entry_rev )
477+ progress .update (1 )
461478
462479 # Return the parent hierarchy and children hierarchy of the specified concept
463480 return reverse_hierarchy [concept_id ], hierarchy [concept_id ]
0 commit comments