Download stats.py from testser2/mzlessons: direct link, hf CLI and curl.
- Browser
- Download file 2.93 kB
-
https://huggingface.co/spaces/testser2/mzlessons/resolve/main/stats.py
- Command line
-
hf download hf://spaces/testser2/mzlessons/stats.py
-
curl -L -o stats.py https://huggingface.co/spaces/testser2/mzlessons/resolve/main/stats.py
2.93 kB
| import pandas as pd | |
| import duckdb | |
| from datetime import datetime, timedelta | |
| from typing import Dict, List, TypedDict | |
| from supabase import create_client | |
| class DailyRun(TypedDict): | |
| date: str | |
| runs: int | |
| class TotalTimeRun(TypedDict): | |
| total: int | |
| avgExecutionTime: str | |
| overTime: List[DailyRun] | |
| def format_code_run_data(user_id: str, con) -> TotalTimeRun: | |
| try: | |
| over_time: List[DailyRun] = [] | |
| total_runs = 0 | |
| total_execution_time = 0 | |
| # Generate dates for the last 5 days | |
| current_date = datetime.now() | |
| dates = [(current_date - timedelta(days=i)).date() for i in range(4, -1, -1)] | |
| try: | |
| for date in dates: | |
| year = date.strftime("%Y") | |
| month = date.strftime("%m") | |
| day = date.strftime("%d") | |
| date_str = date.isoformat() | |
| try: | |
| query = f""" | |
| SELECT | |
| COUNT(*) as run_count, | |
| COALESCE(SUM(execution_time), 0) as total_exec_time | |
| FROM read_parquet('s3://learnpython/coderuns/{year}/{month}/{day}/{user_id}/{user_id}.parquet') | |
| WHERE user_id = '{user_id}' | |
| """ | |
| results = con.execute(query).fetchall() | |
| if results and len(results) > 0: | |
| run_count = int(results[0][0]) | |
| total_runs += run_count | |
| total_execution_time += float(results[0][1] or 0) | |
| over_time.append({ | |
| "date": date_str, | |
| "runs": run_count | |
| }) | |
| else: | |
| over_time.append({ | |
| "date": date_str, | |
| "runs": 0 | |
| }) | |
| except Exception as e: | |
| print(f"Error for {date_str}:", e) | |
| over_time.append({ | |
| "date": date_str, | |
| "runs": 0 | |
| }) | |
| avg_execution_time = f"{(total_execution_time / total_runs):.2f}s" if total_runs > 0 else "0.00s" | |
| return { | |
| "total": total_runs, | |
| "avgExecutionTime": avg_execution_time, | |
| "overTime": over_time | |
| } | |
| except Exception as e: | |
| print("Error in inner try block:", e) | |
| return { | |
| "total": 0, | |
| "avgExecutionTime": "0.00s", | |
| "overTime": [] | |
| } | |
| except Exception as e: | |
| print("Error in format_code_run_data:", e) | |
| return { | |
| "total": 0, | |
| "avgExecutionTime": "0.00s", | |
| "overTime": [] | |
| } | |