|
2 | 2 |
|
3 | 3 | namespace Utopia\Usage; |
4 | 4 |
|
5 | | -use ArrayObject; |
6 | | - |
7 | 5 | /** |
8 | | - * Usage Metric (generic result row from event/gauge queries) |
| 6 | + * Usage Metric — immutable value object for event/gauge query results. |
9 | 7 | * |
10 | | - * @extends ArrayObject<string, mixed> |
| 8 | + * Uses constructor property promotion with readonly properties for type safety. |
| 9 | + * Additional dimensions are stored in an extras map accessible via getAttribute(). |
11 | 10 | */ |
12 | | -class Metric extends ArrayObject |
| 11 | +class Metric |
13 | 12 | { |
14 | | - public function __construct(array $input = []) |
| 13 | + /** |
| 14 | + * @param array<string, mixed> $extras Additional dimension values (path, method, status, userAgent, etc.) |
| 15 | + */ |
| 16 | + public function __construct( |
| 17 | + public readonly string $id = '', |
| 18 | + public readonly string $metric = '', |
| 19 | + public readonly int|float $value = 0, |
| 20 | + public readonly ?string $time = null, |
| 21 | + public readonly string $resource = '', |
| 22 | + public readonly string $resourceId = '', |
| 23 | + public readonly int|string|null $tenant = null, |
| 24 | + public readonly array $extras = [], |
| 25 | + ) { |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Create a Metric from an associative array (e.g. a ClickHouse row). |
| 30 | + * |
| 31 | + * @param array<string, mixed> $data |
| 32 | + */ |
| 33 | + public static function fromArray(array $data): self |
15 | 34 | { |
16 | | - parent::__construct($input); |
| 35 | + $id = (string) ($data['$id'] ?? $data['id'] ?? ''); |
| 36 | + $metric = (string) ($data['metric'] ?? ''); |
| 37 | + |
| 38 | + $rawValue = $data['value'] ?? 0; |
| 39 | + if (is_int($rawValue) || is_float($rawValue)) { |
| 40 | + $value = $rawValue; |
| 41 | + } elseif (is_numeric($rawValue)) { |
| 42 | + $value = $rawValue + 0; |
| 43 | + } else { |
| 44 | + $value = 0; |
| 45 | + } |
| 46 | + |
| 47 | + $time = isset($data['time']) && is_string($data['time']) ? $data['time'] : null; |
| 48 | + $resource = (string) ($data['resource'] ?? ''); |
| 49 | + $resourceId = (string) ($data['resourceId'] ?? ''); |
| 50 | + |
| 51 | + $rawTenant = $data['tenant'] ?? null; |
| 52 | + if ($rawTenant === null) { |
| 53 | + $tenant = null; |
| 54 | + } elseif (is_int($rawTenant)) { |
| 55 | + $tenant = $rawTenant; |
| 56 | + } elseif (is_numeric($rawTenant)) { |
| 57 | + $tenant = $rawTenant + 0; |
| 58 | + } else { |
| 59 | + $tenant = (string) $rawTenant; |
| 60 | + } |
| 61 | + |
| 62 | + // Everything else goes into extras |
| 63 | + $knownKeys = ['$id', 'id', 'metric', 'value', 'time', 'resource', 'resourceId', 'tenant']; |
| 64 | + $extras = array_diff_key($data, array_flip($knownKeys)); |
| 65 | + |
| 66 | + return new self( |
| 67 | + id: $id, |
| 68 | + metric: $metric, |
| 69 | + value: $value, |
| 70 | + time: $time, |
| 71 | + resource: $resource, |
| 72 | + resourceId: $resourceId, |
| 73 | + tenant: $tenant, |
| 74 | + extras: $extras, |
| 75 | + ); |
17 | 76 | } |
18 | 77 |
|
19 | 78 | public function getId(): string |
20 | 79 | { |
21 | | - return (string) ($this['$id'] ?? $this['id'] ?? ''); |
| 80 | + return $this->id; |
22 | 81 | } |
23 | 82 |
|
24 | 83 | public function getMetric(): string |
25 | 84 | { |
26 | | - return (string) ($this['metric'] ?? ''); |
| 85 | + return $this->metric; |
27 | 86 | } |
28 | 87 |
|
29 | 88 | public function getValue(int $default = 0): int|float |
30 | 89 | { |
31 | | - $v = $this['value'] ?? $default; |
32 | | - if (is_int($v) || is_float($v)) { |
33 | | - return $v; |
34 | | - } |
35 | | - return is_numeric($v) ? +$v : $default; |
| 90 | + return $this->value !== 0 ? $this->value : $default; |
36 | 91 | } |
37 | 92 |
|
38 | 93 | public function getTime(): ?string |
39 | 94 | { |
40 | | - return isset($this['time']) && is_string($this['time']) ? $this['time'] : null; |
| 95 | + return $this->time; |
41 | 96 | } |
42 | 97 |
|
43 | 98 | public function getResource(): string |
44 | 99 | { |
45 | | - return (string) ($this['resource'] ?? ''); |
| 100 | + return $this->resource; |
46 | 101 | } |
47 | 102 |
|
48 | 103 | public function getResourceId(): string |
49 | 104 | { |
50 | | - return (string) ($this['resourceId'] ?? ''); |
| 105 | + return $this->resourceId; |
51 | 106 | } |
52 | 107 |
|
53 | 108 | public function getTenant(): int|string|null |
54 | 109 | { |
55 | | - $t = $this['tenant'] ?? null; |
56 | | - if ($t === null) { |
57 | | - return null; |
58 | | - } |
59 | | - if (is_int($t)) { |
60 | | - return $t; |
61 | | - } |
62 | | - if (is_numeric($t)) { |
63 | | - return $t + 0; |
64 | | - } |
65 | | - return (string) $t; |
| 110 | + return $this->tenant; |
66 | 111 | } |
67 | 112 |
|
68 | 113 | public function getAttribute(string $name, mixed $default = null): mixed |
69 | 114 | { |
70 | | - return $this[$name] ?? $default; |
71 | | - } |
72 | | - |
73 | | - public function setAttribute(string $key, mixed $value): static |
74 | | - { |
75 | | - $this[$key] = $value; |
76 | | - return $this; |
| 115 | + return match ($name) { |
| 116 | + 'id', '$id' => $this->id, |
| 117 | + 'metric' => $this->metric, |
| 118 | + 'value' => $this->value, |
| 119 | + 'time' => $this->time, |
| 120 | + 'resource' => $this->resource, |
| 121 | + 'resourceId' => $this->resourceId, |
| 122 | + 'tenant' => $this->tenant, |
| 123 | + default => $this->extras[$name] ?? $default, |
| 124 | + }; |
77 | 125 | } |
78 | 126 |
|
79 | 127 | public function hasAttribute(string $name): bool |
80 | 128 | { |
81 | | - return isset($this[$name]); |
| 129 | + return match ($name) { |
| 130 | + 'id', '$id', 'metric', 'value', 'time', 'resource', 'resourceId', 'tenant' => true, |
| 131 | + default => array_key_exists($name, $this->extras), |
| 132 | + }; |
82 | 133 | } |
83 | 134 |
|
84 | 135 | /** |
85 | 136 | * @return array<string, mixed> |
86 | 137 | */ |
87 | 138 | public function toArray(): array |
88 | 139 | { |
89 | | - return $this->getArrayCopy(); |
| 140 | + return array_merge( |
| 141 | + [ |
| 142 | + 'id' => $this->id, |
| 143 | + 'metric' => $this->metric, |
| 144 | + 'value' => $this->value, |
| 145 | + 'time' => $this->time, |
| 146 | + 'resource' => $this->resource, |
| 147 | + 'resourceId' => $this->resourceId, |
| 148 | + 'tenant' => $this->tenant, |
| 149 | + ], |
| 150 | + $this->extras, |
| 151 | + ); |
90 | 152 | } |
91 | 153 | } |
0 commit comments