-
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Correlation CRUD operations (#417)
- Loading branch information
1 parent
fd51eee
commit e94a62e
Showing
19 changed files
with
1,287 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
export type Correlation = { | ||
version: string; | ||
id: string; | ||
title: string; | ||
tableConfigs: Array<{ | ||
selectedFields: string[]; | ||
tableName: string; | ||
}>; | ||
joinConfig: { | ||
joinConditions: Array<{ | ||
tableName: string; | ||
field: string; | ||
}>; | ||
}; | ||
filter: null; | ||
startTime: string; | ||
endTime: string; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { Correlation } from '@/@types/parseable/api/correlation'; | ||
import { Axios } from './axios'; | ||
import { | ||
DELETE_SAVED_CORRELATION_URL, | ||
GET_SAVED_CORRELATION_URL, | ||
LIST_CORRELATIONS, | ||
UPDATE_CORRELATION_URL, | ||
} from './constants'; | ||
|
||
export const getCorrelations = () => { | ||
return Axios().get<Correlation[]>(LIST_CORRELATIONS); | ||
}; | ||
|
||
export const getCorrelationById = (correlationId: string) => { | ||
return Axios().get(GET_SAVED_CORRELATION_URL(correlationId)); | ||
}; | ||
|
||
export const deleteSavedCorrelation = (correlationId: string) => { | ||
return Axios().delete(DELETE_SAVED_CORRELATION_URL(correlationId)); | ||
}; | ||
|
||
export const saveCorrelation = (correlationData: Correlation) => { | ||
return Axios().post(LIST_CORRELATIONS, correlationData); | ||
}; | ||
|
||
export const updateCorrelation = (correlationData: Correlation) => { | ||
return Axios().put(UPDATE_CORRELATION_URL(correlationData.id), correlationData); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
import { useMutation, useQuery } from 'react-query'; | ||
import _ from 'lodash'; | ||
|
||
import { | ||
deleteSavedCorrelation, | ||
getCorrelationById, | ||
getCorrelations, | ||
saveCorrelation, | ||
updateCorrelation, | ||
} from '@/api/correlations'; | ||
import { correlationStoreReducers, useCorrelationStore } from '@/pages/Correlation/providers/CorrelationProvider'; | ||
import { notifyError, notifySuccess } from '@/utils/notification'; | ||
import { AxiosError, isAxiosError } from 'axios'; | ||
import { appStoreReducers, useAppStore } from '@/layouts/MainLayout/providers/AppProvider'; | ||
import dayjs from 'dayjs'; | ||
|
||
const { | ||
setCorrelations, | ||
setActiveCorrelation, | ||
setCorrelationId, | ||
setSavedCorrelationId, | ||
cleanCorrelationStore, | ||
toggleSavedCorrelationsModal, | ||
} = correlationStoreReducers; | ||
const { setTimeRange, syncTimeRange } = appStoreReducers; | ||
export const useCorrelationsQuery = () => { | ||
const [{ correlationId }, setCorrelatedStore] = useCorrelationStore((store) => store); | ||
const [, setAppStore] = useAppStore((store) => store); | ||
const { | ||
isError: fetchCorrelationsError, | ||
isSuccess: fetchCorrelationsSuccess, | ||
isLoading: fetchCorrelationsLoading, | ||
refetch: fetchCorrelations, | ||
} = useQuery(['correlations'], () => getCorrelations(), { | ||
retry: false, | ||
enabled: false, | ||
refetchOnWindowFocus: false, | ||
onSuccess: (data) => { | ||
setCorrelatedStore((store) => setCorrelations(store, data.data || [])); | ||
}, | ||
onError: () => { | ||
setCorrelatedStore((store) => setCorrelations(store, [])); | ||
notifyError({ message: 'Failed to fetch correlations' }); | ||
}, | ||
}); | ||
|
||
const { | ||
mutate: getCorrelationByIdMutation, | ||
isError: fetchCorrelationIdError, | ||
isSuccess: fetchCorrelationIdSuccess, | ||
isLoading: fetchCorrelationIdLoading, | ||
} = useMutation((correlationId: string) => getCorrelationById(correlationId), { | ||
onSuccess: (data: any) => { | ||
data.data.startTime && | ||
data.data.endTime && | ||
setAppStore((store) => | ||
setTimeRange(store, { | ||
startTime: dayjs(data.data.startTime), | ||
endTime: dayjs(data.data.endTime), | ||
type: 'custom', | ||
}), | ||
); | ||
setCorrelatedStore((store) => setCorrelationId(store, data.data.id)); | ||
setCorrelatedStore((store) => setActiveCorrelation(store, data.data)); | ||
}, | ||
onError: () => { | ||
notifyError({ message: 'Failed to fetch correlation' }); | ||
}, | ||
}); | ||
|
||
const { mutate: deleteSavedCorrelationMutation, isLoading: isDeleting } = useMutation( | ||
(data: { correlationId: string; onSuccess?: () => void }) => deleteSavedCorrelation(data.correlationId), | ||
{ | ||
onSuccess: (_data, variables) => { | ||
variables.onSuccess && variables.onSuccess(); | ||
if (variables.correlationId === correlationId) { | ||
setCorrelatedStore(cleanCorrelationStore); | ||
setAppStore(syncTimeRange); | ||
} | ||
fetchCorrelations(); | ||
setCorrelatedStore((store) => toggleSavedCorrelationsModal(store, false)); | ||
notifySuccess({ message: 'Deleted Successfully' }); | ||
}, | ||
onError: (data: AxiosError) => { | ||
if (isAxiosError(data) && data.response) { | ||
const error = data.response?.data as string; | ||
typeof error === 'string' && notifyError({ message: error }); | ||
} else if (data.message && typeof data.message === 'string') { | ||
notifyError({ message: data.message }); | ||
} | ||
}, | ||
}, | ||
); | ||
|
||
const { mutate: saveCorrelationMutation, isLoading: isCorrelationSaving } = useMutation( | ||
(data: { correlationData: any; onSuccess?: () => void }) => saveCorrelation(data.correlationData), | ||
{ | ||
onSuccess: (data, variables) => { | ||
variables.onSuccess && variables.onSuccess(); | ||
setCorrelatedStore((store) => setCorrelationId(store, data.data.id)); | ||
setCorrelatedStore((store) => setSavedCorrelationId(store, data.data.id)); | ||
fetchCorrelations(); | ||
notifySuccess({ message: 'Correlation saved successfully' }); | ||
}, | ||
onError: (data: AxiosError) => { | ||
if (isAxiosError(data) && data.response) { | ||
const error = data.response?.data as string; | ||
typeof error === 'string' && notifyError({ message: error }); | ||
} else if (data.message && typeof data.message === 'string') { | ||
notifyError({ message: data.message }); | ||
} | ||
}, | ||
}, | ||
); | ||
|
||
const { mutate: updateCorrelationMutation, isLoading: isCorrelationUpdating } = useMutation( | ||
(data: { correlationData: any; onSuccess?: () => void }) => updateCorrelation(data.correlationData), | ||
{ | ||
onSuccess: (data, variables) => { | ||
variables.onSuccess && variables.onSuccess(); | ||
setCorrelatedStore((store) => setCorrelationId(store, data.data.id)); | ||
setCorrelatedStore((store) => setActiveCorrelation(store, data.data)); | ||
fetchCorrelations(); | ||
notifySuccess({ message: 'Correlation updated successfully' }); | ||
}, | ||
onError: (data: AxiosError) => { | ||
if (isAxiosError(data) && data.response) { | ||
const error = data.response?.data as string; | ||
typeof error === 'string' && notifyError({ message: error }); | ||
} else if (data.message && typeof data.message === 'string') { | ||
notifyError({ message: data.message }); | ||
} | ||
}, | ||
}, | ||
); | ||
|
||
return { | ||
fetchCorrelationsError, | ||
fetchCorrelationsSuccess, | ||
fetchCorrelationsLoading, | ||
fetchCorrelations, | ||
|
||
deleteSavedCorrelationMutation, | ||
isDeleting, | ||
|
||
fetchCorrelationIdError, | ||
fetchCorrelationIdSuccess, | ||
fetchCorrelationIdLoading, | ||
getCorrelationByIdMutation, | ||
|
||
saveCorrelationMutation, | ||
isCorrelationSaving, | ||
|
||
updateCorrelationMutation, | ||
isCorrelationUpdating, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.