diff --git a/scripts/openwebui_stats.py b/scripts/openwebui_stats.py index 116538f..a6ce135 100644 --- a/scripts/openwebui_stats.py +++ b/scripts/openwebui_stats.py @@ -1134,20 +1134,50 @@ class OpenWebUIStats: print(f"✅ README 已更新: {readme_path}") - def generate_activity_chart(self, lang: str = "zh") -> str: - """生成 Vega-Lite 趋势图 (内嵌数据,确保稳定性)""" + def upload_chart_data(self, stats: dict): + """上传图表数据到 Gist (独立于徽章数据)""" + if not (self.gist_token and self.gist_id): + return + history = self.load_history() if len(history) < 3: - return "" + return - # 准备数据点 - values = [] + # 准备图表数据点 + chart_data = [] for item in history: - values.append({"date": item["date"], "downloads": item["total_downloads"]}) + chart_data.append( + {"date": item["date"], "downloads": item["total_downloads"]} + ) + + try: + url = f"https://api.github.com/gists/{self.gist_id}" + headers = {"Authorization": f"token {self.gist_token}"} + payload = { + "files": { + "chart-data.json": { + "content": json.dumps(chart_data, ensure_ascii=False, indent=2) + } + } + } + resp = requests.patch(url, headers=headers, json=payload) + if resp.status_code == 200: + print(f"✅ 图表数据已同步至 Gist") + except Exception as e: + print(f"⚠️ 图表数据同步失败: {e}") + + def generate_activity_chart(self, lang: str = "zh") -> str: + """生成 Vega-Lite 趋势图 (使用外部数据源,URL 固定)""" + if not (self.gist_token and self.gist_id): + return "" title = "Total Downloads Trend" if lang == "en" else "总下载量累计趋势" - # Vega-Lite Spec (内嵌数据) + # 使用 Gist Raw URL 作为数据源 + gist_user = "Fu-Jie" # Replace with your GitHub username + data_url = f"https://gist.githubusercontent.com/{gist_user}/{self.gist_id}/raw/chart-data.json" + + # Vega-Lite Spec (数据从外部 URL 加载) vl_spec = { "$schema": "https://vega.github.io/schema/vega-lite/v5.json", "description": title, @@ -1159,7 +1189,7 @@ class OpenWebUIStats: "view": {"stroke": "transparent"}, "axis": {"domain": False, "grid": False}, }, - "data": {"values": values}, + "data": {"url": data_url}, # 外部数据源 "mark": { "type": "area", "line": {"color": "#2563eb"}, @@ -1268,6 +1298,9 @@ def main(): # 生成 Shields.io endpoint JSON (用于动态徽章) badges_dir = script_dir / "docs" / "badges" + # 上传图表数据到 Gist (独立存储) + stats_client.upload_chart_data(stats) + # 生成徽章 stats_client.generate_shields_endpoints(stats, str(badges_dir))