Skip to content

Commit b5e6c1b

Browse files
committed
feat: add script to parse and aggregate daily metric quantities from payload data
1 parent 9f8d5b2 commit b5e6c1b

1 file changed

Lines changed: 44 additions & 0 deletions

File tree

parse_test2.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from datetime import datetime
2+
3+
payload = {
4+
"data": {
5+
"metrics": [
6+
{
7+
"name": "step_count",
8+
"data": [
9+
{"date": "2026-04-26 14:30:00 -0500", "qty": 4500}
10+
]
11+
}
12+
]
13+
}
14+
}
15+
16+
server_today_str = datetime.now().strftime('%Y-%m-%d')
17+
inferred_today_str = None
18+
metrics_received = payload.get('data', {}).get('metrics', [])
19+
for m in metrics_received:
20+
if m.get('data'):
21+
last_point = m['data'][-1]
22+
date_str = last_point.get('date', '')
23+
if len(date_str) >= 10:
24+
inferred_today_str = date_str[:10]
25+
break
26+
27+
today_str = inferred_today_str or server_today_str
28+
print("Inferred date:", today_str)
29+
30+
for metric in metrics_received:
31+
metric_name = metric.get('name')
32+
extracted_value = metric.get('value')
33+
34+
if extracted_value is None and 'data' in metric:
35+
total_qty = 0.0
36+
for point in metric['data']:
37+
date_str = point.get('date', '')
38+
if date_str.startswith(today_str):
39+
try:
40+
total_qty += float(point.get('qty', 0))
41+
except ValueError:
42+
pass
43+
extracted_value = int(total_qty) if float(total_qty).is_integer() else round(total_qty, 2)
44+
print("Value:", extracted_value, "Date:", today_str)

0 commit comments

Comments
 (0)