Skip to content

Commit

Permalink
[SLOs] Show transformed roll up transform query (elastic#182829)
Browse files Browse the repository at this point in the history
## Summary

Show aggs query in transform and allow opening in dev tools directly 

<img width="1726" alt="image"
src="https://github.com/elastic/kibana/assets/3505601/f54cd155-eaae-497f-b795-6e29916792b9">

---------

Co-authored-by: kibanamachine <[email protected]>
  • Loading branch information
shahzad31 and kibanamachine authored May 10, 2024
1 parent 91d2c5f commit 5118fff
Show file tree
Hide file tree
Showing 9 changed files with 273 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,8 @@ interface SLOInspectResponse {
rollUpTransform: TransformPutTransformRequest;
summaryTransform: TransformPutTransformRequest;
temporaryDoc: Record<string, any>;
}

export interface UseInspectSLOResponse {
data: SLOInspectResponse | undefined;
isLoading: boolean;
isSuccess: boolean;
isError: boolean;
rollUpTransformCompositeQuery: string;
summaryTransformCompositeQuery: string;
}

export function useFetchSloInspect(slo: CreateSLOInput, shouldInspect: boolean) {
Expand Down
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 React, { ReactNode } from 'react';
import { EuiAccordion, EuiCodeBlock, EuiTitle } from '@elastic/eui';

export function CodeBlockAccordion({
id,
label,
json,
extraAction,
children,
}: {
id: string;
label: string;
json?: any;
extraAction?: ReactNode;
children?: ReactNode;
}) {
return (
<EuiAccordion
id={id}
extraAction={extraAction}
buttonContent={
<EuiTitle size="xs">
<h3>{label}</h3>
</EuiTitle>
}
>
{children ?? (
<EuiCodeBlock language="json" fontSize="m" paddingSize="m" isCopyable={true}>
{JSON.stringify(json, null, 2)}
</EuiCodeBlock>
)}
</EuiAccordion>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* 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 { EuiFlexGroup, EuiFlexItem, EuiLoadingSpinner } from '@elastic/eui';
import React from 'react';

export function LoadingState() {
return (
<EuiFlexGroup alignItems="center" justifyContent="center" style={{ height: '100%' }}>
<EuiFlexItem grow={false}>
<EuiLoadingSpinner size="xl" />
</EuiFlexItem>
</EuiFlexGroup>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
* 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.
*/

/* eslint-disable @elastic/eui/href-or-on-click */

import { EuiButtonEmpty, EuiCopy, EuiFlexGroup, EuiFlexItem, EuiSpacer } from '@elastic/eui';
import { i18n } from '@kbn/i18n';
import { XJsonLang } from '@kbn/monaco';
import React, { ReactNode, useCallback } from 'react';
import { CodeEditor } from '@kbn/code-editor';
import { compressToEncodedURIComponent } from 'lz-string';
import { useKibana } from '../../../../../utils/kibana_react';

interface RequestCodeViewerProps {
value: string;
}

const copyToClipboardLabel = i18n.translate('xpack.slo.requests.copyToClipboardLabel', {
defaultMessage: 'Copy to clipboard',
});

export function RequestCodeViewer({ value }: RequestCodeViewerProps) {
const {
application: { navigateToUrl },
share: {
url: { locators },
},
} = useKibana().services;

// "Open in Console" button
const devToolsDataUri = compressToEncodedURIComponent(value);
const consoleHref = locators
.get('CONSOLE_APP_LOCATOR')
?.useUrl({ loadFrom: `data:text/plain,${devToolsDataUri}` });

const handleDevToolsLinkClick = useCallback(
() => consoleHref && navigateToUrl && navigateToUrl(consoleHref),
[consoleHref, navigateToUrl]
);

const actions: Array<{ name: string; action: ReactNode }> = [];

actions.push({
name: 'openInConsole',
action: (
<EuiButtonEmpty
size="xs"
flush="right"
iconType="wrench"
href={consoleHref}
onClick={handleDevToolsLinkClick}
data-test-subj="inspectorRequestOpenInConsoleButton"
>
{openInConsoleLabel}
</EuiButtonEmpty>
),
});

return (
<EuiFlexGroup
direction="column"
gutterSize="s"
wrap={false}
responsive={false}
css={{ height: 800 }}
>
<EuiFlexItem grow={false}>
<EuiSpacer size="s" />
<EuiFlexGroup justifyContent="flexEnd" gutterSize="m" wrap>
<EuiFlexItem grow={false}>
<div>
<EuiCopy textToCopy={value}>
{(copy) => (
<EuiButtonEmpty
size="xs"
flush="right"
iconType="copyClipboard"
onClick={copy}
data-test-subj="inspectorRequestCopyClipboardButton"
>
{copyToClipboardLabel}
</EuiButtonEmpty>
)}
</EuiCopy>
</div>
</EuiFlexItem>
{!!actions &&
actions.map((item) => (
<EuiFlexItem grow={false} key={item.name}>
<div>{item.action}</div>
</EuiFlexItem>
))}
</EuiFlexGroup>
</EuiFlexItem>
<EuiFlexItem grow={true} data-test-subj="inspectorRequestCodeViewerContainer">
<CodeEditor
languageId={XJsonLang.ID}
value={value}
options={{
readOnly: true,
lineNumbers: 'off',
fontSize: 12,
minimap: {
enabled: false,
},
folding: true,
scrollBeyondLastLine: false,
wordWrap: 'on',
wrappingIndent: 'indent',
automaticLayout: true,
}}
/>
</EuiFlexItem>
</EuiFlexGroup>
);
}

const openInConsoleLabel = i18n.translate('xpack.slo.requests.openInConsoleLabel', {
defaultMessage: 'Open in Console',
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,13 @@
* 2.0.
*/
import {
EuiAccordion,
EuiButton,
EuiButtonEmpty,
EuiButtonIcon,
EuiCodeBlock,
EuiFlexGroup,
EuiFlexItem,
EuiFlyout,
EuiFlyoutBody,
EuiFlyoutFooter,
EuiFlyoutHeader,
EuiLoadingSpinner,
EuiSpacer,
EuiTitle,
EuiToolTip,
Expand All @@ -29,14 +24,17 @@ import {
} from '@kbn/ingest-pipelines-plugin/public';
import { useFetcher } from '@kbn/observability-shared-plugin/public';
import { GetSLOResponse } from '@kbn/slo-schema';
import React, { ReactNode, useState } from 'react';
import React, { useState } from 'react';
import { useFormContext } from 'react-hook-form';
import { enableInspectEsQueries } from '@kbn/observability-plugin/common';
import { useKibana } from '../../../../utils/kibana_react';
import { useFetchSloInspect } from '../../../../hooks/use_fetch_slo_inspect';
import { usePluginContext } from '../../../../hooks/use_plugin_context';
import { transformCreateSLOFormToCreateSLOInput } from '../../helpers/process_slo_form_values';
import { CreateSLOForm } from '../../types';
import { useKibana } from '../../../../../utils/kibana_react';
import { useFetchSloInspect } from '../../../../../hooks/use_fetch_slo_inspect';
import { usePluginContext } from '../../../../../hooks/use_plugin_context';
import { transformCreateSLOFormToCreateSLOInput } from '../../../helpers/process_slo_form_values';
import { CreateSLOForm } from '../../../types';
import { CodeBlockAccordion } from './code_block_accordion';
import { LoadingState } from './loading_state';
import { RequestCodeViewer } from './req_code_viewer';

interface Props {
slo?: GetSLOResponse;
Expand Down Expand Up @@ -180,6 +178,29 @@ function SLOInspect({ slo, disabled }: Props) {
)}
json={inspectSloData.temporaryDoc}
/>
<EuiSpacer size="s" />

<CodeBlockAccordion
id="rollupTransformQuery"
label={i18n.translate(
'xpack.slo.sLOInspect.codeBlockAccordion.transformQueryLabel',
{ defaultMessage: 'Rollup Transform query composite' }
)}
>
<RequestCodeViewer value={inspectSloData.rollUpTransformCompositeQuery} />
</CodeBlockAccordion>

<EuiSpacer size="s" />

<CodeBlockAccordion
id="summmaryTransformQuery"
label={i18n.translate(
'xpack.slo.sLOInspect.codeBlockAccordion.summaryTransformQueryLabel',
{ defaultMessage: 'Summary Transform query composite' }
)}
>
<RequestCodeViewer value={inspectSloData.summaryTransformCompositeQuery} />
</CodeBlockAccordion>
</>
)}
</EuiFlyoutBody>
Expand Down Expand Up @@ -230,41 +251,3 @@ function SLOInspect({ slo, disabled }: Props) {
</>
);
}

function CodeBlockAccordion({
id,
label,
json,
extraAction,
}: {
id: string;
label: string;
json: any;
extraAction?: ReactNode;
}) {
return (
<EuiAccordion
id={id}
extraAction={extraAction}
buttonContent={
<EuiTitle size="xs">
<h3>{label}</h3>
</EuiTitle>
}
>
<EuiCodeBlock language="json" fontSize="m" paddingSize="m" isCopyable={true}>
{JSON.stringify(json, null, 2)}
</EuiCodeBlock>
</EuiAccordion>
);
}

export function LoadingState() {
return (
<EuiFlexGroup alignItems="center" justifyContent="center" style={{ height: '100%' }}>
<EuiFlexItem grow={false}>
<EuiLoadingSpinner size="xl" />
</EuiFlexItem>
</EuiFlexGroup>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
} from '../helpers/process_slo_form_values';
import { CreateSLOForm } from '../types';
import { EquivalentApiRequest } from './common/equivalent_api_request';
import { SLOInspectWrapper } from './common/slo_inspect';
import { SLOInspectWrapper } from './common/slo_inspect/slo_inspect';

export interface Props {
slo?: GetSLOResponse;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { TransformPutTransformRequest } from '@elastic/elasticsearch/lib/api/typ
import { ElasticsearchClient, IBasePath, Logger } from '@kbn/core/server';
import { ALL_VALUE, CreateSLOParams, CreateSLOResponse } from '@kbn/slo-schema';
import { v4 as uuidv4 } from 'uuid';
import { getTransformQueryComposite } from './utils/get_transform_compite_query';
import {
getSLOSummaryPipelineId,
getSLOSummaryTransformId,
Expand Down Expand Up @@ -94,23 +95,27 @@ export class CreateSLO {
rollUpTransform: TransformPutTransformRequest;
summaryTransform: TransformPutTransformRequest;
temporaryDoc: Record<string, any>;
rollUpTransformCompositeQuery: string;
summaryTransformCompositeQuery: string;
} {
const slo = this.toSLO(params);
validateSLO(slo);

const rollUpTransform = this.transformManager.inspect(slo);
const pipeline = getSLOSummaryPipelineTemplate(slo, this.spaceId, this.basePath);

const summaryTransform = this.summaryTransformManager.inspect(slo);

const temporaryDoc = createTempSummaryDocument(slo, this.spaceId, this.basePath);

return {
slo,
pipeline,
temporaryDoc,
summaryTransform,
rollUpTransform,
slo,
// @ts-expect-error there is some issue with deprecated types being used in es types
rollUpTransformCompositeQuery: getTransformQueryComposite(rollUpTransform),
// @ts-expect-error there is some issue with deprecated types being used in es types
summaryTransformCompositeQuery: getTransformQueryComposite(summaryTransform),
};
}

Expand Down
Loading

0 comments on commit 5118fff

Please sign in to comment.