Added paginated fetching to withdraw link table (#41)
Some checks failed
/ release (push) Has been cancelled
/ pullrequest (push) Has been cancelled

This commit is contained in:
Julian 2024-07-19 07:22:46 +02:00 committed by GitHub
commit 00064f65d0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 50 additions and 23 deletions

33
crud.py
View file

@ -1,5 +1,5 @@
from datetime import datetime from datetime import datetime
from typing import List, Optional, Union from typing import List, Optional, Tuple
import shortuuid import shortuuid
from lnbits.db import Database from lnbits.db import Database
@ -87,19 +87,32 @@ async def get_withdraw_link_by_hash(unique_hash: str, num=0) -> Optional[Withdra
return WithdrawLink.parse_obj(link) return WithdrawLink.parse_obj(link)
async def get_withdraw_links(wallet_ids: Union[str, List[str]]) -> List[WithdrawLink]: async def get_withdraw_links(
if isinstance(wallet_ids, str): wallet_ids: List[str], limit: int, offset: int
wallet_ids = [wallet_ids] ) -> Tuple[List[WithdrawLink], int]:
q = ",".join(["?"] * len(wallet_ids))
rows = await db.fetchall( rows = await db.fetchall(
f""" """
SELECT * FROM withdraw.withdraw_link SELECT * FROM withdraw.withdraw_link
WHERE wallet IN ({q}) ORDER BY open_time DESC WHERE wallet IN ({})
""", ORDER BY open_time DESC
LIMIT ? OFFSET ?
""".format(
",".join("?" * len(wallet_ids))
),
(*wallet_ids, limit, offset),
)
total = await db.fetchone(
"""
SELECT COUNT(*) as total FROM withdraw.withdraw_link
WHERE wallet IN ({})
""".format(
",".join("?" * len(wallet_ids))
),
(*wallet_ids,), (*wallet_ids,),
) )
return [WithdrawLink(**row) for row in rows]
return [WithdrawLink(**row) for row in rows], total["total"]
async def remove_unique_withdraw_link(link: WithdrawLink, unique_hash: str) -> None: async def remove_unique_withdraw_link(link: WithdrawLink, unique_hash: str) -> None:

View file

@ -59,7 +59,9 @@ new Vue({
{name: 'max', align: 'right', label: 'Max (sat)', field: 'max_fsat'} {name: 'max', align: 'right', label: 'Max (sat)', field: 'max_fsat'}
], ],
pagination: { pagination: {
rowsPerPage: 10 page: 1,
rowsPerPage: 10,
rowsNumber: 0
} }
}, },
nfcTagWriting: false, nfcTagWriting: false,
@ -97,19 +99,30 @@ new Vue({
} }
}, },
methods: { methods: {
getWithdrawLinks: function () { getWithdrawLinks: function (props) {
if (props) {
this.withdrawLinksTable.pagination = props.pagination
}
let pagination = this.withdrawLinksTable.pagination
const query = {
limit: pagination.rowsPerPage,
offset: (pagination.page - 1) * pagination.rowsPerPage
}
var self = this var self = this
LNbits.api LNbits.api
.request( .request(
'GET', 'GET',
'/withdraw/api/v1/links?all_wallets=true', `/withdraw/api/v1/links?all_wallets=true&limit=${query.limit}&offset=${query.offset}`,
this.g.user.wallets[0].inkey this.g.user.wallets[0].inkey
) )
.then(function (response) { .then(function (response) {
self.withdrawLinks = response.data.map(function (obj) { self.withdrawLinks = response.data.data.map(function (obj) {
return mapWithdrawLink(obj) return mapWithdrawLink(obj)
}) })
self.withdrawLinksTable.pagination.rowsNumber = response.data.total
}) })
.catch(function (error) { .catch(function (error) {
clearInterval(self.checker) clearInterval(self.checker)
@ -309,11 +322,8 @@ new Vue({
}, },
created: function () { created: function () {
if (this.g.user.wallets.length) { if (this.g.user.wallets.length) {
var getWithdrawLinks = this.getWithdrawLinks this.getWithdrawLinks()
getWithdrawLinks() this.checker = setInterval(this.getWithdrawLinks, 300000)
this.checker = setInterval(function () {
getWithdrawLinks()
}, 300000)
} }
} }
}) })

View file

@ -32,6 +32,7 @@
row-key="id" row-key="id"
:columns="withdrawLinksTable.columns" :columns="withdrawLinksTable.columns"
:pagination.sync="withdrawLinksTable.pagination" :pagination.sync="withdrawLinksTable.pagination"
@request="getWithdrawLinks"
> >
{% raw %} {% raw %}
<template v-slot:header="props"> <template v-slot:header="props">

View file

@ -25,6 +25,8 @@ async def api_links(
req: Request, req: Request,
wallet: WalletTypeInfo = Depends(get_key_type), wallet: WalletTypeInfo = Depends(get_key_type),
all_wallets: bool = Query(False), all_wallets: bool = Query(False),
offset: int = Query(0),
limit: int = Query(0),
): ):
wallet_ids = [wallet.wallet.id] wallet_ids = [wallet.wallet.id]
@ -33,10 +35,11 @@ async def api_links(
wallet_ids = user.wallet_ids if user else [] wallet_ids = user.wallet_ids if user else []
try: try:
return [ links, total = await get_withdraw_links(wallet_ids, limit, offset)
{**link.dict(), **{"lnurl": link.lnurl(req)}} return {
for link in await get_withdraw_links(wallet_ids) "data": [{**link.dict(), **{"lnurl": link.lnurl(req)}} for link in links],
] "total": total,
}
except LnurlInvalidUrl as exc: except LnurlInvalidUrl as exc:
raise HTTPException( raise HTTPException(