|
1 | | -import type {ExplainPlanNodeData, GraphNode, Link} from '@gravity-ui/paranoid'; |
2 | | - |
3 | 1 | import type {ExecuteResponse, ExplainResponse} from '../../../types/api/query'; |
4 | | -import {preparePlan, prepareSimplifiedPlan} from '../../../utils/prepareQueryExplain'; |
5 | | -import {parseQueryAPIResponse, parseQueryExplainPlan} from '../../../utils/query'; |
| 2 | +import {parseQueryAPIResponse} from '../../../utils/query'; |
6 | 3 |
|
| 4 | +import {preparePlanData} from './preparePlanData'; |
7 | 5 | import type {PreparedQueryData} from './types'; |
8 | 6 |
|
9 | | -const explainVersions = { |
10 | | - v2: '0.2', |
11 | | -}; |
12 | | - |
13 | | -const supportedExplainQueryVersions = Object.values(explainVersions); |
14 | | - |
15 | 7 | export function prepareQueryData( |
16 | 8 | response: ExplainResponse | ExecuteResponse | null, |
17 | 9 | ): PreparedQueryData { |
18 | 10 | const result = parseQueryAPIResponse(response); |
19 | 11 | const {plan: rawPlan, stats} = result; |
20 | 12 |
|
21 | | - if (rawPlan) { |
22 | | - const {tables, meta, Plan, SimplifiedPlan} = parseQueryExplainPlan(rawPlan); |
23 | | - |
24 | | - if (supportedExplainQueryVersions.indexOf(meta.version) === -1) { |
25 | | - // Do not prepare plan for not supported versions |
26 | | - return { |
27 | | - ...result, |
28 | | - preparedPlan: { |
29 | | - pristine: rawPlan, |
30 | | - version: meta.version, |
31 | | - }, |
32 | | - }; |
33 | | - } |
34 | | - |
35 | | - let links: Link[] = []; |
36 | | - let nodes: GraphNode<ExplainPlanNodeData>[] = []; |
37 | | - |
38 | | - if (Plan) { |
39 | | - const preparedPlan = preparePlan(Plan); |
40 | | - links = preparedPlan.links; |
41 | | - nodes = preparedPlan.nodes; |
42 | | - } |
43 | | - let preparedSimplifiedPlan; |
44 | | - if (SimplifiedPlan) { |
45 | | - preparedSimplifiedPlan = prepareSimplifiedPlan([SimplifiedPlan]); |
46 | | - } |
47 | | - |
48 | | - return { |
49 | | - ...result, |
50 | | - preparedPlan: { |
51 | | - links, |
52 | | - nodes, |
53 | | - tables, |
54 | | - version: meta.version, |
55 | | - pristine: rawPlan, |
56 | | - }, |
57 | | - simplifiedPlan: {plan: preparedSimplifiedPlan, pristine: SimplifiedPlan}, |
58 | | - }; |
59 | | - } |
60 | | - |
61 | | - const planFromStats = stats?.Executions?.[0]?.TxPlansWithStats?.[0]; |
62 | | - if (planFromStats) { |
63 | | - try { |
64 | | - const planWithStats = JSON.parse(planFromStats); |
65 | | - return { |
66 | | - ...result, |
67 | | - preparedPlan: {...preparePlan(planWithStats), pristine: planWithStats}, |
68 | | - }; |
69 | | - } catch {} |
70 | | - } |
71 | | - |
72 | | - return result; |
| 13 | + const {simplifiedPlan, ...planData} = preparePlanData(rawPlan, stats); |
| 14 | + return { |
| 15 | + ...result, |
| 16 | + preparedPlan: Object.keys(planData).length > 0 ? planData : undefined, |
| 17 | + simplifiedPlan, |
| 18 | + }; |
73 | 19 | } |
0 commit comments