summaryrefslogtreecommitdiffstats
path: root/modules/monitoring/library/Monitoring/DataView/DataView.php
blob: 5b16e28638c077f99135ccc37d6150872ade36fc (plain)
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
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
<?php
/* Icinga Web 2 | (c) 2013 Icinga Development Team | GPLv2+ */

namespace Icinga\Module\Monitoring\DataView;

use Icinga\Data\Filter\FilterExpression;
use Icinga\Data\Filter\FilterMatch;
use IteratorAggregate;
use Icinga\Application\Hook;
use Icinga\Data\ConnectionInterface;
use Icinga\Data\Filter\Filter;
use Icinga\Data\FilterColumns;
use Icinga\Data\PivotTable;
use Icinga\Data\QueryInterface;
use Icinga\Data\SortRules;
use Icinga\Exception\QueryException;
use Icinga\Module\Monitoring\Backend\Ido\Query\IdoQuery;
use Icinga\Module\Monitoring\Backend\MonitoringBackend;
use Icinga\Web\Request;
use Icinga\Web\Url;
use Traversable;

/**
 * A read-only view of an underlying query
 */
abstract class DataView implements QueryInterface, SortRules, FilterColumns, IteratorAggregate
{
    /**
     * The query used to populate the view
     *
     * @var IdoQuery
     */
    protected $query;

    protected $connection;

    protected $isSorted = false;

    /**
     * The cache for all filter columns
     *
     * @var array
     */
    protected $filterColumns;

    /**
     * Create a new view
     *
     * @param ConnectionInterface   $connection
     * @param array                 $columns
     */
    public function __construct(ConnectionInterface $connection, array $columns = null)
    {
        $this->connection = $connection;
        $this->query = $connection->query($this->getQueryName(), $columns);
    }

    /**
     * Return a iterator for all rows of the result set
     *
     * @return  IdoQuery
     */
    public function getIterator(): Traversable
    {
        return $this->getQuery();
    }

    /**
     * Return the current position of the result set's iterator
     *
     * @return  int
     */
    public function getIteratorPosition()
    {
        return $this->query->getIteratorPosition();
    }

    /**
     * Get the query name this data view relies on
     *
     * By default this is this class' name without its namespace
     *
     * @return string
     */
    public static function getQueryName()
    {
        $tableName = explode('\\', get_called_class());
        $tableName = end($tableName);
        return $tableName;
    }

    public function where($condition, $value = null)
    {
        $this->query->where($condition, $value);
        return $this;
    }

    /**
     * Add a filter expression, with as less validation as possible
     *
     * @param FilterExpression $ex
     *
     * @internal If you use this outside the monitoring module, it's your fault if something breaks
     * @return $this
     */
    public function whereEx(FilterExpression $ex)
    {
        $this->query->whereEx($ex);
        return $this;
    }

    public function dump()
    {
        if (! $this->isSorted) {
            $this->order();
        }
        return $this->query->dump();
    }

    /**
     * Retrieve columns provided by this view
     *
     * @return array
     */
    abstract public function getColumns();

    protected function getHookedColumns()
    {
        $columns = array();
        foreach (Hook::all('monitoring/dataviewExtension') as $hook) {
            foreach ($hook->getAdditionalQueryColumns($this->getQueryName()) as $col) {
                $columns[] = $col;
            }
        }

        return $columns;
    }

    /**
     * Create view from params
     *
     * @param   array $params
     * @param   array $columns
     *
     * @return  static
     */
    public static function fromParams(array $params, array $columns = null)
    {
        $view = new static(MonitoringBackend::instance($params['backend']), $columns);

        foreach ($params as $key => $value) {
            if ($view->isValidFilterTarget($key)) {
                $view->where($key, $value);
            }
        }

        if (isset($params['sort'])) {
            $order = isset($params['order']) ? $params['order'] : null;
            if ($order !== null) {
                if (strtolower($order) === 'desc') {
                    $order = self::SORT_DESC;
                } else {
                    $order = self::SORT_ASC;
                }
            }

            $view->order($params['sort'], $order);
        }
        return $view;
    }

    /**
     * Check whether the given column is a valid filter column
     *
     * @param   string  $column
     *
     * @return  bool
     */
    public function isValidFilterTarget($column)
    {
        // Customvar
        if ($column[0] === '_' && preg_match('/^_(?:host|service)_/i', $column)) {
            return true;
        }
        return in_array($column, $this->getColumns()) || in_array($column, $this->getStaticFilterColumns());
    }

    /**
     * Return all filter columns with their optional label as key
     *
     * This will merge the results of self::getColumns(), self::getStaticFilterColumns() and
     * self::getDynamicFilterColumns() *once*. (i.e. subsequent calls of this function will
     * return the same result.)
     *
     * @return  array
     */
    public function getFilterColumns()
    {
        if ($this->filterColumns === null) {
            $columns = array_merge(
                $this->getColumns(),
                $this->getStaticFilterColumns(),
                $this->getDynamicFilterColumns()
            );

            $this->filterColumns = array();
            foreach ($columns as $label => $column) {
                if (is_int($label)) {
                    $label = ucwords(str_replace('_', ' ', $column));
                }

                if ($this->query->isCaseInsensitive($column)) {
                    $label .= ' ' . t('(Case insensitive)');
                }

                $this->filterColumns[$label] = $column;
            }
        }

        return $this->filterColumns;
    }

    /**
     * Return all static filter columns
     *
     * @return  array
     */
    public function getStaticFilterColumns()
    {
        return array();
    }

    /**
     * Return all dynamic filter columns such as custom variables
     *
     * @return  array
     */
    public function getDynamicFilterColumns()
    {
        $columns = array();
        if (! $this->query->allowsCustomVars()) {
            return $columns;
        }

        $query = MonitoringBackend::instance()
            ->select()
            ->from('customvar', array('varname', 'object_type'))
            ->where('is_json', 0)
            ->where('object_type_id', array(1, 2))
            ->getQuery()->group(array('varname', 'object_type'));
        foreach ($query as $row) {
            if ($row->object_type === 'host') {
                $label = t('Host') . ' ' . ucwords(str_replace('_', ' ', $row->varname));
                $columns[$label] = '_host_' . $row->varname;
            } else { // $row->object_type === 'service'
                $label = t('Service') . ' ' . ucwords(str_replace('_', ' ', $row->varname));
                $columns[$label] = '_service_' . $row->varname;
            }
        }

        return $columns;
    }

    /**
     * Return the current filter
     *
     * @return  Filter
     */
    public function getFilter()
    {
        return $this->query->getFilter();
    }

    /**
     * Return a pivot table for the given columns based on the current query
     *
     * @param   string  $xAxisColumn    The column to use for the x axis
     * @param   string  $yAxisColumn    The column to use for the y axis
     * @param   Filter  $xAxisFilter    The filter to apply on a query for the x axis
     * @param   Filter  $yAxisFilter    The filter to apply on a query for the y axis
     *
     * @return  PivotTable
     */
    public function pivot($xAxisColumn, $yAxisColumn, Filter $xAxisFilter = null, Filter $yAxisFilter = null)
    {
        $pivot = new PivotTable($this->query, $xAxisColumn, $yAxisColumn);
        return $pivot->setXAxisFilter($xAxisFilter)->setYAxisFilter($yAxisFilter);
    }

    /**
     * Sort result set either by the given column (and direction) or the sort defaults
     *
     * @param  string   $column
     * @param  string   $direction
     *
     * @return $this
     */
    public function order($column = null, $direction = null)
    {
        $sortRules = $this->getSortRules();
        if ($column === null) {
            // Use first available sort rule as default
            if (empty($sortRules)) {
                return $this;
            }
            $sortColumns = reset($sortRules);
            if (! isset($sortColumns['columns'])) {
                $sortColumns['columns'] = array(key($sortRules));
            }
        } else {
            if (isset($sortRules[$column])) {
                $sortColumns = $sortRules[$column];
                if (! isset($sortColumns['columns'])) {
                    $sortColumns['columns'] = array($column);
                }
            } else {
                $sortColumns = array(
                    'columns' => array($column),
                    'order' => $direction
                );
            };
        }

        $direction = $direction === null ? ($sortColumns['order'] ?? static::SORT_ASC) : $direction;
        $direction = (strtoupper($direction) === static::SORT_ASC) ? 'ASC' : 'DESC';

        foreach ($sortColumns['columns'] as $column) {
            list($column, $order) = $this->query->splitOrder($column);
            if (! $this->isValidFilterTarget($column)) {
                throw new QueryException(
                    mt('monitoring', 'The sort column "%s" is not allowed in "%s".'),
                    $column,
                    get_class($this)
                );
            }
            $this->query->order($column, $order !== null ? $order : $direction);
        }
        $this->isSorted = true;
        return $this;
    }

    /**
     * Retrieve default sorting rules for particular columns. These involve sort order and potential additional to sort
     *
     * @return array
     */
    public function getSortRules()
    {
        return array();
    }

    /**
     * Whether an order is set
     *
     * @return bool
     */
    public function hasOrder()
    {
        return $this->query->hasOrder();
    }

    /**
     * Get the order if any
     *
     * @return array|null
     */
    public function getOrder()
    {
        return $this->query->getOrder();
    }

    public function getMappedField($field)
    {
        return $this->query->getMappedField($field);
    }

    /**
     * Return the query which was created in the constructor
     *
     * @return \Icinga\Data\SimpleQuery
     */
    public function getQuery()
    {
        if (! $this->isSorted) {
            $this->order();
        }
        return $this->query;
    }

    public function applyFilter(Filter $filter)
    {
        $this->validateFilterColumns($filter);

        return $this->addFilter($filter);
    }

    /**
     * Validates recursive the Filter columns against the isValidFilterTarget() method
     *
     * @param Filter $filter
     *
     * @throws \Icinga\Data\Filter\FilterException
     */
    public function validateFilterColumns(Filter $filter)
    {
        if ($filter instanceof FilterMatch) {
            if (! $this->isValidFilterTarget($filter->getColumn())) {
                throw new QueryException(
                    mt('monitoring', 'The filter column "%s" is not allowed here.'),
                    $filter->getColumn()
                );
            }
        }

        if (method_exists($filter, 'filters')) {
            foreach ($filter->filters() as $filter) {
                $this->validateFilterColumns($filter);
            }
        }
    }

    public function clearFilter()
    {
        $this->query->clearFilter();
        return $this;
    }

    /**
     * @deprecated(EL): Only use DataView::applyFilter() for applying filter because all other functions are missing
     * column validation. Filter::matchAny() for the IdoQuery (or the DbQuery or the SimpleQuery I didn't have a look)
     * is required for the filter to work properly.
     */
    public function setFilter(Filter $filter)
    {
        $this->query->setFilter($filter);
        return $this;
    }

    /**
     * Get the view's search columns
     *
     * @return string[]
     */
    public function getSearchColumns()
    {
        return array();
    }

    /**
     * @deprecated(EL): Only use DataView::applyFilter() for applying filter because all other functions are missing
     * column validation.
     */
    public function addFilter(Filter $filter)
    {
        $this->query->addFilter($filter);
        return $this;
    }

    /**
     * Count result set
     *
     * @return int
     */
    public function count(): int
    {
        return $this->query->count();
    }

    /**
     * Set whether the query should peek ahead for more results
     *
     * Enabling this causes the current query limit to be increased by one. The potential extra row being yielded will
     * be removed from the result set. Note that this only applies when fetching multiple results of limited queries.
     *
     * @return  $this
     */
    public function peekAhead($state = true)
    {
        $this->query->peekAhead($state);
        return $this;
    }

    /**
     * Return whether the query did not yield all available results
     *
     * @return  bool
     */
    public function hasMore()
    {
        return $this->query->hasMore();
    }

    /**
     * Return whether this query will or has yielded any result
     *
     * @return  bool
     */
    public function hasResult()
    {
        return $this->query->hasResult();
    }

    /**
     * Set a limit count and offset
     *
     * @param   int $count  Number of rows to return
     * @param   int $offset Start returning after this many rows
     *
     * @return  self
     */
    public function limit($count = null, $offset = null)
    {
        $this->query->limit($count, $offset);
        return $this;
    }

    /**
     * Whether a limit is set
     *
     * @return bool
     */
    public function hasLimit()
    {
        return $this->query->hasLimit();
    }

    /**
     * Get the limit if any
     *
     * @return int|null
     */
    public function getLimit()
    {
        return $this->query->getLimit();
    }

    /**
     * Whether an offset is set
     *
     * @return bool
     */
    public function hasOffset()
    {
        return $this->query->hasOffset();
    }

    /**
     * Get the offset if any
     *
     * @return int|null
     */
    public function getOffset()
    {
        return $this->query->getOffset();
    }

    /**
     * Retrieve an array containing all rows of the result set
     *
     * @return  array
     */
    public function fetchAll()
    {
        return $this->getQuery()->fetchAll();
    }

    /**
     * Fetch the first row of the result set
     *
     * @return  mixed
     */
    public function fetchRow()
    {
        return $this->getQuery()->fetchRow();
    }

    /**
     * Fetch the first column of all rows of the result set as an array
     *
     * @return  array
     */
    public function fetchColumn()
    {
        return $this->getQuery()->fetchColumn();
    }

    /**
     * Fetch the first column of the first row of the result set
     *
     * @return  string
     */
    public function fetchOne()
    {
        return $this->getQuery()->fetchOne();
    }

    /**
     * Fetch all rows of the result set as an array of key-value pairs
     *
     * The first column is the key, the second column is the value.
     *
     * @return  array
     */
    public function fetchPairs()
    {
        return $this->getQuery()->fetchPairs();
    }
}