Skip to content

Commit

Permalink
feat(rca): search and filter investigations (#192920)
Browse files Browse the repository at this point in the history
  • Loading branch information
kdelemme authored Sep 16, 2024
1 parent 0c63a7b commit 7a7cd7b
Show file tree
Hide file tree
Showing 24 changed files with 568 additions and 64 deletions.
6 changes: 4 additions & 2 deletions packages/kbn-investigation-shared/src/rest_specs/find.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ const findInvestigationsParamsSchema = z
query: z
.object({
alertId: z.string(),
page: z.string(),
perPage: z.string(),
search: z.string(),
filter: z.string(),
page: z.coerce.number(),
perPage: z.coerce.number(),
})
.partial(),
})
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import { z } from '@kbn/zod';

const getAllInvestigationTagsParamsSchema = z.object({
query: z.object({}),
});

const getAllInvestigationTagsResponseSchema = z.string().array();

type GetAllInvestigationTagsResponse = z.output<typeof getAllInvestigationTagsResponseSchema>;

export { getAllInvestigationTagsParamsSchema, getAllInvestigationTagsResponseSchema };
export type { GetAllInvestigationTagsResponse };
2 changes: 2 additions & 0 deletions packages/kbn-investigation-shared/src/rest_specs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export type * from './find';
export type * from './get';
export type * from './get_items';
export type * from './get_notes';
export type * from './get_all_investigation_tags';
export type * from './investigation';
export type * from './investigation_item';
export type * from './investigation_note';
Expand All @@ -34,6 +35,7 @@ export * from './find';
export * from './get';
export * from './get_items';
export * from './get_notes';
export * from './get_all_investigation_tags';
export * from './investigation';
export * from './investigation_item';
export * from './investigation_note';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const investigationSchema = z.object({
title: z.string(),
createdAt: z.number(),
createdBy: z.string(),
updatedAt: z.number(),
params: z.object({
timeRange: z.object({ from: z.number(), to: z.number() }),
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const investigationItemSchema = z.intersection(
id: z.string(),
createdAt: z.number(),
createdBy: z.string(),
updatedAt: z.number(),
}),
itemSchema
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const investigationNoteSchema = z.object({
id: z.string(),
content: z.string(),
createdAt: z.number(),
updatedAt: z.number(),
createdBy: z.string(),
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { i18n } from '@kbn/i18n';
import React from 'react';
import { Controller, useFormContext } from 'react-hook-form';
import { InvestigationForm } from '../investigation_edit_form';
import { useFetchAllInvestigationTags } from '../../../hooks/use_fetch_all_investigation_tags';

const I18N_TAGS_LABEL = i18n.translate(
'xpack.investigateApp.investigationEditForm.span.tagsLabel',
Expand All @@ -18,6 +19,7 @@ const I18N_TAGS_LABEL = i18n.translate(

export function TagsField() {
const { control, getFieldState } = useFormContext<InvestigationForm>();
const { isLoading, data: tags } = useFetchAllInvestigationTags();

return (
<EuiFormRow label={I18N_TAGS_LABEL} fullWidth isInvalid={getFieldState('tags').invalid}>
Expand All @@ -32,10 +34,10 @@ export function TagsField() {
aria-label={I18N_TAGS_LABEL}
placeholder={I18N_TAGS_LABEL}
fullWidth
noSuggestions
isInvalid={fieldState.invalid}
isClearable
options={[]}
isLoading={isLoading}
options={tags?.map((tag) => ({ label: tag, value: tag })) ?? []}
selectedOptions={generateTagOptions(field.value)}
onChange={(selected) => {
if (selected.length) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@

export const investigationKeys = {
all: ['investigations'] as const,
tags: () => [...investigationKeys.all, 'tags'] as const,
lists: () => [...investigationKeys.all, 'list'] as const,
list: (params: { page: number; perPage: number }) =>
list: (params: { page: number; perPage: number; search?: string; filter?: string }) =>
[...investigationKeys.lists(), params] as const,
details: () => [...investigationKeys.all, 'detail'] as const,
detail: (investigationId: string) => [...investigationKeys.details(), investigationId] as const,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { i18n } from '@kbn/i18n';
import { useQuery } from '@tanstack/react-query';
import { investigationKeys } from './query_key_factory';
import { useKibana } from './use_kibana';

export interface Response {
isInitialLoading: boolean;
isLoading: boolean;
isRefetching: boolean;
isSuccess: boolean;
isError: boolean;
data: string[] | undefined;
}

export function useFetchAllInvestigationTags(): Response {
const {
core: {
http,
notifications: { toasts },
},
} = useKibana();

const { isInitialLoading, isLoading, isError, isSuccess, isRefetching, data } = useQuery({
queryKey: investigationKeys.tags(),
queryFn: async ({ signal }) => {
return await http.get<string[]>(`/api/observability/investigations/_tags`, {
version: '2023-10-31',
signal,
});
},
cacheTime: 600 * 1000, // 10_minutes
staleTime: 0,
refetchOnWindowFocus: false,
retry: false,
onError: (error: Error) => {
toasts.addError(error, {
title: i18n.translate('xpack.investigateApp.useFetchAllInvestigationTags.errorTitle', {
defaultMessage: 'Something went wrong while fetching the investigation tags',
}),
});
},
});

return {
data,
isInitialLoading,
isLoading,
isRefetching,
isSuccess,
isError,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ const DEFAULT_PAGE_SIZE = 25;
export interface InvestigationListParams {
page?: number;
perPage?: number;
search?: string;
filter?: string;
}

export interface UseFetchInvestigationListResponse {
Expand All @@ -30,6 +32,8 @@ export interface UseFetchInvestigationListResponse {
export function useFetchInvestigationList({
page = 1,
perPage = DEFAULT_PAGE_SIZE,
search,
filter,
}: InvestigationListParams = {}): UseFetchInvestigationListResponse {
const {
core: {
Expand All @@ -42,19 +46,26 @@ export function useFetchInvestigationList({
queryKey: investigationKeys.list({
page,
perPage,
search,
filter,
}),
queryFn: async ({ signal }) => {
return await http.get<FindInvestigationsResponse>(`/api/observability/investigations`, {
version: '2023-10-31',
query: {
...(page !== undefined && { page }),
...(perPage !== undefined && { perPage }),
...(!!search && { search }),
...(!!filter && { filter }),
},
signal,
});
},
retry: false,
refetchInterval: 60 * 1000,
refetchOnWindowFocus: false,
cacheTime: 600 * 1000, // 10 minutes
staleTime: 0,
onError: (error: Error) => {
toasts.addError(error, {
title: i18n.translate('xpack.investigateApp.useFetchInvestigationList.errorTitle', {
Expand Down
Loading

0 comments on commit 7a7cd7b

Please sign in to comment.