Skip to content

Commit 4c98ab5

Browse files
lohanidamodarclaude
andcommitted
fix: align time series return types with actual float aggregation output
PHPStan level max flagged getTimeSeries() annotations as int while the ClickHouse adapter emits floats via agg_value cast. Updates the abstract, both adapters, the Usage facade, and zeroFillTimeSeries to float. Also throws on json_encode failure in Usage::collect so the md5() input is guaranteed string instead of string|false. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 2a2a479 commit 4c98ab5

4 files changed

Lines changed: 10 additions & 10 deletions

File tree

src/Usage/Adapter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ abstract public function addBatch(array $metrics, string $type, int $batchSize =
4747
* @param array<\Utopia\Query\Query> $queries Additional query filters
4848
* @param bool $zeroFill Whether to fill gaps with zero values
4949
* @param string|null $type Metric type: 'event', 'gauge', or null (query both)
50-
* @return array<string, array{total: int, data: array<array{value: int, date: string}>}>
50+
* @return array<string, array{total: float, data: array<array{value: float, date: string}>}>
5151
*/
5252
abstract public function getTimeSeries(array $metrics, string $interval, string $startDate, string $endDate, array $queries = [], bool $zeroFill = true, ?string $type = null): array;
5353

src/Usage/Adapter/ClickHouse.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1896,7 +1896,7 @@ public function sumDailyBatch(array $metrics, array $queries = []): array
18961896
* @param array<Query> $queries
18971897
* @param bool $zeroFill
18981898
* @param string|null $type 'event', 'gauge', or null (both)
1899-
* @return array<string, array{total: int, data: array<array{value: int, date: string}>}>
1899+
* @return array<string, array{total: float, data: array<array{value: float, date: string}>}>
19001900
* @throws Exception
19011901
*/
19021902
public function getTimeSeries(array $metrics, string $interval, string $startDate, string $endDate, array $queries = [], bool $zeroFill = true, ?string $type = null): array
@@ -1967,7 +1967,7 @@ public function getTimeSeries(array $metrics, string $interval, string $startDat
19671967
* @param string $endDate
19681968
* @param array<Query> $queries
19691969
* @param string $type
1970-
* @return array<string, array{total: int, data: array<array{value: int, date: string}>}>
1970+
* @return array<string, array{total: float, data: array<array{value: float, date: string}>}>
19711971
* @throws Exception
19721972
*/
19731973
private function getTimeSeriesFromTable(array $metrics, string $interval, string $startDate, string $endDate, array $queries, string $type): array
@@ -2040,7 +2040,7 @@ private function getTimeSeriesFromTable(array $metrics, string $interval, string
20402040
if (is_array($json) && isset($json['data']) && is_array($json['data'])) {
20412041
foreach ($json['data'] as $row) {
20422042
$metricName = $row['metric'] ?? '';
2043-
$bucketTime = $row['bucket'] ?? '';
2043+
$bucketTime = (string) ($row['bucket'] ?? '');
20442044
$value = (float) ($row['agg_value'] ?? 0);
20452045

20462046
if (!isset($output[$metricName])) {
@@ -2049,7 +2049,7 @@ private function getTimeSeriesFromTable(array $metrics, string $interval, string
20492049

20502050
// Format bucket time
20512051
$formattedDate = $bucketTime;
2052-
if (is_string($bucketTime) && strpos($bucketTime, 'T') === false) {
2052+
if (strpos($bucketTime, 'T') === false) {
20532053
$formattedDate = str_replace(' ', 'T', $bucketTime) . '+00:00';
20542054
}
20552055

@@ -2067,11 +2067,11 @@ private function getTimeSeriesFromTable(array $metrics, string $interval, string
20672067
/**
20682068
* Fill gaps in time series data with zero-value entries.
20692069
*
2070-
* @param array<array{value: int, date: string}> $data Existing data points
2070+
* @param array<array{value: float, date: string}> $data Existing data points
20712071
* @param string $interval '1h' or '1d'
20722072
* @param string $startDate Start datetime
20732073
* @param string $endDate End datetime
2074-
* @return array<array{value: int, date: string}>
2074+
* @return array<array{value: float, date: string}>
20752075
*/
20762076
private function zeroFillTimeSeries(array $data, string $interval, string $startDate, string $endDate): array
20772077
{

src/Usage/Adapter/Database.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ public function addBatch(array $metrics, string $type = Usage::TYPE_EVENT, int $
161161
* @param array<Query> $queries
162162
* @param bool $zeroFill
163163
* @param string|null $type
164-
* @return array<string, array{total: int, data: array<array{value: int, date: string}>}>
164+
* @return array<string, array{total: float, data: array<array{value: float, date: string}>}>
165165
*/
166166
public function getTimeSeries(array $metrics, string $interval, string $startDate, string $endDate, array $queries = [], bool $zeroFill = true, ?string $type = null): array
167167
{

src/Usage/Usage.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public function addBatch(array $metrics, string $type = self::TYPE_EVENT, int $b
105105
* @param array<\Utopia\Query\Query> $queries Additional filters
106106
* @param bool $zeroFill Whether to fill gaps with zero values
107107
* @param string|null $type Metric type: 'event', 'gauge', or null (query both)
108-
* @return array<string, array{total: int, data: array<array{value: int, date: string}>}>
108+
* @return array<string, array{total: float, data: array<array{value: float, date: string}>}>
109109
* @throws \Exception
110110
*/
111111
public function getTimeSeries(array $metrics, string $interval, string $startDate, string $endDate, array $queries = [], bool $zeroFill = true, ?string $type = null): array
@@ -299,7 +299,7 @@ public function collect(string $metric, int $value, string $type, array $tags =
299299
throw new \InvalidArgumentException("Invalid metric type '{$type}'. Allowed: " . self::TYPE_EVENT . ', ' . self::TYPE_GAUGE);
300300
}
301301

302-
$tagsHash = !empty($tags) ? md5(json_encode($tags)) : '';
302+
$tagsHash = !empty($tags) ? md5(json_encode($tags, JSON_THROW_ON_ERROR)) : '';
303303
$key = $metric . ':' . $type . ':' . $tagsHash;
304304

305305
if ($type === 'event') {

0 commit comments

Comments
 (0)