|
| 1 | +import React from 'react'; |
| 2 | + |
| 3 | +import {Label} from '@gravity-ui/uikit'; |
| 4 | + |
| 5 | +import {YDBSyntaxHighlighter} from '../../../../../components/SyntaxHighlighter/YDBSyntaxHighlighter'; |
| 6 | +import {YDBDefinitionList} from '../../../../../components/YDBDefinitionList/YDBDefinitionList'; |
| 7 | +import type {YDBDefinitionListItem} from '../../../../../components/YDBDefinitionList/YDBDefinitionList'; |
| 8 | +import {streamingQueriesApi} from '../../../../../store/reducers/streamingQuery/streamingQuery'; |
| 9 | +import type {ErrorResponse} from '../../../../../types/api/query'; |
| 10 | +import type {TEvDescribeSchemeResult} from '../../../../../types/api/schema'; |
| 11 | +import type {IQueryResult} from '../../../../../types/store/query'; |
| 12 | +import { |
| 13 | + getStringifiedData, |
| 14 | + stripIndentByFirstLine, |
| 15 | + trimOuterEmptyLines, |
| 16 | +} from '../../../../../utils/dataFormatters/dataFormatters'; |
| 17 | +import {isErrorResponse} from '../../../../../utils/query'; |
| 18 | +import {ResultIssuesModal} from '../../../Query/Issues/Issues'; |
| 19 | +import {getEntityName} from '../../../utils'; |
| 20 | + |
| 21 | +import i18n from './i18n'; |
| 22 | + |
| 23 | +interface StreamingQueryProps { |
| 24 | + data?: TEvDescribeSchemeResult; |
| 25 | + database: string; |
| 26 | + path: string; |
| 27 | +} |
| 28 | + |
| 29 | +/** Displays overview for StreamingQuery EPathType */ |
| 30 | +export function StreamingQueryInfo({data, database, path}: StreamingQueryProps) { |
| 31 | + const entityName = getEntityName(data?.PathDescription); |
| 32 | + |
| 33 | + if (!data) { |
| 34 | + return ( |
| 35 | + <div className="error"> |
| 36 | + {i18n('alert_no-data')} {entityName} |
| 37 | + </div> |
| 38 | + ); |
| 39 | + } |
| 40 | + |
| 41 | + const {data: sysData} = streamingQueriesApi.useGetStreamingQueryInfoQuery( |
| 42 | + {database, path}, |
| 43 | + {skip: !database || !path}, |
| 44 | + ); |
| 45 | + |
| 46 | + const items = prepareStreamingQueryItems(sysData); |
| 47 | + |
| 48 | + return <YDBDefinitionList title={entityName} items={items} />; |
| 49 | +} |
| 50 | + |
| 51 | +const STATE_THEME_MAP: Record<string, React.ComponentProps<typeof Label>['theme']> = { |
| 52 | + CREATING: 'info', |
| 53 | + CREATED: 'normal', |
| 54 | + STARTING: 'info', |
| 55 | + RUNNING: 'success', |
| 56 | + STOPPING: 'info', |
| 57 | + STOPPED: 'normal', |
| 58 | + COMPLETED: 'success', |
| 59 | + SUSPENDED: 'warning', |
| 60 | + FAILED: 'danger', |
| 61 | +}; |
| 62 | + |
| 63 | +function StateLabel({state}: {state?: string}) { |
| 64 | + if (!state) { |
| 65 | + return null; |
| 66 | + } |
| 67 | + |
| 68 | + const theme = STATE_THEME_MAP[state] ?? 'normal'; |
| 69 | + |
| 70 | + return <Label theme={theme}>{state}</Label>; |
| 71 | +} |
| 72 | + |
| 73 | +function prepareStreamingQueryItems(sysData?: IQueryResult): YDBDefinitionListItem[] { |
| 74 | + if (!sysData) { |
| 75 | + return []; |
| 76 | + } |
| 77 | + |
| 78 | + const info: YDBDefinitionListItem[] = []; |
| 79 | + const state = getStringifiedData(sysData.resultSets?.[0]?.result?.[0]?.State); |
| 80 | + |
| 81 | + const queryText = getStringifiedData(sysData.resultSets?.[0]?.result?.[0]?.Text); |
| 82 | + let normalizedQueryText = trimOuterEmptyLines(queryText); |
| 83 | + normalizedQueryText = stripIndentByFirstLine(normalizedQueryText); |
| 84 | + |
| 85 | + const errorRaw = sysData.resultSets?.[0]?.result?.[0]?.Error; |
| 86 | + |
| 87 | + // We use custom error check, because error type can be non-standard |
| 88 | + const errorData = parseErrorData(errorRaw); |
| 89 | + |
| 90 | + info.push({ |
| 91 | + name: i18n('field_query-state'), |
| 92 | + content: <StateLabel state={state} />, |
| 93 | + }); |
| 94 | + |
| 95 | + if (errorData) { |
| 96 | + info.push({ |
| 97 | + name: i18n('field_query-error'), |
| 98 | + content: <ResultIssuesModal data={errorData} />, |
| 99 | + }); |
| 100 | + } |
| 101 | + |
| 102 | + info.push({ |
| 103 | + name: i18n('field_query-text'), |
| 104 | + copyText: normalizedQueryText, |
| 105 | + content: normalizedQueryText ? ( |
| 106 | + <YDBSyntaxHighlighter language="yql" text={normalizedQueryText} /> |
| 107 | + ) : null, |
| 108 | + }); |
| 109 | + |
| 110 | + return info; |
| 111 | +} |
| 112 | + |
| 113 | +function parseErrorData(raw: unknown): ErrorResponse | string | undefined { |
| 114 | + if (typeof raw === 'string') { |
| 115 | + try { |
| 116 | + const parsed = JSON.parse(raw); |
| 117 | + return isErrorResponse(parsed) ? parsed : undefined; |
| 118 | + } catch { |
| 119 | + return raw; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + if (isErrorResponse(raw)) { |
| 124 | + return raw; |
| 125 | + } |
| 126 | + |
| 127 | + return undefined; |
| 128 | +} |
0 commit comments