File size: 2,927 Bytes
dc19c4a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | 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": []
}
|