-
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
39 changed files
with
548 additions
and
473 deletions.
There are no files selected for viewing
Validating CODEOWNERS rules …
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# @kbn/saved-search-component | ||
|
||
A component wrapper around Discover's Saved Search embeddable. This can be used in solutions without being within a Dasboard context. | ||
|
||
This can be used to render a context-aware (logs etc) "document table". | ||
|
||
In the past you may have used the Log Stream Component to achieve this, this component supersedes that. | ||
|
||
## Basic usage | ||
|
||
``` | ||
import { LazySavedSearchComponent } from '@kbn/saved-search-component'; | ||
<LazySavedSearchComponent | ||
dependencies={{ | ||
embeddable: dependencies.embeddable, | ||
savedSearch: dependencies.savedSearch, | ||
dataViews: dependencies.dataViews, | ||
searchSource: dependencies.searchSource, | ||
}} | ||
index={anIndexString} | ||
filters={optionalFilters} | ||
query={optionalQuery} | ||
timestampField={optionalTimestampFieldString} | ||
/> | ||
``` |
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 @@ | ||
/* | ||
* 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 { dynamic } from '@kbn/shared-ux-utility'; | ||
|
||
export type { SavedSearchComponentDependencies, SavedSearchComponentProps } from './src/types'; | ||
|
||
export const LazySavedSearchComponent = dynamic(() => | ||
import('./src/components/saved_search').then((mod) => ({ | ||
default: mod.SavedSearchComponent, | ||
})) | ||
); |
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,14 @@ | ||
/* | ||
* 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". | ||
*/ | ||
|
||
module.exports = { | ||
preset: '@kbn/test', | ||
rootDir: '../..', | ||
roots: ['<rootDir>/packages/kbn-saved-search-component'], | ||
}; |
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,5 @@ | ||
{ | ||
"type": "shared-browser", | ||
"id": "@kbn/saved-search-component", | ||
"owner": "@elastic/obs-ux-logs-team" | ||
} |
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,7 @@ | ||
{ | ||
"name": "@kbn/saved-search-component", | ||
"private": true, | ||
"version": "1.0.0", | ||
"license": "Elastic License 2.0 OR AGPL-3.0-only OR SSPL-1.0", | ||
"sideEffects": false | ||
} |
166 changes: 166 additions & 0 deletions
166
packages/kbn-saved-search-component/src/components/saved_search.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,166 @@ | ||
/* | ||
* 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 React, { useEffect, useMemo, useRef, useState } from 'react'; | ||
import { ReactEmbeddableRenderer } from '@kbn/embeddable-plugin/public'; | ||
import { SEARCH_EMBEDDABLE_TYPE } from '@kbn/discover-utils'; | ||
import type { | ||
SearchEmbeddableSerializedState, | ||
SearchEmbeddableRuntimeState, | ||
SearchEmbeddableApi, | ||
} from '@kbn/discover-plugin/public'; | ||
import { SerializedPanelState } from '@kbn/presentation-containers'; | ||
import { SavedSearchComponentProps } from '../types'; | ||
|
||
const TIMESTAMP_FIELD = '@timestamp'; | ||
|
||
export const SavedSearchComponent: React.FC<SavedSearchComponentProps> = (props) => { | ||
// Creates our *initial* search source and set of attributes. | ||
// Future changes to these properties will be facilitated by the Parent API from the embeddable. | ||
const [initialSerializedState, setInitialSerializedState] = | ||
useState<SerializedPanelState<SearchEmbeddableSerializedState>>(); | ||
|
||
const { dependencies, timeRange, query, filters, index, timestampField, height } = props; | ||
|
||
useEffect(() => { | ||
// Ensure we get a stabilised set of initial state incase dependencies change, as | ||
// the data view creation process is async. | ||
const abortController = new AbortController(); | ||
|
||
async function createInitialSerializedState() { | ||
const { dataViews, searchSource: searchSourceService } = dependencies; | ||
const { enableFlyout: flyoutEnabled = true, enableFilters: filtersEnabled = true } = | ||
props.displayOptions ?? {}; | ||
// Ad-hoc data view | ||
const dataView = await dataViews.create({ | ||
title: index, | ||
timeFieldName: timestampField ?? TIMESTAMP_FIELD, | ||
}); | ||
if (!abortController.signal.aborted) { | ||
// Search source | ||
const searchSource = searchSourceService.createEmpty(); | ||
searchSource.setField('index', dataView); | ||
searchSource.setField('query', query); | ||
searchSource.setField('filter', filters); | ||
const { searchSourceJSON, references } = searchSource.serialize(); | ||
// By-value saved object structure | ||
const attributes = { | ||
kibanaSavedObjectMeta: { | ||
searchSourceJSON, | ||
}, | ||
}; | ||
setInitialSerializedState({ | ||
rawState: { | ||
attributes: { ...attributes, references }, | ||
timeRange, | ||
nonPersistedDisplayOptions: { | ||
enableFlyout: flyoutEnabled, | ||
enableFilters: filtersEnabled, | ||
}, | ||
} as SearchEmbeddableSerializedState, | ||
references, | ||
}); | ||
} | ||
} | ||
|
||
createInitialSerializedState(); | ||
|
||
return () => { | ||
abortController.abort(); | ||
}; | ||
}, [dependencies, filters, index, props.displayOptions, query, timeRange, timestampField]); | ||
|
||
return initialSerializedState ? ( | ||
<div style={{ height: height ?? '100%' }}> | ||
<SavedSearchComponentTable {...props} initialSerializedState={initialSerializedState} /> | ||
</div> | ||
) : null; | ||
}; | ||
|
||
const SavedSearchComponentTable: React.FC< | ||
SavedSearchComponentProps & { initialSerializedState: any } | ||
> = (props) => { | ||
const { dependencies, initialSerializedState, filters, query, timeRange, timestampField, index } = | ||
props; | ||
const embeddableApi = useRef<SearchEmbeddableApi | undefined>(undefined); | ||
|
||
const parentApi = useMemo(() => { | ||
return { | ||
getSerializedStateForChild: () => { | ||
return initialSerializedState; | ||
}, | ||
}; | ||
}, [initialSerializedState]); | ||
|
||
useEffect( | ||
function syncIndex() { | ||
if (!embeddableApi.current) return; | ||
|
||
const abortController = new AbortController(); | ||
|
||
async function updateDataView(indexPattern: string) { | ||
const { dataViews } = dependencies; | ||
// Ad-hoc data view | ||
const dataView = await dataViews.create({ | ||
title: index, | ||
timeFieldName: timestampField ?? TIMESTAMP_FIELD, | ||
}); | ||
if (!abortController.signal.aborted) { | ||
embeddableApi?.current?.setDataViews([dataView]); | ||
} | ||
} | ||
|
||
updateDataView(index); | ||
|
||
return () => { | ||
abortController.abort(); | ||
}; | ||
}, | ||
[dependencies, index, timestampField] | ||
); | ||
|
||
useEffect( | ||
function syncFilters() { | ||
if (!embeddableApi.current) return; | ||
embeddableApi.current.setFilters(filters); | ||
}, | ||
[filters] | ||
); | ||
|
||
useEffect( | ||
function syncQuery() { | ||
if (!embeddableApi.current) return; | ||
embeddableApi.current.setQuery(query); | ||
}, | ||
[query] | ||
); | ||
|
||
useEffect( | ||
function syncTimeRange() { | ||
if (!embeddableApi.current) return; | ||
embeddableApi.current.setTimeRange(timeRange); | ||
}, | ||
[timeRange] | ||
); | ||
|
||
return ( | ||
<ReactEmbeddableRenderer< | ||
SearchEmbeddableSerializedState, | ||
SearchEmbeddableRuntimeState, | ||
SearchEmbeddableApi | ||
> | ||
maybeId={undefined} | ||
type={SEARCH_EMBEDDABLE_TYPE} | ||
getParentApi={() => parentApi} | ||
onApiAvailable={(api) => { | ||
embeddableApi.current = api; | ||
}} | ||
/> | ||
); | ||
}; |
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,30 @@ | ||
/* | ||
* 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 { EmbeddableStart } from '@kbn/embeddable-plugin/public'; | ||
import { Filter, Query, TimeRange } from '@kbn/es-query'; | ||
import { DataViewsContract, ISearchStartSearchSource } from '@kbn/data-plugin/public'; | ||
import type { NonPersistedDisplayOptions } from '@kbn/discover-plugin/public'; | ||
|
||
export interface SavedSearchComponentDependencies { | ||
embeddable: EmbeddableStart; | ||
searchSource: ISearchStartSearchSource; | ||
dataViews: DataViewsContract; | ||
} | ||
|
||
export interface SavedSearchComponentProps { | ||
dependencies: SavedSearchComponentDependencies; | ||
index: string; | ||
timeRange?: TimeRange; | ||
query?: Query; | ||
filters?: Filter[]; | ||
timestampField?: string; | ||
height?: string | number; | ||
displayOptions?: NonPersistedDisplayOptions; | ||
} |
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 @@ | ||
{ | ||
"extends": "../../tsconfig.base.json", | ||
"compilerOptions": { | ||
"outDir": "target/types", | ||
"types": [ | ||
"jest", | ||
"node", | ||
"react" | ||
] | ||
}, | ||
"include": [ | ||
"**/*.ts", | ||
"**/*.tsx", | ||
], | ||
"exclude": [ | ||
"target/**/*" | ||
], | ||
"kbn_references": [ | ||
"@kbn/embeddable-plugin", | ||
"@kbn/shared-ux-utility", | ||
"@kbn/discover-utils", | ||
"@kbn/saved-search-plugin", | ||
"@kbn/es-query", | ||
"@kbn/data-plugin", | ||
"@kbn/discover-plugin", | ||
"@kbn/presentation-containers", | ||
] | ||
} |
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.