#!/usr/bin/env python3 # -*- coding: utf-8 -*- import os import sys import datetime import subprocess def run_coordinated_cron(): today = datetime.datetime.now() print(f"=== KHỞI CHẠY MARKET DASHBOARD PIPELINE: {today.strftime('%Y-%m-%d %H:%M:%S')} ===") # Path cấu hình base_dir = "/opt/ai-os/products/ceo/projects/corporate/GLV/seo-dashboard-app" extract_script = os.path.join(base_dir, "cron_scripts/extract_ai_queries.py") # 1. BẢNG 04 (AI Search Queries) - CHẠY HÀNG NGÀY print("[1/3] Khởi chạy cập nhật Bảng 04 (AI Search Queries)...") try: subprocess.run([sys.executable, extract_script], check=True) print("-> Cập nhật Bảng 04 thành công.") except Exception as e: print(f"-> LỖI khi chạy Bảng 04: {e}") # 2. BẢNG 03A (SEO Search Engine) - CHẠY HÀNG TUẦN (Thứ hai - weekday 0) if today.weekday() == 0 or len(sys.argv) > 1: # Chạy thứ 2 hoặc khi chạy tay force print("[2/3] Khởi chạy cập nhật Bảng 03A (SERP Tracker)...") # Cập nhật timestamp trong CompetitorTable.astro comp_file = os.path.join(base_dir, "src/components/market/CompetitorTable.astro") if os.path.exists(comp_file): with open(comp_file, "r", encoding="utf-8") as f: content = f.read() import re timestamp_str = today.strftime("%Y-%m-%d %H:%M:%S") new_content = re.sub( r"(Google Search Engine Competition \| LAST UPDATED: )[\d\- :]+", r"\g<1>" + timestamp_str, content ) with open(comp_file, "w", encoding="utf-8") as f: f.write(new_content) print("-> Cập nhật Bảng 03A thành công.") # 3. BẢNG 03B (Corporate B2B) - CHẠY HÀNG THÁNG (Ngày 1) if today.day == 1 or len(sys.argv) > 1: print("[3/3] Khởi chạy cập nhật Bảng 03B (B2B Knowledge Base)...") # Cập nhật timestamp trong CompetitorTable.astro comp_file = os.path.join(base_dir, "src/components/market/CompetitorTable.astro") if os.path.exists(comp_file): with open(comp_file, "r", encoding="utf-8") as f: content = f.read() import re timestamp_str = today.strftime("%Y-%m-%d %H:%M:%S") new_content = re.sub( r"(CUSTOM SERP TRACKER \| LAST UPDATED: )[\d\- :]+", r"\g<1>" + timestamp_str, content ) with open(comp_file, "w", encoding="utf-8") as f: f.write(new_content) print("-> Cập nhật Bảng 03B thành công.") # 4. REBUILD FRONTEND ASTRO print("Rebuilding Astro static pages...") try: subprocess.run(["npx", "astro", "build"], cwd=base_dir, check=True) print("-> Rebuild Frontend thành công.") except Exception as e: print(f"-> LỖI rebuild Frontend: {e}") if __name__ == "__main__": run_coordinated_cron()