ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
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 (string $stability, string $type)
 
 checkType ($type)
 
 checkValue ($type, $value)
 
 getIndentation (int $indentation=0)
 

Protected Attributes

string $stability
 
string $type
 
 $value
 
string $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 33 of file Metric.php.

Constructor & Destructor Documentation

◆ __construct()

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

Definition at line 84 of file Metric.php.

89 {
91 $this->checkType($type);
92 $this->checkValue($type, $value);
93
94 $this->stability = $stability;
95 $this->type = $type;
96 $this->value = $value;
97 $this->description = $description;
98 }
checkValue($type, $value)
Definition: Metric.php:130
checkStability(string $stability, string $type)
Definition: Metric.php:100

References ILIAS\Setup\Metrics\Metric\$description, ILIAS\Setup\Metrics\Metric\$stability, ILIAS\Setup\Metrics\Metric\$type, ILIAS\Setup\Metrics\Metric\$value, ILIAS\Setup\Metrics\Metric\checkStability(), ILIAS\Setup\Metrics\Metric\checkType(), and ILIAS\Setup\Metrics\Metric\checkValue().

+ Here is the call graph for this function:

Member Function Documentation

◆ checkStability()

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

Definition at line 100 of file Metric.php.

100 : void
101 {
102 if (
103 $stability !== self::STABILITY_CONFIG
104 && $stability !== self::STABILITY_STABLE
105 && $stability !== self::STABILITY_VOLATILE
106 && !($stability === self::STABILITY_MIXED && $type === self::TYPE_COLLECTION)
107 ) {
108 throw new \InvalidArgumentException(
109 "Invalid stability for metric: $stability"
110 );
111 }
112 }

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 114 of file Metric.php.

114 : void
115 {
116 if (
117 $type !== self::TYPE_BOOL
118 && $type !== self::TYPE_COUNTER
119 && $type !== self::TYPE_GAUGE
120 && $type !== self::TYPE_TIMESTAMP
121 && $type !== self::TYPE_TEXT
122 && $type !== self::TYPE_COLLECTION
123 ) {
124 throw new \InvalidArgumentException(
125 "Invalid type for metric: $type"
126 );
127 }
128 }

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 130 of file Metric.php.

130 : void
131 {
132 if (
133 ($type === self::TYPE_BOOL && !is_bool($value))
134 || ($type === self::TYPE_COUNTER && !is_int($value))
135 || ($type === self::TYPE_GAUGE && !(is_int($value) || is_float($value)))
136 || ($type === self::TYPE_TIMESTAMP && !($value instanceof \DateTimeImmutable))
137 || ($type === self::TYPE_TEXT && !is_string($value))
138 || ($type === self::TYPE_COLLECTION && !is_array($value))
139 ) {
140 throw new \InvalidArgumentException(
141 "Invalid type " . gettype($value) . " for metric of type $type"
142 );
143 }
144
145 if ($type === self::TYPE_COLLECTION) {
146 foreach ($value as $v) {
147 if (!($v instanceof Metric)) {
148 throw new \InvalidArgumentException(
149 "Every element of a collection needs to be a metric, found " . gettype($v)
150 );
151 }
152 }
153 }
154 }
A metric is something we can measure about the system.
Definition: Metric.php:34

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 277 of file Metric.php.

277 : array
278 {
279 if ($stability === self::STABILITY_MIXED) {
280 throw new \LogicException("Can not extract by mixed.");
281 }
282
283 if ($this->getStability() === $stability) {
284 return [$this, null];
285 }
286 if ($this->getType() !== self::TYPE_COLLECTION) {
287 return [null, $this];
288 }
289
290 // Now, this is a mixed collection. We need to go down.
291 $values = $this->getValue();
292 $extracted = [];
293 $rest = [];
294 foreach ($values as $k => $v) {
295 list($e, $r) = $v->extractByStability($stability);
296 if ($e !== null) {
297 $extracted[$k] = $e;
298 }
299 if ($r !== null) {
300 $rest[$k] = $r;
301 }
302 }
303
304 if ($extracted !== []) {
305 $extracted = new Metric(
307 self::TYPE_COLLECTION,
308 $extracted,
309 $this->getDescription()
310 );
311 } else {
312 $extracted = null;
313 }
314
315 if ($rest !== []) {
316 $rest = new Metric(
317 $this->getStability(),
318 self::TYPE_COLLECTION,
319 $rest,
320 $this->getDescription()
321 );
322 } else {
323 $rest = null;
324 }
325
326 return [$extracted, $rest];
327 }

References Vendor\Package\$e, 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 174 of file Metric.php.

174 : ?string
175 {
176 return $this->description;
177 }

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 262 of file Metric.php.

262 : string
263 {
264 $res = "";
265 while ($indentation--) {
266 $res .= " ";
267 }
268 return $res;
269 }
$res
Definition: ltiservices.php:69

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 156 of file Metric.php.

156 : string
157 {
158 return $this->stability;
159 }

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 161 of file Metric.php.

161 : string
162 {
163 return $this->type;
164 }

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 169 of file Metric.php.

170 {
171 return $this->value;
172 }

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 225 of file Metric.php.

226 {
227 $value = $this->getValue();
228
229 switch ($this->getType()) {
230 case self::TYPE_BOOL:
231 if ($value) {
232 return "true";
233 } else {
234 return "false";
235 }
236 // no break
238 return (string) $value;
239 case self::TYPE_GAUGE:
240 if (is_int($value)) {
241 return (string) $value;
242 }
243 return sprintf("%.03f", $value);
245 return $value->format(\DateTimeInterface::ISO8601);
246 case self::TYPE_TEXT:
247 if (substr_count($value, "\n") > 0) {
248 return ">" . str_replace("\n", "\n" . $this->getIndentation($indentation), "\n$value");
249 }
250 return $value;
252 $result = [];
253 foreach ($value as $key => $val) {
254 $result[$key] = $val->toArray($indentation + 1);
255 }
256 return $result;
257 default:
258 throw new \LogicException("Unknown type: " . $this->getType());
259 }
260 }
getIndentation(int $indentation=0)
Definition: Metric.php:262
const TYPE_BOOL
The type of the metric tells what to expect of the values.
Definition: Metric.php:53

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.

+ Here is the call graph for this function:

◆ toUIReport()

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

Definition at line 329 of file Metric.php.

329 : Report
330 {
331 $yaml = $this->toYAML();
332 $sub = $f->panel()->sub("", $f->legacy()->content("<pre>" . $yaml . "</pre>"));
333 return $f->panel()->report($name, [$sub]);
334 }
toYAML(int $indentation=0)
Definition: Metric.php:179
This describes how a Report could be modified during construction of UI.
Definition: Report.php:29

References Vendor\Package\$f, 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 179 of file Metric.php.

179 : string
180 {
181 $value = $this->getValue();
182 switch ($this->getType()) {
183 case self::TYPE_BOOL:
184 if ($value) {
185 return "true";
186 } else {
187 return "false";
188 }
189 // no break
191 return "$value";
192 case self::TYPE_GAUGE:
193 if (is_int($value)) {
194 return "$value";
195 }
196 return sprintf("%.03f", $value);
198 return $value->format(\DateTimeInterface::ISO8601);
199 case self::TYPE_TEXT:
200 if (substr_count($value, "\n") > 0) {
201 return ">" . str_replace("\n", "\n" . $this->getIndentation($indentation), "\n$value");
202 }
203 return $value;
205 return implode(
206 "\n",
207 array_map(
208 function (string $k, Metric $v) use ($indentation): string {
209 if ($v->getType() === self::TYPE_COLLECTION) {
210 $split = "\n";
211 } else {
212 $split = " ";
213 }
214 return $this->getIndentation($indentation) . "$k:$split" . $v->toYAML($indentation + 1);
215 },
216 array_keys($value),
217 array_values($value)
218 )
219 );
220 default:
221 throw new \LogicException("Unknown type: " . $this->getType());
222 }
223 }

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\toYAML(), 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(), and ILIAS\Setup\Metrics\Metric\toYAML().

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

Field Documentation

◆ $description

string 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 40 of file Metric.php.

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

◆ STABILITY_MIXED

◆ STABILITY_STABLE

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

Definition at line 43 of file Metric.php.

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

◆ STABILITY_VOLATILE

◆ TYPE_BOOL

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

◆ TYPE_COLLECTION

◆ TYPE_COUNTER

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

◆ TYPE_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: