diff --git a/src/api/caching.ts b/src/api/caching.ts new file mode 100644 index 00000000..5e62f007 --- /dev/null +++ b/src/api/caching.ts @@ -0,0 +1,10 @@ +import { Axios } from './axios'; +import { CACHING_STATUS_URL } from './constants'; + +export const getCachingStatus = (streamName: string) => { + return Axios().get(CACHING_STATUS_URL(streamName)); +}; + +export const updateCaching = (streamName: string, type: boolean) => { + return Axios().put(CACHING_STATUS_URL(streamName), type, { headers: { 'Content-Type': 'application/json' } }); +}; diff --git a/src/api/constants.ts b/src/api/constants.ts index 2f283db6..2d4199ef 100644 --- a/src/api/constants.ts +++ b/src/api/constants.ts @@ -23,12 +23,12 @@ export const DEFAULT_ROLE_URL = `${API_V1}/role/default`; export const ROLES_LIST_URL = `${API_V1}/role`; export const ROLE_URL = (roleName: string) => `${ROLES_LIST_URL}/${roleName}`; - -//USERS LOGIN - +// USERS LOGIN export const LOGIN_URL = `${API_V1}/o/login?redirect=${window.location.origin}`; // LLM queries export const LLM_QUERY_URL = `${API_V1}/llm`; export const IS_LLM_ACTIVE_URL = `${LLM_QUERY_URL}/isactive`; +// caching +export const CACHING_STATUS_URL = (streamName: string) => `${LOG_STREAM_LIST_URL}/${streamName}/cache`; diff --git a/src/hooks/useGetCacheStatus.tsx b/src/hooks/useGetCacheStatus.tsx new file mode 100644 index 00000000..bdf1a108 --- /dev/null +++ b/src/hooks/useGetCacheStatus.tsx @@ -0,0 +1,37 @@ +import useMountedState from './useMountedState'; +import { StatusCodes } from 'http-status-codes'; +import { getCachingStatus } from '@/api/caching'; + +export const useGetCacheStatus = () => { + const [data, setData] = useMountedState(null); + const [error, setError] = useMountedState(null); + const [loading, setLoading] = useMountedState(false); + + const getCacheStatus = async (streamName: string) => { + setLoading(true); + try { + const res = await getCachingStatus(streamName); + + switch (res.status) { + case StatusCodes.OK: { + setData(res.data); + break; + } + default: { + setError('Failed to get cache status'); + console.error(res); + } + } + } catch (error) { + setError('Failed to get cache status'); + } finally { + setLoading(false); + } + }; + + const resetData = () => { + setData(null); + }; + + return { data, error, loading, getCacheStatus, resetData }; +}; diff --git a/src/hooks/usePutCache.tsx b/src/hooks/usePutCache.tsx new file mode 100644 index 00000000..ddc6a330 --- /dev/null +++ b/src/hooks/usePutCache.tsx @@ -0,0 +1,37 @@ +import useMountedState from './useMountedState'; +import { StatusCodes } from 'http-status-codes'; +import { updateCaching } from '@/api/caching'; + +export const usePutCache = () => { + const [data, setData] = useMountedState(null); + const [error, setError] = useMountedState(null); + const [loading, setLoading] = useMountedState(false); + + const updateCache = async (streamName: string, type: boolean) => { + setLoading(true); + try { + const res = await updateCaching(streamName, type); + + switch (res.status) { + case StatusCodes.OK: { + setData(res.data); + break; + } + default: { + setError('Failed to change cache setting'); + console.error(res); + } + } + } catch (error) { + setError('Failed to change cache setting'); + } finally { + setLoading(false); + } + }; + + const resetData = () => { + setData(null); + }; + + return { data, error, loading, updateCache, resetData }; +}; diff --git a/src/pages/Config/index.tsx b/src/pages/Config/index.tsx index 1e145cf4..72def4b8 100644 --- a/src/pages/Config/index.tsx +++ b/src/pages/Config/index.tsx @@ -11,11 +11,19 @@ import { IconCheck, IconFileAlert } from '@tabler/icons-react'; import { usePutLogStreamRetention } from '@/hooks/usePutLogStreamRetention'; import { useGetLogStreamAlert } from '@/hooks/useGetLogStreamAlert'; import { useGetLogStreamRetention } from '@/hooks/useGetLogStreamRetention'; +import { useGetCacheStatus } from '@/hooks/useGetCacheStatus'; +import { usePutCache } from '@/hooks/usePutCache'; const Config: FC = () => { useDocumentTitle('Parseable | Config'); + + const { data, getCacheStatus } = useGetCacheStatus(); + usePutCache(); + const { error: updateCacheError, updateCache } = usePutCache(); + const [alertQuery, setAlertQuery] = useMountedState(''); const [retentionQuery, setRetentionQuery] = useMountedState(''); + const { state: { subLogQuery }, } = useHeaderContext(); @@ -97,10 +105,11 @@ const Config: FC = () => { }; useEffect(() => { - if(!subLogQuery.get().streamName) return; + if (!subLogQuery.get().streamName) return; getLogAlert(subLogQuery.get().streamName); getLogRetention(subLogQuery.get().streamName); - }, []); + getCacheStatus(subLogQuery.get().streamName); + }, [subLogQuery.get().streamName]); useEffect(() => { const subQuery = subLogQuery.subscribe((value: any) => { @@ -211,54 +220,45 @@ const Config: FC = () => { }); resetRetentionData(); } - }, [resultRetentionData, retentionError, retentionLoading]); + if (updateCacheError) { + notifications.update({ + id: 'load-data', + loading: false, + color: 'red', + title: 'Error occurred', + message: `Error occurred, failed to change cache setting`, + icon: , + autoClose: 3000, + }); + } + }, [resultRetentionData, retentionError, retentionLoading, updateCacheError]); const { classes } = useConfigStyles(); - const { container, submitBtn, accordionSt, innerContainer } = classes; + const { container, submitBtn, accordionSt, innerContainer, containerWrapper, primaryBtn } = classes; + return ( - - - - Alert - - - - - - - - - - - - {!subLogQuery.get().access?.some((access: string) => ['PutRetention'].includes(access)) ? null : ( + + + - - - Retention + + + Alert { }} /> - @@ -280,7 +280,40 @@ const Config: FC = () => { - )} + {!subLogQuery.get().access?.some((access: string) => ['PutRetention'].includes(access)) ? null : ( + + + + Retention + + + + + + + + + + + + )} + ); }; diff --git a/src/pages/Config/styles.tsx b/src/pages/Config/styles.tsx index e143e097..50400afd 100644 --- a/src/pages/Config/styles.tsx +++ b/src/pages/Config/styles.tsx @@ -1,13 +1,26 @@ import { createStyles } from '@mantine/core'; export const useConfigStyles = createStyles((theme) => { - const { spacing ,colors} = theme; - const { widths, } = theme.other; + const { spacing, colors } = theme; + const { widths } = theme.other; return { container: { display: 'flex', - margin: spacing.md, + flexDirection: 'column', flex: 1, + margin: spacing.xl, + gap: spacing.lg, }, + containerWrapper: { + display: 'flex', + gap: '20px', + }, + primaryBtn: { + marginTop: spacing.md, + backgroundColor: theme.colors.brandPrimary[0], + color: theme.colors.white, + width: 'max-content', + }, + submitBtn: { marginTop: spacing.md, backgroundColor: theme.colors.brandPrimary[0], @@ -17,7 +30,7 @@ export const useConfigStyles = createStyles((theme) => { borderRadius: '60px', border: 'none', '& .mantine-Accordion-item': { - border:`${widths.px} ${colors.gray[1]} solid`, + border: `${widths.px} ${colors.gray[1]} solid`, }, }, innerContainer: { @@ -25,7 +38,5 @@ export const useConfigStyles = createStyles((theme) => { justifyContent: 'center', display: 'flex', }, - }; }); -