-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdebug_stats_fast.py
More file actions
85 lines (72 loc) · 2.84 KB
/
Copy pathdebug_stats_fast.py
File metadata and controls
85 lines (72 loc) · 2.84 KB
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
import asyncio
import os
import pandas as pd
from motor.motor_asyncio import AsyncIOMotorClient
from datetime import datetime, timedelta
from dotenv import load_dotenv
load_dotenv()
# Try different env variable names or default
MONGODB_URI = os.getenv("MONGODB_URI") or "mongodb://localhost:27017"
async def check_stats():
print(f"Connecting to: {MONGODB_URI}")
client = AsyncIOMotorClient(MONGODB_URI, serverSelectionTimeoutMS=5000)
try:
db = client['smart_office_raw']
collection = db['device_raw_data']
# JUST LAST 15 DAYS for speed
fifteen_days_ago = datetime.utcnow() - timedelta(days=15)
pipeline = [
{"$match": {"timestamp": {"$gte": fifteen_days_ago}}},
{"$project": {
"timestamp": 1,
"device_name": 1,
"power": {"$ifNull": ["$status.cur_power", {"$ifNull": ["$status.power", 0]}]}
}},
{"$addFields": {
"hour_bucket": {"$dateToString": {"format": "%Y-%m-%dT%H:00:00", "date": "$timestamp"}}
}},
{"$group": {
"_id": {"hour": "$hour_bucket", "device": "$device_name"},
"avg_power": {"$avg": "$power"}
}},
{"$group": {
"_id": "$_id.hour",
"total_power": {"$sum": "$avg_power"}
}},
{"$sort": {"_id": 1}}
]
cursor = collection.aggregate(pipeline)
logs = await cursor.to_list(length=1000)
if not logs:
db = client['smart_office_dashboard']
collection = db['device_logs']
cursor = collection.aggregate(pipeline)
logs = await cursor.to_list(length=1000)
if not logs:
print("No data found")
return
rows = []
for bucket in logs:
rows.append({
"ds": pd.to_datetime(bucket["_id"]),
"y": float(bucket["total_power"])
})
df = pd.DataFrame(rows)
df_daily = df.copy()
df_daily['date'] = df_daily['ds'].dt.date
df_daily_kwh = df_daily.groupby('date')['y'].apply(lambda g: g.mean() * len(g) / 1000.0)
print(f"\n--- Last 15 Days Daily kWh Stats ---")
print(df_daily_kwh.describe())
print(f"95th Percentile: {df_daily_kwh.quantile(0.95):.2f}")
print(f"Median: {df_daily_kwh.median():.2f}")
print(f"Max: {df_daily_kwh.max():.2f}")
p95 = df_daily_kwh.quantile(0.95)
median = df_daily_kwh.median()
cap_val = min(p95 * 1.15, median * 2.1)
print(f"\nCalculated Cap (Target): {max(cap_val, 20.0):.2f}")
except Exception as e:
print(f"Error: {e}")
finally:
client.close()
if __name__ == "__main__":
asyncio.run(check_stats())