-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathprepareQueryExplain.ts
132 lines (111 loc) · 3.29 KB
/
prepareQueryExplain.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import {
Link,
GraphNode,
TopologyNodeDataStats,
TopologyNodeDataStatsSection,
ExplainPlanNodeData,
TopologyNodeDataStatsItem,
} from '@yandex-cloud/paranoid';
interface PlanOperator {
Name: string;
[key: string]: any;
}
export interface Plan {
PlanNodeId: number;
'Node Type': string;
Plans?: Plan[];
Operators?: PlanOperator[];
Tables?: string[];
PlanNodeType?: string;
[key: string]: any;
}
export interface RootPlan {
Plan: Plan;
}
const CONNECTION_NODE_META_FIELDS = new Set(['PlanNodeId', 'PlanNodeType', 'Node Type', 'Plans']);
function prepareStats(plan: Plan) {
const stats: TopologyNodeDataStats[] = [];
if (plan.Operators) {
const operatorsSections: TopologyNodeDataStatsSection[] = [];
for (const operator of plan.Operators) {
const section: TopologyNodeDataStatsSection = {name: operator.Name, items: []};
for (const [name, data] of Object.entries(operator)) {
if (name === 'Name') {
continue;
}
const value = Array.isArray(data) ? data.join(', ') : data;
section.items.push({name, value});
}
operatorsSections.push(section);
}
stats.push({
group: 'Operators',
stats: operatorsSections,
});
}
if (plan.PlanNodeType === 'Connection') {
const attrStats: TopologyNodeDataStatsItem[] = [];
for (const [key, value] of Object.entries(plan)) {
if (CONNECTION_NODE_META_FIELDS.has(key)) {
continue;
}
attrStats.push({name: key, value: String(value)});
}
if (attrStats.length > 0) {
stats.push({
group: 'Attributes',
stats: attrStats,
});
}
}
return stats;
}
function getNodeType(plan: Plan) {
switch (plan.PlanNodeType) {
case 'Connection':
return 'connection';
case 'ResultSet':
return 'result';
case 'Query':
return 'query';
default:
return 'stage';
}
}
export function preparePlan(plan: Plan) {
const nodes: GraphNode[] = [];
const links: Link[] = [];
function parsePlans(plans: Plan[] = [], from: string) {
plans.forEach((p) => {
const node: GraphNode<ExplainPlanNodeData> = {
name: String(p.PlanNodeId),
data: {
id: p.PlanNodeId,
type: getNodeType(p),
name: p['Node Type'],
operators: p.Operators?.map((o) => o.Name),
stats: prepareStats(p),
tables: p.Tables,
},
};
nodes.push(node);
links.push({from, to: node.name});
parsePlans(p.Plans, node.name);
});
}
const rootPlan = plan;
const rootNode: GraphNode<ExplainPlanNodeData> = {
name: String(rootPlan.PlanNodeId),
data: {
id: rootPlan.PlanNodeId,
type: getNodeType(rootPlan),
name: rootPlan['Node Type'],
},
};
nodes.push(rootNode);
parsePlans(rootPlan.Plans, rootNode.name);
return {
nodes,
links,
};
}