Skip to content

Commit 0e88cc6

Browse files
committed
updating the previous hardcoded hierarchy file to use the json provided during creation of datasnapshot
1 parent ed8f8d9 commit 0e88cc6

3 files changed

Lines changed: 45 additions & 16 deletions

File tree

applications/sckanner/backend/sckanner/management/commands/update_knowledge_statements.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,24 @@ class JsonData(BaseModel):
5555
class Command(BaseCommand):
5656
help = "Fetch and update knowledge statements from an external server"
5757

58+
def add_arguments(self, parser):
59+
parser.add_argument(
60+
'--a_b_via_c_json_url',
61+
type=str,
62+
help='URL to the A-B-via-C JSON file',
63+
)
64+
5865
def handle(self, *args, **kwargs):
5966
self.stdout.write("Starting the ingestion process...")
60-
61-
# Step 1: Fetch raw JSON from external source
62-
raw_data_url = "https://raw.githubusercontent.com/smtifahim/SCKAN-Apps/master/sckan-explorer/json/a-b-via-c-2.json"
63-
self.stdout.write(f"Fetching data from {raw_data_url}...")
64-
response = requests.get(raw_data_url)
67+
a_b_via_c_json_url = kwargs.get('a_b_via_c_json_url')
68+
69+
# Step 1: Fetch raw JSON from URL
70+
if not a_b_via_c_json_url:
71+
# Fallback to hardcoded URL (for backward compatibility)
72+
a_b_via_c_json_url = "https://raw.githubusercontent.com/smtifahim/SCKAN-Apps/refs/heads/master/sckan-explorer/json/sckanner-data/hierarchy/sckanner-hierarchy.json"
73+
74+
self.stdout.write(f"Fetching data from {a_b_via_c_json_url}...")
75+
response = requests.get(a_b_via_c_json_url)
6576
response.raise_for_status()
6677
raw_data = JsonData(**response.json())
6778

@@ -125,4 +136,4 @@ def update_database(self, detailed_data):
125136
# Insert new data
126137
KnowledgeStatement.objects.bulk_create(
127138
[KnowledgeStatement(data=entry) for entry in detailed_data]
128-
)
139+
)

applications/sckanner/backend/sckanner/migrations/data/composer_ingestion_script.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -123,15 +123,25 @@ def fetch_paginated_data(population_ids: list[str], stdout=None):
123123
return detailed_data
124124

125125

126-
def get_statements(version="", stdout=None):
126+
def get_statements(version="", stdout=None, a_b_via_c_json_file_path=None):
127127
try:
128-
# Step 1: Fetch raw JSON from external source
129-
raw_data_url = "https://raw.githubusercontent.com/smtifahim/SCKAN-Apps/master/sckan-explorer/json/a-b-via-c-2.json"
130-
if stdout:
131-
stdout.write(f"Fetching data from {raw_data_url}...\n")
132-
response = requests.get(raw_data_url)
133-
response.raise_for_status()
134-
raw_data = JsonData(**response.json())
128+
# Step 1: Load raw JSON from file or external source
129+
if a_b_via_c_json_file_path:
130+
# Load from provided file path
131+
if stdout:
132+
stdout.write(f"Loading data from file: {a_b_via_c_json_file_path}...\n")
133+
import json
134+
with open(a_b_via_c_json_file_path, 'r') as f:
135+
raw_json = json.load(f)
136+
raw_data = JsonData(**raw_json)
137+
else:
138+
# Fallback to hardcoded URL (for backward compatibility)
139+
raw_data_url = "https://raw.githubusercontent.com/smtifahim/SCKAN-Apps/refs/heads/master/sckan-explorer/json/sckanner-data/hierarchy/sckanner-hierarchy.json"
140+
if stdout:
141+
stdout.write(f"Fetching data from {raw_data_url}...\n")
142+
response = requests.get(raw_data_url)
143+
response.raise_for_status()
144+
raw_data = JsonData(**response.json())
135145

136146
# Step 2: Extract population IDs
137147
if stdout:
@@ -149,7 +159,7 @@ def get_statements(version="", stdout=None):
149159
# --- NOTE: ONLY FOR TESTING LOCALLY ---
150160

151161
for population_id in batched(population_ids, KNOWLEDGE_STATEMENTS_BATCH_SIZE):
152-
detailed_data.extend(fetch_paginated_data(list(population_id)))
162+
detailed_data.extend(fetch_paginated_data(list(population_id), stdout))
153163

154164
if stdout:
155165
stdout.write(f"Ingestion process completed successfully! Total statements: {len(detailed_data)}\n")

applications/sckanner/backend/sckanner/services/ingestion/connectivity_statement_adapter.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,15 @@ def _parse_and_validate_statements(
4040

4141
# Get statements from the uploaded module
4242
if hasattr(module, "get_statements") and callable(module.get_statements):
43-
statements = module.get_statements(self.snapshot.version)
43+
# Pass the a_b_via_c_json_file path if available
44+
a_b_via_c_json_file_path = None
45+
if self.snapshot.a_b_via_c_json_file:
46+
a_b_via_c_json_file_path = self.snapshot.a_b_via_c_json_file.path
47+
48+
statements = module.get_statements(
49+
self.snapshot.version,
50+
a_b_via_c_json_file_path=a_b_via_c_json_file_path
51+
)
4452
try:
4553
# Validate the statements against the schema
4654
current_path = os.path.dirname(os.path.abspath(__file__))

0 commit comments

Comments
 (0)