From c3a623bd752f07c1600eaf4397635a0f10810b2e Mon Sep 17 00:00:00 2001 From: Fredrik Hatletvedt <32248439+Pespiri@users.noreply.github.com> Date: Fri, 22 Dec 2023 13:15:51 +0100 Subject: [PATCH] Simplify log api log download (#906) * simplify error handling when saving * simplify logapi log download * allow empty logs --- src/components/code/log-helper.ts | 18 ++--- .../environment-variables/index.tsx | 16 ++--- .../component-replica-log-accordion.tsx | 56 ++++------------ .../replica-log-accordion.tsx | 65 +++++++------------ 4 files changed, 47 insertions(+), 108 deletions(-) diff --git a/src/components/code/log-helper.ts b/src/components/code/log-helper.ts index 9377f6660..e414199ef 100644 --- a/src/components/code/log-helper.ts +++ b/src/components/code/log-helper.ts @@ -1,22 +1,22 @@ import { BaseQueryFn, QueryDefinition } from '@reduxjs/toolkit/query'; -import { UseLazyQuery } from '@reduxjs/toolkit/dist/query/react/buildHooks'; +import { LazyQueryTrigger } from '@reduxjs/toolkit/dist/query/react/buildHooks'; import { errorToast } from '../global-top-nav/styled-toaster'; import { getFetchErrorMessage } from '../../store/utils'; import { copyToTextFile } from '../../utils/string'; +export type LazyQueryTriggerPlain> = + LazyQueryTrigger>; + export function downloadLazyLogCb< - T extends ReturnType< - UseLazyQuery> - >[0], + T extends LazyQueryTriggerPlain>, >(filename: string, func: T, ...args: Parameters): () => Promise { return async () => { - const { data, error, isError, isSuccess } = await func( - args?.[0], - args?.[1] - ); + const { data, error, isError, isSuccess } = await ( + func as unknown as (...args: Parameters) => ReturnType + )(...((args || []) as Parameters)); - if (isSuccess && data) { + if (isSuccess) { copyToTextFile(filename, data); } else if (isError) { const message = getFetchErrorMessage(error); diff --git a/src/components/environment-variables/index.tsx b/src/components/environment-variables/index.tsx index 4d1b16109..07662e570 100644 --- a/src/components/environment-variables/index.tsx +++ b/src/components/environment-variables/index.tsx @@ -113,19 +113,11 @@ export const EnvironmentVariables: FunctionComponent<{ .filter((x) => x.value !== x.original.value) .map((x) => ({ name: x.original.name, value: x.value })); if (body.length > 0) { - const response = await saveFunc({ - appName, - envName, - componentName, - body, - }); - - if ('error' in response) { - errorToast( - `Failed to save variables. ${getFetchErrorMessage(response.error)}` - ); - } else { + try { + await saveFunc({ appName, envName, componentName, body }).unwrap(); refetchEnvVars(); + } catch (error) { + errorToast(`Failed to save variables. ${getFetchErrorMessage(error)}`); } } setInEditMode(false); diff --git a/src/components/page-active-component/component-replica-log-accordion.tsx b/src/components/page-active-component/component-replica-log-accordion.tsx index 14e497a7a..848057035 100644 --- a/src/components/page-active-component/component-replica-log-accordion.tsx +++ b/src/components/page-active-component/component-replica-log-accordion.tsx @@ -18,7 +18,7 @@ import { } from 'react'; import AsyncResource from '../async-resource/another-async-resource'; -import { errorToast } from '../global-top-nav/styled-toaster'; +import { LazyQueryTriggerPlain, downloadLazyLogCb } from '../code/log-helper'; import { Duration } from '../time/duration'; import { RelativeToNow } from '../time/relative-to-now'; import { @@ -27,18 +27,12 @@ import { logApi, useGetComponentInventoryQuery, } from '../../store/log-api'; -import { FetchQueryResult } from '../../store/types'; -import { getFetchErrorData } from '../../store/utils'; import { dataSorter, sortCompareDate, sortDirection, } from '../../utils/sort-utils'; -import { - copyToTextFile, - smallGithubCommitHash, - smallReplicaName, -} from '../../utils/string'; +import { smallGithubCommitHash, smallReplicaName } from '../../utils/string'; import { TableSortIcon, getNewSortDir } from '../../utils/table-sort-utils'; interface ComponentNameProps { @@ -66,19 +60,6 @@ const LogDownloadButton: FunctionComponent<{ ); -function saveLog( - query: Pick, - filename: string, - errMsg = 'Failed to download log' -): void { - if (query.isSuccess) { - copyToTextFile(filename, query.data as string); - } else if (query.isError) { - const { code, message } = getFetchErrorData(query.error); - errorToast(`${errMsg}: ${code && `[${code}] `}${message}`); - } -} - export const ComponentReplicaLogAccordion: FunctionComponent< ComponentReplicaLogAccordionProps > = ({ appName, envName, componentName, title, isExpanded }) => { @@ -228,17 +209,11 @@ const ReplicaLogTableRow: FunctionComponent< { - const response = await getLog({ - appName, - envName, - componentName, - replicaName: name, - }); - - const filename = `${appName}_${envName}_${componentName}_${name}.txt`; - saveLog(response, filename); - }} + onClick={downloadLazyLogCb( + `${appName}_${envName}_${componentName}_${name}.txt`, + getLog as LazyQueryTriggerPlain[0]>, + { appName, envName, componentName, replicaName: name } + )} disabled={isFetching} /> @@ -290,18 +265,11 @@ const ReplicaContainerTableRow: FunctionComponent< { - const response = await getLog({ - appName, - envName, - componentName, - replicaName, - containerId: id, - }); - - const filename = `${appName}_${envName}_${componentName}_${replicaName}_${id}.txt`; - saveLog(response, filename); - }} + onClick={downloadLazyLogCb( + `${appName}_${envName}_${componentName}_${replicaName}_${id}.txt`, + getLog as LazyQueryTriggerPlain[0]>, + { appName, envName, componentName, replicaName, containerId: id } + )} disabled={isFetching} /> diff --git a/src/components/page-scheduled-job/replica-log-accordion.tsx b/src/components/page-scheduled-job/replica-log-accordion.tsx index db670d60a..c113269ae 100644 --- a/src/components/page-scheduled-job/replica-log-accordion.tsx +++ b/src/components/page-scheduled-job/replica-log-accordion.tsx @@ -18,7 +18,7 @@ import { } from 'react'; import AsyncResource from '../async-resource/another-async-resource'; -import { errorToast } from '../global-top-nav/styled-toaster'; +import { LazyQueryTriggerPlain, downloadLazyLogCb } from '../code/log-helper'; import { Duration } from '../time/duration'; import { RelativeToNow } from '../time/relative-to-now'; import { RawModel } from '../../models/model-types'; @@ -28,18 +28,12 @@ import { logApi, useGetJobInventoryQuery, } from '../../store/log-api'; -import { FetchQueryResult } from '../../store/types'; -import { getFetchErrorData } from '../../store/utils'; import { dataSorter, sortCompareDate, sortDirection, } from '../../utils/sort-utils'; -import { - copyToTextFile, - smallGithubCommitHash, - smallReplicaName, -} from '../../utils/string'; +import { smallGithubCommitHash, smallReplicaName } from '../../utils/string'; import { TableSortIcon, getNewSortDir } from '../../utils/table-sort-utils'; interface JobNameProps { @@ -80,19 +74,6 @@ function getTimespan( }; } -function saveLog( - query: Pick, - filename: string, - errMsg = 'Failed to download log' -): void { - if (query.isSuccess) { - copyToTextFile(filename, query.data as string); - } else if (query.isError) { - const { code, message } = getFetchErrorData(query.error); - errorToast(`${errMsg}: ${code && `[${code}] `}${message}`); - } -} - export const JobReplicaLogAccordion: FunctionComponent< JobReplicaLogAccordionProps > = ({ @@ -256,20 +237,13 @@ const ReplicaLogTableRow: FunctionComponent< { - const response = await getLog({ - appName, - envName, - jobComponentName, - jobName, - replicaName: name, - }); - - const filename = `${appName}_${envName}_${jobComponentName}_${jobName}_${smallReplicaName( + onClick={downloadLazyLogCb( + `${appName}_${envName}_${jobComponentName}_${jobName}_${smallReplicaName( name - )}.txt`; - saveLog(response, filename); - }} + )}.txt`, + getLog as LazyQueryTriggerPlain[0]>, + { appName, envName, jobComponentName, jobName, replicaName: name } + )} disabled={isFetching} /> @@ -322,21 +296,20 @@ const ReplicaContainerTableRow: FunctionComponent< { - const response = await getLog({ + onClick={downloadLazyLogCb( + `${appName}_${envName}_${jobComponentName}_${jobName}_${smallReplicaName( + replicaName + )}_${id}.txt`, + getLog as LazyQueryTriggerPlain[0]>, + { appName, envName, jobComponentName, jobName, replicaName, containerId: id, - }); - - const filename = `${appName}_${envName}_${jobComponentName}_${jobName}_${smallReplicaName( - replicaName - )}_${id}.txt`; - saveLog(response, filename); - }} + } + )} disabled={isFetching} /> @@ -351,4 +324,10 @@ JobReplicaLogAccordion.propTypes = { jobComponentName: PropTypes.string.isRequired, jobName: PropTypes.string.isRequired, isExpanded: PropTypes.bool, + timeSpan: PropTypes.shape< + PropTypes.ValidationMap + >({ + start: PropTypes.instanceOf(Date).isRequired, + end: PropTypes.instanceOf(Date), + }) as PropTypes.Validator, };