Passed
Push — develop ( 7eecac...7414a0 )
by Felipe
03:57
created

InfoController::_printChildren()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 34
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 34
rs 9.6
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * PHPPgAdmin v6.0.0-beta.50
5
 */
6
7
namespace PHPPgAdmin\Controller;
8
9
use PHPPgAdmin\Decorators\Decorator;
10
11
/**
12
 * Base controller class.
13
 *
14
 * @package PHPPgAdmin
15
 */
16
class InfoController extends BaseController
17
{
18
    public $controller_title = 'strtables';
19
20
    /**
21
     * Default method to render the controller according to the action parameter.
22
     */
23
    public function render()
24
    {
25
        $this->printHeader($this->headerTitle('', '', $_REQUEST['table'].' - '.$this->lang['strinfo']));
26
        $this->printBody();
27
28
        switch ($this->action) {
29
            default:
30
                $this->doDefault();
31
32
                break;
33
        }
34
35
        $this->printFooter();
36
    }
37
38
    /**
39
     * List all the information on the table.
40
     *
41
     * @param string $msg
42
     *
43
     * @return string|void
44
     */
45
    public function doDefault($msg = '')
46
    {
47
        $data = $this->misc->getDatabaseAccessor();
48
49
        $this->printTrail('table');
50
        $this->printTabs('table', 'info');
51
        $this->printMsg($msg);
52
53
        // common params for printVal
54
        $this->shownull = ['null' => true];
55
56
        // Fetch info
57
        $referrers      = $data->getReferrers($_REQUEST['table']);
58
        $parents        = $data->getTableParents($_REQUEST['table']);
59
        $children       = $data->getTableChildren($_REQUEST['table']);
60
        $tablestatstups = $data->getStatsTableTuples($_REQUEST['table']);
61
        $tablestatsio   = $data->getStatsTableIO($_REQUEST['table']);
62
        $indexstatstups = $data->getStatsIndexTuples($_REQUEST['table']);
63
        $indexstatsio   = $data->getStatsIndexIO($_REQUEST['table']);
64
65
        // Check that there is some info
66
        if (($referrers === -99 || ($referrers !== -99 && 0 == $referrers->recordCount()))
67
            && 0 == $parents->recordCount() && 0 == $children->recordCount()
68
            && (0 == $tablestatstups->recordCount() && 0 == $tablestatsio->recordCount()
69
                && 0 == $indexstatstups->recordCount() && 0 == $indexstatsio->recordCount())) {
70
            $this->printMsg($this->lang['strnoinfo']);
71
72
            return;
73
        }
74
        // Referring foreign tables
75
        if ($referrers !== -99 && $referrers->recordCount() > 0) {
76
            $this->_printReferring($referrers);
77
        }
78
79
        // Parent tables
80
        if ($parents->recordCount() > 0) {
81
            $this->_printParents($parents);
82
        }
83
84
        // Child tables
85
        if ($children->recordCount() > 0) {
86
            $this->_printChildren($children);
87
        }
88
89
        // Row performance
90
        if ($tablestatstups->recordCount() > 0) {
91
            $this->_printTablestatstups($tablestatstups);
92
        }
93
94
        // I/O performance
95
        if ($tablestatsio->recordCount() > 0) {
96
            $this->_printTablestatsio($tablestatsio);
97
        }
98
99
        // Index row performance
100
        if ($indexstatstups->recordCount() > 0) {
101
            $this->_printIndexstatstups($indexstatstups);
102
        }
103
104
        // Index I/0 performance
105
        if ($indexstatsio->recordCount() > 0) {
106
            $this->_printIndexstatsio($indexstatsio);
107
        }
108
    }
109
110
    private function _printChildren($children)
111
    {
112
        echo "<h3>{$this->lang['strchildtables']}</h3>".PHP_EOL;
113
114
        $columns = [
115
            'schema'  => [
116
                'title' => $this->lang['strschema'],
117
                'field' => Decorator::field('nspname'),
118
            ],
119
            'table'   => [
120
                'title' => $this->lang['strtable'],
121
                'field' => Decorator::field('relname'),
122
            ],
123
            'actions' => [
124
                'title' => $this->lang['stractions'],
125
            ],
126
        ];
127
128
        $actions = [
129
            'properties' => [
130
                'content' => $this->lang['strproperties'],
131
                'attr'    => [
132
                    'href' => [
133
                        'url'     => 'tblproperties',
134
                        'urlvars' => [
135
                            'schema' => Decorator::field('nspname'),
136
                            'table'  => Decorator::field('relname'),
137
                        ],
138
                    ],
139
                ],
140
            ],
141
        ];
142
143
        echo $this->printTable($children, $columns, $actions, 'info-children', $this->lang['strnodata']);
144
    }
145
146
    private function _printTablestatstups($tablestatstups)
147
    {
148
        echo "<h3>{$this->lang['strrowperf']}</h3>".PHP_EOL;
149
150
        echo '<table>'.PHP_EOL;
151
        echo "\t<tr>".PHP_EOL;
152
        echo "\t\t<th class=\"data\" colspan=\"2\">{$this->lang['strsequential']}</th>".PHP_EOL;
153
        echo "\t\t<th class=\"data\" colspan=\"2\">{$this->lang['strindex']}</th>".PHP_EOL;
154
        echo "\t\t<th class=\"data\" colspan=\"3\">{$this->lang['strrows2']}</th>".PHP_EOL;
155
        echo "\t</tr>".PHP_EOL;
156
        echo "\t<tr>".PHP_EOL;
157
        echo "\t\t<th class=\"data\">{$this->lang['strscan']}</th>".PHP_EOL;
158
        echo "\t\t<th class=\"data\">{$this->lang['strread']}</th>".PHP_EOL;
159
        echo "\t\t<th class=\"data\">{$this->lang['strscan']}</th>".PHP_EOL;
160
        echo "\t\t<th class=\"data\">{$this->lang['strfetch']}</th>".PHP_EOL;
161
        echo "\t\t<th class=\"data\">{$this->lang['strinsert']}</th>".PHP_EOL;
162
        echo "\t\t<th class=\"data\">{$this->lang['strupdate']}</th>".PHP_EOL;
163
        echo "\t\t<th class=\"data\">{$this->lang['strdelete']}</th>".PHP_EOL;
164
        echo "\t</tr>".PHP_EOL;
165
        $i = 0;
166
167
        while (!$tablestatstups->EOF) {
168
            $id = (0 == ($i % 2) ? '1' : '2');
169
            echo "\t<tr class=\"data{$id}\">".PHP_EOL;
170
            echo "\t\t<td>", $this->misc->printVal($tablestatstups->fields['seq_scan'], 'int4', $this->shownull), '</td>'.PHP_EOL;
171
            echo "\t\t<td>", $this->misc->printVal($tablestatstups->fields['seq_tup_read'], 'int4', $this->shownull), '</td>'.PHP_EOL;
172
            echo "\t\t<td>", $this->misc->printVal($tablestatstups->fields['idx_scan'], 'int4', $this->shownull), '</td>'.PHP_EOL;
173
            echo "\t\t<td>", $this->misc->printVal($tablestatstups->fields['idx_tup_fetch'], 'int4', $this->shownull), '</td>'.PHP_EOL;
174
            echo "\t\t<td>", $this->misc->printVal($tablestatstups->fields['n_tup_ins'], 'int4', $this->shownull), '</td>'.PHP_EOL;
175
            echo "\t\t<td>", $this->misc->printVal($tablestatstups->fields['n_tup_upd'], 'int4', $this->shownull), '</td>'.PHP_EOL;
176
            echo "\t\t<td>", $this->misc->printVal($tablestatstups->fields['n_tup_del'], 'int4', $this->shownull), '</td>'.PHP_EOL;
177
            echo "\t</tr>".PHP_EOL;
178
            $tablestatstups->movenext();
179
            ++$i;
180
        }
181
182
        echo '</table>'.PHP_EOL;
183
    }
184
185
    private function _printTablestatsio($tablestatsio)
186
    {
187
        echo "<h3>{$this->lang['strioperf']}</h3>".PHP_EOL;
188
189
        echo '<table>'.PHP_EOL;
190
        echo "\t<tr>".PHP_EOL;
191
        echo "\t\t<th class=\"data\" colspan=\"3\">{$this->lang['strheap']}</th>".PHP_EOL;
192
        echo "\t\t<th class=\"data\" colspan=\"3\">{$this->lang['strindex']}</th>".PHP_EOL;
193
        echo "\t\t<th class=\"data\" colspan=\"3\">{$this->lang['strtoast']}</th>".PHP_EOL;
194
        echo "\t\t<th class=\"data\" colspan=\"3\">{$this->lang['strtoastindex']}</th>".PHP_EOL;
195
        echo "\t</tr>".PHP_EOL;
196
        echo "\t<tr>".PHP_EOL;
197
        echo "\t\t<th class=\"data\">{$this->lang['strdisk']}</th>".PHP_EOL;
198
        echo "\t\t<th class=\"data\">{$this->lang['strcache']}</th>".PHP_EOL;
199
        echo "\t\t<th class=\"data\">{$this->lang['strpercent']}</th>".PHP_EOL;
200
        echo "\t\t<th class=\"data\">{$this->lang['strdisk']}</th>".PHP_EOL;
201
        echo "\t\t<th class=\"data\">{$this->lang['strcache']}</th>".PHP_EOL;
202
        echo "\t\t<th class=\"data\">{$this->lang['strpercent']}</th>".PHP_EOL;
203
        echo "\t\t<th class=\"data\">{$this->lang['strdisk']}</th>".PHP_EOL;
204
        echo "\t\t<th class=\"data\">{$this->lang['strcache']}</th>".PHP_EOL;
205
        echo "\t\t<th class=\"data\">{$this->lang['strpercent']}</th>".PHP_EOL;
206
        echo "\t\t<th class=\"data\">{$this->lang['strdisk']}</th>".PHP_EOL;
207
        echo "\t\t<th class=\"data\">{$this->lang['strcache']}</th>".PHP_EOL;
208
        echo "\t\t<th class=\"data\">{$this->lang['strpercent']}</th>".PHP_EOL;
209
        echo "\t</tr>".PHP_EOL;
210
        $i = 0;
211
212
        while (!$tablestatsio->EOF) {
213
            $id = (0 == ($i % 2) ? '1' : '2');
214
            echo "\t<tr class=\"data{$id}\">".PHP_EOL;
215
216
            $total = $tablestatsio->fields['heap_blks_hit'] + $tablestatsio->fields['heap_blks_read'];
217
            if ($total > 0) {
218
                $percentage = round(($tablestatsio->fields['heap_blks_hit'] / $total) * 100);
219
            } else {
220
                $percentage = 0;
221
            }
222
223
            echo "\t\t<td>", $this->misc->printVal($tablestatsio->fields['heap_blks_read'], 'int4', $this->shownull), '</td>'.PHP_EOL;
224
            echo "\t\t<td>", $this->misc->printVal($tablestatsio->fields['heap_blks_hit'], 'int4', $this->shownull), '</td>'.PHP_EOL;
225
            echo "\t\t<td>({$percentage}{$this->lang['strpercent']})</td>".PHP_EOL;
226
227
            $total = $tablestatsio->fields['idx_blks_hit'] + $tablestatsio->fields['idx_blks_read'];
228
            if ($total > 0) {
229
                $percentage = round(($tablestatsio->fields['idx_blks_hit'] / $total) * 100);
230
            } else {
231
                $percentage = 0;
232
            }
233
234
            echo "\t\t<td>", $this->misc->printVal($tablestatsio->fields['idx_blks_read'], 'int4', $this->shownull), '</td>'.PHP_EOL;
235
            echo "\t\t<td>", $this->misc->printVal($tablestatsio->fields['idx_blks_hit'], 'int4', $this->shownull), '</td>'.PHP_EOL;
236
            echo "\t\t<td>({$percentage}{$this->lang['strpercent']})</td>".PHP_EOL;
237
238
            $total = $tablestatsio->fields['toast_blks_hit'] + $tablestatsio->fields['toast_blks_read'];
239
            if ($total > 0) {
240
                $percentage = round(($tablestatsio->fields['toast_blks_hit'] / $total) * 100);
241
            } else {
242
                $percentage = 0;
243
            }
244
245
            echo "\t\t<td>", $this->misc->printVal($tablestatsio->fields['toast_blks_read'], 'int4', $this->shownull), '</td>'.PHP_EOL;
246
            echo "\t\t<td>", $this->misc->printVal($tablestatsio->fields['toast_blks_hit'], 'int4', $this->shownull), '</td>'.PHP_EOL;
247
            echo "\t\t<td>({$percentage}{$this->lang['strpercent']})</td>".PHP_EOL;
248
249
            $total = $tablestatsio->fields['tidx_blks_hit'] + $tablestatsio->fields['tidx_blks_read'];
250
            if ($total > 0) {
251
                $percentage = round(($tablestatsio->fields['tidx_blks_hit'] / $total) * 100);
252
            } else {
253
                $percentage = 0;
254
            }
255
256
            echo "\t\t<td>", $this->misc->printVal($tablestatsio->fields['tidx_blks_read'], 'int4', $this->shownull), '</td>'.PHP_EOL;
257
            echo "\t\t<td>", $this->misc->printVal($tablestatsio->fields['tidx_blks_hit'], 'int4', $this->shownull), '</td>'.PHP_EOL;
258
            echo "\t\t<td>({$percentage}{$this->lang['strpercent']})</td>".PHP_EOL;
259
            echo "\t</tr>".PHP_EOL;
260
            $tablestatsio->movenext();
261
            ++$i;
262
        }
263
264
        echo '</table>'.PHP_EOL;
265
    }
266
267
    private function _printIndexstatstups($indexstatstups)
268
    {
269
        echo "<h3>{$this->lang['stridxrowperf']}</h3>".PHP_EOL;
270
271
        echo '<table>'.PHP_EOL;
272
        echo "\t<tr>".PHP_EOL;
273
        echo "\t\t<th class=\"data\">{$this->lang['strindex']}</th>".PHP_EOL;
274
        echo "\t\t<th class=\"data\">{$this->lang['strscan']}</th>".PHP_EOL;
275
        echo "\t\t<th class=\"data\">{$this->lang['strread']}</th>".PHP_EOL;
276
        echo "\t\t<th class=\"data\">{$this->lang['strfetch']}</th>".PHP_EOL;
277
        echo "\t</tr>".PHP_EOL;
278
        $i = 0;
279
280
        while (!$indexstatstups->EOF) {
281
            $id = (0 == ($i % 2) ? '1' : '2');
282
            echo "\t<tr class=\"data{$id}\">".PHP_EOL;
283
            echo "\t\t<td>", $this->misc->printVal($indexstatstups->fields['indexrelname']), '</td>'.PHP_EOL;
284
            echo "\t\t<td>", $this->misc->printVal($indexstatstups->fields['idx_scan'], 'int4', $this->shownull), '</td>'.PHP_EOL;
285
            echo "\t\t<td>", $this->misc->printVal($indexstatstups->fields['idx_tup_read'], 'int4', $this->shownull), '</td>'.PHP_EOL;
286
            echo "\t\t<td>", $this->misc->printVal($indexstatstups->fields['idx_tup_fetch'], 'int4', $this->shownull), '</td>'.PHP_EOL;
287
            echo "\t</tr>".PHP_EOL;
288
            $indexstatstups->movenext();
289
            ++$i;
290
        }
291
292
        echo '</table>'.PHP_EOL;
293
    }
294
295
    private function _printIndexstatsio($indexstatsio)
296
    {
297
        echo "<h3>{$this->lang['stridxioperf']}</h3>".PHP_EOL;
298
299
        echo '<table>'.PHP_EOL;
300
        echo "\t<tr>".PHP_EOL;
301
        echo "\t\t<th class=\"data\">{$this->lang['strindex']}</th>".PHP_EOL;
302
        echo "\t\t<th class=\"data\">{$this->lang['strdisk']}</th>".PHP_EOL;
303
        echo "\t\t<th class=\"data\">{$this->lang['strcache']}</th>".PHP_EOL;
304
        echo "\t\t<th class=\"data\">{$this->lang['strpercent']}</th>".PHP_EOL;
305
        echo "\t</tr>".PHP_EOL;
306
        $i = 0;
307
308
        while (!$indexstatsio->EOF) {
309
            $id = (0 == ($i % 2) ? '1' : '2');
310
            echo "\t<tr class=\"data{$id}\">".PHP_EOL;
311
            $total = $indexstatsio->fields['idx_blks_hit'] + $indexstatsio->fields['idx_blks_read'];
312
            if ($total > 0) {
313
                $percentage = round(($indexstatsio->fields['idx_blks_hit'] / $total) * 100);
314
            } else {
315
                $percentage = 0;
316
            }
317
318
            echo "\t\t<td>", $this->misc->printVal($indexstatsio->fields['indexrelname']), '</td>'.PHP_EOL;
319
            echo "\t\t<td>", $this->misc->printVal($indexstatsio->fields['idx_blks_read'], 'int4', $this->shownull), '</td>'.PHP_EOL;
320
            echo "\t\t<td>", $this->misc->printVal($indexstatsio->fields['idx_blks_hit'], 'int4', $this->shownull), '</td>'.PHP_EOL;
321
            echo "\t\t<td>({$percentage}{$this->lang['strpercent']})</td>".PHP_EOL;
322
            echo "\t</tr>".PHP_EOL;
323
            $indexstatsio->movenext();
324
            ++$i;
325
        }
326
327
        echo '</table>'.PHP_EOL;
328
    }
329
330
    private function _printParents($parents)
331
    {
332
        echo "<h3>{$this->lang['strparenttables']}</h3>".PHP_EOL;
333
334
        $columns = [
335
            'schema'  => [
336
                'title' => $this->lang['strschema'],
337
                'field' => Decorator::field('nspname'),
338
            ],
339
            'table'   => [
340
                'title' => $this->lang['strtable'],
341
                'field' => Decorator::field('relname'),
342
            ],
343
            'actions' => [
344
                'title' => $this->lang['stractions'],
345
            ],
346
        ];
347
348
        $actions = [
349
            'properties' => [
350
                'content' => $this->lang['strproperties'],
351
                'attr'    => [
352
                    'href' => [
353
                        'url'     => 'tblproperties',
354
                        'urlvars' => [
355
                            'schema' => Decorator::field('nspname'),
356
                            'table'  => Decorator::field('relname'),
357
                        ],
358
                    ],
359
                ],
360
            ],
361
        ];
362
363
        echo $this->printTable($parents, $columns, $actions, 'info-parents', $this->lang['strnodata']);
364
    }
365
366
    private function _printReferring($referrers)
367
    {
368
        echo "<h3>{$this->lang['strreferringtables']}</h3>".PHP_EOL;
369
370
        $columns = [
371
            'schema'     => [
372
                'title' => $this->lang['strschema'],
373
                'field' => Decorator::field('nspname'),
374
            ],
375
            'table'      => [
376
                'title' => $this->lang['strtable'],
377
                'field' => Decorator::field('relname'),
378
            ],
379
            'name'       => [
380
                'title' => $this->lang['strname'],
381
                'field' => Decorator::field('conname'),
382
            ],
383
            'definition' => [
384
                'title' => $this->lang['strdefinition'],
385
                'field' => Decorator::field('consrc'),
386
            ],
387
            'actions'    => [
388
                'title' => $this->lang['stractions'],
389
            ],
390
        ];
391
392
        $actions = [
393
            'properties' => [
394
                'content' => $this->lang['strproperties'],
395
                'attr'    => [
396
                    'href' => [
397
                        'url'     => 'constraints',
398
                        'urlvars' => [
399
                            'schema' => Decorator::field('nspname'),
400
                            'table'  => Decorator::field('relname'),
401
                        ],
402
                    ],
403
                ],
404
            ],
405
        ];
406
407
        echo $this->printTable($referrers, $columns, $actions, 'info-referrers', $this->lang['strnodata']);
408
    }
409
}
410