Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds signals to the fetches #6

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export const useAnomaliesTableData = ({

useEffect(() => {
let isSubscribed = true;
const abortCtrl = new AbortController();
setLoading(true);

async function fetchAnomaliesTableData(
Expand All @@ -98,7 +99,8 @@ export const useAnomaliesTableData = ({
},
{
'kbn-version': kbnVersion,
}
},
abortCtrl.signal
);
if (isSubscribed) {
setTableData(data);
Expand All @@ -123,6 +125,7 @@ export const useAnomaliesTableData = ({
fetchAnomaliesTableData(influencers, criteriaFields, startDate, endDate);
return () => {
isSubscribed = false;
abortCtrl.abort();
};
}, [
influencersOrCriteriaToString(influencers),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ export interface Body {

export const anomaliesTableData = async (
body: Body,
headers: Record<string, string | undefined>
headers: Record<string, string | undefined>,
signal: AbortSignal
): Promise<Anomalies> => {
const [kbnVersion] = useKibanaUiSetting(DEFAULT_KBN_VERSION);
const response = await fetch(`${chrome.getBasePath()}/api/ml/results/anomalies_table_data`, {
Expand All @@ -38,6 +39,7 @@ export const anomaliesTableData = async (
'kbn-xsrf': kbnVersion,
...headers,
},
signal,
});
await throwIfNotOk(response);
return await response.json();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ export interface Body {
}

export const getMlCapabilities = async (
headers: Record<string, string | undefined>
headers: Record<string, string | undefined>,
signal: AbortSignal
): Promise<MlCapabilities> => {
const [kbnVersion] = useKibanaUiSetting(DEFAULT_KBN_VERSION);
const response = await fetch(`${chrome.getBasePath()}/api/ml/ml_capabilities`, {
Expand All @@ -37,6 +38,7 @@ export const getMlCapabilities = async (
'kbn-xsrf': kbnVersion,
...headers,
},
signal,
});
await throwIfNotOk(response);
return await response.json();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,14 @@ export const MlCapabilitiesProvider = React.memo<{ children: JSX.Element }>(({ c

useEffect(() => {
let isSubscribed = true;
const abortCtrl = new AbortController();

async function fetchMlCapabilities() {
try {
const mlCapabilities = await getMlCapabilities({ 'kbn-version': kbnVersion });
const mlCapabilities = await getMlCapabilities(
{ 'kbn-version': kbnVersion },
abortCtrl.signal
);
if (isSubscribed) {
setCapabilities(mlCapabilities);
}
Expand All @@ -48,6 +52,7 @@ export const MlCapabilitiesProvider = React.memo<{ children: JSX.Element }>(({ c
fetchMlCapabilities();
return () => {
isSubscribed = false;
abortCtrl.abort();
};
}, []);

Expand Down
14 changes: 11 additions & 3 deletions x-pack/legacy/plugins/siem/public/components/ml_popover/api.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ const emptyIndexPattern: IndexPatternSavedObject[] = [];
*
* @param headers
*/
export const groupsData = async (headers: Record<string, string | undefined>): Promise<Group[]> => {
export const groupsData = async (
headers: Record<string, string | undefined>,
signal: AbortSignal
): Promise<Group[]> => {
const [kbnVersion] = useKibanaUiSetting(DEFAULT_KBN_VERSION);
const response = await fetch(`${chrome.getBasePath()}/api/ml/jobs/groups`, {
method: 'GET',
Expand All @@ -42,6 +45,7 @@ export const groupsData = async (headers: Record<string, string | undefined>): P
'kbn-xsrf': kbnVersion,
...headers,
},
signal,
});
await throwIfNotOk(response);
return await response.json();
Expand Down Expand Up @@ -179,7 +183,8 @@ export const stopDatafeeds = async (
*/
export const jobsSummary = async (
jobIds: string[],
headers: Record<string, string | undefined>
headers: Record<string, string | undefined>,
signal: AbortSignal
): Promise<Job[]> => {
const [kbnVersion] = useKibanaUiSetting(DEFAULT_KBN_VERSION);
const response = await fetch(`${chrome.getBasePath()}/api/ml/jobs/jobs_summary`, {
Expand All @@ -192,6 +197,7 @@ export const jobsSummary = async (
'kbn-system-api': 'true',
...headers,
},
signal,
});
await throwIfNotOk(response);
return await response.json();
Expand All @@ -203,7 +209,8 @@ export const jobsSummary = async (
* @param headers
*/
export const getIndexPatterns = async (
headers: Record<string, string | undefined>
headers: Record<string, string | undefined>,
signal: AbortSignal
): Promise<IndexPatternSavedObject[]> => {
const [kbnVersion] = useKibanaUiSetting(DEFAULT_KBN_VERSION);
const response = await fetch(
Expand All @@ -217,6 +224,7 @@ export const getIndexPatterns = async (
'kbn-system-api': 'true',
...headers,
},
signal,
}
);
await throwIfNotOk(response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,17 @@ export const useIndexPatterns = (refreshToggle = false): Return => {

useEffect(() => {
let isSubscribed = true;
const abortCtrl = new AbortController();
setIsLoading(true);

async function fetchIndexPatterns() {
try {
const data = await getIndexPatterns({
'kbn-version': kbnVersion,
});
const data = await getIndexPatterns(
{
'kbn-version': kbnVersion,
},
abortCtrl.signal
);

if (isSubscribed) {
setIndexPatterns(data);
Expand All @@ -50,6 +54,7 @@ export const useIndexPatterns = (refreshToggle = false): Return => {
fetchIndexPatterns();
return () => {
isSubscribed = false;
abortCtrl.abort();
};
}, [refreshToggle]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,19 @@ export const useJobSummaryData = (jobIds: string[] = [], refreshToggle = false):

useEffect(() => {
let isSubscribed = true;
const abortCtrl = new AbortController();
setLoading(true);

async function fetchSiemJobsFromJobsSummary() {
if (userPermissions) {
try {
const data: Job[] = await jobsSummary(jobIds, {
'kbn-version': kbnVersion,
});
const data: Job[] = await jobsSummary(
jobIds,
{
'kbn-version': kbnVersion,
},
abortCtrl.signal
);

// TODO: API returns all jobs even though we specified jobIds -- jobsSummary call seems to match request in ML App?
const siemJobs = getSiemJobsFromJobsSummary(data);
Expand All @@ -62,6 +67,7 @@ export const useJobSummaryData = (jobIds: string[] = [], refreshToggle = false):
fetchSiemJobsFromJobsSummary();
return () => {
isSubscribed = false;
abortCtrl.abort();
};
}, [refreshToggle, userPermissions]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,18 @@ export const useSiemJobs = (refetchData: boolean): Return => {

useEffect(() => {
let isSubscribed = true;
const abortCtrl = new AbortController();
setLoading(true);

async function fetchSiemJobIdsFromGroupsData() {
if (userPermissions) {
try {
const data = await groupsData({
'kbn-version': kbnVersion,
});
const data = await groupsData(
{
'kbn-version': kbnVersion,
},
abortCtrl.signal
);

const siemJobIds = getSiemJobIdsFromGroupsData(data);
if (isSubscribed) {
Expand All @@ -61,6 +65,7 @@ export const useSiemJobs = (refetchData: boolean): Return => {
fetchSiemJobIdsFromGroupsData();
return () => {
isSubscribed = false;
abortCtrl.abort();
};
}, [refetchData, userPermissions]);

Expand Down