Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 44 additions & 2 deletions mytoncore/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import requests
import subprocess

from mytoncore.mytoncore import MyTonCore, Dec2HexAddr
from mytoncore.tonblocksscanner import TonBlocksScanner
from mytoncore.mytoncore import MyTonCore
from mytoncore.utils import parse_db_stats
from mypylib.mypylib import (
b2mb,
get_timestamp,
Expand Down Expand Up @@ -419,6 +419,47 @@ def GetValidatorProcessInfo():
# end define


def get_db_stats():
result = {
'rocksdb': {
'ok': True,
'message': '',
'data': {}
},
'celldb': {
'ok': True,
'message': '',
'data': {}
},
}
rocksdb_stats_path = '/var/ton-work/db/db_stats.txt'
celldb_stats_path = '/var/ton-work/db/celldb/db_stats.txt'
if os.path.exists(rocksdb_stats_path):
try:
result['rocksdb']['data'] = parse_db_stats(rocksdb_stats_path)
except Exception as e:
result['rocksdb']['ok'] = False
result['rocksdb']['message'] = f'failed to fetch db stats: {e}'
else:
result['rocksdb']['ok'] = False
result['rocksdb']['message'] = 'db stats file is not exists'
# end if

if os.path.exists(celldb_stats_path):
try:
result['celldb']['data'] = parse_db_stats(celldb_stats_path)
except Exception as e:
result['celldb']['ok'] = False
result['celldb']['message'] = f'failed to fetch db stats: {e}'
else:
result['celldb']['ok'] = False
result['celldb']['message'] = 'db stats file is not exists'
# end if

return result
# end define


def Telemetry(local, ton):
sendTelemetry = local.db.get("sendTelemetry")
if sendTelemetry is not True:
Expand All @@ -442,6 +483,7 @@ def Telemetry(local, ton):
data["swap"] = GetSwapInfo()
data["uname"] = GetUname()
data["vprocess"] = GetValidatorProcessInfo()
data["dbStats"] = get_db_stats()
elections = local.try_function(ton.GetElectionEntries)
complaints = local.try_function(ton.GetComplaints)

Expand Down
46 changes: 33 additions & 13 deletions mytoncore/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import base64
import json
import re


def str2b64(s):
Expand Down Expand Up @@ -49,19 +50,19 @@ def b642hex(input):


def xhex2hex(x):
try:
b = x[1:]
h = b.lower()
return h
except:
return None
try:
b = x[1:]
h = b.lower()
return h
except:
return None
#end define

def hex2base64(h): # TODO: remove duplicates
b = bytes.fromhex(h)
b64 = base64.b64encode(b)
s = b64.decode("utf-8")
return s
b = bytes.fromhex(h)
b64 = base64.b64encode(b)
s = b64.decode("utf-8")
return s
#end define


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


def ng2g(ng):
if ng is None:
return
return int(ng)/10**9
if ng is None:
return
return int(ng)/10**9
#end define


def parse_db_stats(path: str):
with open(path) as f:
lines = f.readlines()
result = {}
for line in lines:
s = line.strip().split(maxsplit=1)
items = re.findall(r"(\S+)\s:\s(\S+)", s[1])
if len(items) == 1:
item = items[0]
if float(item[1]) > 0:
result[s[0]] = float(item[1])
else:
if any(float(v) > 0 for k, v in items):
result[s[0]] = {}
result[s[0]] = {k: float(v) for k, v in items}
return result
# end define