From 00064f65d085d4d31c5e4be25b29ecfe6591e3b8 Mon Sep 17 00:00:00 2001 From: Julian Date: Fri, 19 Jul 2024 07:22:46 +0200 Subject: [PATCH] Added paginated fetching to withdraw link table (#41) --- crud.py | 33 +++++++++++++++++++++++---------- static/js/index.js | 28 +++++++++++++++++++--------- templates/withdraw/index.html | 1 + views_api.py | 11 +++++++---- 4 files changed, 50 insertions(+), 23 deletions(-) diff --git a/crud.py b/crud.py index 3da4be3..aa45ecb 100644 --- a/crud.py +++ b/crud.py @@ -1,5 +1,5 @@ from datetime import datetime -from typing import List, Optional, Union +from typing import List, Optional, Tuple import shortuuid 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) -async def get_withdraw_links(wallet_ids: Union[str, List[str]]) -> List[WithdrawLink]: - if isinstance(wallet_ids, str): - wallet_ids = [wallet_ids] - - q = ",".join(["?"] * len(wallet_ids)) +async def get_withdraw_links( + wallet_ids: List[str], limit: int, offset: int +) -> Tuple[List[WithdrawLink], int]: rows = await db.fetchall( - f""" + """ 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,), ) - 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: diff --git a/static/js/index.js b/static/js/index.js index 2e978c5..46eea7c 100644 --- a/static/js/index.js +++ b/static/js/index.js @@ -59,7 +59,9 @@ new Vue({ {name: 'max', align: 'right', label: 'Max (sat)', field: 'max_fsat'} ], pagination: { - rowsPerPage: 10 + page: 1, + rowsPerPage: 10, + rowsNumber: 0 } }, nfcTagWriting: false, @@ -97,19 +99,30 @@ new Vue({ } }, 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 LNbits.api .request( '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 ) .then(function (response) { - self.withdrawLinks = response.data.map(function (obj) { + self.withdrawLinks = response.data.data.map(function (obj) { return mapWithdrawLink(obj) }) + self.withdrawLinksTable.pagination.rowsNumber = response.data.total }) .catch(function (error) { clearInterval(self.checker) @@ -309,11 +322,8 @@ new Vue({ }, created: function () { if (this.g.user.wallets.length) { - var getWithdrawLinks = this.getWithdrawLinks - getWithdrawLinks() - this.checker = setInterval(function () { - getWithdrawLinks() - }, 300000) + this.getWithdrawLinks() + this.checker = setInterval(this.getWithdrawLinks, 300000) } } }) diff --git a/templates/withdraw/index.html b/templates/withdraw/index.html index 6b41fd4..dc165a0 100644 --- a/templates/withdraw/index.html +++ b/templates/withdraw/index.html @@ -32,6 +32,7 @@ row-key="id" :columns="withdrawLinksTable.columns" :pagination.sync="withdrawLinksTable.pagination" + @request="getWithdrawLinks" > {% raw %}