Skip to content

Commit 549fde2

Browse files
authored
Merge pull request #218 from yungwine/rocksdb_stats
Rocksdb stats
2 parents 2efaa49 + 3c3c805 commit 549fde2

File tree

2 files changed

+77
-15
lines changed

2 files changed

+77
-15
lines changed

mytoncore/functions.py

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
import requests
99
import subprocess
1010

11-
from mytoncore.mytoncore import MyTonCore, Dec2HexAddr
12-
from mytoncore.tonblocksscanner import TonBlocksScanner
11+
from mytoncore.mytoncore import MyTonCore
12+
from mytoncore.utils import parse_db_stats
1313
from mypylib.mypylib import (
1414
b2mb,
1515
get_timestamp,
@@ -419,6 +419,47 @@ def GetValidatorProcessInfo():
419419
# end define
420420

421421

422+
def get_db_stats():
423+
result = {
424+
'rocksdb': {
425+
'ok': True,
426+
'message': '',
427+
'data': {}
428+
},
429+
'celldb': {
430+
'ok': True,
431+
'message': '',
432+
'data': {}
433+
},
434+
}
435+
rocksdb_stats_path = '/var/ton-work/db/db_stats.txt'
436+
celldb_stats_path = '/var/ton-work/db/celldb/db_stats.txt'
437+
if os.path.exists(rocksdb_stats_path):
438+
try:
439+
result['rocksdb']['data'] = parse_db_stats(rocksdb_stats_path)
440+
except Exception as e:
441+
result['rocksdb']['ok'] = False
442+
result['rocksdb']['message'] = f'failed to fetch db stats: {e}'
443+
else:
444+
result['rocksdb']['ok'] = False
445+
result['rocksdb']['message'] = 'db stats file is not exists'
446+
# end if
447+
448+
if os.path.exists(celldb_stats_path):
449+
try:
450+
result['celldb']['data'] = parse_db_stats(celldb_stats_path)
451+
except Exception as e:
452+
result['celldb']['ok'] = False
453+
result['celldb']['message'] = f'failed to fetch db stats: {e}'
454+
else:
455+
result['celldb']['ok'] = False
456+
result['celldb']['message'] = 'db stats file is not exists'
457+
# end if
458+
459+
return result
460+
# end define
461+
462+
422463
def Telemetry(local, ton):
423464
sendTelemetry = local.db.get("sendTelemetry")
424465
if sendTelemetry is not True:
@@ -442,6 +483,7 @@ def Telemetry(local, ton):
442483
data["swap"] = GetSwapInfo()
443484
data["uname"] = GetUname()
444485
data["vprocess"] = GetValidatorProcessInfo()
486+
data["dbStats"] = get_db_stats()
445487
elections = local.try_function(ton.GetElectionEntries)
446488
complaints = local.try_function(ton.GetComplaints)
447489

mytoncore/utils.py

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import base64
22
import json
3+
import re
34

45

56
def str2b64(s):
@@ -49,19 +50,19 @@ def b642hex(input):
4950

5051

5152
def xhex2hex(x):
52-
try:
53-
b = x[1:]
54-
h = b.lower()
55-
return h
56-
except:
57-
return None
53+
try:
54+
b = x[1:]
55+
h = b.lower()
56+
return h
57+
except:
58+
return None
5859
#end define
5960

6061
def hex2base64(h): # TODO: remove duplicates
61-
b = bytes.fromhex(h)
62-
b64 = base64.b64encode(b)
63-
s = b64.decode("utf-8")
64-
return s
62+
b = bytes.fromhex(h)
63+
b64 = base64.b64encode(b)
64+
s = b64.decode("utf-8")
65+
return s
6566
#end define
6667

6768

@@ -73,7 +74,26 @@ def str2bool(str):
7374

7475

7576
def ng2g(ng):
76-
if ng is None:
77-
return
78-
return int(ng)/10**9
77+
if ng is None:
78+
return
79+
return int(ng)/10**9
7980
#end define
81+
82+
83+
def parse_db_stats(path: str):
84+
with open(path) as f:
85+
lines = f.readlines()
86+
result = {}
87+
for line in lines:
88+
s = line.strip().split(maxsplit=1)
89+
items = re.findall(r"(\S+)\s:\s(\S+)", s[1])
90+
if len(items) == 1:
91+
item = items[0]
92+
if float(item[1]) > 0:
93+
result[s[0]] = float(item[1])
94+
else:
95+
if any(float(v) > 0 for k, v in items):
96+
result[s[0]] = {}
97+
result[s[0]] = {k: float(v) for k, v in items}
98+
return result
99+
# end define

0 commit comments

Comments
 (0)