Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Usage/Adapter/ClickHouse.php
Original file line number Diff line number Diff line change
Expand Up @@ -1216,6 +1216,8 @@ private function getColumnType(string $id, string $type = 'event'): string
'clientType', 'clientCode', 'clientName', 'clientVersion',
'clientEngine', 'clientEngineVersion',
'deviceName', 'deviceBrand', 'deviceModel',
// metric-scoped dimensions; small closed value spaces per metric
'category', 'outcome',
'hostname', 'ip',
// premium geo (lower-cardinality only; city/isp/AS org/connection org
// are high-cardinality and intentionally fall through to Nullable(String))
Expand Down
15 changes: 14 additions & 1 deletion src/Usage/Metric.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ class Metric extends ArrayObject
// sdk identity
'sdk', 'sdkVersion',
'deviceName', 'deviceBrand', 'deviceModel',
Comment thread
greptile-apps[bot] marked this conversation as resolved.
// metric-scoped: what the row is about, and how it ended.
// Named `category`, not `type`: the SQL adapter already writes a `type`
// column holding 'event'/'gauge', and a dimension of that name silently
// overwrote it through array_merge.
'category', 'outcome',
];

/**
Expand Down Expand Up @@ -670,6 +675,13 @@ public static function getEventSchema(): array
// sdk identity
$stringColumn('sdk', 256),
$stringColumn('sdkVersion', 255),
// metric-scoped dimensions. `category` is whatever the metric is
// broken down by — a messaging channel, a calling code — and
// `outcome` is how the attempt ended. Both are read with `metric`
// pinned, so the same column carries different value spaces across
// metrics, the way resourceId already carries ids of every kind.
$stringColumn('category', 64),
$stringColumn('outcome', 32),
Comment on lines +683 to +684

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Existing Database schemas stay stale

If a Database-backed installation already has its event collection, Database::setup() swallows the duplicate-collection result without adding these new attributes, while addBatch() starts including them in documents. Consequently, upgraded deployments cannot reliably persist events containing category or outcome.

Knowledge Base Used:

Prompt To Fix With AI
This is a comment left during a code review.
Path: src/Usage/Metric.php
Line: 683-684

Comment:
**Existing Database schemas stay stale**

If a Database-backed installation already has its event collection, `Database::setup()` swallows the duplicate-collection result without adding these new attributes, while `addBatch()` starts including them in documents. Consequently, upgraded deployments cannot reliably persist events containing `category` or `outcome`.

**Knowledge Base Used:**
- [Usage domain model](https://app.greptile.com/appwrite/-/custom-context/knowledge-base/utopia-php/usage/-/docs/usage-domain-model.md)
- [ClickHouse usage adapter](https://app.greptile.com/appwrite/-/custom-context/knowledge-base/utopia-php/usage/-/docs/clickhouse-usage-adapter.md)

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Fix in Claude Code Fix in Codex

$stringColumn('deviceName', 256),
$stringColumn('deviceBrand', 256),
$stringColumn('deviceModel', 255),
Expand Down Expand Up @@ -756,14 +768,15 @@ public static function getSchema(): array
public static function getEventIndexes(): array
{
$indexed = [
'category', 'outcome',
'path', 'method', 'status',
'service', 'resourceType', 'resourceId', 'resourceInternalId',
'teamId', 'teamInternalId',
'country', 'region', 'hostname', 'ip',
'osName', 'clientType', 'clientName', 'deviceName',
];

$setIndexed = ['status', 'method', 'country', 'service', 'clientType', 'osName'];
$setIndexed = ['status', 'method', 'country', 'service', 'clientType', 'osName', 'category', 'outcome'];

return array_map(
static function (string $col) use ($setIndexed): array {
Expand Down
1 change: 1 addition & 0 deletions tests/Usage/Adapter/ClickHouseSchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ private function expectedDimAssertions(array $columns, string $type): array
'connectionUsageType', 'autonomousSystemNumber',
'sdk', 'sdkVersion',
'ordinal',
'category', 'outcome',
];

$baseKey = ['id', 'metric', 'value', 'time', 'tenant'];
Expand Down
1 change: 1 addition & 0 deletions tests/Usage/MetricTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,7 @@ public function testEventColumnsConstant(): void
'clientEngine', 'clientEngineVersion',
'sdk', 'sdkVersion',
'deviceName', 'deviceBrand', 'deviceModel',
'category', 'outcome',
];
$this->assertSame($expected, Metric::EVENT_COLUMNS);
}
Expand Down
Loading