This repository was archived by the owner on May 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcachet.py
More file actions
44 lines (38 loc) · 1.57 KB
/
cachet.py
File metadata and controls
44 lines (38 loc) · 1.57 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
import os
from datetime import datetime
import requests
import json
import certifi
import traceback
class cachet():
def __init__(self):
self.cachet_token = os.environ["cachet_token"]
def report(self, method, url, value_dict):
headers = {"Content-Type": "application/json", "X-Cachet-Token": self.cachet_token}
req = requests.request(method=method, url=url, json=value_dict, headers=headers, verify=certifi.where())
return req
def report_login_time(self, milliseconds, login_metric_id):
data = {
"value": int(milliseconds),
"timestamp": int(datetime.utcnow().timestamp())
}
req = self.report("POST", "https://status.projectalt.is/api/v1/metrics/" + str(login_metric_id) + "/points", data)
return req
def report_component(self, status_value, component_id):
if status_value == self.get_component(component_id):
print("Not updating component " + str(component_id) + " with the same value")
return
data = {
"status": int(status_value)
}
req = self.report("PUT", "https://status.projectalt.is/api/v1/components/" + str(component_id), data)
return req
@staticmethod
def get_component(component_id):
req = requests.get("https://status.projectalt.is/api/v1/components/" + str(component_id))
try:
jsn = json.loads(req.text)
return int(jsn["data"]["status"])
except:
print("Cachet API is dying with code " + str(req.status_code) + ": " + req.text)
return 0