-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathgrid_proxy.py
More file actions
101 lines (87 loc) · 4.11 KB
/
grid_proxy.py
File metadata and controls
101 lines (87 loc) · 4.11 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import requests
from utils.base import Base
"""
This module contains Grid Proxy getters.
"""
class GridProxy:
def __init__(self, browser):
self.browser = browser
def get_rentable_node(self):
r = requests.post(Base.gridproxy_url + 'nodes?rentable=true&status=up')
node_list = r.json()
r = requests.post(Base.gridproxy_url + 'nodes?rented=true&status=up')
node_list.extend(r.json())
return node_list
def get_farm_details(self, farm_name):
r = requests.post(Base.gridproxy_url + 'farms?name=' + farm_name)
details = r.json()
return details
def get_dedicate_status(self, node_id):
r = requests.post(Base.gridproxy_url + 'nodes/'+ str(node_id))
dedicate_status = r.json()
return (dedicate_status['rentedByTwinId'])
def get_node_ipv4(self, node_id):
r = requests.post(Base.gridproxy_url + 'nodes/'+ str(node_id))
farm_node = r.json()
return (farm_node['publicConfig']['ipv4'])
def get_node_fee(self, node_id):
r = requests.post(Base.gridproxy_url + 'nodes/'+ str(node_id))
farm_node = r.json()
return (farm_node['extraFee'])/1000
def get_twin_address(self, twin_id):
r = requests.post(Base.gridproxy_url + 'twins?twin_id='+ twin_id)
details = r.json()
return details[0]['accountId']
def get_twin_relay(self, twin_id):
r = requests.post(Base.gridproxy_url + 'twins?twin_id='+ twin_id)
details = r.json()
return details[0]['relay']
def get_farm_ips(self, farm_id):
r = requests.post(Base.gridproxy_url + 'farms?farm_id='+ farm_id)
farm_list = r.json()
return len(farm_list[0]['publicIps'])
def get_twin_node(self, twin_id):
r = requests.post(Base.gridproxy_url + 'farms?twin_id=' + twin_id)
details = r.json()
farms = ''
for detail in details:
farms += str(detail['farmId']) + ','
r = requests.post(Base.gridproxy_url + 'nodes?farm_ids=' + farms[:-1])
details = r.json()
return details
def get_stats_capicity(self):
r = requests.post('https://stats.' + Base.net + '.grid.tf/api/stats-summary', timeout=10)
stats_json = r.json()
return list(stats_json.values())
def get_stats(self):
up = requests.get(Base.gridproxy_url + 'stats?status=up', timeout=10).json()
standby = requests.get(Base.gridproxy_url + 'stats?status=standby', timeout=10).json()
# Initialize a dictionary to store the merged data
merged_data = {}
# Merge simple values, summing if they differ
keys_to_sum = ['nodes', 'accessNodes', 'totalCru', 'totalSru', 'totalMru', 'totalHru', 'gpus', 'dedicatedNodes', 'workloads_number']
for key in keys_to_sum:
merged_data[key] = up[key] + standby[key]
# Merge the "farms", "publicIps", "gateways", "twins", and "contracts" fields (they are the same)
keys_to_add_once = ['farms', 'publicIps', 'gateways', 'twins', 'contracts']
for key in keys_to_add_once:
merged_data[key] = up[key]
# Merge nodesDistribution and calculate unique and common countries
up_distribution = up['nodesDistribution']
standby_distribution = standby['nodesDistribution']
merged_distribution = {}
common_countries = 0
for country, up_count in up_distribution.items():
standby_count = standby_distribution.get(country, 0)
merged_distribution[country] = up_count + standby_count
if standby_count > 0:
common_countries += 1
for country, standby_count in standby_distribution.items():
if country not in merged_distribution:
merged_distribution[country] = standby_count
merged_data['nodesDistribution'] = merged_distribution
# Calculate the total countries: all unique countries minus common countries
total_countries = len(merged_distribution) # Total unique countries
merged_data['countries'] = total_countries
# Return the dictionary directly
return merged_data