Skill: prettybusysolutions-eng/xzenia-financial-telemetry (version 1.1.0)

--- name: financial-telemetry description: Ingest and normalize revenue-relevant data from billing, CRM, ERP, or CSV/manual sources into Xzenia's unified financial event model. Use when pulling client data, refreshing connectors, importing CSVs, or checking ingestion quality. --- # Financial Telemetry Normalize source data into a unified financial events stream. ## Premium upgrade - Pro version with Stripe/Salesforce ingestion and executive briefing: `https://buy.stripe.com/aFacN45NS7476ZObkG0kE0c` ## Sources - Stripe - Salesforce - CSV/manual import - future: QuickBooks and others ## Outputs - normalized financial events in SQLite - raw JSON backups - data quality report - ingestion state file

path: scripts/ingest.py
content: #!/usr/bin/env python3
import argparse, json, os, sqlite3, sys
from datetime import datetime, timezone
import pandas as pd
DB_PATH=os.path.expanduser('~/.openclaw/workspace/data/xzenia/causal_memory.db')
DATA_DIR=os.path.expanduser('~/.openclaw/workspace/data/xzenia')
def get_db(): return sqlite3.connect(DB_PATH)
def store_events(client_id, events):
    conn=get_db(); c=conn.cursor(); now=datetime.now(timezone.utc).isoformat(); inserted=0
    for e in events:
        existing=c.execute('SELECT 1 FROM financial_events WHERE client_id=? AND source_system=? AND source_id=?',(client_id,e['source_system'],e['source_id'])).fetchone()
        if existing: continue
        c.execute('INSERT INTO financial_events (client_id,event_type,event_date,amount,currency,source_system,source_id,contract_id,customer_id,description,metadata,ingested_at) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)', (client_id,e['event_type'],e['event_date'],e['amount'],e.get('currency','USD'),e['source_system'],e['source_id'],e.get('contract_id'),e.get('customer_id'),e.get('description',''),json.dumps(e.get('metadata',{})),now)); inserted += 1
    conn.commit(); conn.close(); return inserted

def ingest_csv(client_id, file_path):
    df=pd.read_csv(file_path); required=['event_type','event_date','amount']; missing=[c for c in required if c not in df.columns]
    if missing: print(f'ERROR: CSV missing required columns: {missing}'); sys.exit(1)
    events=[]
    for idx,row in df.iterrows():
        events.append({'event_type':str(row['event_type']),'event_date':str(row['event_date']),'amount':float(row['amount']),'currency':str(row.get('currency','USD')),'source_system':'csv_import','source_id':str(row.get('source_id',f'csv_{idx}')),'customer_id':str(row.get('customer_id','unknown')),'contract_id':str(row.get('contract_id','')) if pd.notna(row.get('contract_id')) else None,'description':str(row.get('description','')),'metadata':{}})
    return events

def run_quality_checks(client_id):
    conn=get_db(); events=pd.read_sql_query('SELECT * FROM financial_events WHERE client_id = ?', conn, params=[client_id]); conn.close(); checks={}; checks['total_events']=len(events)
    if len(events)==0:
        checks['status']='no_events';
    else:
        events['event_date']=pd.to_datetime(events['event_date'], errors='coerce'); checks['date_range']={'earliest':str(events['event_date'].min()),'latest':str(events['event_date'].max())}; checks['currencies']=events['currency'].unique().tolist(); checks['multi_currency']=len(checks['currencies'])>1; checks['duplicate_count']=int(events.duplicated(subset=['source_system','source_id']).sum()); amounts=events['amount']; checks['amount_stats']={'mean':float(amounts.mean()),'median':float(amounts.median()),'std':float(amounts.std() if len(amounts) > 1 else 0.0),'min':float(amounts.min()),'max':float(amounts.max())}; checks['event_types']=events['event_type'].value_counts().to_dict()
    report_path=os.path.join(DATA_DIR,'clients',client_id,'data_quality.json'); os.makedirs(os.path.dirname(report_path), exist_ok=True)
    with open(report_path,'w') as f: json.dump(checks,f,indent=2,default=str)
    return checks

def main():
    p=argparse.ArgumentParser(); p.add_argument('--client', required=True); p.add_argument('--source', required=True, choices=['csv','stripe','salesforce','quickbooks']); p.add_argument('--csv-file'); p.add_argument('--output'); args=p.parse_args(); client_id=args.client
    if args.source!='csv': print(f"ERROR: source '{args.source}' not implemented in safe local scaffold yet"); sys.exit(1)
    if not args.csv_file: print('ERROR: --csv-file required when source=csv'); sys.exit(1)
    events=ingest_csv(client_id, args.csv_file); inserted=store_events(client_id, events); out_dir=args.output or os.path.join(DATA_DIR,'clients',client_id,'raw'); os.makedirs(out_dir, exist_ok=True)
    backup_path=os.path.join(out_dir, f"{args.source}_{datetime.now(timezone.utc).strftime('%Y%m%d_%H%M%S')}.json")
    with open(backup_path,'w') as f: json.dump(events,f,indent=2,default=str)
    run_quality_checks(client_id)
    state_path=os.path.join(DATA_DIR,'clients',client_id,'ingestion_state.json'); state={}
    if os.path.exists(state_path):
        with open(state_path) as f: state=json.load(f)
    state[args.source]={'last_pull': datetime.now(timezone.utc).isoformat(),'events_pulled': len(events),'events_stored': inserted}
    with open(state_path,'w') as f: json.dump(state,f,indent=2)
    print(f'INGESTION COMPLETE — {inserted} new events')
if __name__ == '__main__': main()


Security scan verdicts — ClawScan: suspicious | static analysis: clean | VirusTotal: clean | SkillSpector: suspicious (HIGH)