Added mini gerty api support to front end

This commit is contained in:
Black Coffee 2022-11-17 15:11:31 +00:00
parent 592a52e9a2
commit b2e2c11af8
3 changed files with 137 additions and 99 deletions

View file

@ -13,7 +13,7 @@ def get_percent_difference(current, previous, precision=4):
# 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):
def get_text_item_dict(text: str, font_size: int, x_pos: int = None, y_pos: int = None, gerty_type: str = 'Gerty'):
# Get line size by font size
line_width = 20
if font_size <= 12:
@ -25,6 +25,19 @@ def get_text_item_dict(text: str, font_size: int, x_pos: int = None, y_pos: int
elif font_size <= 40:
line_width = 25
# Get font sizes for Gerty mini
if(gerty_type.lower() == 'mini gerty'):
if font_size <= 15:
font_size = 2
elif font_size <= 20:
font_size = 3
elif font_size <= 40:
font_size = 4
else:
font_size = 5
# wrap the text
wrapper = textwrap.TextWrapper(width=line_width)
word_list = wrapper.wrap(text=text)
@ -68,18 +81,18 @@ async def get_mining_dashboard(gerty):
hashrateOneWeekAgo = data["hashrates"][6]["avgHashrate"]
text = []
text.append(get_text_item_dict("Current mining hashrate", 12))
text.append(get_text_item_dict(text="Current mining hashrate", font_size=12,gerty_type=gerty.type))
text.append(
get_text_item_dict(
"{0}hash".format(si_format(hashrateNow, 6, True, " ")), 20
text="{0}hash".format(si_format(hashrateNow, 6, True, " ")), font_size=20,gerty_type=gerty.type
)
)
text.append(
get_text_item_dict(
"{0} vs 7 days ago".format(
text="{0} vs 7 days ago".format(
get_percent_difference(hashrateNow, hashrateOneWeekAgo, 3)
),
12,
font_size=12,gerty_type=gerty.type
)
)
areas.append(text)
@ -91,27 +104,27 @@ async def get_mining_dashboard(gerty):
# timeAvg
text = []
progress = "{0}%".format(round(r.json()["progressPercent"], 2))
text.append(get_text_item_dict("Progress through current epoch", 12))
text.append(get_text_item_dict(progress, 60))
text.append(get_text_item_dict(text="Progress through current epoch", font_size=12,gerty_type=gerty.type))
text.append(get_text_item_dict(text=progress, font_size=60,gerty_type=gerty.type))
areas.append(text)
# difficulty adjustment
text = []
stat = r.json()["remainingTime"]
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))
text.append(get_text_item_dict(text="Time to next difficulty adjustment", font_size=12,gerty_type=gerty.type))
text.append(get_text_item_dict(text=get_time_remaining(stat / 1000, 3), font_size=12,gerty_type=gerty.type))
areas.append(text)
# difficultyChange
text = []
difficultyChange = round(r.json()["difficultyChange"], 2)
text.append(get_text_item_dict("Estimated difficulty change", 12))
text.append(get_text_item_dict(text="Estimated difficulty change", font_size=12,gerty_type=gerty.type))
text.append(
get_text_item_dict(
"{0}{1}%".format(
text="{0}{1}%".format(
"+" if difficultyChange > 0 else "", round(difficultyChange, 2)
),
60,
font_size=60,gerty_type=gerty.type
)
)
areas.append(text)
@ -142,49 +155,49 @@ async def get_lightning_stats(gerty):
areas = []
text = []
text.append(get_text_item_dict("Channel Count", 12))
text.append(get_text_item_dict(format_number(data["latest"]["channel_count"]), 20))
text.append(get_text_item_dict(text="Channel Count", font_size=12, gerty_type=gerty.type))
text.append(get_text_item_dict(text=format_number(data["latest"]["channel_count"]), font_size=20, gerty_type=gerty.type))
difference = get_percent_difference(
current=data["latest"]["channel_count"],
previous=data["previous"]["channel_count"],
)
text.append(get_text_item_dict("{0} in last 7 days".format(difference), 12))
text.append(get_text_item_dict(text="{0} in last 7 days".format(difference), font_size=12, gerty_type=gerty.type))
areas.append(text)
text = []
text.append(get_text_item_dict("Number of Nodes", 12))
text.append(get_text_item_dict(format_number(data["latest"]["node_count"]), 20))
text.append(get_text_item_dict(text="Number of Nodes", font_size=12,gerty_type=gerty.type))
text.append(get_text_item_dict(text=format_number(data["latest"]["node_count"]), font_size=20,gerty_type=gerty.type))
difference = get_percent_difference(
current=data["latest"]["node_count"], previous=data["previous"]["node_count"]
)
text.append(get_text_item_dict("{0} in last 7 days".format(difference), 12))
text.append(get_text_item_dict(text="{0} in last 7 days".format(difference), font_size=12,gerty_type=gerty.type))
areas.append(text)
text = []
text.append(get_text_item_dict("Total Capacity", 12))
text.append(get_text_item_dict(text="Total Capacity", font_size=12,gerty_type=gerty.type))
avg_capacity = float(data["latest"]["total_capacity"]) / float(100000000)
text.append(
get_text_item_dict("{0} BTC".format(format_number(avg_capacity, 2)), 20)
get_text_item_dict(text="{0} BTC".format(format_number(avg_capacity, 2)), font_size=20,gerty_type=gerty.type)
)
difference = get_percent_difference(
current=data["latest"]["total_capacity"],
previous=data["previous"]["total_capacity"],
)
text.append(get_text_item_dict("{0} in last 7 days".format(difference), 12))
text.append(get_text_item_dict(text="{0} in last 7 days".format(difference), font_size=12,gerty_type=gerty.type))
areas.append(text)
text = []
text.append(get_text_item_dict("Average Channel Capacity", 12))
text.append(get_text_item_dict(text="Average Channel Capacity", font_size=12,gerty_type=gerty.type))
text.append(
get_text_item_dict(
"{0} sats".format(format_number(data["latest"]["avg_capacity"])), 20
text="{0} sats".format(format_number(data["latest"]["avg_capacity"])), font_size=20,gerty_type=gerty.type
)
)
difference = get_percent_difference(
current=data["latest"]["avg_capacity"],
previous=data["previous"]["avg_capacity"],
)
text.append(get_text_item_dict("{0} in last 7 days".format(difference), 12))
text.append(get_text_item_dict(text="{0} in last 7 days".format(difference), font_size=12, gerty_type=gerty.type))
areas.append(text)
return areas
@ -247,17 +260,17 @@ async def get_mining_stat(stat_slug: str, gerty):
stat = await api_get_mining_stat(stat_slug, gerty)
logger.debug(stat)
current = "{0}hash".format(si_format(stat['current'], 6, True, " "))
text.append(get_text_item_dict("Current Mining Hashrate", 20))
text.append(get_text_item_dict(current, 40))
text.append(get_text_item_dict(text="Current Mining Hashrate", font_size=20,gerty_type=gerty.type))
text.append(get_text_item_dict(text=current, font_size=40,gerty_type=gerty.type))
# compare vs previous time period
difference = get_percent_difference(current=stat['current'], previous=stat['1w'])
text.append(get_text_item_dict("{0} in last 7 days".format(difference), 12))
text.append(get_text_item_dict(text="{0} in last 7 days".format(difference), font_size=12,gerty_type=gerty.type))
elif stat_slug == "mining_current_difficulty":
stat = await api_get_mining_stat(stat_slug, gerty)
text.append(get_text_item_dict("Current Mining Difficulty", 20))
text.append(get_text_item_dict(format_number(stat['current']), 40))
text.append(get_text_item_dict(text="Current Mining Difficulty", font_size=20,gerty_type=gerty.type))
text.append(get_text_item_dict(text=format_number(stat['current']), font_size=40,gerty_type=gerty.type))
difference = get_percent_difference(current=stat['current'], previous=stat['previous'])
text.append(get_text_item_dict("{0} since last adjustment".format(difference), 12))
text.append(get_text_item_dict(text="{0} since last adjustment".format(difference), font_size=12,gerty_type=gerty.type))
# text.append(get_text_item_dict("Required threshold for mining proof-of-work", 12))
return text

View file

@ -198,7 +198,9 @@
<p>Use the toggles below to control what your Gerty will display</p>
<q-expansion-item
v-if="!isMiniGerty"
expand-separator
icon="grid_view"
label="Dashboards"
@ -208,17 +210,6 @@
label="LNbits Dashboard"
></q-toggle>
<q-toggle
v-model="formDialog.data.display_preferences.fun_satoshi_quotes"
label="Satoshi Quotes"
>
<q-tooltip>Displays random quotes from Satoshi</q-tooltip>
</q-toggle>
<q-toggle
v-model="formDialog.data.display_preferences.fun_exchange_market_rate"
label="Fiat to BTC price"
></q-toggle>
<q-toggle
v-model="formDialog.data.display_preferences.dashboard_onchain"
label="Onchain Dashboard"
@ -241,8 +232,20 @@
expand-separator
icon="pin"
label="Single Data Points"
ref="single-data-points-expansion"
>
<q-toggle
v-model="formDialog.data.display_preferences.fun_exchange_market_rate"
label="Fiat to BTC price"
></q-toggle>
<q-toggle
v-if="!isMiniGerty"
v-model="formDialog.data.display_preferences.fun_satoshi_quotes"
label="Satoshi Quotes"
>
<q-tooltip>Displays random quotes from Satoshi</q-tooltip>
</q-toggle>
<q-expansion-item
expand-separator
icon="perm_identity"
@ -757,12 +760,30 @@
LNbits.utils.exportCSV(this.gertysTable.columns, this.gertys)
}
},
computed: {
isMiniGerty() {
return (this.formDialog.data.type == 'Mini Gerty')
}
},
created: function () {
if (this.g.user.wallets.length) {
this.getGertys()
}
},
watch: {
'formDialog.data.type': {
handler(value) {
if (value == 'Mini Gerty') {
this.formDialog.data.display_preferences.dashboard = false;
this.formDialog.data.display_preferences.dashboard_onchain = false;
this.formDialog.data.display_preferences.dashboard_mining = false;
this.formDialog.data.display_preferences.lightning_dashboard = false;
this.formDialog.data.display_preferences.fun_satoshi_quotes = false;
this.formDialog.data.display_preferences.mempool_recommended_fees = false;
this.formDialog.data.display_preferences.onchain = false;
}
}
},
toggleStates: {
handler(toggleStatesValue) {
// Switch all the toggles in each section to the relevant state

View file

@ -177,11 +177,11 @@ async def get_screen_data(screen_num: int, screens_list: dict, gerty):
wallets = await get_lnbits_wallet_balances(gerty)
text = []
for wallet in wallets:
text.append(get_text_item_dict("{0}'s Wallet".format(wallet['name']), 20))
text.append(get_text_item_dict("{0} sats".format(format_number(wallet['balance'])), 40))
text.append(get_text_item_dict(text="{0}'s Wallet".format(wallet['name']), font_size=20,gerty_type=gerty.type))
text.append(get_text_item_dict(text="{0} sats".format(format_number(wallet['balance'])), font_size=40,gerty_type=gerty.type))
areas.append(text)
elif screen_slug == "fun_satoshi_quotes":
areas.append(await get_satoshi_quotes())
areas.append(await get_satoshi_quotes(gerty))
elif screen_slug == "fun_exchange_market_rate":
areas.append(await get_exchange_rate(gerty))
elif screen_slug == "onchain_difficulty_epoch_progress":
@ -223,34 +223,34 @@ async def get_dashboard(gerty):
# XC rate
text = []
amount = await satoshis_amount_as_fiat(100000000, gerty.exchange)
text.append(get_text_item_dict(format_number(amount), 40))
text.append(get_text_item_dict("BTC{0} price".format(gerty.exchange), 15))
text.append(get_text_item_dict(text=format_number(amount), font_size=40,gerty_type=gerty.type))
text.append(get_text_item_dict(text="BTC{0} price".format(gerty.exchange), font_size=15,gerty_type=gerty.type))
areas.append(text)
# balance
text = []
wallets = await get_lnbits_wallet_balances(gerty)
text = []
for wallet in wallets:
text.append(get_text_item_dict("{0}".format(wallet["name"]), 15))
text.append(get_text_item_dict(text="{0}".format(wallet["name"]), font_size=15,gerty_type=gerty.type))
text.append(
get_text_item_dict("{0} sats".format(format_number(wallet["balance"])), 20)
get_text_item_dict(text="{0} sats".format(format_number(wallet["balance"])), font_size=20,gerty_type=gerty.type)
)
areas.append(text)
# Mempool fees
text = []
text.append(get_text_item_dict(format_number(await get_block_height(gerty)), 40))
text.append(get_text_item_dict("Current block height", 15))
text.append(get_text_item_dict(text=format_number(await get_block_height(gerty)), font_size=40,gerty_type=gerty.type))
text.append(get_text_item_dict(text="Current block height", font_size=15,gerty_type=gerty.type))
areas.append(text)
# difficulty adjustment time
text = []
text.append(
get_text_item_dict(
await get_time_remaining_next_difficulty_adjustment(gerty), 15
text=await get_time_remaining_next_difficulty_adjustment(gerty), font_size=15,gerty_type=gerty.type
)
)
text.append(get_text_item_dict("until next difficulty adjustment", 12))
text.append(get_text_item_dict(text="until next difficulty adjustment", font_size=12,gerty_type=gerty.type))
areas.append(text)
return areas
@ -276,21 +276,21 @@ async def get_lnbits_wallet_balances(gerty):
async def get_placeholder_text():
return [
get_text_item_dict("Some placeholder text", 15, 10, 50),
get_text_item_dict("Some placeholder text", 15, 10, 50),
get_text_item_dict(text="Some placeholder text", x_pos=15, y_pos=10, font_size=50,gerty_type=gerty.type),
get_text_item_dict(text="Some placeholder text", x_pos=15, y_pos=10, font_size=50,gerty_type=gerty.type),
]
async def get_satoshi_quotes():
async def get_satoshi_quotes(gerty):
# Get Satoshi quotes
text = []
quote = await api_gerty_satoshi()
if quote:
if quote["text"]:
text.append(get_text_item_dict(quote["text"], 15))
text.append(get_text_item_dict(text=quote["text"], font_size=15,gerty_type=gerty.type))
if quote["date"]:
text.append(
get_text_item_dict("Satoshi Nakamoto - {0}".format(quote["date"]), 15)
get_text_item_dict(text="Satoshi Nakamoto - {0}".format(quote["date"]), font_size=15,gerty_type=gerty.type)
)
return text
@ -305,10 +305,10 @@ async def get_exchange_rate(gerty):
price = format_number(amount)
text.append(
get_text_item_dict(
"Current {0}/BTC price".format(gerty.exchange), 15
text="Current {0}/BTC price".format(gerty.exchange), font_size=15,gerty_type=gerty.type
)
)
text.append(get_text_item_dict(price, 80))
text.append(get_text_item_dict(text=price, font_size=80,gerty_type=gerty.type))
except:
pass
return text
@ -325,21 +325,21 @@ async def get_onchain_stat(stat_slug: str, gerty):
r = await client.get(gerty.mempool_endpoint + "/api/v1/difficulty-adjustment")
if stat_slug == "onchain_difficulty_epoch_progress":
stat = round(r.json()['progressPercent'])
text.append(get_text_item_dict("Progress through current difficulty epoch", 15))
text.append(get_text_item_dict("{0}%".format(stat), 80))
text.append(get_text_item_dict(text="Progress through current difficulty epoch", font_size=15,gerty_type=gerty.type))
text.append(get_text_item_dict(text="{0}%".format(stat), font_size=80,gerty_type=gerty.type))
elif stat_slug == "onchain_difficulty_retarget_date":
stat = r.json()['estimatedRetargetDate']
dt = datetime.fromtimestamp(stat / 1000).strftime("%e %b %Y at %H:%M")
text.append(get_text_item_dict("Estimated date of next difficulty adjustment", 15))
text.append(get_text_item_dict(dt, 40))
text.append(get_text_item_dict(text="Estimated date of next difficulty adjustment", font_size=15,gerty_type=gerty.type))
text.append(get_text_item_dict(text=dt, font_size=40,gerty_type=gerty.type))
elif stat_slug == "onchain_difficulty_blocks_remaining":
stat = r.json()['remainingBlocks']
text.append(get_text_item_dict("Blocks remaining until next difficulty adjustment", 15))
text.append(get_text_item_dict("{0}".format(format_number(stat)), 80))
text.append(get_text_item_dict(text="Blocks remaining until next difficulty adjustment", font_size=15,gerty_type=gerty.type))
text.append(get_text_item_dict(text="{0}".format(format_number(stat)), font_size=80,gerty_type=gerty.type))
elif stat_slug == "onchain_difficulty_epoch_time_remaining":
stat = r.json()['remainingTime']
text.append(get_text_item_dict("Blocks remaining until next difficulty adjustment", 15))
text.append(get_text_item_dict(get_time_remaining(stat / 1000, 4), 20))
text.append(get_text_item_dict(text="Blocks remaining until next difficulty adjustment", font_size=15,gerty_type=gerty.type))
text.append(get_text_item_dict(text=get_time_remaining(stat / 1000, 4), font_size=20,gerty_type=gerty.type))
return text
async def get_onchain_dashboard(gerty):
@ -351,27 +351,27 @@ async def get_onchain_dashboard(gerty):
)
text = []
stat = round(r.json()["progressPercent"])
text.append(get_text_item_dict("Progress through epoch", 12))
text.append(get_text_item_dict("{0}%".format(stat), 60))
text.append(get_text_item_dict(text="Progress through epoch", font_size=12,gerty_type=gerty.type))
text.append(get_text_item_dict(text="{0}%".format(stat), font_size=60,gerty_type=gerty.type))
areas.append(text)
text = []
stat = r.json()["estimatedRetargetDate"]
dt = datetime.fromtimestamp(stat / 1000).strftime("%e %b %Y at %H:%M")
text.append(get_text_item_dict("Date of next adjustment", 12))
text.append(get_text_item_dict(dt, 20))
text.append(get_text_item_dict(text="Date of next adjustment", font_size=12,gerty_type=gerty.type))
text.append(get_text_item_dict(text=dt, font_size=20,gerty_type=gerty.type))
areas.append(text)
text = []
stat = r.json()["remainingBlocks"]
text.append(get_text_item_dict("Blocks until adjustment", 12))
text.append(get_text_item_dict("{0}".format(format_number(stat)), 60))
text.append(get_text_item_dict(text="Blocks until adjustment", font_size=12,gerty_type=gerty.type))
text.append(get_text_item_dict(text="{0}".format(format_number(stat)), font_size=60,gerty_type=gerty.type))
areas.append(text)
text = []
stat = r.json()["remainingTime"]
text.append(get_text_item_dict("Time until adjustment", 12))
text.append(get_text_item_dict(get_time_remaining(stat / 1000, 4), 20))
text.append(get_text_item_dict(text="Time until adjustment", font_size=12,gerty_type=gerty.type))
text.append(get_text_item_dict(text=get_time_remaining(stat / 1000, 4), font_size=20,gerty_type=gerty.type))
areas.append(text)
return areas
@ -404,30 +404,30 @@ async def get_mempool_stat(stat_slug: str, gerty):
r = await client.get(gerty.mempool_endpoint + "/api/mempool")
if stat_slug == "mempool_tx_count":
stat = round(r.json()["count"])
text.append(get_text_item_dict("Transactions in the mempool", 15))
text.append(get_text_item_dict(text="Transactions in the mempool", font_size=15,gerty_type=gerty.type))
text.append(
get_text_item_dict("{0}".format(format_number(stat)), 80)
get_text_item_dict(text="{0}".format(format_number(stat)), font_size=80,gerty_type=gerty.type)
)
elif stat_slug == "mempool_recommended_fees":
y_offset = 60
fees = await get_mempool_recommended_fees(gerty)
pos_y = 80 + y_offset
text.append(get_text_item_dict("mempool.space", 40, 160, pos_y))
text.append(get_text_item_dict("mempool.space", 40, 160, pos_y, gerty.type))
pos_y = 180 + y_offset
text.append(get_text_item_dict("Recommended Tx Fees", 20, 240, pos_y))
text.append(get_text_item_dict("Recommended Tx Fees", 20, 240, pos_y, gerty.type))
pos_y = 280 + y_offset
text.append(
get_text_item_dict("{0}".format("None"), 15, 30, pos_y)
get_text_item_dict("{0}".format("None"), 15, 30, pos_y, gerty.type)
)
text.append(
get_text_item_dict("{0}".format("Low"), 15, 235, pos_y)
get_text_item_dict("{0}".format("Low"), 15, 235, pos_y, gerty.type)
)
text.append(
get_text_item_dict("{0}".format("Medium"), 15, 460, pos_y)
get_text_item_dict("{0}".format("Medium"), 15, 460, pos_y, gerty.type)
)
text.append(
get_text_item_dict("{0}".format("High"), 15, 750, pos_y)
get_text_item_dict("{0}".format("High"), 15, 750, pos_y, gerty.type)
)
pos_y = 340 + y_offset
@ -436,56 +436,60 @@ async def get_mempool_stat(stat_slug: str, gerty):
fee_rate = fees["economyFee"]
text.append(
get_text_item_dict(
"{0} {1}{2}".format(
text="{0} {1}{2}".format(
format_number(fee_rate),
("sat" if fee_rate == 1 else "sats"),
fee_append,
),
font_size,
30,
pos_y,
font_size=font_size,
x_pos=30,
y_pos=pos_y,
gerty_type=gerty.type
)
)
fee_rate = fees["hourFee"]
text.append(
get_text_item_dict(
"{0} {1}{2}".format(
text="{0} {1}{2}".format(
format_number(fee_rate),
("sat" if fee_rate == 1 else "sats"),
fee_append,
),
font_size,
235,
pos_y,
font_size=font_size,
x_pos=235,
y_pos=pos_y,
gerty_type=gerty.type
)
)
fee_rate = fees["halfHourFee"]
text.append(
get_text_item_dict(
"{0} {1}{2}".format(
text="{0} {1}{2}".format(
format_number(fee_rate),
("sat" if fee_rate == 1 else "sats"),
fee_append,
),
font_size,
460,
pos_y,
font_size=font_size,
x_pos=460,
y_pos=pos_y,
gerty_type=gerty.type
)
)
fee_rate = fees["fastestFee"]
text.append(
get_text_item_dict(
"{0} {1}{2}".format(
text="{0} {1}{2}".format(
format_number(fee_rate),
("sat" if fee_rate == 1 else "sats"),
fee_append,
),
font_size,
750,
pos_y,
font_size=font_size,
x_pos=750,
y_pos=pos_y,
gerty_type=gerty.type
)
)
return text