Add store to disk
This commit is contained in:
parent
a72fcbe919
commit
84e89dbfae
@ -18,7 +18,10 @@ from chainlib.eth.connection import EthHTTPConnection
|
|||||||
from chainlib.eth.block import block_latest
|
from chainlib.eth.block import block_latest
|
||||||
|
|
||||||
# local imports
|
# local imports
|
||||||
from eth_stat_syncer.store import GasAggregator
|
from eth_stat_syncer.store import (
|
||||||
|
GasAggregator,
|
||||||
|
RunStore,
|
||||||
|
)
|
||||||
|
|
||||||
logging.basicConfig(level=logging.WARNING)
|
logging.basicConfig(level=logging.WARNING)
|
||||||
logg = logging.getLogger()
|
logg = logging.getLogger()
|
||||||
@ -68,7 +71,8 @@ class GasPriceFilter(SyncFilter):
|
|||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
gas_aggregator = GasAggregator(360)
|
gas_store = RunStore()
|
||||||
|
gas_aggregator = GasAggregator(gas_store, 360)
|
||||||
gas_filter = GasPriceFilter(chain_spec, gas_aggregator)
|
gas_filter = GasPriceFilter(chain_spec, gas_aggregator)
|
||||||
|
|
||||||
o = block_latest()
|
o = block_latest()
|
||||||
|
@ -1,18 +1,42 @@
|
|||||||
# standard imports
|
# standard imports
|
||||||
|
import os
|
||||||
import logging
|
import logging
|
||||||
import datetime
|
import datetime
|
||||||
|
|
||||||
logg = logging.getLogger().getChild(__name__)
|
logg = logging.getLogger().getChild(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class RunStore:
|
||||||
|
|
||||||
|
def __init__(self, basedir='/run'):
|
||||||
|
self.procstore = os.path.join(basedir, 'eth_stat_syncerd')
|
||||||
|
|
||||||
|
|
||||||
|
def put(self, o, postfix):
|
||||||
|
for k in o:
|
||||||
|
file_path = os.path.join(self.procstore, postfix, k)
|
||||||
|
d = os.path.dirname(file_path)
|
||||||
|
try:
|
||||||
|
os.stat(d)
|
||||||
|
except FileNotFoundError:
|
||||||
|
os.makedirs(d)
|
||||||
|
f = open(file_path, 'w')
|
||||||
|
f.write(str(o[k]))
|
||||||
|
f.close()
|
||||||
|
|
||||||
|
|
||||||
class GasAggregator:
|
class GasAggregator:
|
||||||
|
|
||||||
def __init__(self, capacity):
|
def __init__(self, store, capacity):
|
||||||
|
self.store = store
|
||||||
self.avg = 0
|
self.avg = 0
|
||||||
self.count = 0
|
self.count = 0
|
||||||
self.timestamp = datetime.datetime.utcnow()
|
self.timestamp = datetime.datetime.utcnow()
|
||||||
self.buffer_cursor = 0
|
self.buffer_cursor = 0
|
||||||
self.buffer_capacity = capacity
|
self.buffer_capacity = capacity
|
||||||
self.buffer = [None] * self.buffer_capacity
|
self.buffer_average = [None] * self.buffer_capacity
|
||||||
|
self.buffer_high = [None] * self.buffer_capacity
|
||||||
|
self.buffer_low = [None] * self.buffer_capacity
|
||||||
self.initial = False
|
self.initial = False
|
||||||
self.aggr = 0
|
self.aggr = 0
|
||||||
self.local_aggr = 0
|
self.local_aggr = 0
|
||||||
@ -43,10 +67,14 @@ class GasAggregator:
|
|||||||
self.local_count = 0
|
self.local_count = 0
|
||||||
if not self.initial:
|
if not self.initial:
|
||||||
for i in range(self.buffer_capacity):
|
for i in range(self.buffer_capacity):
|
||||||
self.buffer[i] = v
|
self.buffer_average[i] = v
|
||||||
|
self.buffer_high[i] = self.local_high
|
||||||
|
self.buffer_low[i] = self.local_low
|
||||||
self.initial = True
|
self.initial = True
|
||||||
else:
|
else:
|
||||||
self.buffer[self.buffer_cursor] = v
|
self.buffer_average[self.buffer_cursor] = v
|
||||||
|
self.buffer_low[self.buffer_cursor] = self.local_low
|
||||||
|
self.buffer_high[self.buffer_cursor] = self.local_high
|
||||||
self.buffer_cursor += 1
|
self.buffer_cursor += 1
|
||||||
self.buffer_cursor %= self.buffer_capacity
|
self.buffer_cursor %= self.buffer_capacity
|
||||||
|
|
||||||
@ -60,26 +88,44 @@ class GasAggregator:
|
|||||||
def get(self, n=0):
|
def get(self, n=0):
|
||||||
if n == 0:
|
if n == 0:
|
||||||
n = self.capacity
|
n = self.capacity
|
||||||
|
|
||||||
cursor = self.buffer_cursor
|
aggrs = {
|
||||||
aggr = 0
|
'average': 0,
|
||||||
i = 0
|
'low': 0,
|
||||||
while i < n:
|
'high': 0,
|
||||||
v = self.buffer[cursor]
|
}
|
||||||
aggr += v
|
for o in [
|
||||||
cursor -= 1
|
(self.buffer_average, 'average',),
|
||||||
if cursor < 0:
|
(self.buffer_low, 'low',),
|
||||||
cursor = self.buffer_capacity - 1
|
(self.buffer_high, 'high',),
|
||||||
i += 1
|
]:
|
||||||
|
cursor = self.buffer_cursor
|
||||||
|
aggr = 0
|
||||||
|
i = 0
|
||||||
|
while i < n:
|
||||||
|
v = o[0][cursor]
|
||||||
|
aggrs[o[1]] += v
|
||||||
|
cursor -= 1
|
||||||
|
if cursor < 0:
|
||||||
|
cursor = self.buffer_capacity - 1
|
||||||
|
i += 1
|
||||||
|
|
||||||
return int(aggr / n)
|
return {
|
||||||
|
'average': int(aggrs['average']/ n),
|
||||||
|
'high': int(aggrs['high']/ n),
|
||||||
|
'low': int(aggrs['low']/ n),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
def block_callback(self, block, tx):
|
def block_callback(self, block, tx):
|
||||||
logg.info('synced {}'.format(block))
|
logg.info('synced {}'.format(block))
|
||||||
if self.process():
|
if self.process():
|
||||||
|
last = self.get(1)
|
||||||
v = self.get(10)
|
self.store.put(last, 'block')
|
||||||
logg.info('last 6 average now {}'.format(v))
|
minute = self.get(6)
|
||||||
v = self.get(360)
|
self.store.put(minute, 'minute')
|
||||||
logg.info('last 360 average now {}'.format(v))
|
hour = self.get(360)
|
||||||
|
self.store.put(hour, 'hour')
|
||||||
|
self.store.put({
|
||||||
|
'average': int(self.aggr / self.count),
|
||||||
|
}, 'all')
|
||||||
|
Loading…
Reference in New Issue
Block a user