mirror of
https://github.com/aljazceru/plugins.git
synced 2025-12-23 16:14:20 +01:00
summary: fix flake8 nits
This commit is contained in:
committed by
Christian Decker
parent
258c0e647a
commit
2034c29214
@@ -4,7 +4,6 @@ from packaging import version
|
|||||||
from collections import namedtuple
|
from collections import namedtuple
|
||||||
from summary_avail import *
|
from summary_avail import *
|
||||||
import pyln.client
|
import pyln.client
|
||||||
from math import floor, log10
|
|
||||||
import requests
|
import requests
|
||||||
import shelve
|
import shelve
|
||||||
import threading
|
import threading
|
||||||
@@ -73,19 +72,19 @@ class PriceThread(threading.Thread):
|
|||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
plugin.log("[PriceThread] " + str(ex), 'warn')
|
plugin.log("[PriceThread] " + str(ex), 'warn')
|
||||||
# Six hours is more than often enough for polling
|
# Six hours is more than often enough for polling
|
||||||
time.sleep(6*3600)
|
time.sleep(6 * 3600)
|
||||||
|
|
||||||
|
|
||||||
def to_fiatstr(msat: Millisatoshi):
|
def to_fiatstr(msat: Millisatoshi):
|
||||||
return "{}{:.2f}".format(plugin.currency_prefix,
|
return "{}{:.2f}".format(plugin.currency_prefix,
|
||||||
int(msat) / 10**11 * plugin.fiat_per_btc)
|
int(msat) / 10**11 * plugin.fiat_per_btc)
|
||||||
|
|
||||||
|
|
||||||
# appends an output table header that explains fields and capacity
|
# appends an output table header that explains fields and capacity
|
||||||
def append_header(table, max_msat):
|
def append_header(table, max_msat):
|
||||||
short_str = Millisatoshi(max_msat).to_approx_str()
|
short_str = Millisatoshi(max_msat).to_approx_str()
|
||||||
table.append("%c%-13sOUT/OURS %c IN/THEIRS%12s%c SCID FLAG AVAIL ALIAS"
|
table.append("%c%-13sOUT/OURS %c IN/THEIRS%12s%c SCID FLAG AVAIL ALIAS"
|
||||||
% (draw.left, short_str, draw.mid, short_str, draw.right))
|
% (draw.left, short_str, draw.mid, short_str, draw.right))
|
||||||
|
|
||||||
|
|
||||||
@plugin.method("summary", long_desc=summary_description)
|
@plugin.method("summary", long_desc=summary_description)
|
||||||
@@ -174,7 +173,6 @@ def summary(plugin, exclude=''):
|
|||||||
# Create simple line graph, 47 chars wide.
|
# Create simple line graph, 47 chars wide.
|
||||||
our_len = int(round(int(c.ours) / biggest * 23))
|
our_len = int(round(int(c.ours) / biggest * 23))
|
||||||
their_len = int(round(int(c.theirs) / biggest * 23))
|
their_len = int(round(int(c.theirs) / biggest * 23))
|
||||||
divided = False
|
|
||||||
|
|
||||||
# We put midpoint in the middle.
|
# We put midpoint in the middle.
|
||||||
mid = draw.mid
|
mid = draw.mid
|
||||||
@@ -232,10 +230,10 @@ def init(options, configuration, plugin):
|
|||||||
plugin.currency_prefix = options['summary-currency-prefix']
|
plugin.currency_prefix = options['summary-currency-prefix']
|
||||||
plugin.fiat_per_btc = 0
|
plugin.fiat_per_btc = 0
|
||||||
|
|
||||||
plugin.avail_interval = float(options['summary-availability-interval'])
|
plugin.avail_interval = float(options['summary-availability-interval'])
|
||||||
plugin.avail_window = 60 * 60 * int(options['summary-availability-window'])
|
plugin.avail_window = 60 * 60 * int(options['summary-availability-window'])
|
||||||
plugin.persist = shelve.open('summary.dat', writeback=True)
|
plugin.persist = shelve.open('summary.dat', writeback=True)
|
||||||
if not 'peerstate' in plugin.persist:
|
if 'peerstate' not in plugin.persist:
|
||||||
plugin.persist['peerstate'] = {}
|
plugin.persist['peerstate'] = {}
|
||||||
plugin.persist['availcount'] = 0
|
plugin.persist['availcount'] = 0
|
||||||
|
|
||||||
@@ -246,8 +244,8 @@ def init(options, configuration, plugin):
|
|||||||
# Default port in 9050
|
# Default port in 9050
|
||||||
if ':' not in paddr:
|
if ':' not in paddr:
|
||||||
paddr += ':9050'
|
paddr += ':9050'
|
||||||
proxies = { 'https': 'socks5h://' + paddr,
|
proxies = {'https': 'socks5h://' + paddr,
|
||||||
'http': 'socks5h://' + paddr }
|
'http': 'socks5h://' + paddr}
|
||||||
else:
|
else:
|
||||||
proxies = None
|
proxies = None
|
||||||
|
|
||||||
|
|||||||
@@ -1,13 +1,15 @@
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
# ensure an rpc peer is added
|
# ensure an rpc peer is added
|
||||||
|
|
||||||
|
|
||||||
def addpeer(p, rpcpeer):
|
def addpeer(p, rpcpeer):
|
||||||
pid = rpcpeer['id']
|
pid = rpcpeer['id']
|
||||||
if not pid in p.persist['peerstate']:
|
if pid not in p.persist['peerstate']:
|
||||||
p.persist['peerstate'][pid] = {
|
p.persist['peerstate'][pid] = {
|
||||||
'connected' : rpcpeer['connected'],
|
'connected': rpcpeer['connected'],
|
||||||
'last_seen' : datetime.now() if rpcpeer['connected'] else None,
|
'last_seen': datetime.now() if rpcpeer['connected'] else None,
|
||||||
'avail' : 1.0 if rpcpeer['connected'] else 0.0
|
'avail': 1.0 if rpcpeer['connected'] else 0.0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -16,8 +18,8 @@ def trace_availability(p, rpcpeers):
|
|||||||
p.persist['availcount'] += 1
|
p.persist['availcount'] += 1
|
||||||
leadwin = max(min(p.avail_window, p.persist['availcount'] * p.avail_interval), p.avail_interval)
|
leadwin = max(min(p.avail_window, p.persist['availcount'] * p.avail_interval), p.avail_interval)
|
||||||
samples = leadwin / p.avail_interval
|
samples = leadwin / p.avail_interval
|
||||||
alpha = 1.0 / samples
|
alpha = 1.0 / samples
|
||||||
beta = 1.0 - alpha
|
beta = 1.0 - alpha
|
||||||
|
|
||||||
for rpcpeer in rpcpeers['peers']:
|
for rpcpeer in rpcpeers['peers']:
|
||||||
pid = rpcpeer['id']
|
pid = rpcpeer['id']
|
||||||
@@ -26,7 +28,7 @@ def trace_availability(p, rpcpeers):
|
|||||||
if rpcpeer['connected']:
|
if rpcpeer['connected']:
|
||||||
p.persist['peerstate'][pid]['last_seen'] = datetime.now()
|
p.persist['peerstate'][pid]['last_seen'] = datetime.now()
|
||||||
p.persist['peerstate'][pid]['connected'] = True
|
p.persist['peerstate'][pid]['connected'] = True
|
||||||
p.persist['peerstate'][pid]['avail'] = 1.0 * alpha + p.persist['peerstate'][pid]['avail'] * beta
|
p.persist['peerstate'][pid]['avail'] = 1.0 * alpha + p.persist['peerstate'][pid]['avail'] * beta
|
||||||
else:
|
else:
|
||||||
p.persist['peerstate'][pid]['connected'] = False
|
p.persist['peerstate'][pid]['connected'] = False
|
||||||
p.persist['peerstate'][pid]['avail'] = 0.0 * alpha + p.persist['peerstate'][pid]['avail'] * beta
|
p.persist['peerstate'][pid]['avail'] = 0.0 * alpha + p.persist['peerstate'][pid]['avail'] * beta
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import subprocess
|
|||||||
import unittest
|
import unittest
|
||||||
import time
|
import time
|
||||||
import re
|
import re
|
||||||
|
import os
|
||||||
|
|
||||||
from pyln.client import Plugin
|
from pyln.client import Plugin
|
||||||
from pyln.testing.fixtures import * # noqa: F401,F403
|
from pyln.testing.fixtures import * # noqa: F401,F403
|
||||||
@@ -15,8 +16,8 @@ pluginopt = {'plugin': os.path.join(os.path.dirname(__file__), "summary.py")}
|
|||||||
# returns a test plugin stub
|
# returns a test plugin stub
|
||||||
def get_stub():
|
def get_stub():
|
||||||
plugin = Plugin()
|
plugin = Plugin()
|
||||||
plugin.avail_interval = 60
|
plugin.avail_interval = 60
|
||||||
plugin.avail_window = 3600
|
plugin.avail_window = 3600
|
||||||
plugin.persist = {}
|
plugin.persist = {}
|
||||||
plugin.persist['peerstate'] = {}
|
plugin.persist['peerstate'] = {}
|
||||||
plugin.persist['availcount'] = 0
|
plugin.persist['availcount'] = 0
|
||||||
@@ -26,7 +27,7 @@ def get_stub():
|
|||||||
def test_summary_peer_thread(node_factory):
|
def test_summary_peer_thread(node_factory):
|
||||||
# in order to give the PeerThread a chance in a unit test
|
# in order to give the PeerThread a chance in a unit test
|
||||||
# we need to give it a low interval
|
# we need to give it a low interval
|
||||||
opts = {'summary-availability-interval' : 0.1}
|
opts = {'summary-availability-interval': 0.1}
|
||||||
opts.update(pluginopt)
|
opts.update(pluginopt)
|
||||||
l1, l2 = node_factory.line_graph(2, opts=opts)
|
l1, l2 = node_factory.line_graph(2, opts=opts)
|
||||||
|
|
||||||
@@ -49,10 +50,10 @@ def test_summary_avail_101():
|
|||||||
# given
|
# given
|
||||||
plugin = get_stub()
|
plugin = get_stub()
|
||||||
rpcpeers = {
|
rpcpeers = {
|
||||||
'peers' : [
|
'peers': [
|
||||||
{ 'id' : '1', 'connected' : True },
|
{'id': '1', 'connected': True},
|
||||||
{ 'id' : '2', 'connected' : False },
|
{'id': '2', 'connected': False},
|
||||||
{ 'id' : '3', 'connected' : True },
|
{'id': '3', 'connected': True},
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -64,9 +65,9 @@ def test_summary_avail_101():
|
|||||||
assert(plugin.persist['peerstate']['1']['avail'] == 1.0)
|
assert(plugin.persist['peerstate']['1']['avail'] == 1.0)
|
||||||
assert(plugin.persist['peerstate']['2']['avail'] == 0.0)
|
assert(plugin.persist['peerstate']['2']['avail'] == 0.0)
|
||||||
assert(plugin.persist['peerstate']['3']['avail'] == 1.0)
|
assert(plugin.persist['peerstate']['3']['avail'] == 1.0)
|
||||||
assert(plugin.persist['peerstate']['1']['connected'] == True)
|
assert(plugin.persist['peerstate']['1']['connected'] is True)
|
||||||
assert(plugin.persist['peerstate']['2']['connected'] == False)
|
assert(plugin.persist['peerstate']['2']['connected'] is False)
|
||||||
assert(plugin.persist['peerstate']['3']['connected'] == True)
|
assert(plugin.persist['peerstate']['3']['connected'] is True)
|
||||||
|
|
||||||
|
|
||||||
# tests for 50% downtime
|
# tests for 50% downtime
|
||||||
@@ -74,13 +75,13 @@ def test_summary_avail_50():
|
|||||||
# given
|
# given
|
||||||
plugin = get_stub()
|
plugin = get_stub()
|
||||||
rpcpeers_on = {
|
rpcpeers_on = {
|
||||||
'peers' : [
|
'peers': [
|
||||||
{ 'id' : '1', 'connected' : True },
|
{'id': '1', 'connected': True},
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
rpcpeers_off = {
|
rpcpeers_off = {
|
||||||
'peers' : [
|
'peers': [
|
||||||
{ 'id' : '1', 'connected' : False },
|
{'id': '1', 'connected': False},
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -99,13 +100,13 @@ def test_summary_avail_33():
|
|||||||
# given
|
# given
|
||||||
plugin = get_stub()
|
plugin = get_stub()
|
||||||
rpcpeers_on = {
|
rpcpeers_on = {
|
||||||
'peers' : [
|
'peers': [
|
||||||
{ 'id' : '1', 'connected' : True },
|
{'id': '1', 'connected': True},
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
rpcpeers_off = {
|
rpcpeers_off = {
|
||||||
'peers' : [
|
'peers': [
|
||||||
{ 'id' : '1', 'connected' : False },
|
{'id': '1', 'connected': False},
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -124,13 +125,13 @@ def test_summary_avail_66():
|
|||||||
# given
|
# given
|
||||||
plugin = get_stub()
|
plugin = get_stub()
|
||||||
rpcpeers_on = {
|
rpcpeers_on = {
|
||||||
'peers' : [
|
'peers': [
|
||||||
{ 'id' : '1', 'connected' : True },
|
{'id': '1', 'connected': True},
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
rpcpeers_off = {
|
rpcpeers_off = {
|
||||||
'peers' : [
|
'peers': [
|
||||||
{ 'id' : '1', 'connected' : False },
|
{'id': '1', 'connected': False},
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -150,13 +151,13 @@ def test_summary_avail_leadwin():
|
|||||||
# given
|
# given
|
||||||
plugin = get_stub()
|
plugin = get_stub()
|
||||||
rpcpeers_on = {
|
rpcpeers_on = {
|
||||||
'peers' : [
|
'peers': [
|
||||||
{ 'id' : '1', 'connected' : True },
|
{'id': '1', 'connected': True},
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
rpcpeers_off = {
|
rpcpeers_off = {
|
||||||
'peers' : [
|
'peers': [
|
||||||
{ 'id' : '1', 'connected' : False },
|
{'id': '1', 'connected': False},
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user