Skip to content

Commit 554c12d

Browse files
loks0nclaude
andcommitted
refactor: fix namespace/sharedTables at construction, thread tenant explicitly
Namespace and shared-tables mode are deployment-fixed, yet were mutable setters on the adapter — and each setter flushed the buffer. Tenant was likewise adapter state, so writing one project's metrics required a setTenant()+flush() per message (one ClickHouse round-trip each). - ClickHouse adapter takes namespace + sharedTables as constructor args; setNamespace/setSharedTables/setTenant setters and their getters removed, along with all adapter tenant state. - collect() gains an optional per-row $tenant, stored on the buffer entry and folded into the dedup key, so one buffer holds many tenants and flushes without per-tenant round-trips. - Read/purge methods take an explicit ?string $tenant, applied as a normal query filter (parseQueries already supports the 'tenant' attribute) — no hidden adapter state. Base Adapter + Database adapter signatures updated to match; Database honours it via its existing setTenant. PHPStan max clean. Test migration to the new signatures follows in the next commit. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent c5c721d commit 554c12d

7 files changed

Lines changed: 727 additions & 803 deletions

File tree

src/Usage/Adapter.php

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,10 @@ 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+
* @param string|null $tenant Tenant to scope the query to (shared-tables mode)
5051
* @return array<string, array{total: float, data: array<array{value: float, date: string}>}>
5152
*/
52-
abstract public function getTimeSeries(array $metrics, string $interval, string $startDate, string $endDate, array $queries = [], bool $zeroFill = true, ?string $type = null): array;
53+
abstract public function getTimeSeries(array $metrics, string $interval, string $startDate, string $endDate, array $queries = [], bool $zeroFill = true, ?string $type = null, ?string $tenant = null): array;
5354

5455
/**
5556
* Get total value for a single metric.
@@ -60,9 +61,8 @@ abstract public function getTimeSeries(array $metrics, string $interval, string
6061
* @param string $metric Metric name
6162
* @param array<\Utopia\Query\Query> $queries Additional query filters
6263
* @param string|null $type Metric type: 'event', 'gauge', or null (query both)
63-
* @return int
6464
*/
65-
abstract public function getTotal(string $metric, array $queries = [], ?string $type = null): int;
65+
abstract public function getTotal(string $metric, array $queries = [], ?string $type = null, ?string $tenant = null): int;
6666

6767
/**
6868
* Get totals for multiple metrics in a single query.
@@ -74,25 +74,25 @@ abstract public function getTotal(string $metric, array $queries = [], ?string $
7474
* @param string|null $type Metric type: 'event', 'gauge', or null (query both)
7575
* @return array<string, int>
7676
*/
77-
abstract public function getTotalBatch(array $metrics, array $queries = [], ?string $type = null): array;
77+
abstract public function getTotalBatch(array $metrics, array $queries = [], ?string $type = null, ?string $tenant = null): array;
7878

7979
/**
8080
* Purge usage metrics matching the given queries.
8181
* When no queries are provided, all metrics are deleted.
8282
*
83-
* @param array<\Utopia\Query\Query> $queries
84-
* @param string|null $type Metric type: 'event', 'gauge', or null (purge both)
83+
* @param array<\Utopia\Query\Query> $queries
84+
* @param string|null $type Metric type: 'event', 'gauge', or null (purge both)
8585
*/
86-
abstract public function purge(array $queries = [], ?string $type = null): bool;
86+
abstract public function purge(array $queries = [], ?string $type = null, ?string $tenant = null): bool;
8787

8888
/**
8989
* Find metrics using Query objects.
9090
*
91-
* @param array<\Utopia\Query\Query> $queries
92-
* @param string|null $type Metric type: 'event', 'gauge', or null (query both)
91+
* @param array<\Utopia\Query\Query> $queries
92+
* @param string|null $type Metric type: 'event', 'gauge', or null (query both)
9393
* @return array<Metric>
9494
*/
95-
abstract public function find(array $queries = [], ?string $type = null): array;
95+
abstract public function find(array $queries = [], ?string $type = null, ?string $tenant = null): array;
9696

9797
/**
9898
* Count metrics using Query objects.
@@ -102,25 +102,23 @@ abstract public function find(array $queries = [], ?string $type = null): array;
102102
* This keeps large counts cheap for endpoints that only need a capped
103103
* total. When $max is null the count is unbounded.
104104
*
105-
* @param array<\Utopia\Query\Query> $queries
106-
* @param string|null $type Metric type: 'event', 'gauge', or null (count both)
107-
* @param int|null $max Optional upper bound for the count (inclusive)
108-
* @return int
105+
* @param array<\Utopia\Query\Query> $queries
106+
* @param string|null $type Metric type: 'event', 'gauge', or null (count both)
107+
* @param int|null $max Optional upper bound for the count (inclusive)
109108
*/
110-
abstract public function count(array $queries = [], ?string $type = null, ?int $max = null): int;
109+
abstract public function count(array $queries = [], ?string $type = null, ?int $max = null, ?string $tenant = null): int;
111110

112111
/**
113112
* Sum metric values using Query objects.
114113
*
115114
* Events-only by default because summing gauges is semantically meaningless
116115
* (adding point-in-time snapshots doesn't produce a useful total).
117116
*
118-
* @param array<\Utopia\Query\Query> $queries
119-
* @param string $attribute Attribute to sum (default: 'value')
120-
* @param string $type Metric type: 'event' or 'gauge'
121-
* @return int
117+
* @param array<\Utopia\Query\Query> $queries
118+
* @param string $attribute Attribute to sum (default: 'value')
119+
* @param string $type Metric type: 'event' or 'gauge'
122120
*/
123-
abstract public function sum(array $queries = [], string $attribute = 'value', string $type = Usage::TYPE_EVENT): int;
121+
abstract public function sum(array $queries = [], string $attribute = 'value', string $type = Usage::TYPE_EVENT, ?string $tenant = null): int;
124122

125123
/**
126124
* Find event metrics from the pre-aggregated daily table.
@@ -130,34 +128,33 @@ abstract public function sum(array $queries = [], string $attribute = 'value', s
130128
* Note: Daily MV only stores event metrics. This method always queries
131129
* the daily events table — gauges are never pre-aggregated.
132130
*
133-
* @param array<\Utopia\Query\Query> $queries Filters (metric, time range, resource, etc.)
131+
* @param array<\Utopia\Query\Query> $queries Filters (metric, time range, resource, etc.)
134132
* @return array<Metric>
135133
*/
136-
abstract public function findDaily(array $queries = []): array;
134+
abstract public function findDaily(array $queries = [], ?string $tenant = null): array;
137135

138136
/**
139137
* Sum event metric values from the pre-aggregated daily table.
140138
*
141139
* Note: Daily MV only stores event metrics. This method always queries
142140
* the daily events table — gauges are never pre-aggregated.
143141
*
144-
* @param array<\Utopia\Query\Query> $queries
145-
* @param string $attribute Attribute to sum (default: 'value')
146-
* @return int
142+
* @param array<\Utopia\Query\Query> $queries
143+
* @param string $attribute Attribute to sum (default: 'value')
147144
*/
148-
abstract public function sumDaily(array $queries = [], string $attribute = 'value'): int;
145+
abstract public function sumDaily(array $queries = [], string $attribute = 'value', ?string $tenant = null): int;
149146

150147
/**
151148
* Sum multiple event metrics from the pre-aggregated daily table in one query.
152149
*
153150
* Note: Daily MV only stores event metrics. This method always queries
154151
* the daily events table — gauges are never pre-aggregated.
155152
*
156-
* @param array<string> $metrics List of metric names
157-
* @param array<\Utopia\Query\Query> $queries Additional filters (e.g. date range)
153+
* @param array<string> $metrics List of metric names
154+
* @param array<\Utopia\Query\Query> $queries Additional filters (e.g. date range)
158155
* @return array<string, int> Metric name => sum value
159156
*/
160-
abstract public function sumDailyBatch(array $metrics, array $queries = []): array;
157+
abstract public function sumDailyBatch(array $metrics, array $queries = [], ?string $tenant = null): array;
161158

162159
/**
163160
* Enable parity sampling for routed reads. At rate=0 the sampler is

0 commit comments

Comments
 (0)