This repository has been archived by the owner on Dec 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcount_parse.py
executable file
·100 lines (91 loc) · 4.6 KB
/
count_parse.py
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
#!/usr/bin/env python
import os, sys, re, time
from plumbum import local, cli, FG, BG, TF, TEE, ProcessExecutionError, colors
from plumbum.cmd import echo
from contextlib import contextmanager
from tinydb import TinyDB, Query, where
import pendulum
from loguru import logger
from mako.template import Template
class MyApp(cli.Application):
def main(self, db : cli.ExistingFile, block : cli.ExistingFile, out_html : cli.NonexistentPath, validators_elected_for = 65536 ):
try:
with open(block, 'r') as infile:
data = infile.read()
blocks = data.splitlines()
template = Template(filename='template.txt')
db = TinyDB(db)
except Exception as error:
logger.opt(exception=True).debug('Failed on open files')
query = Query()
start = 0 # set to another number if you want to skip some records from start
query_res = db.search(((query['success'] == True) | (query['reward'] != -1)) & (query['id'] >= start))
work=[]
t = 0
count=0
count_reward = 0
try:
for i in range(len(query_res)):
if (query_res[i]['success'] == True ):
count+=1
#print(count,query_res[i])
t=i
while (t+1 < len(query_res)):
t+=1
if (query_res[t]['reward'] != -1):
if ( (query_res[t]['time'] - query_res[i]['election_time']) < (int(validators_elected_for) + 500) ): # empirically found value, when 'validators_elected_for' was 4000 = 4500
continue
z = {'id_reward': query_res[t]['id'], 'election_time': query_res[i]['election_time'], 'reward_time': query_res[t]['time'], 'reward': query_res[t]['reward'] , 'blocks': 0}
work.append(z)
count_reward+=1
#print(count_reward, z)
break
except Exception as error:
logger.opt(exception=True).debug('Failed on binding')
# cat db.json | jq -r '._default' | jq '[.[]]' | grep "reward" | grep -v '"reward": -1,' | awk 'BEGIN{FS=":"} {print ($2/1000000000) }''| wc -l
logger.info("Count of parsed reward records = {}, success records = {}, please check it with cat | grep", count_reward, count)
try:
for i in range(len(work)):
if (i+1 < len(work)):
#print(i,work[i])
if ((work[i]['reward'] == work[i+1]['reward'])):
logger.info("Duplicates found, deleting first occurence")
#print(i,work[i])
del(work[i])
except Exception as error:
logger.opt(exception=True).debug('Failed on deleting duplicates')
try:
for i in range(len(work)):
a = work[i]['election_time']
b = a + int(validators_elected_for)
sum = 0
for y in blocks:
d = int(y)
if (d >= a and d <= b):
sum+=1
if (sum != 0) :
work[i]['blocks'] = sum
#print(work[i]['blocks'])
print("------------------------------------")
for i in range(len(work)):
print(work[i]) # Resulting array
print("------------------------------------")
text=[]
sep=","
for i in range(len(work)):
#if ((work[i]['blocks'] != 0)): # Maybe you can get rewards without validating any blocks
dt = pendulum.from_timestamp(work[i]['election_time'])
pr=dt.format('YYYY,MM,DD HH:mm:ss')
#pr=dt.to_datetime_string()
#pr=dt.to_iso8601_string()
#print(f"[new Date(\"{pr}\"), {work[i]['reward'] / 1000000000}, \"{work[i]['election_time']}\", {work[i]['blocks']}]")
text.append(f"[new Date(\"{pr}\"), {work[i]['reward'] / 1000000000}, \"{work[i]['election_time']}\", {work[i]['blocks']}]")
FilledTemplate = template.render(Variable=(sep.join( text )))
FileName = out_html
f= open(FileName,"w+")
f.write(FilledTemplate)
f.close()
except Exception as error:
logger.opt(exception=True).debug('Failed on last iter')
if __name__ == "__main__":
MyApp.run()