-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
105 additions
and
89 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
71 changes: 0 additions & 71 deletions
71
...ic/detection_engine/rule_creation_ui/components/esql_fields_select/esql_fields_select.tsx
This file was deleted.
Oops, something went wrong.
8 changes: 0 additions & 8 deletions
8
..._solution/public/detection_engine/rule_creation_ui/components/esql_fields_select/index.ts
This file was deleted.
Oops, something went wrong.
6 changes: 0 additions & 6 deletions
6
...on/public/detection_engine/rule_creation_ui/components/esql_fields_select/translations.ts
This file was deleted.
Oops, something went wrong.
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
93 changes: 93 additions & 0 deletions
93
...urity_solution/public/detection_engine/rule_creation_ui/hooks/use_investigation_fields.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,93 @@ | ||
/* | ||
* 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 { useMemo } from 'react'; | ||
import type { Datatable, ExpressionsStart } from '@kbn/expressions-plugin/public'; | ||
import type { DataViewFieldBase } from '@kbn/es-query'; | ||
import { computeIsESQLQueryAggregating } from '@kbn/securitysolution-utils'; | ||
|
||
import { useQuery } from '@tanstack/react-query'; | ||
|
||
import { useKibana } from '@kbn/kibana-react-plugin/public'; | ||
|
||
import { getEsqlQueryConfig } from '../../rule_creation/logic/get_esql_query_config'; | ||
|
||
const esqlToFields = ( | ||
data: { error: unknown } | Datatable | undefined | null | ||
): DataViewFieldBase[] => { | ||
if (data && 'error' in data) { | ||
return []; | ||
} | ||
|
||
const fields = (data?.columns ?? []).map(({ id, meta }) => { | ||
return { | ||
name: id, | ||
type: meta.type, | ||
}; | ||
}); | ||
|
||
return fields; | ||
}; | ||
|
||
type UseEsqlFields = (esqlQuery: string | undefined) => { | ||
isLoading: boolean; | ||
fields: DataViewFieldBase[]; | ||
}; | ||
|
||
/** | ||
* fetches ES|QL fields and convert them to DataViewBase fields | ||
*/ | ||
const useEsqlFields: UseEsqlFields = (esqlQuery) => { | ||
const kibana = useKibana<{ expressions: ExpressionsStart }>(); | ||
|
||
const { expressions } = kibana.services; | ||
|
||
const queryConfig = getEsqlQueryConfig({ esqlQuery, expressions }); | ||
const { data, isLoading } = useQuery(queryConfig); | ||
|
||
const fields = useMemo(() => { | ||
return esqlToFields(data); | ||
}, [data]); | ||
|
||
return { | ||
fields, | ||
isLoading, | ||
}; | ||
}; | ||
|
||
type UseInvestigationFields = (params: { | ||
isEsqlRule: boolean; | ||
esqlQuery: string | undefined; | ||
indexPatternsFields: DataViewFieldBase[]; | ||
}) => { | ||
isLoading: boolean; | ||
investigationFields: DataViewFieldBase[]; | ||
}; | ||
|
||
export const useInvestigationFields: UseInvestigationFields = ({ | ||
isEsqlRule, | ||
esqlQuery, | ||
indexPatternsFields, | ||
}) => { | ||
const { fields: esqlFields, isLoading } = useEsqlFields(esqlQuery); | ||
|
||
const investigationFields = useMemo(() => { | ||
if (!esqlQuery || !isEsqlRule) { | ||
return indexPatternsFields; | ||
} | ||
|
||
// alerts generated from non-aggregating queries are enriched with source document | ||
// so, index patterns fields should be included in the list of investigation fields | ||
const isEsqlQueryAggregating = computeIsESQLQueryAggregating(esqlQuery); | ||
|
||
return isEsqlQueryAggregating ? esqlFields : [...esqlFields, ...indexPatternsFields]; | ||
}, [esqlFields, esqlQuery, indexPatternsFields, isEsqlRule]); | ||
|
||
return { | ||
investigationFields, | ||
isLoading, | ||
}; | ||
}; |