|
| 1 | +import requests |
| 2 | +from requests.exceptions import RequestException |
| 3 | +from datetime import date, datetime, timedelta, timezone |
| 4 | +from jinja2 import Environment, FileSystemLoader, select_autoescape |
| 5 | +from typing import Optional, Set |
| 6 | +from _projects import get_mongo_db |
| 7 | +from _keycloak import Keycloak |
| 8 | +from _ches import Ches |
| 9 | + |
| 10 | + |
| 11 | +def get_holiday_dates(province: str) -> set: |
| 12 | + url = f"https://canada-holidays.ca/api/v1/provinces/{province}" |
| 13 | + response = requests.get(url) |
| 14 | + response.raise_for_status() |
| 15 | + |
| 16 | + data = response.json() |
| 17 | + holiday_dates = {holiday["date"] for holiday in data["province"]["holidays"]} |
| 18 | + print(holiday_dates) |
| 19 | + return holiday_dates |
| 20 | + |
| 21 | + |
| 22 | +def get_n_business_days_ago(n: int, province: str, base_date: Optional[date] = None) -> date: |
| 23 | + holidays = get_holiday_dates(province) |
| 24 | + current_day = base_date or date.today() |
| 25 | + count = 0 |
| 26 | + |
| 27 | + while count < n: |
| 28 | + current_day -= timedelta(days=1) |
| 29 | + weekday = current_day.weekday() # Monday is 0, Sunday is 6 |
| 30 | + |
| 31 | + if weekday >= 5: # Weekend |
| 32 | + continue |
| 33 | + if current_day.isoformat() in holidays: # Holiday |
| 34 | + continue |
| 35 | + |
| 36 | + count += 1 |
| 37 | + |
| 38 | + return current_day |
| 39 | + |
| 40 | + |
| 41 | +def generate_email_html(app_url: str, licence_plate: str, request_id: str): |
| 42 | + env = Environment( |
| 43 | + loader=FileSystemLoader("/opt/airflow/dags"), |
| 44 | + autoescape=select_autoescape( |
| 45 | + enabled_extensions=("html", "xml"), |
| 46 | + default_for_string=True, |
| 47 | + ), |
| 48 | + ) |
| 49 | + |
| 50 | + template = env.get_template("_request_review_reminder.html") |
| 51 | + html_content = template.render( |
| 52 | + request_url=f"{app_url}/private-cloud/requests/{request_id}/decision", licence_plate=licence_plate |
| 53 | + ) |
| 54 | + |
| 55 | + return html_content |
| 56 | + |
| 57 | + |
| 58 | +def send_request_review_reminders( |
| 59 | + app_url, |
| 60 | + mongo_conn_id, |
| 61 | + kc_auth_url, |
| 62 | + kc_realm, |
| 63 | + kc_client_id, |
| 64 | + kc_client_secret, |
| 65 | + ches_api_url, |
| 66 | + ches_auth_url, |
| 67 | + ches_realm, |
| 68 | + ches_client_id, |
| 69 | + ches_client_secret, |
| 70 | +): |
| 71 | + kc = Keycloak(kc_auth_url, kc_realm, kc_client_id, kc_client_secret) |
| 72 | + admins = kc.find_users_by_client_role("pltsvc", "private-admin") |
| 73 | + adminEmails = [admin["email"] for admin in admins if "email" in admin] |
| 74 | + if not adminEmails: |
| 75 | + print("No admin emails found.") |
| 76 | + return None |
| 77 | + |
| 78 | + ches = Ches(ches_api_url, ches_auth_url, ches_realm, ches_client_id, ches_client_secret) |
| 79 | + db = get_mongo_db(mongo_conn_id) |
| 80 | + |
| 81 | + three_business_days_ago = get_n_business_days_ago(3, "BC") |
| 82 | + print(f"Three business days ago: {three_business_days_ago}") |
| 83 | + |
| 84 | + three_business_days_ago_dt = datetime.combine(three_business_days_ago, datetime.min.time()) |
| 85 | + query = { |
| 86 | + "type": "REVIEW_PRIVATE_CLOUD_REQUEST", |
| 87 | + "status": "ASSIGNED", |
| 88 | + "createdAt": {"$lt": three_business_days_ago_dt}, |
| 89 | + } |
| 90 | + projection = {"_id": True, "data": True} |
| 91 | + |
| 92 | + print(f"Querying {query}...") |
| 93 | + tasks = db.Task.find(query, projection=projection) |
| 94 | + |
| 95 | + success = 0 |
| 96 | + failure = 0 |
| 97 | + for task in tasks: |
| 98 | + try: |
| 99 | + data = task.get("data") |
| 100 | + licence_plate = data["licencePlate"] |
| 101 | + request_id = data["requestId"] |
| 102 | + print(f"Processing task {task['_id']} with data: {data}") |
| 103 | + |
| 104 | + ches.send_email( |
| 105 | + { |
| 106 | + "subject": "Private Cloud Request Review Reminder", |
| 107 | + "body": generate_email_html(app_url, licence_plate, request_id), |
| 108 | + "to": adminEmails, |
| 109 | + } |
| 110 | + ) |
| 111 | + success += 1 |
| 112 | + except Exception as e: |
| 113 | + print(f"Failed to process task {task['_id']}: {e}") |
| 114 | + failure += 1 |
| 115 | + |
| 116 | + return {"success": success, "failure": failure} |
0 commit comments