Skip to content

Commit

Permalink
[investigate] Copy changes from POC (#187936)
Browse files Browse the repository at this point in the history
  • Loading branch information
kdelemme authored Jul 16, 2024
1 parent 9d75f32 commit 4dca5f5
Show file tree
Hide file tree
Showing 13 changed files with 1,145 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
* 2.0.
*/
export type {
InvestigateTimeline,
Investigation,
InvestigationRevision,
InvestigateWidget,
InvestigateWidgetCreate,
WorkflowBlock,
} from './types';

export { mergePlainObjects } from './utils/merge_plain_objects';

export { InvestigateWidgetColumnSpan } from './types';
22 changes: 13 additions & 9 deletions x-pack/plugins/observability_solution/investigate/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@
import type { EuiThemeComputed } from '@elastic/eui';
import type { Filter } from '@kbn/es-query';
import type { DeepPartial, PickByValue } from 'utility-types';

export interface InvestigateUser {
name: string;
}
import type { AuthenticatedUser } from '@kbn/core/public';

export interface GlobalWidgetParameters {
timeRange: {
Expand All @@ -32,12 +29,19 @@ export enum InvestigateWidgetColumnSpan {
Four = 4,
}

export interface InvestigateTimeline {
export interface InvestigationRevision {
id: string;
title: string;
'@timestamp': number;
user: InvestigateUser;
items: InvestigateWidget[];
parameters: GlobalWidgetParameters;
}

export interface Investigation {
id: string;
'@timestamp': number;
user: AuthenticatedUser;
revisions: InvestigationRevision[];
title: string;
revision: string;
}

export interface InvestigateWidget<
Expand All @@ -48,7 +52,7 @@ export interface InvestigateWidget<
created: number;
last_updated: number;
type: string;
user: InvestigateUser;
user: AuthenticatedUser;
parameters: GlobalWidgetParameters & TParameters;
data: TData;
title: string;
Expand Down
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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import type { IconType } from '@elastic/eui';
import type { Ast } from '@kbn/interpreter';
import type { InvestigateWidgetCreate } from '../../common';
import type { GlobalWidgetParameters } from '../../common/types';

// copied over from the Lens plugin to prevent dependency hell
type TableChangeType = 'initial' | 'unchanged' | 'reduced' | 'extended' | 'reorder' | 'layers';
Expand All @@ -33,7 +32,6 @@ interface Suggestion<T = unknown, V = unknown> {
export interface EsqlWidgetParameters {
esql: string;
suggestion?: Suggestion;
predefined?: Partial<GlobalWidgetParameters>;
}

export type EsqlWidgetCreate = InvestigateWidgetCreate<EsqlWidgetParameters>;
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;
}
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],
};
}
Loading

0 comments on commit 4dca5f5

Please sign in to comment.