diff options
Diffstat (limited to 'packages')
-rw-r--r-- | packages/demobank-ui/src/Routing.tsx | 5 | ||||
-rw-r--r-- | packages/demobank-ui/src/pages/DownloadStats.tsx | 122 | ||||
-rw-r--r-- | packages/demobank-ui/src/pages/admin/AdminHome.tsx | 7 |
3 files changed, 133 insertions, 1 deletions
diff --git a/packages/demobank-ui/src/Routing.tsx b/packages/demobank-ui/src/Routing.tsx index f8a625621..711b7f871 100644 --- a/packages/demobank-ui/src/Routing.tsx +++ b/packages/demobank-ui/src/Routing.tsx @@ -38,6 +38,7 @@ import { WireTransfer } from "./pages/WireTransfer.js"; import { AccountPage } from "./pages/AccountPage/index.js"; import { useSettingsContext } from "./context/settings.js"; import { useBankCoreApiContext } from "./context/config.js"; +import { DownloadStats } from "./pages/DownloadStats.js"; export function Routing(): VNode { const history = createHashHistory(); @@ -119,6 +120,10 @@ export function Routing(): VNode { path="/public-accounts" component={() => <PublicHistoriesPage />} /> + <Route + path="/download-stats" + component={() => <DownloadStats />} + /> <Route path="/new-account" diff --git a/packages/demobank-ui/src/pages/DownloadStats.tsx b/packages/demobank-ui/src/pages/DownloadStats.tsx new file mode 100644 index 000000000..cd3e6d875 --- /dev/null +++ b/packages/demobank-ui/src/pages/DownloadStats.tsx @@ -0,0 +1,122 @@ +/* + This file is part of GNU Taler + (C) 2022 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +import { AccessToken, Logger, RefreshReason, TalerCoreBankHttpClient, TalerCorebankApi, TalerError } from "@gnu-taler/taler-util"; +import { useTranslationContext } from "@gnu-taler/web-util/browser"; +import { Fragment, VNode, h } from "preact"; +import { useState } from "preact/hooks"; +import { Loading } from "@gnu-taler/web-util/browser"; +import { Transactions } from "../components/Transactions/index.js"; +import { usePublicAccounts } from "../hooks/access.js"; +import { useBackendState } from "../hooks/backend.js"; +import { useBankCoreApiContext } from "../context/config.js"; +import { getTimeframesForDate } from "./admin/AdminHome.js"; + +const logger = new Logger("PublicHistoriesPage"); + +interface Props { } + +/** + * Show histories of public accounts. + */ +export function DownloadStats({ }: Props): VNode { + const { i18n } = useTranslationContext(); + + const { state: credentials } = useBackendState(); + const creds = credentials.status !== "loggedIn" || !credentials.isUserAdministrator ? undefined : credentials + const { api, config } = useBankCoreApiContext(); + + const [state, setState] = useState<number>() + + if (!creds) { + return <div>only admin can download stats</div> + } + + return ( + <Fragment> + <h1 class="nav">{i18n.str`Stats`}</h1> + <button type="button" + class="inline-flex items-center disabled:opacity-50 disabled:cursor-default cursor-pointer rounded-md bg-indigo-600 px-3 py-2 text-sm font-semibold text-white shadow-sm hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600" + onClick={() => { + fetchAllStatus(api, creds.token, new Date(), (p, total) => { + console.log("doing...", total - p) + setState(total - p) + }) + }} + > + start + </button> + progress {state} + </Fragment> + ); +} + + +async function fetchAllStatus(api: TalerCoreBankHttpClient, token: AccessToken, reference: Date, progres: (current: number, total: number) => void) { + const allMetrics = [ + TalerCorebankApi.MonitorTimeframeParam.day, + TalerCorebankApi.MonitorTimeframeParam.hour, + // TalerCorebankApi.MonitorTimeframeParam.month, + // TalerCorebankApi.MonitorTimeframeParam.year, + // TalerCorebankApi.MonitorTimeframeParam.decade, + ] + const allFrames = allMetrics.map(timeframe => ({ + timeframe, moment: getTimeframesForDate(reference, timeframe) + })) + + type Data = { + previous: TalerCorebankApi.MonitorResponse; + current: TalerCorebankApi.MonitorResponse; + } + const total = allFrames.length + + const dataResolvers = allFrames.map((frame, index) => async function getData(): Promise<Data | undefined> { + const previous = await api.getMonitor(token, { + timeframe: frame.timeframe, + which: frame.moment.previous + }) + await delay() + if (previous.type !== "ok") return undefined; + const current = await api.getMonitor(token, { + timeframe: frame.timeframe, + which: frame.moment.current + }) + await delay() + if (current.type !== "ok") return undefined; + return { previous: previous.body, current: current.body } + }); + + const csv = await dataResolvers.reduce(async (prev, resolver, index) => { + const accumulatedMap = await prev + console.log(index) + progres(index, total) + const data = await resolver() + if (!data) return accumulatedMap + + const metricName = TalerCorebankApi.MonitorTimeframeParam[allMetrics[index]] + accumulatedMap[metricName] = data + return accumulatedMap + }, Promise.resolve({} as Record<string, Data>)) + progres(total, total) + console.log(csv) +} +async function delay() { + return new Promise((res, rej) => { + setTimeout(() => { + res(null); + }, 1000) + }) +}
\ No newline at end of file diff --git a/packages/demobank-ui/src/pages/admin/AdminHome.tsx b/packages/demobank-ui/src/pages/admin/AdminHome.tsx index 18e88a409..9c6e6cde6 100644 --- a/packages/demobank-ui/src/pages/admin/AdminHome.tsx +++ b/packages/demobank-ui/src/pages/admin/AdminHome.tsx @@ -54,7 +54,7 @@ function getDateForTimeframe(which: number, timeframe: TalerCorebankApi.MonitorT assertUnreachable(timeframe) } -function getTimeframesForDate(time: Date, timeframe: TalerCorebankApi.MonitorTimeframeParam): { current: number, previous: number } { +export function getTimeframesForDate(time: Date, timeframe: TalerCorebankApi.MonitorTimeframeParam): { current: number, previous: number } { switch (timeframe) { case TalerCorebankApi.MonitorTimeframeParam.hour: return { current: getHours(sub(time, { hours: 1 })), @@ -183,6 +183,11 @@ function Metrics(): VNode { /> </div> </dl> + <div class="flex justify-end mt-2"> + <a href="#/download-stats" class="link"><i18n.Translate> + download stats as csv + </i18n.Translate></a> + </div> </Fragment> } |