Lines 100.00% 8 / 8
Functions and Methods 100.00% 2 / 2
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
Histogram 100.00% 8 / 8 100.00% 2 / 2 4 100.00% 1 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 fromLatencyHistogram 100.00% 7 / 7 100.00% 1 / 1 3
1<?php
2
3declare(strict_types=1);
4
5namespace Stewart\Runtime\Metrics\Exposition;
6
7use LogicException;
8use Stewart\Contracts\Time\Duration;
9use Stewart\Runtime\Control\Protocol\Status\LatencyHistogram;
10
11final readonly class Histogram
12{
13    /** @param list<HistogramBucket> $buckets */
14    public function __construct(
15        public array $buckets,
16        public int $count,
17        public Duration $sum,
18    ) {}
19
20    public static function fromLatencyHistogram(LatencyHistogram $latency): self
21    {
22        if (\count($latency->boundsMs) !== \count($latency->cumulativeCounts)) {
23            throw new LogicException(\sprintf('A latency histogram has %d bounds but %d counts.', \count($latency->boundsMs), \count($latency->cumulativeCounts)));
24        }
25
26        $buckets = [];
27
28        foreach ($latency->boundsMs as $index => $bound) {
29            $buckets[] = new HistogramBucket(Duration::milliseconds($bound)->toSeconds(), $latency->cumulativeCounts[$index]);
30        }
31
32        $buckets[] = new HistogramBucket(INF, $latency->count);
33
34        return new self($buckets, $latency->count, $latency->sum);
35    }
36}