Skip to content

Commit 0b68834

Browse files
committed
Fix period validation and SummingMergeTree ORDER BY key
- Fix validatePeriod() using isset() which returns false for null values; use array_key_exists() instead so 'inf' period is accepted correctly - Include all dimension columns (region, resource, resourceId, path, method, status) in events table ORDER BY key so SummingMergeTree preserves distinct dimension values instead of collapsing rows with different dimensions https://claude.ai/code/session_01UCN4sKD3Zkdbimn3QgTHSB
1 parent d68b268 commit 0b68834

1 file changed

Lines changed: 5 additions & 3 deletions

File tree

src/Usage/Adapter/ClickHouse.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*
1414
* 1. **events** (SummingMergeTree) – append-only API analytics & metered usage.
1515
* Columns: timestamp, metric, value, projectId, region, path, method, status, userAgent, resource, resourceId, tags, tenant.
16-
* ORDER BY (tenant, projectId, metric, timestamp) for fast range scans.
16+
* ORDER BY (tenant, projectId, metric, region, resource, ..., timestamp) for fast range scans.
1717
* Hourly and daily rollups via SummingMergeTree time-bucket grouping.
1818
*
1919
* 2. **gauges** (ReplacingMergeTree) – point-in-time resource counts.
@@ -130,7 +130,9 @@ public function setup(): void
130130
// Events table (SummingMergeTree – value column is summed on merge)
131131
$eventsTable = $this->eventsTable();
132132
$tenantCol = $this->sharedTables ? "tenant Nullable(UInt64),\n " : '';
133-
$orderKey = $this->sharedTables ? '(tenant, projectId, metric, timestamp)' : '(projectId, metric, timestamp)';
133+
$orderKey = $this->sharedTables
134+
? '(tenant, projectId, metric, region, resource, resourceId, path, method, status, timestamp)'
135+
: '(projectId, metric, region, resource, resourceId, path, method, status, timestamp)';
134136

135137
$this->query("
136138
CREATE TABLE IF NOT EXISTS {$eventsTable} (
@@ -870,7 +872,7 @@ private function validateHost(string $host): void
870872

871873
private function validatePeriod(string $period): void
872874
{
873-
if (!isset(self::PERIODS[$period])) {
875+
if (!array_key_exists($period, self::PERIODS)) {
874876
throw new \InvalidArgumentException("Invalid period '{$period}'. Allowed: " . implode(', ', array_keys(self::PERIODS)));
875877
}
876878
}

0 commit comments

Comments
 (0)