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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
/////////////////////////////////////////////////////////////
//
// pgAdmin 4 - PostgreSQL Tools
//
// Copyright (C) 2013 - 2024, The pgAdmin Development Team
// This software is released under the PostgreSQL Licence
//
//////////////////////////////////////////////////////////////
import { Box, Tab, Tabs } from '@mui/material';
import React from 'react';
import _ from 'lodash';
import Graphical from './Graphical';
import TabPanel from '../components/TabPanel';
import gettext  from 'sources/gettext';
import ImageMapper from './ImageMapper';
import { makeStyles } from '@mui/styles';
import Analysis from './Analysis';
import ExplainStatistics from './ExplainStatistics';
import PropTypes from 'prop-types';
import EmptyPanelMessage from '../components/EmptyPanelMessage';

const useStyles = makeStyles((theme)=>({
  tabPanel: {
    padding: 0,
    backgroundColor: theme.palette.background.default,
  },
}));

// Some predefined constants used to calculate image location and its border
let pWIDTH = 100;
let pHEIGHT = 100;
let offsetX = 200,
  offsetY = 60;
let xMargin = 25,
  yMargin = 25;

const DEFAULT_ARROW_SIZE = 2;

function nodeExplainTableData(_planData, _ctx) {
  let node_info,
    display_text = [],
    tooltip = [],
    node_extra_info = [],
    info = _ctx.explainTable;

  // Display: <NODE>[ using <Index> ] [ on <Schema>.<Table>[ as <Alias>]]

  if (/Scan/.test(_planData['Node Type'])) {
    display_text.push(_planData['Node Type']);
    tooltip.push(_planData['Node Type']);
  } else {
    display_text.push(_planData['image_text']);
    tooltip.push(_planData['image_text']);
  }
  node_info = tooltip.join('');

  if (typeof(_planData['Index Name']) !== 'undefined') {
    display_text.push(' using ');
    tooltip.push(' using ');
    display_text.push(_.escape(_planData['Index Name']));
    tooltip.push(_planData['Index Name']);
  }

  if (typeof(_planData['Relation Name']) !== 'undefined') {
    display_text.push(' on ');
    tooltip.push(' on ');
    if (typeof(_planData['Schema']) !== 'undefined') {
      display_text.push(_.escape(_planData['Schema']));
      tooltip.push(_planData['Schema']);
      display_text.push('.');
      tooltip.push('.');
    }
    display_text.push(_.escape(_planData['Relation Name']));
    tooltip.push(_planData['Relation Name']);

    if (typeof(_planData['Alias']) !== 'undefined') {
      display_text.push(' as ');
      tooltip.push(' as ');
      display_text.push(_.escape(_planData['Alias']));
      tooltip.push(_.escape(_planData['Alias']));
    }
  }

  if (
    typeof(_planData['Plan Rows']) !== 'undefined' &&
    typeof(_planData['Plan Width']) !== 'undefined'
  ) {
    let cost = [
      ' (cost=',
      (typeof(_planData['Startup Cost']) !== 'undefined' ?
        _planData['Startup Cost'] : ''),
      '..',
      (typeof(_planData['Total Cost']) !== 'undefined' ?
        _planData['Total Cost'] : ''),
      ' rows=',
      _planData['Plan Rows'],
      ' width=',
      _planData['Plan Width'],
      ')',
    ].join('');
    display_text.push(cost);
    tooltip.push(cost);
  }

  if (
    typeof(_planData['Actual Startup Time']) !== 'undefined' ||
    typeof(_planData['Actual Total Time']) !== 'undefined' ||
    typeof(_planData['Actual Rows']) !== 'undefined'
  ) {
    let actual = [
      ' (',
      (typeof(_planData['Actual Startup Time']) !== 'undefined' ?
        ('actual=' + _planData['Actual Startup Time']) + '..' : ''
      ),
      (typeof(_planData['Actual Total Time']) !== 'undefined' ?
        _planData['Actual Total Time'] + ' ' : ''
      ),
      (typeof(_planData['Actual Rows']) !== 'undefined' ?
        ('rows=' + _planData['Actual Rows']) : ''
      ),
      (typeof(_planData['Actual Loops']) !== 'undefined' ?
        (' loops=' + _planData['Actual Loops']) : ''
      ),
      ')',
    ].join('');

    display_text.push(actual);
    tooltip.push(actual);
  }

  if ('Join Filter' in _planData) {
    node_extra_info.push(
      '<strong>' + gettext('Join Filter') + '</strong>: ' + _.escape(_planData['Join Filter'])
    );
  }

  if ('Filter' in _planData) {
    node_extra_info.push('<strong>' + gettext('Filter') + '</strong>: ' + _.escape(_planData['Filter']));
  }

  if ('Index Cond' in _planData) {
    node_extra_info.push('<strong>' + gettext('Index Cond') + '</strong>: ' + _.escape(_planData['Index Cond']));
  }

  if ('Hash Cond' in _planData) {
    node_extra_info.push('<strong>' + gettext('Hash Cond') + '</strong>: ' + _.escape(_planData['Hash Cond']));
  }

  if ('Rows Removed by Filter' in _planData) {
    node_extra_info.push(
      '<strong>' + gettext('Rows Removed by Filter') + '</strong>: ' +
        _.escape(_planData['Rows Removed by Filter'])
    );
  }

  if ('Peak Memory Usage' in _planData) {
    let buffer = [
      '<strong>' + gettext('Buckets') + '</strong>:', _.escape(_planData['Hash Buckets']),
      '<strong>' + gettext('Batches') + '</strong>:', _.escape(_planData['Hash Batches']),
      '<strong>' + gettext('Memory Usage') + '</strong>:', _.escape(_planData['Peak Memory Usage']), 'kB',
    ].join(' ');
    node_extra_info.push(buffer);
  }

  if ('Recheck Cond' in _planData) {
    node_extra_info.push('<strong>' + gettext('Recheck Cond') + '</strong>: ' + _planData['Recheck Cond']);
  }

  if ('Exact Heap Blocks' in _planData) {
    node_extra_info.push('<strong>' + gettext('Heap Blocks') + '</strong>: exact=' + _planData['Exact Heap Blocks']);
  }

  info.rows.push({
    data: _planData,
    display_text: display_text.join(''),
    tooltip_text: tooltip.join(''),
    node_extra_info: node_extra_info,
  });

  if (typeof(_planData['exclusive_flag']) !== 'undefined') {
    info.show_timings = true;
  }

  if (typeof(_planData['rowsx_flag']) !== 'undefined') {
    info.show_rowsx = true;
  }

  if (typeof(_planData['Actual Loops']) !== 'undefined') {
    info.show_rows = true;
  }

  if (typeof(_planData['Plan Rows']) !== 'undefined') {
    info.show_plan_rows = true;
  }

  if (typeof(_planData['total_time']) !== 'undefined') {
    info.total_time = _planData['total_time'];
  }

  let node;

  if (typeof(_planData['Relation Name']) !== 'undefined') {
    let relationName = (
        typeof(_planData['Schema']) !== 'undefined' ?
          (_planData['Schema'] + '.') : ''
      ) + _planData['Relation Name'],
      table = info.statistics.tables[relationName] || {
        name: relationName,
        count: 0,
        sum_of_times: 0,
        nodes: {},
      };

    node = table.nodes[node_info] || {
      name: node_info,
      count: 0,
      sum_of_times: 0,
    };

    table.count++;
    table.sum_of_times += _planData['exclusive'];
    node.count++;
    node.sum_of_times += _planData['exclusive'];

    table.nodes[node_info] = node;
    info.statistics.tables[relationName] = table;
  }

  node = info.statistics.nodes[node_info] || {
    name: node_info,
    count: 0,
    sum_of_times: 0,
  };

  node.count++;
  node.sum_of_times += _planData['exclusive'];
  info.statistics.nodes[node_info] = node;
}

function parseExplainTableData(plan, ctx) {
  nodeExplainTableData(plan, ctx);

  plan['Plans']?.map((p)=>{
    parseExplainTableData(p, ctx);
  });
}

function parsePlan(data, ctx) {
  let idx = 1,
    lvl = data.level = data.level || [idx],
    plans = [],
    nodeType = data['Node Type'],
    // Calculating relative xpos of current node from top node
    xpos = data.xpos = data.xpos - pWIDTH,
    // Calculating relative ypos of current node from top node
    ypos = data.ypos,
    maxChildWidth = 0;

  ctx.totalNodes++;
  ctx.explainTable.total_time = data['total_time'] || data['Actual Total Time'];

  data['_serial'] = ctx.totalNodes;
  data['width'] = pWIDTH;
  data['height'] = pHEIGHT;

  // Calculate arrow width according to cost of a particular plan
  let arrowSize = DEFAULT_ARROW_SIZE;
  let startCost = data['Startup Cost'],
    totalCost = data['Total Cost'];
  if (startCost != undefined && totalCost != undefined) {
    arrowSize = Math.round(Math.log((startCost + totalCost) / 2 + startCost));
    if (arrowSize < 1) {
      arrowSize = 1;
    } else if (arrowSize > 10) {
      arrowSize = 10;
    }
  }
  data['arr_id'] = _.uniqueId('arr');
  ctx.arrows[data['arr_id']] = arrowSize;
  /*
   * calculating xpos, ypos, width and height if current node is a subplan
   */
  if (data['Parent Relationship'] === 'SubPlan') {
    data['width'] += (xMargin * 2) + (xMargin / 2);
    data['height'] += (yMargin * 2);
    data['ypos'] += yMargin;
    xpos -= xMargin;
    ypos += yMargin;
  }

  if (nodeType.startsWith('(slice'))
    nodeType = nodeType.substring(0, 7);

  // Get the image information for current node
  let mappedImage = (_.isFunction(ImageMapper[nodeType]) &&
      ImageMapper[nodeType].apply(undefined, [data])) ||
    ImageMapper[nodeType] || {
    'image': 'ex_unknown.svg',
    'image_text': nodeType,
  };

  data['image'] = mappedImage['image'];
  data['image_text'] = mappedImage['image_text'];

  if ('Actual Total Time' in data && 'Actual Loops' in data) {
    data['inclusive'] = Math.ceil10(
      data['Actual Total Time'], -3
    );
    data['exclusive'] = data['inclusive'];
    data['inclusive_factor'] =  data['inclusive'] / (
      data['total_time'] || data['Actual Total Time']
    );
    if (data['inclusive_factor'] <= 0.1) {
      data['inclusive_flag'] = '1';
    } else if (data['inclusive_factor'] < 0.5) {
      data['inclusive_flag'] = '2';
    } else if (data['inclusive_factor'] <= 0.9) {
      data['inclusive_flag'] = '3';
    } else {
      data['inclusive_flag'] = '4';
    }
  }

  if ('Actual Rows' in data && 'Plan Rows' in data) {
    if (
      data['Actual Rows'] === 0 || data['Actual Rows'] > data['Plan Rows']
    ) {
      data['rowsx'] = data['Plan Rows'] === 0 ? 0 :
        (data['Actual Rows'] / data['Plan Rows']);
      data['rowsx_direction'] = 'negative';
    } else {
      data['rowsx'] = data['Actual Rows'] === 0 ? 0 : (
        data['Plan Rows'] / data['Actual Rows']
      );
      data['rowsx_direction'] = 'positive';
    }
    if (data['rowsx'] <= 10) {
      data['rowsx_flag'] = '1';
    } else if (data['rowsx'] <= 100 ) {
      data['rowsx_flag'] = '2';
    } else if (data['rowsx'] <= 1000 ) {
      data['rowsx_flag'] = '3';
    } else {
      data['rowsx_flag'] = '4';
    }
    if('loops' in data) {
      data['rowsx'] = Math.ceil10(data['rowsx'] / data['loops'] || 1, -2);
    } else {
      data['rowsx'] = Math.ceil10(data['rowsx'], -2);
    }
  }

  // Start calculating xpos, ypos, width and height for child plans if any
  if ('Plans' in data) {
    data['width'] += offsetX;

    data['Plans'].forEach(function(p) {
      let level = _.clone(lvl);
      level.push(idx);

      let plan = parsePlan({
        ...p,
        'level': level,
        xpos: xpos - offsetX,
        ypos: ypos,
        total_time: data['total_time'] || data['Actual Total Time'],
        parent_node: lvl.join('_'),
        loops: data['Actual Loops']
      }, ctx);

      if (maxChildWidth < plan.width) {
        maxChildWidth = plan.width;
      }

      if ('exclusive' in data) {
        if (plan.inclusive < data['exclusive']) {
          data['exclusive'] -= plan.inclusive;
        }
      }

      let childHeight = plan.height;

      if (idx !== 1) {
        data['height'] = data['height'] + childHeight + offsetY;
      } else if (childHeight > data['height']) {
        data['height'] = childHeight;
      }
      ypos += childHeight + offsetY;

      plans.push(plan);
      idx++;
    });
  } else if('loops' in data && 'exclusive' in data) {
    data['inclusive'] = Math.ceil10(data['Actual Total Time'] / data['loops'] || 1, -3);
    data['exclusive'] = data['inclusive'];
  }

  if ('exclusive' in data) {
    data['exclusive'] = Math.ceil10(data['exclusive'], -3);
    data['exclusive_factor'] = (
      data['exclusive'] / (data['total_time'] || data['Actual Total Time'])
    );
    if (data['exclusive_factor'] <= 0.1) {
      data['exclusive_flag'] = '1';
    } else if (data['exclusive_factor'] < 0.5) {
      data['exclusive_flag'] = '2';
    } else if (data['exclusive_factor'] <= 0.9) {
      data['exclusive_flag'] = '3';
    } else {
      data['exclusive_flag'] = '4';
    }
  }

  // Final Width and Height of current node
  data['width'] += maxChildWidth;
  data['Plans'] = plans;

  return data;
}

function parsePlanData(data, ctx) {
  let retPlan = {};
  if(data) {
    if ('Plan' in data) {
      let plan = parsePlan({
        ...data['Plan'],
        xpos: 0,
        ypos: 0,
        loops: 1,
      }, ctx);
      retPlan['Plan'] = plan;
      retPlan['xpos'] = 0;
      retPlan['ypos'] = 0;
      retPlan['width'] = plan.width + (xMargin * 2);
      retPlan['height'] = plan.height + (yMargin * 4);
    }

    retPlan['Statistics'] = {
      'JIT': [],
      'Triggers': [],
      'Summary': {},
    };
    if ('JIT' in data) {
      retPlan['Statistics']['JIT'] = retPlan['JIT'];
    }
    if ('Triggers' in data) {
      retPlan['Statistics']['Triggers'] = retPlan['JITriggersT'];
    }
    if ('Settings' in data) {
      retPlan['Statistics']['Settings'] = data['Settings'];
    }
    let summKeys = ['Planning Time', 'Execution Time'],
      summary = {};

    summKeys.forEach((key)=>{
      if (key in data) {
        summary[key] = data[key];
      }
    });

    retPlan['Statistics']['Summary'] = summary;

    parseExplainTableData(retPlan['Plan'], ctx);
  }
  return retPlan;
}

export default function Explain({plans=[]}) {
  const classes = useStyles();
  const [tabValue, setTabValue] = React.useState(0);

  let ctx = React.useRef({});
  let planData = React.useMemo(()=>{
    ctx.current = {
      totalNodes: 0,
      totalDownloadedNodes: 0,
      isDownloaded: 0,
      explainTable: {
        rows: [],
        statistics: {
          tables: {},
          nodes: {},
        },
      },
      arrows: {},
    };
    return plans && parsePlanData(plans[0], ctx.current);
  }, [plans]);

  if(_.isEmpty(plans)) {
    return <Box height="100%" display="flex" flexDirection="column">
      <EmptyPanelMessage text={gettext('Use Explain/Explain analyze button to generate the plan for a query. Alternatively, you can also execute "EXPLAIN (FORMAT JSON) [QUERY]".')} />
    </Box>;
  }
  return (
    <Box height="100%" display="flex" flexDirection="column">
      <Box>
        <Tabs
          value={tabValue}
          onChange={(_e, selTabValue) => {
            setTabValue(selTabValue);
          }}
          // indicatorColor="primary"
          variant="scrollable"
          scrollButtons="auto"
          action={(ref)=>ref?.updateIndicator()}
        >
          <Tab label="Graphical" />
          <Tab label="Analysis" />
          <Tab label="Statistics" />
        </Tabs>
      </Box>
      <TabPanel value={tabValue} index={0} classNameRoot={classes.tabPanel}>
        <Graphical planData={planData} ctx={ctx.current}/>
      </TabPanel>
      <TabPanel value={tabValue} index={1} classNameRoot={classes.tabPanel}>
        <Analysis explainTable={ctx.current.explainTable} />
      </TabPanel>
      <TabPanel value={tabValue} index={2} classNameRoot={classes.tabPanel}>
        <ExplainStatistics explainTable={ctx.current.explainTable} />
      </TabPanel>
    </Box>
  );
}

Explain.propTypes = {
  plans: PropTypes.array,
};