Skip to content

Commit 15f14c6

Browse files
committed
Add query_nodes using SDK for Meile 2.0 cache results of online nodes. API endpoint is provided by metabase.
1 parent 5963913 commit 15f14c6

File tree

2 files changed

+156
-0
lines changed

2 files changed

+156
-0
lines changed

query_nodes.py

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
import random
2+
import json
3+
import pymysql
4+
import scrtsxx
5+
from sentinel_sdk.sdk import SDKInstance
6+
from sentinel_sdk.types import PageRequest, Status
7+
from sentinel_sdk.modules.node import NodeModule
8+
9+
10+
GRPC_HOST = "aimokoivunen.mathnodes.com"
11+
GRPC_PORT = 9090
12+
TIMEOUT = 30
13+
VERSION = 20240202.1954
14+
15+
class OnlineNodes():
16+
17+
def connDB(self):
18+
db = pymysql.connect(host=scrtsxx.HOST,
19+
port=scrtsxx.PORT,
20+
user=scrtsxx.USERNAME,
21+
passwd=scrtsxx.PASSWORD,
22+
db=scrtsxx.DB,
23+
charset="utf8mb4",
24+
cursorclass=pymysql.cursors.DictCursor
25+
)
26+
27+
return db
28+
29+
def DropTableAndCreateNew(self, db):
30+
31+
drop_query = "DROP TABLE online_nodes;"
32+
c = db.cursor()
33+
34+
c.execute(drop_query)
35+
db.commit()
36+
37+
create_query = '''
38+
CREATE TABLE online_nodes (node_address VARCHAR(100) NOT NULL, moniker VARCHAR(500), country VARCHAR(100), city VARCHAR(100),
39+
latitude DECIMAL(7,4), longitude DECIMAL(7,4), gigabyte_prices VARCHAR(2000), hourly_prices VARCHAR(2000),
40+
bandwidth_down BIGINT UNSIGNED, bandwidth_up BIGINT UNSIGNED, wallet VARCHAR(100), handshake BOOLEAN, connected_peers SMALLINT UNSIGNED,
41+
max_peers SMALLINT UNSIGNED, node_type TINYINT UNSIGNED, node_version VARCHAR(20), PRIMARY KEY(node_address));
42+
'''
43+
44+
c.execute(create_query)
45+
db.commit()
46+
47+
def InsertRow(self, db, q):
48+
49+
c= db.cursor()
50+
c.execute(q)
51+
db.commit()
52+
53+
54+
def QueryAndRepopulateDB(self, db):
55+
sdk = SDKInstance("aimokoivunen.mathnodes.com", 9090)
56+
nm = NodeModule(sdk._channel,TIMEOUT,sdk._account,sdk._client)
57+
58+
nodes = nm.QueryNodes(status=Status.ACTIVE, pagination=PageRequest(limit=1000))
59+
60+
nodesStatus = sdk.nodes.QueryNodesStatus(nodes)
61+
62+
'''value of nodesStatus
63+
('sentnode16gswagztkv4q8h4hc89stk2ndc2avgthc45lpx', '{"success":true,"result":{"address":"sentnode16gswagztkv4q8h4hc89stk2ndc2avgthc45lpx","bandwidth":{"download":166750000,"upload":321125000},"handshake":{"enable":false,"peers":8},"interval_set_sessions":10000000000,"interval_update_sessions":6900000000000,"interval_update_status":3300000000000,
64+
"location":{"city":"Bangkok","country":"Thailand","latitude":13.8054,"longitude":100.6751},"moniker":"Mrsilent-317","operator":"sent16gswagztkv4q8h4hc89stk2ndc2avgthwr4xys",
65+
"peers":0,"gigabyte_prices":"52573ibc/31FEE1A2A9F9C01113F90BD0BBCCE8FD6BBB8585FAF109A2101827DD1D5B95B8,9204ibc/A8C2D23A1E6F95DA4E48BA349667E322BD7A6C996D8A4AAE8BA72E190F3D1477,1180852ibc/B1C0DDB14F25279A2026BC8794E12B259F8BDA546A3C5132CCAEE4431CE36783,122740ibc/ED07A3391A112B175915CD8FAF43A2DA8E4790EDE12566649D0C2F97716B8518,15342624udvpn",
66+
"hourly_prices":"18480ibc/31FEE1A2A9F9C01113F90BD0BBCCE8FD6BBB8585FAF109A2101827DD1D5B95B8,770ibc/A8C2D23A1E6F95DA4E48BA349667E322BD7A6C996D8A4AAE8BA72E190F3D1477,1871892ibc/B1C0DDB14F25279A2026BC8794E12B259F8BDA546A3C5132CCAEE4431CE36783,18897ibc/ED07A3391A112B175915CD8FAF43A2DA8E4790EDE12566649D0C2F97716B8518,4160000udvpn",
67+
"qos":{"max_peers":250},"type":2,"version":"0.7.1"}}')
68+
'''
69+
for a,d in nodesStatus.items():
70+
if json.loads(d)['success']:
71+
result = json.loads(d)['result']
72+
address = result['address'] # VARCHAR(100)
73+
bandwidth_down = int(result['bandwidth']['download']) # BIGINT
74+
bandwidth_up = int(result['bandwidth']['upload']) # BIGINT
75+
handshake = result['handshake']['enable'] # Boolean
76+
city = result['location']['city'] # VARCHAR(100)
77+
country = result['location']['country'] #VARCHAR(100)
78+
latitude = float(result['location']['latitude']) #DECIMAL(7,4)
79+
longitude = float(result['location']['longitude']) #DECIMAL(7,4)
80+
moniker = result['moniker'] # VARCHAR(200)
81+
wallet = result['operator'] #VARCHAR(100)
82+
peers = int(result['peers']) #SMALLINT
83+
gb_prices = result['gigabyte_prices'] #VARCHAR(2000)
84+
hr_prices = result['hourly_prices'] #VARCHAR(2000)
85+
max_peers = int(result['qos']['max_peers']) #SMALLINT
86+
node_type = int(result['type']) #SMALLINT
87+
node_version = result['version'] #VARCHAR(20)
88+
89+
iquery = '''
90+
INSERT IGNORE INTO online_nodes (node_address, moniker, country, city, latitude, longitude, gigabyte_prices, hourly_prices, bandwidth_down, bandwidth_up, wallet, handshake, connected_peers, max_peers, node_type, node_version)
91+
VALUES ("%s", "%s", "%s", "%s", %.4f, %.4f, "%s", "%s", %d, %d, "%s", "%s", %d, %d, %d, "%s")
92+
''' % (address,
93+
moniker,
94+
country,
95+
city,
96+
latitude,
97+
longitude,
98+
gb_prices,
99+
hr_prices,
100+
bandwidth_down,
101+
bandwidth_up,
102+
wallet,
103+
handshake,
104+
peers,
105+
max_peers,
106+
node_type,
107+
node_version)
108+
self.InsertRow(db, iquery)
109+
110+
if __name__ == "__main__":
111+
on = OnlineNodes()
112+
db = on.connDB()
113+
on.DropTableAndCreateNew(db)
114+
on.QueryAndRepopulateDB(db)
115+

remove_dead_nodes_from_db.sh

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/bin/bash
2+
#
3+
# This script removes nodes that have an uptime less than 5%
4+
# from the relevant tables within the Meile Cache System
5+
#
6+
# We store 3 months worth of nodes and this script runs
7+
# on the last day of the month every three months.
8+
9+
10+
11+
mysql --defaults-extra-file=/home/sentinel/SQL/mysqldb.conf <<EOF
12+
USE meile
13+
DELETE node_cities
14+
FROM node_cities
15+
INNER JOIN node_uptime ON node_cities.node_address = node_uptime.node_address
16+
WHERE node_uptime.success_rate < 0.05;
17+
EOF
18+
19+
mysql --defaults-extra-file=/home/sentinel/SQL/mysqldb.conf <<EOF
20+
USE meile
21+
DELETE node_geoip
22+
FROM node_geoip
23+
INNER JOIN node_uptime ON node_geoip.node_address = node_uptime.node_address
24+
WHERE node_uptime.success_rate < 0.05;
25+
EOF
26+
27+
mysql --defaults-extra-file=/home/sentinel/SQL/mysqldb.conf <<EOF
28+
USE meile
29+
DELETE node_score
30+
FROM node_score
31+
INNER JOIN node_uptime ON node_score.node_address = node_uptime.node_address
32+
WHERE node_uptime.success_rate < 0.05;
33+
EOF
34+
35+
mysql --defaults-extra-file=/home/sentinel/SQL/mysqldb.conf <<EOF
36+
USE meile
37+
DELETE
38+
FROM node_uptime
39+
WHERE node_uptime.success_rate < 0.05;
40+
EOF
41+

0 commit comments

Comments
 (0)