ILIAS  release_7 Revision v7.30-3-g800a261c036
ILIAS\Setup\Metrics\Metric Class Reference

A metric is something we can measure about the system. More...

+ Collaboration diagram for ILIAS\Setup\Metrics\Metric:

Public Member Functions

 __construct (string $stability, string $type, $value, string $description=null)
 
 getStability ()
 
 getType ()
 
 getValue ()
 
 getDescription ()
 
 toYAML (int $indentation=0)
 
 toArray (int $indentation=0)
 
 extractByStability (string $stability)
 The extracted part will be the first entry of the array, the second will be the rest of the metrics. More...
 
 toUIReport (Factory $f, string $name)
 

Data Fields

const STABILITY_CONFIG = "config"
 The stability of a metric tells how often we expect changes in the metric. More...
 
const STABILITY_STABLE = "stable"
 
const STABILITY_VOLATILE = "volatile"
 
const STABILITY_MIXED = "mixed"
 
const TYPE_BOOL = "bool"
 The type of the metric tells what to expect of the values. More...
 
const TYPE_COUNTER = "counter"
 
const TYPE_GAUGE = "gauge"
 
const TYPE_TIMESTAMP = "timestamp"
 
const TYPE_TEXT = "text"
 
const TYPE_COLLECTION = "collection"
 

Protected Member Functions

 checkStability ($stability, $type)
 
 checkType ($type)
 
 checkValue ($type, $value)
 
 getIndentation (int $indentation=0)
 

Protected Attributes

 $stability
 
 $type
 
 $value
 
 $description
 

Detailed Description

A metric is something we can measure about the system.

To make metrics processable and understandable for the setup, we use a closed sum type to represent them. So basically, this class will contain every kind of metric that can exist and the types are not extendable.

Definition at line 17 of file Metric.php.

Constructor & Destructor Documentation

◆ __construct()

ILIAS\Setup\Metrics\Metric::__construct ( string  $stability,
string  $type,
  $value,
string  $description = null 
)

Member Function Documentation

◆ checkStability()

ILIAS\Setup\Metrics\Metric::checkStability (   $stability,
  $type 
)
protected

Definition at line 86 of file Metric.php.

87 {
88 if (
89 $stability !== self::STABILITY_CONFIG
90 && $stability !== self::STABILITY_STABLE
91 && $stability !== self::STABILITY_VOLATILE
92 && !($stability === self::STABILITY_MIXED && $type === self::TYPE_COLLECTION)
93 ) {
94 throw new \InvalidArgumentException(
95 "Invalid stability for metric: $stability"
96 );
97 }
98 }

References ILIAS\Setup\Metrics\Metric\$stability, and ILIAS\Setup\Metrics\Metric\$type.

Referenced by ILIAS\Setup\Metrics\Metric\__construct().

+ Here is the caller graph for this function:

◆ checkType()

ILIAS\Setup\Metrics\Metric::checkType (   $type)
protected

Definition at line 100 of file Metric.php.

101 {
102 if (
103 $type !== self::TYPE_BOOL
104 && $type !== self::TYPE_COUNTER
105 && $type !== self::TYPE_GAUGE
106 && $type !== self::TYPE_TIMESTAMP
107 && $type !== self::TYPE_TEXT
108 && $type !== self::TYPE_COLLECTION
109 ) {
110 throw new \InvalidArgumentException(
111 "Invalid type for metric: $type"
112 );
113 }
114 }

References ILIAS\Setup\Metrics\Metric\$type.

Referenced by ILIAS\Setup\Metrics\Metric\__construct().

+ Here is the caller graph for this function:

◆ checkValue()

ILIAS\Setup\Metrics\Metric::checkValue (   $type,
  $value 
)
protected

Definition at line 116 of file Metric.php.

117 {
118 if (
119 ($type === self::TYPE_BOOL && !is_bool($value))
120 || ($type === self::TYPE_COUNTER && !is_int($value))
121 || ($type === self::TYPE_GAUGE && !(is_int($value) || is_float($value)))
122 || ($type === self::TYPE_TIMESTAMP && !($value instanceof \DateTimeImmutable))
123 || ($type === self::TYPE_TEXT && !is_string($value))
124 || ($type === self::TYPE_COLLECTION && !is_array($value))
125 ) {
126 throw new \InvalidArgumentException(
127 "Invalid type " . gettype($value) . " for metric of type $type"
128 );
129 }
130
131 if ($type === self::TYPE_COLLECTION) {
132 foreach ($value as $v) {
133 if (!($v instanceof Metric)) {
134 throw new \InvalidArgumentException(
135 "Every element of a collection needs to be a metric, found " . gettype($v)
136 );
137 }
138 }
139 }
140 }

References ILIAS\Setup\Metrics\Metric\$type, and ILIAS\Setup\Metrics\Metric\$value.

Referenced by ILIAS\Setup\Metrics\Metric\__construct().

+ Here is the caller graph for this function:

◆ extractByStability()

ILIAS\Setup\Metrics\Metric::extractByStability ( string  $stability)

The extracted part will be the first entry of the array, the second will be the rest of the metrics.

Returns
(Metric|null)[]

Definition at line 262 of file Metric.php.

262 : array
263 {
264 if ($stability === self::STABILITY_MIXED) {
265 throw new \LogicException("Can not extract by mixed.");
266 }
267
268 if ($this->getStability() === $stability) {
269 return [$this, null];
270 }
271 if ($this->getType() !== self::TYPE_COLLECTION) {
272 return [null, $this];
273 }
274
275 // Now, this is a mixed collection. We need to go down.
276 $values = $this->getValue();
277 $extracted = [];
278 $rest = [];
279 foreach ($values as $k => $v) {
280 list($e, $r) = $v->extractByStability($stability);
281 if ($e !== null) {
282 $extracted[$k] = $e;
283 }
284 if ($r !== null) {
285 $rest[$k] = $r;
286 }
287 }
288
289 if (count($extracted)) {
290 $extracted = new Metric(
292 self::TYPE_COLLECTION,
293 $extracted,
294 $this->getDescription()
295 );
296 } else {
297 $extracted = null;
298 }
299
300 if (count($rest)) {
301 $rest = new Metric(
302 $this->getStability(),
303 self::TYPE_COLLECTION,
304 $rest,
305 $this->getDescription()
306 );
307 } else {
308 $rest = null;
309 }
310
311 return [$extracted, $rest];
312 }
$rest
Definition: goto.php:48

References Vendor\Package\$e, $rest, ILIAS\Setup\Metrics\Metric\$stability, ILIAS\Setup\Metrics\Metric\getDescription(), ILIAS\Setup\Metrics\Metric\getStability(), ILIAS\Setup\Metrics\Metric\getType(), and ILIAS\Setup\Metrics\Metric\getValue().

+ Here is the call graph for this function:

◆ getDescription()

ILIAS\Setup\Metrics\Metric::getDescription ( )

Definition at line 160 of file Metric.php.

160 : ?string
161 {
162 return $this->description;
163 }

References ILIAS\Setup\Metrics\Metric\$description.

Referenced by ILIAS\Setup\Metrics\Metric\extractByStability().

+ Here is the caller graph for this function:

◆ getIndentation()

ILIAS\Setup\Metrics\Metric::getIndentation ( int  $indentation = 0)
protected

Definition at line 247 of file Metric.php.

248 {
249 $res = "";
250 while ($indentation--) {
251 $res .= " ";
252 }
253 return $res;
254 }
foreach($_POST as $key=> $value) $res

References $res.

Referenced by ILIAS\Setup\Metrics\Metric\toArray(), and ILIAS\Setup\Metrics\Metric\toYAML().

+ Here is the caller graph for this function:

◆ getStability()

ILIAS\Setup\Metrics\Metric::getStability ( )

Definition at line 142 of file Metric.php.

142 : string
143 {
144 return $this->stability;
145 }

References ILIAS\Setup\Metrics\Metric\$stability.

Referenced by ILIAS\Setup\Metrics\Metric\extractByStability().

+ Here is the caller graph for this function:

◆ getType()

ILIAS\Setup\Metrics\Metric::getType ( )

Definition at line 147 of file Metric.php.

147 : string
148 {
149 return $this->type;
150 }

References ILIAS\Setup\Metrics\Metric\$type.

Referenced by ILIAS\Setup\Metrics\Metric\extractByStability(), ILIAS\Setup\Metrics\Metric\toArray(), and ILIAS\Setup\Metrics\Metric\toYAML().

+ Here is the caller graph for this function:

◆ getValue()

ILIAS\Setup\Metrics\Metric::getValue ( )
Returns
mixed

Definition at line 155 of file Metric.php.

156 {
157 return $this->value;
158 }

References ILIAS\Setup\Metrics\Metric\$value.

Referenced by ILIAS\Setup\Metrics\Metric\extractByStability(), ILIAS\Setup\Metrics\Metric\toArray(), and ILIAS\Setup\Metrics\Metric\toYAML().

+ Here is the caller graph for this function:

◆ toArray()

ILIAS\Setup\Metrics\Metric::toArray ( int  $indentation = 0)

Definition at line 211 of file Metric.php.

212 {
213 $value = $this->getValue();
214
215 switch ($this->getType()) {
216 case self::TYPE_BOOL:
217 if ($value) {
218 return "true";
219 } else {
220 return "false";
221 }
223 return (string)$value;
224 case self::TYPE_GAUGE:
225 if (is_int($value)) {
226 return (string)$value;
227 }
228 return sprintf("%.03f", $value);
230 return $value->format(\DateTimeInterface::ISO8601);
231 case self::TYPE_TEXT:
232 if (substr_count($value, "\n") > 0) {
233 return ">" . str_replace("\n", "\n" . $this->getIndentation($indentation), "\n$value");
234 }
235 return $value;
237 $result = [];
238 foreach ($value as $key => $val) {
239 $result[$key] = $val->toArray($indentation + 1);
240 }
241 return $result;
242 default:
243 throw new \LogicException("Unknown type: " . $this->getType());
244 }
245 }
$result
getIndentation(int $indentation=0)
Definition: Metric.php:247
const TYPE_BOOL
The type of the metric tells what to expect of the values.
Definition: Metric.php:37

References $result, ILIAS\Setup\Metrics\Metric\$value, ILIAS\Setup\Metrics\Metric\getIndentation(), ILIAS\Setup\Metrics\Metric\getType(), ILIAS\Setup\Metrics\Metric\getValue(), ILIAS\Setup\Metrics\Metric\TYPE_BOOL, ILIAS\Setup\Metrics\Metric\TYPE_COLLECTION, ILIAS\Setup\Metrics\Metric\TYPE_COUNTER, ILIAS\Setup\Metrics\Metric\TYPE_GAUGE, ILIAS\Setup\Metrics\Metric\TYPE_TEXT, and ILIAS\Setup\Metrics\Metric\TYPE_TIMESTAMP.

+ Here is the call graph for this function:

◆ toUIReport()

ILIAS\Setup\Metrics\Metric::toUIReport ( Factory  $f,
string  $name 
)

Definition at line 314 of file Metric.php.

314 : Report
315 {
316 $yaml = $this->toYAML();
317 $sub = $f->panel()->sub("", $f->legacy("<pre>" . $yaml . "</pre>"));
318 return $f->panel()->report($name, [$sub]);
319 }
toYAML(int $indentation=0)
Definition: Metric.php:165
if($format !==null) $name
Definition: metadata.php:230

References Vendor\Package\$f, $name, and ILIAS\Setup\Metrics\Metric\toYAML().

+ Here is the call graph for this function:

◆ toYAML()

ILIAS\Setup\Metrics\Metric::toYAML ( int  $indentation = 0)

Definition at line 165 of file Metric.php.

165 : string
166 {
167 $value = $this->getValue();
168 switch ($this->getType()) {
169 case self::TYPE_BOOL:
170 if ($value) {
171 return "true";
172 } else {
173 return "false";
174 }
175 // no break
177 return "$value";
178 case self::TYPE_GAUGE:
179 if (is_int($value)) {
180 return "$value";
181 }
182 return sprintf("%.03f", $value);
184 return $value->format(\DateTimeInterface::ISO8601);
185 case self::TYPE_TEXT:
186 if (substr_count($value, "\n") > 0) {
187 return ">" . str_replace("\n", "\n" . $this->getIndentation($indentation), "\n$value");
188 }
189 return $value;
191 return implode(
192 "\n",
193 array_map(
194 function ($k, $v) use ($indentation) {
195 if ($v->getType() === self::TYPE_COLLECTION) {
196 $split = "\n";
197 } else {
198 $split = " ";
199 }
200 return $this->getIndentation($indentation) . "$k:$split" . $v->toYAML($indentation + 1);
201 },
202 array_keys($value),
203 array_values($value)
204 )
205 );
206 default:
207 throw new \LogicException("Unknown type: " . $this->getType());
208 }
209 }

References ILIAS\Setup\Metrics\Metric\$value, ILIAS\Setup\Metrics\Metric\getIndentation(), ILIAS\Setup\Metrics\Metric\getType(), ILIAS\Setup\Metrics\Metric\getValue(), ILIAS\Setup\Metrics\Metric\TYPE_BOOL, ILIAS\Setup\Metrics\Metric\TYPE_COLLECTION, ILIAS\Setup\Metrics\Metric\TYPE_COUNTER, ILIAS\Setup\Metrics\Metric\TYPE_GAUGE, ILIAS\Setup\Metrics\Metric\TYPE_TEXT, and ILIAS\Setup\Metrics\Metric\TYPE_TIMESTAMP.

Referenced by ILIAS\Setup\Metrics\Metric\toUIReport().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Field Documentation

◆ $description

ILIAS\Setup\Metrics\Metric::$description
protected

◆ $stability

◆ $type

◆ $value

◆ STABILITY_CONFIG

const ILIAS\Setup\Metrics\Metric::STABILITY_CONFIG = "config"

The stability of a metric tells how often we expect changes in the metric.

Definition at line 24 of file Metric.php.

Referenced by ILIAS\Setup\CLI\StatusCommand\getMetrics(), and ILIAS\Tests\Setup\Metrics\MetricTest\metricProvider().

◆ STABILITY_MIXED

const ILIAS\Setup\Metrics\Metric::STABILITY_MIXED = "mixed"

◆ STABILITY_STABLE

const ILIAS\Setup\Metrics\Metric::STABILITY_STABLE = "stable"

Definition at line 27 of file Metric.php.

Referenced by ILIAS\Tests\Setup\Metrics\MetricTest\metricProvider().

◆ STABILITY_VOLATILE

const ILIAS\Setup\Metrics\Metric::STABILITY_VOLATILE = "volatile"

Definition at line 29 of file Metric.php.

Referenced by ILIAS\Tests\Setup\Metrics\MetricTest\metricProvider().

◆ TYPE_BOOL

const ILIAS\Setup\Metrics\Metric::TYPE_BOOL = "bool"

The type of the metric tells what to expect of the values.

Definition at line 37 of file Metric.php.

Referenced by ILIAS\Tests\Setup\Metrics\MetricTest\metricProvider(), ILIAS\Setup\Metrics\Metric\toArray(), and ILIAS\Setup\Metrics\Metric\toYAML().

◆ TYPE_COLLECTION

◆ TYPE_COUNTER

const ILIAS\Setup\Metrics\Metric::TYPE_COUNTER = "counter"

◆ TYPE_GAUGE

const ILIAS\Setup\Metrics\Metric::TYPE_GAUGE = "gauge"

◆ TYPE_TEXT

const ILIAS\Setup\Metrics\Metric::TYPE_TEXT = "text"

◆ TYPE_TIMESTAMP

const ILIAS\Setup\Metrics\Metric::TYPE_TIMESTAMP = "timestamp"

The documentation for this class was generated from the following file: