Skip to content

Commit 7b5a7e8

Browse files
committed
Address remaining CodeRabbit review comments
- Include time and tenant in gauge buffer dedup key to prevent overwrites of distinct snapshots - Add non-root user to Dockerfile for production safety - Add getValue() edge case tests for floats, numeric strings, and non-numeric fallback https://claude.ai/code/session_01UCN4sKD3Zkdbimn3QgTHSB
1 parent 97bddfa commit 7b5a7e8

3 files changed

Lines changed: 29 additions & 1 deletion

File tree

Dockerfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ FROM php:8.3.3-cli-alpine3.19 as final
1212

1313
LABEL maintainer="team@appwrite.io"
1414

15+
RUN addgroup -S appgroup && adduser -S appuser -G appgroup
16+
1517
WORKDIR /code
1618

1719
COPY --from=step0 /src/vendor /code/vendor
@@ -21,4 +23,6 @@ COPY ./tests /code/tests
2123
COPY ./src /code/src
2224
COPY ./phpunit.xml /code/phpunit.xml
2325

26+
USER appuser
27+
2428
CMD [ "tail", "-f", "/dev/null" ]

src/Usage/Usage.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,8 @@ public function setGauges(array $gauges, int $batchSize = 1000): bool
151151
public function collectGauge(array $gauge): self
152152
{
153153
$key = ($gauge['metric'] ?? '') . ':' . ($gauge['resource'] ?? '') . ':' . ($gauge['projectId'] ?? '')
154-
. ':' . ($gauge['resourceId'] ?? '') . ':' . ($gauge['region'] ?? '') . ':' . ($gauge['period'] ?? '');
154+
. ':' . ($gauge['resourceId'] ?? '') . ':' . ($gauge['region'] ?? '') . ':' . ($gauge['period'] ?? '')
155+
. ':' . ($gauge['time'] ?? '') . ':' . ($gauge['$tenant'] ?? '');
155156
$this->gaugeBuffer[$key] = $gauge;
156157
$this->bufferCount++;
157158
return $this;

tests/Usage/MetricTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,27 @@ public function testNonNumericTenantReturnsNull(): void
9595
$metric = new Metric(['tenant' => 'abc']);
9696
$this->assertNull($metric->getTenant());
9797
}
98+
99+
public function testGetValueWithFloat(): void
100+
{
101+
$metric = new Metric(['value' => 1.75]);
102+
$this->assertSame(1.75, $metric->getValue());
103+
$this->assertIsFloat($metric->getValue());
104+
}
105+
106+
public function testGetValueWithNumericString(): void
107+
{
108+
$metric = new Metric(['value' => '42']);
109+
$this->assertSame(42, $metric->getValue());
110+
111+
$metric = new Metric(['value' => '3.14']);
112+
$this->assertSame(3.14, $metric->getValue());
113+
}
114+
115+
public function testGetValueWithNonNumericStringReturnsDefault(): void
116+
{
117+
$metric = new Metric(['value' => 'abc']);
118+
$this->assertSame(0, $metric->getValue());
119+
$this->assertSame(99, $metric->getValue(99));
120+
}
98121
}

0 commit comments

Comments
 (0)