forked from waltzofpearls/healthstats-collector
-
Notifications
You must be signed in to change notification settings - Fork 1
/
grafana_api.py
78 lines (71 loc) · 2.73 KB
/
grafana_api.py
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
import datetime
import math
import os
import pytz
import requests
import traceback
class GrafanaAPI:
def __init__(self, logger):
self.logger = logger
self.timezone = os.environ.get("TIMEZONE", "UTC")
self.api = os.environ.get("GRAFANA_API")
self.api_key = os.environ.get("GRAFANA_API_KEY")
def activities_as_annotations(self, activities):
tz = pytz.timezone(self.timezone)
utc = datetime.datetime.now()
now = tz.fromutc(utc)
midnight = now.replace(hour=0, minute=0, second=0, microsecond=0)
seconds_from_midnight = (now - midnight).seconds
# start from 1 hour ago and end at the current time
from_time = now - datetime.timedelta(hours=1)
to_time = now
# when it's the first hour of the day, start from 2 hours ago
# so we can also grab the activities from the last hour of yesterday
if seconds_from_midnight <= 3600:
from_time = now - datetime.timedelta(hours=2)
for activity in activities:
ts = math.floor(activity["beginTimestamp"] / 1000)
utc = datetime.datetime.utcfromtimestamp(ts)
duration = math.ceil(activity["duration"])
delta = datetime.timedelta(seconds=duration)
time = tz.fromutc(utc)
time_end = time + delta
if from_time < time <= to_time or from_time < time_end <= to_time:
self.annotation(
{
"time": int(time.timestamp() * 1000),
"timeEnd": int(time_end.timestamp() * 1000),
"isRegion": True,
"tags": ["healthstats"],
"text": "{} with time {}, average HR {}, and calories {}".format(
activity["activityName"],
str(delta),
activity["averageHR"],
activity["calories"],
),
}
)
def annotation(self, data):
"""
{
"dashboardId":468,
"panelId":1,
"time":1507037197339,
"isRegion":true,
"timeEnd":1507180805056,
"tags":["tag1","tag2"],
"text":"Annotation Description"
}
"""
try:
response = requests.post(
self.api + "/annotations",
json=data,
headers={
"Authorization": "Bearer {}".format(self.api_key),
"Context-Type": "application/json",
},
)
self.logger.info(response.text)
except Exception:
self.logger.error(traceback.format_exc())