Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Auto trigger schema setup in assets creation flow of get started page #2200

Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
EuiButton,
EuiIcon,
EuiCard,
EuiSelectableOption,
} from '@elastic/eui';
import React, { useEffect, useState } from 'react';

Expand All @@ -44,6 +45,7 @@
const cardOne = 'Logs';
const cardTwo = 'Metrics';
const cardThree = 'Traces';
const OTEL_LOGS_OPTION = { label: 'Open Telemetry', value: 'otelLogs' };

interface CollectAndShipDataProps {
isOpen: boolean;
Expand All @@ -52,7 +54,7 @@
selectedDataSourceLabel: string;
}

interface CollectorOption {
export interface CollectorOption {
label: string;
value: string;
}
Expand All @@ -63,14 +65,17 @@
}) => {
const [collectionMethod, setCollectionMethod] = useState('');
const [specificMethod, setSpecificMethod] = useState('');
const [_gettingStarted, setGettingStarted] = useState<any>(null);

Check warning on line 68 in public/components/getting_started/components/getting_started_collectData.tsx

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
const [selectedTabId, setSelectedTabId] = useState('workflow_0');
const [_selectedWorkflow, setSelectedWorkflow] = useState('');
const [workflows, setWorkflows] = useState<any[]>([]);

Check warning on line 71 in public/components/getting_started/components/getting_started_collectData.tsx

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
const [collectorOptions, setCollectorOptions] = useState<CollectorOption[]>([]);
const [patternsContent, setPatternsContent] = useState<any[]>([]);

Check warning on line 73 in public/components/getting_started/components/getting_started_collectData.tsx

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
const [selectedIntegration, setSelectedIntegration] = useState<
Array<EuiSelectableOption<CollectorOption>>
>([OTEL_LOGS_OPTION]);

const technologyJsonMap: Record<string, any> = {

Check warning on line 78 in public/components/getting_started/components/getting_started_collectData.tsx

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
otelLogs: otelJsonLogs,
otelMetrics: otelJsonMetrics,
otelTraces: otelJsonTraces,
Expand Down Expand Up @@ -120,15 +125,16 @@
return () => {
isMounted = false;
};
}, [specificMethod]);

Check warning on line 128 in public/components/getting_started/components/getting_started_collectData.tsx

View workflow job for this annotation

GitHub Actions / Lint

React Hook useEffect has a missing dependency: 'technologyJsonMap'. Either include it or remove the dependency array

const handleSpecificMethodChange = (newOption: any) => {

Check warning on line 130 in public/components/getting_started/components/getting_started_collectData.tsx

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
const selectedOptionValue = newOption[0]?.value;

if (selectedOptionValue === specificMethod) {
return;
}

setSelectedIntegration(newOption);
setSpecificMethod(selectedOptionValue);
setSelectedWorkflow('');
setGettingStarted(null);
Expand All @@ -138,9 +144,9 @@
// Auto-select first collector if nothing is selected and a collection method is set
useEffect(() => {
if (collectorOptions.length > 0 && !specificMethod && collectionMethod) {
handleSpecificMethodChange([{ value: collectorOptions[0].value }]);
handleSpecificMethodChange([{ ...OTEL_LOGS_OPTION }]);
mengweieric marked this conversation as resolved.
Show resolved Hide resolved
}
}, [collectorOptions, specificMethod, collectionMethod]);

Check warning on line 149 in public/components/getting_started/components/getting_started_collectData.tsx

View workflow job for this annotation

GitHub Actions / Lint

React Hook useEffect has a missing dependency: 'handleSpecificMethodChange'. Either include it or remove the dependency array

const handleCollectionMethodChange = (value: string) => {
setCollectionMethod(value);
Expand All @@ -151,7 +157,7 @@

if (value === cardOne) {
setCollectorOptions([
{ label: 'Open Telemetry', value: 'otelLogs' },
{ ...OTEL_LOGS_OPTION },
{ label: 'Nginx', value: 'nginx' },
{ label: 'Java', value: 'java' },
{ label: 'Python', value: 'python' },
Expand Down Expand Up @@ -214,7 +220,7 @@
};

const renderIndexPatternStep = (
patternsContentRender: any[],

Check warning on line 223 in public/components/getting_started/components/getting_started_collectData.tsx

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
selectedDataSourceIdRender: string
) => {
if (!patternsContentRender || patternsContentRender.length === 0) return null;
Expand Down Expand Up @@ -262,7 +268,7 @@
);
};

const renderSchema = (schemas: any[]) =>

Check warning on line 271 in public/components/getting_started/components/getting_started_collectData.tsx

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
schemas.map((schema, idx) => {
const indexPatternName = schema['index-pattern-name'] || '';

Expand All @@ -287,7 +293,7 @@
</>
)}
{Array.isArray(schema.info) &&
schema.info.map((link: any, linkIdx: number) =>

Check warning on line 296 in public/components/getting_started/components/getting_started_collectData.tsx

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
link && typeof link.url === 'string' ? (
<EuiLink key={linkIdx} href={link.url} target="_blank">
{typeof link.title === 'string' && link.title.trim() !== ''
Expand Down Expand Up @@ -347,7 +353,15 @@
</EuiListGroup>
<EuiButton
onClick={async () => {
await UploadAssets(specificMethod, selectedDataSourceId, selectedDataSourceLabel);
await UploadAssets(
specificMethod,
selectedDataSourceId,
selectedDataSourceLabel,
technologyJsonMap[specificMethod]?.['getting-started']?.schema ||
technologyJsonMap[specificMethod]?.schema ||
[],
selectedIntegration
);
}}
fill
>
Expand Down
56 changes: 55 additions & 1 deletion public/components/getting_started/components/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,27 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { MappingTypeMapping } from '@opensearch-project/opensearch/api/types';
import { EuiSelectableOption } from '@elastic/eui';
import { coreRefs } from '../../../framework/core_refs';
import { useToast } from '../../../../public/components/common/toast';
import { CollectorOption } from './getting_started_collectData';

export interface ICollectorIndexTemplate {
name: string;
templatePath: string;
template: MappingTypeMapping;
}

export interface ICollectorSchema {
alias: string;
content: string;
description: string;
'index-pattern-name': string;
type: string;
'index-template': string;
info: string[];
}

const fetchAssets = async (tutorialId: string, assetFilter?: 'dashboards' | 'indexPatterns') => {
const assetFilterParam = assetFilter ? `${assetFilter}/` : '';
Expand All @@ -20,16 +39,51 @@ const fetchAssets = async (tutorialId: string, assetFilter?: 'dashboards' | 'ind
return responeData;
};

export const UploadAssets = async (tutorialId: string, mdsId: string, mdsLabel: string) => {
export const UploadAssets = async (
tutorialId: string,
mdsId: string,
mdsLabel: string,
schema: ICollectorSchema[],
selectedIntegration: Array<EuiSelectableOption<CollectorOption>>
) => {
const { setToast } = useToast();
const http = coreRefs.http;

let curIntegration: string | undefined;
if (selectedIntegration !== undefined) {
if (/^otel[A-Za-z]+$/i.test(selectedIntegration[0].value)) {
curIntegration = 'otel-services';
} else if (selectedIntegration[0].value === 'nginx') {
curIntegration = selectedIntegration[0].value;
}
}

try {
// Auto-generate index templates based on the selected integration
let templates: ICollectorIndexTemplate[] = [];
if (curIntegration !== undefined) {
const indexTemplateMappings = await http!.get(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we making this call on every selection or only once? we can cache this right.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes this is actually what I was considering also for an improvement later, but this is only triggered per 'create assets' button clicked, simply clicking on different cards won't trigger this call

`/api/integrations/repository/${curIntegration}/schema`
);
templates = schema.reduce((acc: ICollectorIndexTemplate[], sh) => {
const templateMapping = indexTemplateMappings?.data?.mappings?.[sh.type.toLowerCase()];
if (!!templateMapping) {
acc.push({
name: sh.content.match(/[^/]+$/)?.[0] || '',
templatePath: sh.content.match(/PUT\s+(.+)/)?.[1] || '',
template: templateMapping,
});
}
return acc;
}, []);
}

const response = await http!.post(`/api/observability/gettingStarted/createAssets`, {
body: JSON.stringify({
mdsId,
mdsLabel,
tutorialId,
indexTemplates: templates,
}),
});

Expand Down
17 changes: 15 additions & 2 deletions server/routes/getting_started/getting_started_router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
SavedObject,
} from '../../../../../src/core/server';
import { createSavedObjectsStreamFromNdJson } from '../../../../../src/core/server/saved_objects/routes/utils';
import { loadAssetsFromFile } from './helper';
import { loadAssetsFromFile, createAllTemplatesSettled } from './helper';
import { getWorkspaceState } from '../../../../../src/core/server/utils';
import { TutorialId } from '../../../common/constants/getting_started_routes';

Expand Down Expand Up @@ -99,6 +99,14 @@ export function registerGettingStartedRoutes(router: IRouter) {
mdsId: schema.string(),
mdsLabel: schema.string(),
tutorialId: schema.string(),
indexTemplates: schema.arrayOf(
schema.object({
name: schema.string(),
templatePath: schema.string(),
template: schema.recordOf(schema.string(), schema.any()),
}),
{ defaultValue: [] }
),
}),
},
},
Expand All @@ -108,10 +116,15 @@ export function registerGettingStartedRoutes(router: IRouter) {
response
): Promise<IOpenSearchDashboardsResponse<any | ResponseError>> => {
try {
const { mdsId, mdsLabel, tutorialId } = request.body;
const { mdsId, mdsLabel, tutorialId, indexTemplates } = request.body;
const { requestWorkspaceId } = getWorkspaceState(request);
const fileData = await loadAssetsFromFile(tutorialId as TutorialId);

// create related index templates
if (indexTemplates.length > 0) {
await createAllTemplatesSettled(context, indexTemplates, mdsId);
}

const objects = await createSavedObjectsStreamFromNdJson(Readable.from(fileData));
const loadedObjects = await objects.toArray();

Expand Down
45 changes: 45 additions & 0 deletions server/routes/getting_started/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@

import fs from 'fs';
import path from 'path';
import { MappingTypeMapping } from '@opensearch-project/opensearch/api/types';
import {
COMPONENT_MAP,
VERSION_MAP,
SIGNAL_MAP,
TutorialId,
} from '../../../common/constants/getting_started_routes';
import { RequestHandlerContext } from '../../../../../src/core/server';

export const assetMapper = (tutorialId: TutorialId): string => {
const component = COMPONENT_MAP[tutorialId] || 'default-component';
Expand All @@ -30,3 +32,46 @@ export const loadAssetsFromFile = async (tutorialId: TutorialId) => {
throw new Error(`Error loading asset: ${tutorialId}`);
}
};

export const createAllTemplatesSettled = async (
context: RequestHandlerContext,
indexTemplates: Array<{ name: string; template: MappingTypeMapping; templatePath: string }>,
dataSourceMDSId: string
) => {
const results = await Promise.allSettled(
indexTemplates.map(({ name, template, templatePath }) =>
createIndexTemplate(context, name, template, dataSourceMDSId, templatePath)
)
);

return results.map((result, index) => {
const templateName = indexTemplates[index].name;
if (result.status === 'fulfilled') {
return { name: templateName, success: true };
}
return { name: templateName, success: false, reason: result.reason };
});
};

export const createIndexTemplate = async (
context: RequestHandlerContext,
name: string,
template: MappingTypeMapping,
dataSourceMDSId: string,
templatePath: string
) => {
try {
const osClient = dataSourceMDSId
? await context.dataSource.opensearch.getClient(dataSourceMDSId)
: context.core.opensearch.client.asCurrentUser;

return await osClient.transport.request({
method: 'PUT',
path: templatePath,
body: template,
});
} catch (error) {
console.error(`Failed to create index template ${name}:`, error);
throw error;
}
};
Loading