-
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.
[investigate] Copy changes from POC (#187936)
- Loading branch information
Showing
13 changed files
with
1,145 additions
and
16 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
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
74 changes: 74 additions & 0 deletions
74
x-pack/plugins/observability_solution/investigate/common/utils/merge_plain_objects.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,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 type { RequiredKeys } from 'utility-types'; | ||
import { isPlainObject, mergeWith, MergeWithCustomizer } from 'lodash'; | ||
|
||
type DeepOverwrite<T, U> = U extends Record<string, any> | ||
? Omit<T, RequiredKeys<U>> & { | ||
[K in keyof U]: K extends keyof T ? DeepOverwrite<T[K], U[K]> : U[K]; | ||
} | ||
: U extends undefined | ||
? T | ||
: U; | ||
|
||
type DeepPartialPlainObjects<T> = T extends Record<string, any> | ||
? Partial<{ | ||
[TKey in keyof T]: DeepPartialPlainObjects<T[TKey]>; | ||
}> | ||
: T; | ||
|
||
type Mergable = Record<string, any>; | ||
|
||
type MergeRecursively<TMergables extends Mergable[]> = TMergables extends [ | ||
infer THead, | ||
...infer TTail | ||
] | ||
? TTail extends Mergable[] | ||
? DeepOverwrite<THead, MergeRecursively<TTail>> | ||
: THead | ||
: TMergables extends [infer THead] | ||
? THead | ||
: TMergables extends [] | ||
? {} | ||
: {}; | ||
|
||
const customMergeFunction: MergeWithCustomizer = (value, sourceValue) => { | ||
if (isPlainObject(sourceValue)) { | ||
return mergeWith(value, sourceValue, customMergeFunction); | ||
} | ||
return undefined; | ||
}; | ||
|
||
function mergePlainObjectsOnly(...sources: Mergable[]) { | ||
return mergeWith({}, ...sources.concat(customMergeFunction)); | ||
} | ||
|
||
export function mergePlainObjects<T1 extends Record<string, any> | undefined>(t1: T1): T1; | ||
|
||
export function mergePlainObjects<T1 extends Mergable, T2 extends DeepPartialPlainObjects<T1>>( | ||
t1: T1, | ||
t2: T2 | ||
): MergeRecursively<[T1, T2]>; | ||
|
||
export function mergePlainObjects< | ||
T1 extends Mergable, | ||
T2 extends DeepPartialPlainObjects<T1>, | ||
T3 extends DeepPartialPlainObjects<T2> | ||
>(t1: T1, t2: T2, t3: T3): MergeRecursively<[T1, T2, T3]>; | ||
|
||
export function mergePlainObjects< | ||
T1 extends Mergable, | ||
T2 extends DeepPartialPlainObjects<T1>, | ||
T3 extends DeepPartialPlainObjects<T2>, | ||
T4 extends DeepPartialPlainObjects<T3> | ||
>(t1: T1, t2: T2, t3: T4): MergeRecursively<[T1, T2, T3, T4]>; | ||
|
||
export function mergePlainObjects(...sources: Array<Record<string, any>>) { | ||
const merged = mergePlainObjectsOnly(...sources); | ||
|
||
return merged; | ||
} |
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
30 changes: 30 additions & 0 deletions
30
x-pack/plugins/observability_solution/investigate/public/hooks/use_investigate_widget.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,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; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
import { useContext, createContext } from 'react'; | ||
import type { InvestigateWidgetCreate, WorkflowBlock } from '../../common'; | ||
|
||
type UnregisterBlocksFunction = () => void; | ||
|
||
export interface UseInvestigateWidgetApi< | ||
TParameters extends Record<string, any> = {}, | ||
TData extends Record<string, any> = {} | ||
> { | ||
onWidgetAdd: (create: InvestigateWidgetCreate) => Promise<void>; | ||
blocks: { | ||
publish: (blocks: WorkflowBlock[]) => UnregisterBlocksFunction; | ||
}; | ||
} | ||
|
||
const InvestigateWidgetApiContext = createContext<UseInvestigateWidgetApi | undefined>(undefined); | ||
|
||
export const InvestigateWidgetApiContextProvider = InvestigateWidgetApiContext.Provider; | ||
|
||
export function useInvestigateWidget(): UseInvestigateWidgetApi | undefined { | ||
const context = useContext(InvestigateWidgetApiContext); | ||
|
||
return context; | ||
} |
41 changes: 41 additions & 0 deletions
41
...rvability_solution/investigate/public/hooks/use_investigation/create_new_investigation.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,41 @@ | ||
/* | ||
* 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 type { AuthenticatedUser } from '@kbn/security-plugin/common'; | ||
import { v4 } from 'uuid'; | ||
import { i18n } from '@kbn/i18n'; | ||
import type { Investigation, InvestigationRevision } from '../../../common'; | ||
import { GlobalWidgetParameters } from '../../../common/types'; | ||
|
||
export function createNewInvestigation({ | ||
id, | ||
user, | ||
globalWidgetParameters, | ||
}: { | ||
id?: string; | ||
user: AuthenticatedUser; | ||
globalWidgetParameters: GlobalWidgetParameters; | ||
}): Investigation { | ||
const revisionId = v4(); | ||
|
||
const revision: InvestigationRevision = { | ||
id: revisionId, | ||
items: [], | ||
parameters: globalWidgetParameters, | ||
}; | ||
|
||
return { | ||
'@timestamp': new Date().getTime(), | ||
user, | ||
id: id ?? v4(), | ||
title: i18n.translate('xpack.investigate.newInvestigationTitle', { | ||
defaultMessage: 'New investigation', | ||
}), | ||
revision: revisionId, | ||
revisions: [revision], | ||
}; | ||
} |
Oops, something went wrong.