-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SLO: Creation Form: Add Query Builder component (#149880)
Resolves #149867
- Loading branch information
1 parent
7693b2f
commit a706a11
Showing
8 changed files
with
262 additions
and
81 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
x-pack/plugins/observability/public/hooks/use_create_data_view.ts
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,42 @@ | ||
/* | ||
* 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 { useEffect, useState } from 'react'; | ||
import { DataView } from '@kbn/data-views-plugin/common'; | ||
import { useKibana } from '../utils/kibana_react'; | ||
|
||
interface UseCreateDataViewProps { | ||
indexPatternString: string | undefined; | ||
} | ||
|
||
export function useCreateDataView({ indexPatternString }: UseCreateDataViewProps) { | ||
const { dataViews } = useKibana().services; | ||
|
||
const [stateDataView, setStateDataView] = useState<DataView | undefined>(); | ||
const [isLoading, setIsLoading] = useState<boolean>(false); | ||
|
||
useEffect(() => { | ||
const createDataView = () => | ||
dataViews.create({ | ||
title: indexPatternString, | ||
allowNoIndex: true, | ||
}); | ||
|
||
if (indexPatternString) { | ||
setIsLoading(true); | ||
createDataView() | ||
.then((value) => { | ||
setStateDataView(value); | ||
}) | ||
.finally(() => { | ||
setIsLoading(false); | ||
}); | ||
} | ||
}, [indexPatternString, dataViews]); | ||
|
||
return { dataView: stateDataView, loading: isLoading }; | ||
} |
39 changes: 39 additions & 0 deletions
39
...ugins/observability/public/pages/slo_edit/components/custom_kql/query_builder.stories.tsx
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,39 @@ | ||
/* | ||
* 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 React from 'react'; | ||
import { ComponentStory } from '@storybook/react'; | ||
|
||
import { FormProvider, useForm } from 'react-hook-form'; | ||
import { KibanaReactStorybookDecorator } from '../../../../utils/kibana_react.storybook_decorator'; | ||
import { QueryBuilder as Component, Props } from './query_builder'; | ||
import { SLO_EDIT_FORM_DEFAULT_VALUES } from '../../constants'; | ||
|
||
export default { | ||
component: Component, | ||
title: 'app/SLO/EditPage/CustomKQL/QueryBuilder', | ||
decorators: [KibanaReactStorybookDecorator], | ||
}; | ||
|
||
const Template: ComponentStory<typeof Component> = (props: Props) => { | ||
const methods = useForm({ defaultValues: SLO_EDIT_FORM_DEFAULT_VALUES }); | ||
return ( | ||
<FormProvider {...methods}> | ||
<Component {...props} control={methods.control} /> | ||
</FormProvider> | ||
); | ||
}; | ||
|
||
const defaultProps = { | ||
dataTestSubj: 'dataTestSubj', | ||
indexPatternString: 'log*', | ||
name: 'name' as const, | ||
placeholder: 'Enter something if you dare', | ||
}; | ||
|
||
export const QueryBuilder = Template.bind({}); | ||
QueryBuilder.args = defaultProps; |
74 changes: 74 additions & 0 deletions
74
x-pack/plugins/observability/public/pages/slo_edit/components/custom_kql/query_builder.tsx
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,74 @@ | ||
/* | ||
* 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 React from 'react'; | ||
import { Control, Controller, FieldPath } from 'react-hook-form'; | ||
import { EuiFormRow } from '@elastic/eui'; | ||
import { CreateSLOInput } from '@kbn/slo-schema'; | ||
import { QueryStringInput } from '@kbn/unified-search-plugin/public'; | ||
import { useKibana } from '../../../../utils/kibana_react'; | ||
import { useCreateDataView } from '../../../../hooks/use_create_data_view'; | ||
|
||
export interface Props { | ||
control: Control<CreateSLOInput>; | ||
dataTestSubj: string; | ||
indexPatternString: string | undefined; | ||
label: string; | ||
name: FieldPath<CreateSLOInput>; | ||
placeholder: string; | ||
} | ||
|
||
export function QueryBuilder({ | ||
control, | ||
dataTestSubj, | ||
indexPatternString, | ||
label, | ||
name, | ||
placeholder, | ||
}: Props) { | ||
const { data, dataViews, docLinks, http, notifications, storage, uiSettings, unifiedSearch } = | ||
useKibana().services; | ||
|
||
const { dataView } = useCreateDataView({ indexPatternString }); | ||
|
||
return ( | ||
<EuiFormRow label={label} fullWidth> | ||
<Controller | ||
name={name} | ||
control={control} | ||
render={({ field }) => ( | ||
<QueryStringInput | ||
appName="Observability" | ||
bubbleSubmitEvent={false} | ||
dataTestSubj={dataTestSubj} | ||
deps={{ | ||
data, | ||
dataViews, | ||
docLinks, | ||
http, | ||
notifications, | ||
storage, | ||
uiSettings, | ||
unifiedSearch, | ||
}} | ||
disableAutoFocus | ||
disableLanguageSwitcher | ||
indexPatterns={dataView ? [dataView] : []} | ||
isDisabled={!indexPatternString} | ||
languageSwitcherPopoverAnchorPosition="rightDown" | ||
placeholder={placeholder} | ||
query={{ query: String(field.value), language: 'kuery' }} | ||
size="s" | ||
onChange={(value) => { | ||
field.onChange(value.query); | ||
}} | ||
/> | ||
)} | ||
/> | ||
</EuiFormRow> | ||
); | ||
} |
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
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.