Includes: API (Express/TypeScript), Astro site, Python workers, document generators, FCC compliance tools, Canada CRTC formation, Ansible infrastructure, and deployment scripts. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
98 lines
3.5 KiB
Python
98 lines
3.5 KiB
Python
"""
|
|
/cdr-buckets — Portal page to tag trunk groups & account IDs as wholesale/retail.
|
|
|
|
Lists every distinct trunk_group_id and customer_account_id seen in the
|
|
carrier's last 90 days of CDRs; admin tags each as wholesale or retail.
|
|
Mappings persist to ``cdr_bucket_mappings`` and drive 499-A Block 3 vs.
|
|
Block 4-A attribution.
|
|
"""
|
|
|
|
import frappe
|
|
import os
|
|
|
|
|
|
def get_context(context):
|
|
if frappe.session.user == "Guest":
|
|
frappe.local.flags.redirect_location = "/login?redirect-to=/cdr-buckets"
|
|
raise frappe.Redirect
|
|
|
|
context.no_cache = 1
|
|
context.show_sidebar = True
|
|
context.title = "Wholesale / Retail Mapping"
|
|
|
|
profile = _load_profile(frappe.session.user)
|
|
context.profile = profile
|
|
if not profile:
|
|
context.trunk_groups = []
|
|
context.account_ids = []
|
|
context.existing = []
|
|
return
|
|
|
|
context.trunk_groups, context.account_ids = _distinct_identifiers(profile["id"])
|
|
context.existing = _existing_mappings(profile["id"])
|
|
|
|
|
|
def _load_profile(user_email: str):
|
|
try:
|
|
import psycopg2
|
|
import psycopg2.extras
|
|
with psycopg2.connect(os.environ["DATABASE_URL"]) as conn:
|
|
with conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor) as cur:
|
|
cur.execute(
|
|
"""
|
|
SELECT p.*
|
|
FROM cdr_ingestion_profiles p
|
|
JOIN customers c ON c.id = p.customer_id
|
|
WHERE LOWER(c.email) = LOWER(%s)
|
|
ORDER BY p.id DESC LIMIT 1
|
|
""",
|
|
(user_email,),
|
|
)
|
|
row = cur.fetchone()
|
|
return dict(row) if row else None
|
|
except Exception as exc:
|
|
frappe.log_error(f"cdr-buckets profile lookup failed: {exc}", "cdr-buckets")
|
|
return None
|
|
|
|
|
|
def _distinct_identifiers(profile_id: int):
|
|
import psycopg2
|
|
import psycopg2.extras
|
|
try:
|
|
with psycopg2.connect(os.environ["DATABASE_URL"]) as conn:
|
|
with conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor) as cur:
|
|
cur.execute(
|
|
"SELECT DISTINCT trunk_group_id FROM cdr_calls "
|
|
"WHERE profile_id=%s AND trunk_group_id IS NOT NULL "
|
|
"AND start_time > NOW() - INTERVAL '90 days' "
|
|
"ORDER BY trunk_group_id LIMIT 500",
|
|
(profile_id,),
|
|
)
|
|
trunks = [r["trunk_group_id"] for r in cur.fetchall()]
|
|
cur.execute(
|
|
"SELECT DISTINCT customer_account_id FROM cdr_calls "
|
|
"WHERE profile_id=%s AND customer_account_id IS NOT NULL "
|
|
"AND start_time > NOW() - INTERVAL '90 days' "
|
|
"ORDER BY customer_account_id LIMIT 500",
|
|
(profile_id,),
|
|
)
|
|
accts = [r["customer_account_id"] for r in cur.fetchall()]
|
|
return trunks, accts
|
|
except Exception:
|
|
return [], []
|
|
|
|
|
|
def _existing_mappings(profile_id: int):
|
|
import psycopg2
|
|
import psycopg2.extras
|
|
try:
|
|
with psycopg2.connect(os.environ["DATABASE_URL"]) as conn:
|
|
with conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor) as cur:
|
|
cur.execute(
|
|
"SELECT match_type, match_value, bucket FROM cdr_bucket_mappings "
|
|
"WHERE profile_id=%s",
|
|
(profile_id,),
|
|
)
|
|
return [dict(r) for r in cur.fetchall()]
|
|
except Exception:
|
|
return []
|