Spaces:
Running
Running
Download generate_sql.py from Churchgate/facilityxperience: direct link, hf CLI and curl.
- Browser
- Download file 3.9 kB
-
https://huggingface.co/spaces/Churchgate/facilityxperience/resolve/main/generate_sql.py
- Command line
-
hf download hf://spaces/Churchgate/facilityxperience/generate_sql.py
-
curl -L -o generate_sql.py https://huggingface.co/spaces/Churchgate/facilityxperience/resolve/main/generate_sql.py
3.9 kB
| import pandas as pd | |
| # Read your cleaned CSV | |
| df = pd.read_csv("clean_assets_final.csv") # Change to your filename | |
| # Department mapping | |
| dept_map = { | |
| "MEP-ELECTRICAL": ("Engineering", "Electrical"), | |
| "MEP-HVAC": ("Engineering", "HVAC"), | |
| "MEP-PLUMBING": ("Engineering", "Plumbing"), | |
| "MEP-LIFT": ("Engineering", "Vertical Transportation (Lifts)"), | |
| "FIRE-FIGHTING": ("Engineering", "Fire Fighting"), | |
| "FM-OPERATIONS": ("Facility Management", "FM Operations"), | |
| "FM-HARD-SERVICES": ("Facility Management", "Hard Services"), | |
| "FM-SOFT-SERVICES": ("Facility Management", "Soft Services"), | |
| "FM-FITOUT": ("Facility Management", "Fitout Works"), | |
| "FM-FRONT-OF-HOUSE": ("Facility Management", "Front of House"), | |
| "SECURITY": ("Security", "Security"), | |
| "ELV-ACCESS-CONTROL": ("Technology Group", "Access Control"), | |
| "ELV-AUTOMATION": ("Technology Group", "Automation"), | |
| "ELV-BMS": ("Technology Group", "BMS"), | |
| "ELV-VIDEO-MANAGEMENT": ("Technology Group", "CCTV"), | |
| "ELV-FIRE-ALARM-EVAC": ("Technology Group", "Fire Alarm & Voice Evac"), | |
| "ELV-NETWORK-VOIP": ("Technology Group", "Networks & Connectivity"), | |
| "ELV-MDTH": ("Technology Group", "MDTH (DSTV)"), | |
| "GATES AND SHIELD (CONTRACTOR)": ("Contractor", "Gates and Shield"), | |
| } | |
| def map_dept(val): | |
| if pd.isna(val) or str(val).strip() == "" or str(val).strip() == "NA": | |
| return ("Uncategorized", "Uncategorized") | |
| val = str(val).strip().upper() | |
| return dept_map.get(val, (str(val), str(val))) | |
| def safe_val(val, default="NULL"): | |
| if pd.isna(val) or str(val).strip() in ["", "NA", "na"]: | |
| return default | |
| cleaned = str(val).strip().replace("'", "''") | |
| return f"'{cleaned}'" | |
| # Generate SQL | |
| sql_lines = [] | |
| sql_lines.append("-- Auto-generated asset insert\n") | |
| sql_lines.append("DELETE FROM assets WHERE facility_code = 'WTC';\n\n") | |
| batch_size = 100 | |
| total = len(df) | |
| for batch_start in range(0, total, batch_size): | |
| batch = df.iloc[batch_start:batch_start+batch_size] | |
| sql_lines.append("INSERT INTO assets (facility_code, name, asset_tag, department, sub_division, category_name, parent_asset, status, priority, manufacturer, model, model_no, serial_number, capacity, description, location_building, location_floor, barcode, checklist, ppm, verification_frequency, gross_weight, dimensions, health_condition, vendor, warranty_applicable, condition_rating, created_at) VALUES\n") | |
| values = [] | |
| for _, row in batch.iterrows(): | |
| dept, sub = map_dept(row.get("Department")) | |
| name = safe_val(row.get("Assetname")) | |
| serial = safe_val(row.get("Serial NO")) | |
| mfg = safe_val(row.get("Manufacturer")) | |
| model = safe_val(row.get("Model")) | |
| cap = safe_val(row.get("Capacity")) | |
| loc = safe_val(row.get("Location")) | |
| parent = safe_val(row.get("Parent Asset")) | |
| cat = safe_val(row.get("Category")) | |
| val = f"('WTC', {name}, {serial if serial != 'NULL' else safe_val(f'AUTO-{batch_start}')}, {safe_val(dept)}, {safe_val(sub)}, {cat}, {parent}, 'active', 'medium', {mfg}, {model}, {safe_val(row.get('Model NO'))}, {serial}, {cap}, {safe_val(row.get('Description'))}, {loc}, {safe_val(row.get('Sub Location'))}, {safe_val(row.get('Barcode'))}, {safe_val(row.get('Checklist'))}, {safe_val(row.get('PPM'))}, {safe_val(row.get('Verification Frequency'))}, {safe_val(row.get('Gross Weight'))}, {safe_val(row.get('Size and Dimensions'))}, {safe_val(row.get('Health Condition'))}, {safe_val(row.get('Vendor'))}, {safe_val(row.get('Warrenty Applicable'))}, 5, NOW())" | |
| values.append(val) | |
| sql_lines.append(",\n".join(values) + ";\n\n") | |
| with open("assets_insert.sql", "w") as f: | |
| f.writelines(sql_lines) | |
| print(f"Done! {total} rows written to assets_insert.sql") | |
| print("Run this file in Supabase SQL Editor") |