CoolFace
Apppublic

Frknrg/RgReport

sourceHugging Faceupdated 5mo agoView on Hugging Face
0likes
weekly_refresh.py141 linesDownload Raw Back to root
1"""2weekly_refresh.py — Unified data refresh script.3 4Run this locally once a week (or via cron / GitHub Actions) to keep all data current.5 6Usage:7    python weekly_refresh.py              # all sources8    python weekly_refresh.py returns      # only returns9    python weekly_refresh.py deliveries   # only deliveries10    python weekly_refresh.py sales        # only sales (shipstation_orders cache)11    python weekly_refresh.py marketing    # only marketing (Shopify → parquets)12 13Sources:14  • Returns:    Firebase Firestore → Supabase report_returns15  • Deliveries: Firebase Firestore → GCP PostgreSQL report_deliveries16  • Sales:      Supabase shipstation_orders → local parquet cache17  • Marketing:  Shopify GraphQL API → local parquet files + GCS18"""19 20import sys21import os22from datetime import datetime23 24BASE_DIR = os.path.dirname(os.path.abspath(__file__))25sys.path.insert(0, BASE_DIR)26 27 28def refresh_returns():29    print("\n" + "="*55)30    print("📦  RETURNS  (Firestore → Supabase report_returns)")31    print("="*55)32    try:33        import return_daily_refresh34        msg = return_daily_refresh.run_daily_refresh()35        print(msg)36        return True37    except Exception as e:38        print(f"❌ Returns refresh failed: {e}")39        return False40 41 42def refresh_deliveries():43    print("\n" + "="*55)44    print("🚚  DELIVERIES  (Firestore → GCP report_deliveries)")45    print("="*55)46    try:47        import deliveries_daily_refresh48        msg = deliveries_daily_refresh.run_daily_refresh()49        print(msg)50        return True51    except Exception as e:52        print(f"❌ Deliveries refresh failed: {e}")53        return False54 55 56def refresh_sales():57    print("\n" + "="*55)58    print("💰  SALES  (Supabase shipstation_orders — re-cache)")59    print("="*55)60    try:61        import daily_refresh62        msg = daily_refresh.run_daily_refresh()63        print(msg)64        # Refresh materialized view for country filter65        _refresh_mv_countries()66        return True67    except Exception as e:68        print(f"❌ Sales refresh failed: {e}")69        return False70 71 72def _refresh_mv_countries():73    """Refresh the mv_countries materialized view in Supabase."""74    import psycopg275    try:76        conn = psycopg2.connect(77            host=os.environ.get("SUPABASE_HOST", "aws-1-eu-north-1.pooler.supabase.com"),78            port=5432,79            database=os.environ.get("SUPABASE_DB", "postgres"),80            user=os.environ.get("SUPABASE_USER", "postgres.wawaztdbewvxugcfkzcw"),81            password=os.environ.get("SUPABASE_PASSWORD", "f2kDElCfRDChelbh"),82            connect_timeout=30,83            options="-c statement_timeout=300000",84        )85        conn.autocommit = True86        cur = conn.cursor()87        cur.execute("REFRESH MATERIALIZED VIEW CONCURRENTLY mv_countries")88        cur.close()89        conn.close()90        print("✅ mv_countries refreshed.")91    except Exception as e:92        print(f"⚠️  mv_countries refresh failed: {e}")93 94 95def refresh_marketing():96    print("\n" + "="*55)97    print("📊  MARKETING  (Shopify GraphQL → parquets + GCS)")98    print("="*55)99    try:100        import marketing_daily_refresh101        marketing_daily_refresh.run_daily_refresh()102        print("✅ Marketing refresh completed.")103        return True104    except Exception as e:105        print(f"❌ Marketing refresh failed: {e}")106        return False107 108 109def main():110    target = sys.argv[1].lower() if len(sys.argv) > 1 else "all"111 112    print(f"\n🔄  Weekly Refresh started at {datetime.now().strftime('%Y-%m-%d %H:%M')}")113    results = {}114 115    if target in ("all", "returns"):116        results["returns"] = refresh_returns()117 118    if target in ("all", "deliveries"):119        results["deliveries"] = refresh_deliveries()120 121    if target in ("all", "sales"):122        results["sales"] = refresh_sales()123 124    if target in ("all", "marketing"):125        results["marketing"] = refresh_marketing()126 127    print("\n" + "="*55)128    print("📊  SUMMARY")129    print("="*55)130    for name, ok in results.items():131        icon = "✅" if ok else "❌"132        print(f"  {icon}  {name}")133 134    all_ok = all(results.values())135    print(f"\n{'✅ All done!' if all_ok else '⚠️  Some refreshes failed — check logs above.'}")136    sys.exit(0 if all_ok else 1)137 138 139if __name__ == "__main__":140    main()141