Skip to content

Commit e99d3a9

Browse files
committed
fix: remove FINAL from MergeTree queries, fix PHPStan errors, fix Database adapter
1 parent 31da962 commit e99d3a9

3 files changed

Lines changed: 47 additions & 13 deletions

File tree

src/Usage/Adapter/ClickHouse.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class ClickHouse extends SQL
6161
private Client $client;
6262

6363
/** @var bool Whether to use FINAL in SELECT queries to force merge-on-read (tests) */
64-
private bool $useFinal = true;
64+
private bool $useFinal = false;
6565

6666
protected ?string $tenant = null;
6767

@@ -523,9 +523,8 @@ private function getTableName(): string
523523
*/
524524
private function buildTableReference(string $tableName, ?bool $useFinal = null): string
525525
{
526-
$useFinal = $useFinal ?? $this->useFinal;
527526
$escapedTable = $this->escapeIdentifier($this->database) . '.' . $this->escapeIdentifier($tableName);
528-
return $escapedTable . ($useFinal ? ' FINAL' : '');
527+
return $escapedTable;
529528
}
530529

531530
/**
@@ -1307,7 +1306,7 @@ public function addBatch(array $metrics, int $batchSize = self::INSERT_BATCH_SIZ
13071306
$rows[] = $encoded;
13081307
}
13091308

1310-
if (!empty($rows)) {
1309+
if (count($rows) > 0) {
13111310
$this->insert($tableName, $rows);
13121311
}
13131312
}
@@ -1332,7 +1331,7 @@ private function resolveTenantFromMetric(array $metricData): ?string
13321331
return $tenant;
13331332
}
13341333

1335-
return (string) $tenant;
1334+
return is_numeric($tenant) ? (string) $tenant : (string) ($tenant ?? '');
13361335
}
13371336

13381337
/**

src/Usage/Adapter/Database.php

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public function addBatch(array $metrics, int $batchSize = 1000): bool
108108
$this->db->getAuthorization()->skip(function () use ($metrics, $batchSize) {
109109
$documents = [];
110110
foreach ($metrics as $metric) {
111-
$type = $metric['type'] ?? 'event';
111+
$type = $metric['type'];
112112

113113
if ($type !== 'event' && $type !== 'gauge') {
114114
throw new \InvalidArgumentException("Invalid type '{$type}'. Allowed: event, gauge");
@@ -164,30 +164,65 @@ public function getTimeSeries(array $metrics, string $interval, string $startDat
164164
/**
165165
* Get total value for a single metric.
166166
*
167-
* Stub implementation for Database adapter.
167+
* Returns SUM for event metrics, latest value for gauge metrics.
168168
*
169169
* @param string $metric
170170
* @param array<Query> $queries
171171
* @return int
172172
*/
173173
public function getTotal(string $metric, array $queries = []): int
174174
{
175-
// Stub: not yet implemented
176-
return 0;
175+
$allQueries = array_merge($queries, [
176+
Query::equal('metric', [$metric]),
177+
]);
178+
179+
/** @var array<Metric> $results */
180+
$results = $this->find($allQueries);
181+
182+
if (empty($results)) {
183+
return 0;
184+
}
185+
186+
// Determine type from first result
187+
$type = $results[0]->getType();
188+
189+
if ($type === 'gauge') {
190+
// For gauge, return the last (most recently inserted) value
191+
$lastResult = end($results);
192+
return $lastResult !== false ? ($lastResult->getValue(0) ?? 0) : 0;
193+
}
194+
195+
// For events, SUM all values
196+
$sum = 0;
197+
foreach ($results as $result) {
198+
$sum += (int) ($result->getValue(0) ?? 0);
199+
}
200+
201+
return $sum;
177202
}
178203

179204
/**
180205
* Get totals for multiple metrics.
181206
*
182-
* Stub implementation for Database adapter.
207+
* Returns SUM for event metrics, latest value for gauge metrics.
183208
*
184209
* @param array<string> $metrics
185210
* @param array<Query> $queries
186211
* @return array<string, int>
187212
*/
188213
public function getTotalBatch(array $metrics, array $queries = []): array
189214
{
190-
return \array_fill_keys($metrics, 0);
215+
if (empty($metrics)) {
216+
return [];
217+
}
218+
219+
$totals = \array_fill_keys($metrics, 0);
220+
221+
foreach ($metrics as $metric) {
222+
$totals[$metric] = $this->getTotal($metric, $queries);
223+
}
224+
225+
return $totals;
191226
}
192227

193228
/**
@@ -377,7 +412,7 @@ public function setNamespace(string $namespace): self
377412
*/
378413
public function setTenant(?string $tenant): self
379414
{
380-
$this->db->setTenant($tenant);
415+
$this->db->setTenant($tenant !== null ? (int) $tenant : null);
381416
return $this;
382417
}
383418

src/Usage/Metric.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public function getTenant(): ?string
163163
return null;
164164
}
165165

166-
return (string) $tenant;
166+
return is_string($tenant) ? $tenant : (is_numeric($tenant) ? (string) $tenant : null);
167167
}
168168

169169
/**

0 commit comments

Comments
 (0)