Skip to content

Commit 5e21f64

Browse files
lohanidamodarclaude
andcommitted
refactor: split events and gauges tables, add event-specific columns
Split the single MergeTree table into two separate tables: - Events table with dedicated columns for path, method, status, resource, resourceId - Gauges table with simple metric/value/time/tags schema Event-specific columns are automatically extracted from tags during addBatch. The daily SummingMergeTree MV now aggregates by metric, resource, resourceId. All read methods accept an optional $type parameter to target specific tables, with null querying both tables transparently. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent e39dc7e commit 5e21f64

9 files changed

Lines changed: 1514 additions & 543 deletions

File tree

src/Usage/Adapter.php

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@ abstract public function setup(): void;
2424
/**
2525
* Add metrics in batch (raw append).
2626
*
27-
* Appends rows to the single MergeTree table. Each row must include
28-
* a 'type' field ('event' or 'gauge') and a 'metric' name.
27+
* Routes rows to the correct table based on the $type parameter.
28+
* For events, path/method/status/resource/resourceId are extracted from tags
29+
* into dedicated columns; remaining tags stay in the tags JSON.
2930
*
3031
* @param array<array{metric: string, value: int, type: string, tags?: array<string,mixed>}> $metrics
32+
* @param string $type Metric type: 'event' or 'gauge' — determines which table to write to
3133
* @param int $batchSize Maximum number of metrics per INSERT statement
3234
*/
33-
abstract public function addBatch(array $metrics, int $batchSize = 1000): bool;
35+
abstract public function addBatch(array $metrics, string $type, int $batchSize = 1000): bool;
3436

3537
/**
3638
* Get time series data for metrics with query-time aggregation.
@@ -44,21 +46,23 @@ abstract public function addBatch(array $metrics, int $batchSize = 1000): bool;
4446
* @param string $endDate End datetime string
4547
* @param array<\Utopia\Query\Query> $queries Additional query filters
4648
* @param bool $zeroFill Whether to fill gaps with zero values
49+
* @param string|null $type Metric type: 'event', 'gauge', or null (query both)
4750
* @return array<string, array{total: int, data: array<array{value: int, date: string}>}>
4851
*/
49-
abstract public function getTimeSeries(array $metrics, string $interval, string $startDate, string $endDate, array $queries = [], bool $zeroFill = true): array;
52+
abstract public function getTimeSeries(array $metrics, string $interval, string $startDate, string $endDate, array $queries = [], bool $zeroFill = true, ?string $type = null): array;
5053

5154
/**
5255
* Get total value for a single metric.
5356
*
5457
* Returns sum for event metrics, latest value for gauge metrics.
55-
* Auto-detects type from stored data.
58+
* When $type is null, queries both tables.
5659
*
5760
* @param string $metric Metric name
5861
* @param array<\Utopia\Query\Query> $queries Additional query filters
62+
* @param string|null $type Metric type: 'event', 'gauge', or null (query both)
5963
* @return int
6064
*/
61-
abstract public function getTotal(string $metric, array $queries = []): int;
65+
abstract public function getTotal(string $metric, array $queries = [], ?string $type = null): int;
6266

6367
/**
6468
* Get totals for multiple metrics in a single query.
@@ -67,42 +71,47 @@ abstract public function getTotal(string $metric, array $queries = []): int;
6771
*
6872
* @param array<string> $metrics List of metric names
6973
* @param array<\Utopia\Query\Query> $queries Additional query filters
74+
* @param string|null $type Metric type: 'event', 'gauge', or null (query both)
7075
* @return array<string, int>
7176
*/
72-
abstract public function getTotalBatch(array $metrics, array $queries = []): array;
77+
abstract public function getTotalBatch(array $metrics, array $queries = [], ?string $type = null): array;
7378

7479
/**
7580
* Purge usage metrics matching the given queries.
7681
* When no queries are provided, all metrics are deleted.
7782
*
7883
* @param array<\Utopia\Query\Query> $queries
84+
* @param string|null $type Metric type: 'event', 'gauge', or null (purge both)
7985
*/
80-
abstract public function purge(array $queries = []): bool;
86+
abstract public function purge(array $queries = [], ?string $type = null): bool;
8187

8288
/**
8389
* Find metrics using Query objects.
8490
*
8591
* @param array<\Utopia\Query\Query> $queries
92+
* @param string|null $type Metric type: 'event', 'gauge', or null (query both)
8693
* @return array<Metric>
8794
*/
88-
abstract public function find(array $queries = []): array;
95+
abstract public function find(array $queries = [], ?string $type = null): array;
8996

9097
/**
9198
* Count metrics using Query objects.
9299
*
93100
* @param array<\Utopia\Query\Query> $queries
101+
* @param string|null $type Metric type: 'event', 'gauge', or null (count both)
94102
* @return int
95103
*/
96-
abstract public function count(array $queries = []): int;
104+
abstract public function count(array $queries = [], ?string $type = null): int;
97105

98106
/**
99107
* Sum metric values using Query objects.
100108
*
101109
* @param array<\Utopia\Query\Query> $queries
102110
* @param string $attribute Attribute to sum (default: 'value')
111+
* @param string|null $type Metric type: 'event', 'gauge', or null (sum both)
103112
* @return int
104113
*/
105-
abstract public function sum(array $queries = [], string $attribute = 'value'): int;
114+
abstract public function sum(array $queries = [], string $attribute = 'value', ?string $type = null): int;
106115

107116
/**
108117
* Set the namespace prefix for table names.

0 commit comments

Comments
 (0)