File size: 3,139 Bytes
f726fd6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
"""Shared helpers for the cloud data warehouse environment."""
from __future__ import annotations
import copy
import re
from datetime import datetime
from typing import Any, Dict, Iterable, Optional
RUNTIME_NOW = "2026-06-01T00:00:00Z"
ID_PREFIXES: Dict[str, str] = {
"organization": "org",
"user": "user",
"warehouse": "wh",
"dataset": "ds",
"table": "tbl",
"column": "col",
"access_grant": "grant",
"query_job": "query",
"import_job": "import",
"export_job": "export",
"cost_usage_record": "usage",
"quota_policy": "quota",
}
LEGACY_ID_PREFIXES: Dict[str, str] = {
"organization": "ORG",
"dataset": "DST",
"table": "TBL",
"query_job": "QRY",
}
def success_response(data: Dict[str, Any], message: str = "") -> Dict[str, Any]:
return {"status": "success", "data": data, "message": message}
def error_response(error_code: str, message: str) -> Dict[str, Any]:
return {"status": "error", "data": {}, "message": f"{error_code}: {message}"}
def safe_copy(value: Any) -> Any:
return copy.deepcopy(value)
def now_iso() -> str:
return RUNTIME_NOW
def slugify(value: Any) -> str:
text = str(value or "").strip().lower()
text = re.sub(r"[^a-z0-9]+", "-", text).strip("-")
return text or "untitled"
def org_slug(org_id: str) -> str:
return org_id[4:] if isinstance(org_id, str) and org_id.startswith("org-") else slugify(org_id)
def build_humanized_id(entity_type: str, record: Dict[str, Any], *, existing_ids: Optional[Iterable[str]] = None) -> str:
existing = set(existing_ids or [])
prefix = ID_PREFIXES.get(entity_type, entity_type)
if entity_type == "dataset":
base = f"ds-{org_slug(record.get('org_id', 'org'))}-{slugify(record.get('business_domain') or record.get('name'))}"
elif entity_type == "table":
dataset_id = str(record.get("dataset_id") or "")
dataset_slug = dataset_id[3:] if dataset_id.startswith("ds-") else slugify(dataset_id)
base = f"tbl-{dataset_slug}-{slugify(record.get('name'))}"
elif entity_type == "quota_policy":
scope = "warehouse-cap" if record.get("warehouse_id") else "monthly"
base = f"quota-{org_slug(record.get('org_id', 'org'))}-{slugify(record.get('resource_group'))}-{scope}"
else:
base = f"{prefix}-{slugify(record.get('name') or record.get('display_name') or entity_type)}"
candidate = base
index = 2
while candidate in existing:
candidate = f"{base}-{index}"
index += 1
return candidate
def is_iso_datetime(value: Any) -> bool:
if not isinstance(value, str):
return False
try:
datetime.strptime(value, "%Y-%m-%dT%H:%M:%SZ")
return True
except ValueError:
return False
def is_date(value: Any) -> bool:
if not isinstance(value, str):
return False
try:
datetime.strptime(value, "%Y-%m-%d")
return True
except ValueError:
return False
def datetime_gt(left: str, right: str) -> bool:
return datetime.strptime(left, "%Y-%m-%dT%H:%M:%SZ") > datetime.strptime(right, "%Y-%m-%dT%H:%M:%SZ")
|