Skip to content

Commit 31da962

Browse files
lohanidamodarclaude
andcommitted
fix: tenant as string, rewrite tests for new API, audit fixes
- Change tenant type from ?int to ?string everywhere (Adapter, Usage, ClickHouse, Database, Metric) - ClickHouse tenant column: Nullable(String) instead of Nullable(UInt64) - Fix tenant key mismatch: validateMetricsBatch now checks '$tenant' matching resolveTenantFromMetric - Fix MV GROUP BY: conditional on sharedTables (no tenant column when sharedTables=false) - Fix billing/daily target tables: conditional tenant column and ORDER BY - Add collect() validation: empty metric name and negative value checks - Fix ltrim() misuse in buildWhereClause: getTenantFilter now returns bare condition without ' AND ' prefix - Fix SQL.php: replace 'Audit' references with 'Usage', remove unused Database import - Fix parseQueries: use Int64 param type for value attribute instead of String - Rewrite all tests (UsageBase, ClickHouseTest, MetricTest) for new API: replace increment/set/period-based methods with addBatch/collect/ getTotal/getTotalBatch/getTimeSeries Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 15112a0 commit 31da962

9 files changed

Lines changed: 474 additions & 544 deletions

File tree

src/Usage/Adapter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,10 @@ abstract public function setNamespace(string $namespace): self;
115115
/**
116116
* Set the tenant ID for multi-tenant support.
117117
*
118-
* @param int|null $tenant
118+
* @param string|null $tenant
119119
* @return self
120120
*/
121-
abstract public function setTenant(?int $tenant): self;
121+
abstract public function setTenant(?string $tenant): self;
122122

123123
/**
124124
* Enable or disable shared tables mode (multi-tenant with tenant column).

src/Usage/Adapter/ClickHouse.php

Lines changed: 71 additions & 52 deletions
Large diffs are not rendered by default.

src/Usage/Adapter/Database.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -372,10 +372,10 @@ public function setNamespace(string $namespace): self
372372
/**
373373
* Set the tenant ID for multi-tenant support.
374374
*
375-
* @param int|null $tenant
375+
* @param string|null $tenant
376376
* @return self
377377
*/
378-
public function setTenant(?int $tenant): self
378+
public function setTenant(?string $tenant): self
379379
{
380380
$this->db->setTenant($tenant);
381381
return $this;

src/Usage/Adapter/SQL.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@
44

55
use Utopia\Usage\Adapter;
66
use Utopia\Usage\Metric;
7-
use Utopia\Database\Database;
87
use Utopia\Database\Document;
98

109
/**
11-
* Base SQL Adapter for Audit
10+
* Base SQL Adapter for Usage
1211
*
1312
* This is an abstract base class for SQL-based adapters (Database, ClickHouse, etc.)
1413
* It provides common functionality and references schema definitions from the Metric class.
@@ -18,7 +17,7 @@ abstract class SQL extends Adapter
1817
public const COLLECTION = 'usage';
1918

2019
/**
21-
* Get the collection/table name for audit logs.
20+
* Get the collection/table name for usage metrics.
2221
*
2322
* @return string
2423
*/
@@ -28,7 +27,7 @@ public function getCollectionName(): string
2827
}
2928

3029
/**
31-
* Get attribute definitions for audit logs.
30+
* Get attribute definitions for usage metrics.
3231
*
3332
* Delegates to Metric class which defines the metric schema.
3433
*
@@ -40,7 +39,7 @@ public function getAttributes(): array
4039
}
4140

4241
/**
43-
* Get attribute documents for audit logs.
42+
* Get attribute documents for usage metrics.
4443
*
4544
* @return array<Document>
4645
*/
@@ -50,7 +49,7 @@ public function getAttributeDocuments(): array
5049
}
5150

5251
/**
53-
* Get index definitions for audit logs.
52+
* Get index definitions for usage metrics.
5453
*
5554
* Delegates to Metric class which defines the metric indexes.
5655
*
@@ -62,7 +61,7 @@ public function getIndexes(): array
6261
}
6362

6463
/**
65-
* Get index documents for audit logs.
64+
* Get index documents for usage metrics.
6665
*
6766
* @return array<Document>
6867
*/

src/Usage/Metric.php

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -153,25 +153,17 @@ public function getTags(): array
153153
* architectures. This allows data isolation at the application level while
154154
* sharing the same database tables.
155155
*
156-
* @return int|null The tenant ID, or null if not set or not using multi-tenancy
156+
* @return string|null The tenant ID, or null if not set or not using multi-tenancy
157157
*/
158-
public function getTenant(): ?int
158+
public function getTenant(): ?string
159159
{
160160
$tenant = $this->getAttribute('tenant');
161161

162162
if ($tenant === null) {
163163
return null;
164164
}
165165

166-
if (is_int($tenant)) {
167-
return $tenant;
168-
}
169-
170-
if (is_numeric($tenant)) {
171-
return (int) $tenant;
172-
}
173-
174-
return null;
166+
return (string) $tenant;
175167
}
176168

177169
/**

src/Usage/Usage.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,10 @@ public function setNamespace(string $namespace): self
201201
/**
202202
* Set the tenant ID for multi-tenant support.
203203
*
204-
* @param int|null $tenant
204+
* @param string|null $tenant
205205
* @return $this
206206
*/
207-
public function setTenant(?int $tenant): self
207+
public function setTenant(?string $tenant): self
208208
{
209209
$this->adapter->setTenant($tenant);
210210
return $this;
@@ -237,6 +237,12 @@ public function setSharedTables(bool $sharedTables): self
237237
*/
238238
public function collect(string $metric, int $value, string $type, array $tags = []): self
239239
{
240+
if (empty($metric)) {
241+
throw new \InvalidArgumentException('Metric name cannot be empty');
242+
}
243+
if ($value < 0) {
244+
throw new \InvalidArgumentException('Value cannot be negative');
245+
}
240246
if ($type !== self::TYPE_EVENT && $type !== self::TYPE_GAUGE) {
241247
throw new \InvalidArgumentException("Invalid metric type '{$type}'. Allowed: " . self::TYPE_EVENT . ', ' . self::TYPE_GAUGE);
242248
}

0 commit comments

Comments
 (0)