Skip to content

Instantly share code, notes, and snippets.

@banteg
Created October 7, 2018 11:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save banteg/8dd42b74cbb321f73f518062af792be1 to your computer and use it in GitHub Desktop.
Save banteg/8dd42b74cbb321f73f518062af792be1 to your computer and use it in GitHub Desktop.
"""
estimates remaining time to sync tezos node.
dependencies:
pip3 install click requests python-dateutil
"""
import requests
from requests.exceptions import ConnectionError
from dateutil.parser import parse
from datetime import datetime, timedelta, timezone
from time import time, sleep
from click import style
from collections import namedtuple, deque
def main():
synced = timedelta(minutes=1)
lag = namedtuple('Lag', ['ts', 'time', 'lvl'])
lags = deque(maxlen=60) # 10 minutes
while True:
try:
r = requests.get('http://127.0.0.1:8732/chains/main/blocks/head/header/shell').json()
except ConnectionError:
print('could not connect to node')
sleep(10)
continue
ts = parse(r['timestamp'])
lvl = r['level']
now = datetime.now(tz=timezone.utc)
lags.append(lag(ts, now, lvl))
diff = now - ts
if diff < synced:
print('synced, exiting...')
return
if len(lags) < 2:
print('waiting for more data')
sleep(10)
continue
first, last = lags[0], lags[-1]
speed = (last.ts - first.ts) / (last.time - first.time)
eta = timedelta(seconds=round(((now - ts) / speed).total_seconds())) if speed != 0 else '--'
bps = (last.lvl - first.lvl) / (last.time - first.time).total_seconds()
diff_round = timedelta(seconds=round(diff.total_seconds()))
print(
style(f'{bps:.2f}/s', bold=True, fg='cyan'),
style(f'{eta} left', bold=True, fg='green'),
style(f'level {lvl:,d} ({ts.strftime("%F %T")})', fg='yellow'),
style(f'{diff_round} behind', fg='blue')
)
sleep(10)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment