summaryrefslogtreecommitdiffstats
path: root/library/Icingadb/Util/PerfDataSet.php
blob: df31393043a1502bed6101423437d09877e90c57 (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
<?php

/* Icinga DB Web | (c) 2021 Icinga GmbH | GPLv2 */

namespace Icinga\Module\Icingadb\Util;

use ArrayIterator;
use IteratorAggregate;

class PerfDataSet implements IteratorAggregate
{
    /**
     * The performance data being parsed
     *
     * @var string
     */
    protected $perfdataStr;

    /**
     * The current parsing position
     *
     * @var int
     */
    protected $parserPos = 0;

    /**
     * A list of PerfData objects
     *
     * @var array
     */
    protected $perfdata = array();

    /**
     * Create a new set of performance data
     *
     * @param   string      $perfdataStr    A space separated list of label/value pairs
     */
    protected function __construct(string $perfdataStr)
    {
        if (($perfdataStr = trim($perfdataStr)) !== '') {
            $this->perfdataStr = $perfdataStr;
            $this->parse();
        }
    }

    /**
     * Return a iterator for this set of performance data
     *
     * @return  ArrayIterator
     */
    public function getIterator(): ArrayIterator
    {
        return new ArrayIterator($this->asArray());
    }

    /**
     * Return a new set of performance data
     *
     * @param   string      $perfdataStr    A space separated list of label/value pairs
     *
     * @return  PerfDataSet
     */
    public static function fromString(string $perfdataStr): self
    {
        return new static($perfdataStr);
    }

    /**
     * Return this set of performance data as array
     *
     * @return  array
     */
    public function asArray(): array
    {
        return $this->perfdata;
    }

    /**
     * Parse the current performance data
     */
    protected function parse()
    {
        while ($this->parserPos < strlen($this->perfdataStr)) {
            $label = trim($this->readLabel());
            $value = trim($this->readUntil(' '));

            if ($label) {
                $this->perfdata[] = new PerfData($label, $value);
            }
        }

        uasort(
            $this->perfdata,
            function ($a, $b) {
                if ($a->isVisualizable() && ! $b->isVisualizable()) {
                    return -1;
                } elseif (! $a->isVisualizable() && $b->isVisualizable()) {
                    return 1;
                } elseif (! $a->isVisualizable() && ! $b->isVisualizable()) {
                    return 0;
                }

                return $a->worseThan($b) ? -1 : ($b->worseThan($a) ? 1 : 0);
            }
        );
    }

    /**
     * Return the next label found in the performance data
     *
     * @return  string      The label found
     */
    protected function readLabel(): string
    {
        $this->skipSpaces();
        if (in_array($this->perfdataStr[$this->parserPos], array('"', "'"))) {
            $quoteChar = $this->perfdataStr[$this->parserPos++];
            $label = $this->readUntil($quoteChar, '=');
            $this->parserPos++;

            if ($this->perfdataStr[$this->parserPos] === '=') {
                $this->parserPos++;
            }
        } else {
            $label = $this->readUntil('=');
            $this->parserPos++;
        }

        $this->skipSpaces();
        return $label;
    }

    /**
     * Return all characters between the current parser position and the given character
     *
     * @param string $stopChar The character on which to stop
     * @param string $backtrackOn The character on which to backtrack
     *
     * @return string
     */
    protected function readUntil(string $stopChar, string $backtrackOn = null): string
    {
        $start = $this->parserPos;
        $breakCharEncounteredAt = null;
        $stringExhaustedAt = strlen($this->perfdataStr);
        while ($this->parserPos < $stringExhaustedAt) {
            if ($this->perfdataStr[$this->parserPos] === $stopChar) {
                break;
            } elseif ($breakCharEncounteredAt === null && $this->perfdataStr[$this->parserPos] === $backtrackOn) {
                $breakCharEncounteredAt = $this->parserPos;
            }

            $this->parserPos++;
        }

        if ($breakCharEncounteredAt !== null && $this->parserPos === $stringExhaustedAt) {
            $this->parserPos = $breakCharEncounteredAt;
        }

        return substr($this->perfdataStr, $start, $this->parserPos - $start);
    }

    /**
     * Advance the parser position to the next non-whitespace character
     */
    protected function skipSpaces()
    {
        while ($this->parserPos < strlen($this->perfdataStr) && $this->perfdataStr[$this->parserPos] === ' ') {
            $this->parserPos++;
        }
    }
}