lnbits-legend/lnbits/extensions/gerty/helpers.py

242 lines
7.8 KiB
Python
Raw Normal View History

2022-10-20 16:31:09 +01:00
import textwrap
2022-10-10 13:38:21 +01:00
from datetime import datetime, timedelta
2022-10-20 16:31:09 +01:00
import httpx
2022-10-06 13:14:31 +01:00
from loguru import logger
2022-10-06 12:37:11 +01:00
from .number_prefixer import *
2022-10-20 16:31:09 +01:00
2022-10-06 16:12:48 +01:00
def get_percent_difference(current, previous, precision=4):
difference = (current - previous) / current * 100
return "{0}{1}%".format("+" if difference > 0 else "", round(difference, precision))
2022-10-06 12:37:11 +01:00
2022-10-20 16:31:09 +01:00
2022-10-06 12:37:11 +01:00
# A helper function get a nicely formated dict for the text
def get_text_item_dict(text: str, font_size: int, x_pos: int = None, y_pos: int = None):
# Get line size by font size
2022-11-07 16:18:40 +00:00
line_width = 20
2022-10-06 12:37:11 +01:00
if font_size <= 12:
2022-11-07 16:18:40 +00:00
line_width = 60
2022-10-06 12:37:11 +01:00
elif font_size <= 15:
2022-11-07 16:18:40 +00:00
line_width = 45
2022-10-06 12:37:11 +01:00
elif font_size <= 20:
2022-11-07 16:18:40 +00:00
line_width = 35
2022-10-06 12:37:11 +01:00
elif font_size <= 40:
2022-11-07 16:18:40 +00:00
line_width = 25
2022-10-06 12:37:11 +01:00
# wrap the text
wrapper = textwrap.TextWrapper(width=line_width)
word_list = wrapper.wrap(text=text)
# logger.debug("number of chars = {0}".format(len(text)))
2022-10-20 16:31:09 +01:00
multilineText = "\n".join(word_list)
2022-10-06 12:37:11 +01:00
# logger.debug("number of lines = {0}".format(len(word_list)))
# logger.debug('multilineText')
# logger.debug(multilineText)
2022-10-20 16:31:09 +01:00
text = {"value": multilineText, "size": font_size}
2022-10-06 12:37:11 +01:00
if x_pos is None and y_pos is None:
2022-10-20 16:31:09 +01:00
text["position"] = "center"
2022-10-06 12:37:11 +01:00
else:
2022-10-20 16:31:09 +01:00
text["x"] = x_pos
text["y"] = y_pos
2022-10-06 12:37:11 +01:00
return text
2022-10-20 16:31:09 +01:00
2022-10-06 12:37:11 +01:00
# format a number for nice display output
2022-10-06 16:12:48 +01:00
def format_number(number, precision=None):
2022-10-20 16:31:09 +01:00
return "{:,}".format(round(number, precision))
2022-10-06 12:37:11 +01:00
async def get_mempool_recommended_fees(gerty):
if isinstance(gerty.mempool_endpoint, str):
async with httpx.AsyncClient() as client:
r = await client.get(gerty.mempool_endpoint + "/api/v1/fees/recommended")
2022-10-06 12:37:11 +01:00
return r.json()
2022-10-20 16:31:09 +01:00
2022-10-20 15:59:55 +01:00
async def get_mining_dashboard(gerty):
areas = []
2022-10-06 12:37:11 +01:00
if isinstance(gerty.mempool_endpoint, str):
async with httpx.AsyncClient() as client:
2022-10-20 15:59:55 +01:00
# current hashrate
r = await client.get(gerty.mempool_endpoint + "/api/v1/mining/hashrate/1w")
data = r.json()
2022-10-20 16:31:09 +01:00
hashrateNow = data["currentHashrate"]
hashrateOneWeekAgo = data["hashrates"][6]["avgHashrate"]
2022-10-20 15:59:55 +01:00
text = []
text.append(get_text_item_dict("Current mining hashrate", 12))
2022-10-20 16:31:09 +01:00
text.append(
get_text_item_dict(
"{0}hash".format(si_format(hashrateNow, 6, True, " ")), 20
)
)
text.append(
get_text_item_dict(
"{0} vs 7 days ago".format(
get_percent_difference(hashrateNow, hashrateOneWeekAgo, 3)
),
12,
)
)
2022-10-20 15:59:55 +01:00
areas.append(text)
2022-10-20 16:31:09 +01:00
r = await client.get(
gerty.mempool_endpoint + "/api/v1/difficulty-adjustment"
)
2022-10-20 15:59:55 +01:00
# timeAvg
text = []
2022-10-20 16:31:09 +01:00
progress = "{0}%".format(round(r.json()["progressPercent"], 2))
text.append(get_text_item_dict("Progress through current epoch", 12))
2022-10-20 17:15:12 +01:00
text.append(get_text_item_dict(progress, 60))
2022-10-20 15:59:55 +01:00
areas.append(text)
# difficulty adjustment
text = []
2022-10-20 16:31:09 +01:00
stat = r.json()["remainingTime"]
2022-10-20 15:59:55 +01:00
text.append(get_text_item_dict("Time to next difficulty adjustment", 12))
text.append(get_text_item_dict(get_time_remaining(stat / 1000, 3), 12))
2022-10-20 15:59:55 +01:00
areas.append(text)
# difficultyChange
text = []
2022-10-20 16:31:09 +01:00
difficultyChange = round(r.json()["difficultyChange"], 2)
2022-10-20 15:59:55 +01:00
text.append(get_text_item_dict("Estimated difficulty change", 12))
2022-10-20 16:31:09 +01:00
text.append(
get_text_item_dict(
"{0}{1}%".format(
"+" if difficultyChange > 0 else "", round(difficultyChange, 2)
),
2022-10-20 17:15:12 +01:00
60,
2022-10-20 16:31:09 +01:00
)
)
2022-10-20 15:59:55 +01:00
areas.append(text)
r = await client.get(gerty.mempool_endpoint + "/api/v1/mining/hashrate/1m")
data = r.json()
stat = {}
2022-10-20 16:31:09 +01:00
stat["current"] = data["currentDifficulty"]
stat["previous"] = data["difficulty"][len(data["difficulty"]) - 2][
"difficulty"
]
2022-10-20 15:59:55 +01:00
return areas
2022-10-06 12:37:11 +01:00
2022-10-20 16:31:09 +01:00
2022-10-06 16:12:48 +01:00
async def api_get_lightning_stats(gerty):
stat = {}
if isinstance(gerty.mempool_endpoint, str):
async with httpx.AsyncClient() as client:
2022-10-20 16:31:09 +01:00
r = await client.get(
gerty.mempool_endpoint + "/api/v1/lightning/statistics/latest"
)
2022-10-06 16:12:48 +01:00
data = r.json()
return data
2022-10-20 16:31:09 +01:00
2022-10-06 16:12:48 +01:00
async def get_lightning_stats(gerty):
data = await api_get_lightning_stats(gerty)
areas = []
text = []
text.append(get_text_item_dict("Channel Count", 12))
2022-10-20 16:31:09 +01:00
text.append(get_text_item_dict(format_number(data["latest"]["channel_count"]), 20))
difference = get_percent_difference(
current=data["latest"]["channel_count"],
previous=data["previous"]["channel_count"],
)
2022-10-06 16:12:48 +01:00
text.append(get_text_item_dict("{0} in last 7 days".format(difference), 12))
areas.append(text)
text = []
text.append(get_text_item_dict("Number of Nodes", 12))
2022-10-20 16:31:09 +01:00
text.append(get_text_item_dict(format_number(data["latest"]["node_count"]), 20))
difference = get_percent_difference(
current=data["latest"]["node_count"], previous=data["previous"]["node_count"]
)
2022-10-06 16:12:48 +01:00
text.append(get_text_item_dict("{0} in last 7 days".format(difference), 12))
areas.append(text)
text = []
text.append(get_text_item_dict("Total Capacity", 12))
2022-10-20 16:31:09 +01:00
avg_capacity = float(data["latest"]["total_capacity"]) / float(100000000)
text.append(
get_text_item_dict("{0} BTC".format(format_number(avg_capacity, 2)), 20)
)
difference = get_percent_difference(
current=data["latest"]["total_capacity"],
previous=data["previous"]["total_capacity"],
)
2022-10-06 16:12:48 +01:00
text.append(get_text_item_dict("{0} in last 7 days".format(difference), 12))
areas.append(text)
text = []
text.append(get_text_item_dict("Average Channel Capacity", 12))
2022-10-20 16:31:09 +01:00
text.append(
get_text_item_dict(
"{0} sats".format(format_number(data["latest"]["avg_capacity"])), 20
)
)
difference = get_percent_difference(
current=data["latest"]["avg_capacity"],
previous=data["previous"]["avg_capacity"],
)
2022-10-06 16:12:48 +01:00
text.append(get_text_item_dict("{0} in last 7 days".format(difference), 12))
areas.append(text)
return areas
2022-10-06 12:37:11 +01:00
2022-10-20 16:31:09 +01:00
2022-10-20 17:15:12 +01:00
def get_next_update_time(sleep_time_seconds: int = 0, utc_offset: int = 0):
2022-10-20 17:01:03 +01:00
utc_now = datetime.utcnow()
2022-10-10 13:38:21 +01:00
next_refresh_time = utc_now + timedelta(0, sleep_time_seconds)
2022-10-20 17:15:12 +01:00
local_refresh_time = next_refresh_time + timedelta(hours=utc_offset)
2022-10-20 16:31:09 +01:00
return "{0} {1}".format(
2022-10-20 17:15:12 +01:00
"I'll wake up at" if gerty_should_sleep(utc_offset) else "Next update at",
2022-10-20 16:31:09 +01:00
local_refresh_time.strftime("%H:%M on %e %b %Y"),
)
2022-10-07 10:25:54 +01:00
2022-10-20 17:15:12 +01:00
def gerty_should_sleep(utc_offset: int = 0):
utc_now = datetime.utcnow()
local_time = utc_now + timedelta(hours=utc_offset)
2022-10-07 10:25:54 +01:00
hours = local_time.strftime("%H")
hours = int(hours)
logger.debug("HOURS")
logger.debug(hours)
2022-10-20 16:31:09 +01:00
if hours >= 22 and hours <= 23:
2022-10-07 10:25:54 +01:00
return True
else:
return False
2022-10-20 15:59:55 +01:00
def get_date_suffix(dayNumber):
if 4 <= dayNumber <= 20 or 24 <= dayNumber <= 30:
return "th"
else:
return ["st", "nd", "rd"][dayNumber % 10 - 1]
def get_time_remaining(seconds, granularity=2):
intervals = (
# ('weeks', 604800), # 60 * 60 * 24 * 7
2022-10-20 16:31:09 +01:00
("days", 86400), # 60 * 60 * 24
("hours", 3600), # 60 * 60
("minutes", 60),
("seconds", 1),
2022-10-20 15:59:55 +01:00
)
result = []
for name, count in intervals:
value = seconds // count
if value:
seconds -= value * count
if value == 1:
2022-10-20 16:31:09 +01:00
name = name.rstrip("s")
2022-10-20 15:59:55 +01:00
result.append("{} {}".format(round(value), name))
2022-10-20 16:31:09 +01:00
return ", ".join(result[:granularity])