Add bound averages, local low and high
This commit is contained in:
@@ -3,7 +3,6 @@ import sys
|
||||
import os
|
||||
import logging
|
||||
import argparse
|
||||
import datetime
|
||||
|
||||
# external imports
|
||||
import confini
|
||||
@@ -18,6 +17,9 @@ from chainlib.chain import ChainSpec
|
||||
from chainlib.eth.connection import EthHTTPConnection
|
||||
from chainlib.eth.block import block_latest
|
||||
|
||||
# local imports
|
||||
from eth_stat_syncer.store import GasAggregator
|
||||
|
||||
logging.basicConfig(level=logging.WARNING)
|
||||
logg = logging.getLogger()
|
||||
|
||||
@@ -54,46 +56,20 @@ chain_spec = ChainSpec.from_chain_str(config.get('CHAIN_SPEC'))
|
||||
conn = EthHTTPConnection(args.p)
|
||||
|
||||
|
||||
class GasStore:
|
||||
|
||||
def __init__(self, store):
|
||||
self.store = store
|
||||
self.avg = 0
|
||||
self.count = 0
|
||||
self.timestamp = datetime.datetime.utcnow()
|
||||
|
||||
|
||||
def put(self, v):
|
||||
aggr = self.avg * self.count
|
||||
aggr += v
|
||||
self.count += 1
|
||||
self.avg = int(aggr / self.count)
|
||||
logg.info('added {} to aggregate {} new average {} from {} samples'.format(v, aggr, self.avg, self.count))
|
||||
return self.avg
|
||||
|
||||
|
||||
def get(self):
|
||||
return self.avg
|
||||
|
||||
|
||||
class GasPriceFilter(SyncFilter):
|
||||
|
||||
def __init__(self, chain_spec, gas_store):
|
||||
def __init__(self, chain_spec, gas_aggregator):
|
||||
self.chain_spec = chain_spec
|
||||
self.gas_store = gas_store
|
||||
self.gas_aggregator = gas_aggregator
|
||||
|
||||
|
||||
def filter(self, conn, block, tx, db_session):
|
||||
self.gas_store.put(tx.gas_price)
|
||||
|
||||
|
||||
def block_callback(block, tx):
|
||||
logg.info('synced {}'.format(block))
|
||||
self.gas_aggregator.put(tx.gas_price)
|
||||
|
||||
|
||||
def main():
|
||||
gas_store = GasStore(None)
|
||||
gas_filter = GasPriceFilter(chain_spec, gas_store)
|
||||
gas_aggregator = GasAggregator(360)
|
||||
gas_filter = GasPriceFilter(chain_spec, gas_aggregator)
|
||||
|
||||
o = block_latest()
|
||||
r = conn.do(o)
|
||||
@@ -105,7 +81,7 @@ def main():
|
||||
offset = start_block - config.get('_START')
|
||||
syncer_backend = MemBackend(chain_spec, None, target_block=start_block)
|
||||
syncer_backend.set(offset, 0)
|
||||
syncer = HistorySyncer(syncer_backend, block_callback=block_callback)
|
||||
syncer = HistorySyncer(syncer_backend, block_callback=gas_aggregator.block_callback)
|
||||
syncer.add_filter(gas_filter)
|
||||
try:
|
||||
syncer.loop(0.0, conn)
|
||||
@@ -115,7 +91,7 @@ def main():
|
||||
|
||||
syncer_backend = MemBackend(chain_spec, None)
|
||||
syncer_backend.set(start_block + 1, 0)
|
||||
syncer = HeadSyncer(syncer_backend, block_callback=block_callback)
|
||||
syncer = HeadSyncer(syncer_backend, block_callback=gas_aggregator.block_callback)
|
||||
syncer.add_filter(gas_filter)
|
||||
syncer.loop(1.0, conn)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user