-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery_service.py
More file actions
80 lines (66 loc) · 2.33 KB
/
query_service.py
File metadata and controls
80 lines (66 loc) · 2.33 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
from collections import defaultdict
import pandas as pd
import requests
import os
from dotenv import load_dotenv
load_dotenv()
API_URL = os.getenv('API_URL')
def get_users():
try:
users = []
response = requests.get(API_URL + '/user/')
if response.status_code == 200:
for user in response.json().get('data'):
users.append(
{
'label': user['username'],
'value': user['id']
}
)
return users
except requests.exceptions.RequestException as e:
print(e)
def get_miners(user_id):
try:
miners = []
response = requests.get(API_URL + f"/miner/{user_id}")
if response.status_code == 200 and response.json().get('data') is not None:
for miner in response.json().get('data'):
miners.append(
{
'label': miner['name'],
'value': miner['id']
}
)
return miners
except requests.exceptions.RequestException as e:
print(e)
return None
def get_miner_shares(miner_id):
try:
response = requests.get(API_URL + f"/miner/{miner_id}/share")
if response.ok:
# create a dataframe from the share data
frame = pd.json_normalize(response.json())
# convert the time strings into a tz aware datetime
frame['start'] = pd.to_datetime(frame['start']).dt.tz_localize('UTC')
return frame
else:
print(response)
except requests.exceptions.RequestException as e:
print(e)
def get_miner_healths(miner_id):
try:
response = requests.get(API_URL + f"/miner/{miner_id}/health")
if response.ok:
# create a dataframe from the share share_data
frame = pd.json_normalize(response.json())
# convert the time strings into a tz aware datetime
frame['start'] = pd.to_datetime(frame['start']).dt.tz_localize('UTC')
frame['fan_speed'] = frame['fan_speed'] / 100
frame['hashrate'] = frame['hashrate'] / 1000000
return frame
else:
print(response)
except requests.exceptions.RequestException as e:
print(e)