Skip to content

Commit a84b440

Browse files
committed
populate rules task executor
1 parent 77d78e3 commit a84b440

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import logging
2+
3+
from fastapi import requests
4+
from shared.database.database import with_db_session
5+
from shared.database_gen.sqlacodegen_models import Rule
6+
7+
RULES_JSON_URL = (
8+
"https://raw.githubusercontent.com/MobilityData/licenses-aas/main/data/rules.json"
9+
)
10+
11+
12+
def populate_license_rules_handler(payload):
13+
"""
14+
Handler for populating license rules.
15+
16+
Args:
17+
payload (dict): Incoming payload data.
18+
19+
"""
20+
(dry_run) = get_parameters(payload)
21+
return populate_license_rules_task(dry_run)
22+
23+
24+
@with_db_session
25+
def populate_license_rules_task(dry_run, db_session):
26+
"""
27+
Populates license rules in the database. This function is triggered by a Cloud Task.
28+
29+
Args:
30+
dry_run (bool): If True, the function will simulate the operation without making changes.
31+
db_session: Database session for executing queries.
32+
"""
33+
logging.info(f"Starting populate_license_rules_task with dry_run={dry_run}")
34+
35+
try:
36+
logging.info(f"Downloading rules from {RULES_JSON_URL}")
37+
response = requests.get(RULES_JSON_URL)
38+
response.raise_for_status()
39+
rules_data = response.json()
40+
logging.info(f"Rules data downloaded: {rules_data}")
41+
logging.info(f"Successfully downloaded {len(rules_data)} rules.")
42+
43+
if dry_run:
44+
logging.info("Dry run enabled. No changes will be made to the database.")
45+
logging.info(f"Would attempt to upsert {len(rules_data)} rules.")
46+
else:
47+
logging.info("Populating license rules in the database...")
48+
49+
for rule_data in rules_data:
50+
# Create a Rule ORM object from the downloaded data
51+
rule_object = Rule(
52+
name=rule_data.get("name"),
53+
label=rule_data.get("label"),
54+
description=rule_data.get("description"),
55+
type=rule_data.get("type"),
56+
)
57+
# Merge the object into the session.
58+
# If a rule with the same primary key (name) exists, it will be updated.
59+
# If not, a new one will be inserted.
60+
db_session.merge(rule_object)
61+
62+
db_session.commit()
63+
logging.info(
64+
f"License rules populated successfully. {len(rules_data)} rules were upserted."
65+
)
66+
except requests.exceptions.RequestException as e:
67+
logging.error(f"Failed to download rules JSON file: {e}")
68+
raise
69+
except Exception as e:
70+
logging.error(f"An error occurred while populating license rules: {e}")
71+
db_session.rollback()
72+
raise
73+
74+
75+
def get_parameters(payload):
76+
"""
77+
Get parameters from the payload and environment variables.
78+
79+
Args:
80+
payload (dict): dictionary containing the payload data.
81+
Returns:
82+
tuple: (dry_run, after_date)
83+
"""
84+
dry_run = payload.get("dry_run", False)
85+
return dry_run

0 commit comments

Comments
 (0)