Skip to content

Commit

Permalink
Merge branch 'main' into dataview-mode-suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
stratoula committed Dec 18, 2023
2 parents 894d665 + 99f3001 commit 541bb58
Show file tree
Hide file tree
Showing 128 changed files with 2,020 additions and 769 deletions.
32 changes: 32 additions & 0 deletions packages/kbn-apm-synthtrace-client/src/lib/apm/apm_fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,35 @@ export interface GeoLocation {
type: string;
}

export interface APMStacktrace {
abs_path?: string;
classname?: string;
context?: {
post?: string[];
pre?: string[];
};
exclude_from_grouping?: boolean;
filename?: string;
function?: string;
module?: string;
library_frame?: boolean;
line?:
| {
column?: number;
number: number;
}
| {
context?: string;
};
sourcemap?: {
error?: string;
updated?: boolean;
};
vars?: {
[key: string]: unknown;
};
}

type ExperimentalFields = Partial<{
'metricset.interval': string;
'transaction.duration.summary': string;
Expand Down Expand Up @@ -80,6 +109,8 @@ export type ApmFields = Fields<{
'cloud.provider': string;
'cloud.region': string;
'cloud.service.name': string;
// otel
'code.stacktrace': string;
'container.id': string;
'destination.address': string;
'destination.port': number;
Expand Down Expand Up @@ -169,6 +200,7 @@ export type ApmFields = Fields<{
'span.duration.us': number;
'span.id': string;
'span.name': string;
'span.stacktrace': APMStacktrace[];
'span.self_time.count': number;
'span.self_time.sum.us': number;
'span.subtype': string;
Expand Down
3 changes: 3 additions & 0 deletions packages/kbn-check-mappings-update-cli/current_fields.json
Original file line number Diff line number Diff line change
Expand Up @@ -968,5 +968,8 @@
"kuery",
"serviceEnvironmentFilterEnabled",
"serviceNameFilterEnabled"
],
"cloud-security-posture-settings": [
"rules"
]
}
4 changes: 4 additions & 0 deletions packages/kbn-check-mappings-update-cli/current_mappings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2349,6 +2349,10 @@
}
}
},
"cloud-security-posture-settings": {
"dynamic": false,
"properties": {}
},
"slo": {
"dynamic": false,
"properties": {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,19 @@ import { DocViewsRegistry } from '../..';
describe('<DocViewer />', () => {
test('Render <DocViewer/> with 3 different tabs', () => {
const registry = new DocViewsRegistry();
registry.addDocView({ order: 10, title: 'Render function', render: jest.fn() });
registry.addDocView({ order: 20, title: 'React component', component: () => <div>test</div> });
registry.add({ id: 'function', order: 10, title: 'Render function', render: jest.fn() });
registry.add({
id: 'component',
order: 20,
title: 'React component',
component: () => <div>test</div>,
});
// @ts-expect-error This should be invalid and will throw an error when rendering
registry.addDocView({ order: 30, title: 'Invalid doc view' });
registry.add({ id: 'invalid', order: 30, title: 'Invalid doc view' });

const renderProps = { hit: {} } as DocViewRenderProps;

const wrapper = shallow(
<DocViewer docViews={registry.getDocViewsSorted(renderProps.hit)} {...renderProps} />
);
const wrapper = shallow(<DocViewer docViews={registry.getAll()} {...renderProps} />);

expect(wrapper).toMatchSnapshot();
});
Expand All @@ -38,7 +41,8 @@ describe('<DocViewer />', () => {
}

const registry = new DocViewsRegistry();
registry.addDocView({
registry.add({
id: 'component',
order: 10,
title: 'React component',
component: SomeComponent,
Expand All @@ -49,9 +53,7 @@ describe('<DocViewer />', () => {
} as DocViewRenderProps;
const errorMsg = 'Catch me if you can!';

const wrapper = mount(
<DocViewer docViews={registry.getDocViewsSorted(renderProps.hit)} {...renderProps} />
);
const wrapper = mount(<DocViewer docViews={registry.getAll()} {...renderProps} />);
const error = new Error(errorMsg);
wrapper.find(SomeComponent).simulateError(error);
const errorMsgComponent = findTestSubject(wrapper, 'docViewerError');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,25 @@ export interface DocViewerProps extends DocViewRenderProps {
* a `render` function.
*/
export function DocViewer({ docViews, ...renderProps }: DocViewerProps) {
const tabs = docViews.map(({ title, render, component }: DocView, idx: number) => {
const tabs = docViews.map(({ id, title, render, component }: DocView) => {
return {
id: `kbn_doc_viewer_tab_${idx}`,
id: `kbn_doc_viewer_tab_${id}`,
name: title,
content: (
<DocViewerTab
id={idx}
id={id}
title={title}
component={component}
renderProps={renderProps}
render={render}
/>
),
['data-test-subj']: `docViewerTab-${idx}`,
['data-test-subj']: `docViewerTab-${id}`,
};
});

if (!tabs.length) {
// There there's a minimum of 2 tabs active in Discover.
// There's a minimum of 2 tabs active in Discover.
// This condition takes care of unit tests with 0 tabs.
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ describe('DocViewerTab', () => {
test('changing columns triggers an update', () => {
const hit = buildDataTableRecord({ _index: 'test', _id: '1' }, dataViewMock);
const props = {
id: 'doc_view_test',
title: 'test',
component: jest.fn(),
id: 1,
render: jest.fn(),
renderProps: {
hit,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { DocViewerError } from './doc_viewer_error';
import type { DocViewRenderFn, DocViewRenderProps } from '../../types';

interface Props {
id: number;
id: string;
renderProps: DocViewRenderProps;
title: string;
render?: DocViewRenderFn;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* 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 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 or the Server
* Side Public License, v 1.
*/

import React from 'react';
import { DocViewsRegistry } from './doc_views_registry';

const fnDocView = {
id: 'function-doc-view',
order: 10,
title: 'Render function',
render: jest.fn(),
};
const componentDocView = {
id: 'component-doc-view',
order: 20,
title: 'React component',
component: () => <div>test</div>,
};

describe('DocViewerRegistry', () => {
test('can be initialized from an array of doc views', () => {
const registry = new DocViewsRegistry([fnDocView, componentDocView]);

expect(registry.getAll()).toHaveLength(2);
});

test('can be initialized from another DocViewsRegistry instance', () => {
const registry = new DocViewsRegistry([fnDocView, componentDocView]);
const newRegistry = new DocViewsRegistry(registry);

expect(registry.getAll()).toHaveLength(2);
expect(newRegistry.getAll()).toHaveLength(2);
expect(registry).not.toBe(newRegistry);
});

describe('#add', () => {
test('should add a doc view to the registry in the correct order', () => {
const registry = new DocViewsRegistry([componentDocView]);

registry.add(fnDocView);

const docViews = registry.getAll();

expect(docViews[0]).toHaveProperty('id', 'function-doc-view');
expect(docViews[1]).toHaveProperty('id', 'component-doc-view');
});

test('should throw an error when the passed doc view already exists for the given id', () => {
const registry = new DocViewsRegistry([fnDocView]);

expect(() => registry.add(fnDocView)).toThrow(
'DocViewsRegistry#add: a DocView is already registered with id "function-doc-view".'
);
});
});

describe('#removeById', () => {
test('should remove a doc view given the passed id', () => {
const registry = new DocViewsRegistry([fnDocView, componentDocView]);

const docViews = registry.getAll();

expect(docViews[0]).toHaveProperty('id', 'function-doc-view');
expect(docViews[1]).toHaveProperty('id', 'component-doc-view');

registry.removeById('function-doc-view');

expect(registry.getAll()[0]).toHaveProperty('id', 'component-doc-view');
});
});

describe('#clone', () => {
test('should return a new DocViewRegistry instance starting from the current one', () => {
const registry = new DocViewsRegistry([fnDocView, componentDocView]);

const clonedRegistry = registry.clone();
const docViews = clonedRegistry.getAll();

expect(docViews[0]).toHaveProperty('id', 'function-doc-view');
expect(docViews[1]).toHaveProperty('id', 'component-doc-view');
expect(registry).not.toBe(clonedRegistry);
});
});
});
61 changes: 42 additions & 19 deletions packages/kbn-unified-doc-viewer/src/services/doc_views_registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import type { DataTableRecord } from '@kbn/discover-utils/types';
import { DocView, DocViewInput, DocViewInputFn } from './types';
import type { DocView, DocViewFactory } from './types';

export enum ElasticRequestState {
Loading,
Expand All @@ -18,24 +16,49 @@ export enum ElasticRequestState {
}

export class DocViewsRegistry {
private docViews: DocView[] = [];
private docViews: Map<string, DocView>;

constructor(initialValue?: DocViewsRegistry | DocView[]) {
if (initialValue instanceof DocViewsRegistry) {
this.docViews = new Map(initialValue.docViews);
} else if (Array.isArray(initialValue)) {
this.docViews = new Map(initialValue.map((docView) => [docView.id, docView]));
} else {
this.docViews = new Map();
}
}

/**
* Extends and adds the given doc view to the registry array
*/
addDocView(docViewRaw: DocViewInput | DocViewInputFn) {
getAll() {
return [...this.docViews.values()];
}

add(docViewRaw: DocView | DocViewFactory) {
const docView = typeof docViewRaw === 'function' ? docViewRaw() : docViewRaw;
this.docViews.push({
...docView,
shouldShow: docView.shouldShow ?? (() => true),
});

if (this.docViews.has(docView.id)) {
throw new Error(
`DocViewsRegistry#add: a DocView is already registered with id "${docView.id}".`
);
}

this.docViews.set(docView.id, docView);
// Sort the doc views at insertion time to perform this operation once and not on every retrieval.
this.sortDocViews();
}
/**
* Returns a sorted array of doc_views for rendering tabs
*/
getDocViewsSorted(hit: DataTableRecord) {
return this.docViews
.filter((docView) => docView.shouldShow(hit))
.sort((a, b) => (Number(a.order) > Number(b.order) ? 1 : -1));

removeById(id: string) {
this.docViews.delete(id);
}

clone() {
return new DocViewsRegistry(this);
}

private sortDocViews() {
const sortedEntries = [...this.docViews.entries()].sort(
([_currKey, curr], [_nextKey, next]) => curr.order - next.order
);

this.docViews = new Map(sortedEntries);
}
}

0 comments on commit 541bb58

Please sign in to comment.