From 87e735e077c956bae1c651cfdb1d35396d673163 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Mon, 31 Aug 2026 16:40:43 +0545 Subject: [PATCH 1/2] fix(usage): don't bill build compute when buildStartedAt is null buildDuration falls back to wall clock since the deployment was created when a build never stamped buildStartedAt, so queue wait was billed as build compute and flowed into builds.mbSeconds. Zero buildDuration and mbSeconds in that case. Both StatsUsage workers skip zero-valued metrics, so the compute rows drop out while the build counters and storage metrics still land. --- src/Appwrite/Usage/Build.php | 7 ++ tests/unit/Usage/BuildTest.php | 123 +++++++++++++++++++++++++++++++++ 2 files changed, 130 insertions(+) create mode 100644 tests/unit/Usage/BuildTest.php diff --git a/src/Appwrite/Usage/Build.php b/src/Appwrite/Usage/Build.php index e2ab30893b..a961c49281 100644 --- a/src/Appwrite/Usage/Build.php +++ b/src/Appwrite/Usage/Build.php @@ -32,6 +32,13 @@ final class Build $buildDuration = (int) $deployment->getAttribute('buildDuration', 0) * 1000; $mbSeconds = (int) ($memory * $deployment->getAttribute('buildDuration', 0) * $cpus); + // Without a start stamp buildDuration falls back to wall clock since the + // deployment was created, which would bill queue wait as build compute. + if (empty($deployment->getAttribute('buildStartedAt'))) { + $buildDuration = 0; + $mbSeconds = 0; + } + // Per-resource breakdown now travels as resource dimensions on the // Context (resolved to resourceType + resourceId in the usage pipeline) // instead of per-{resourceInternalId} metric-name templates. diff --git a/tests/unit/Usage/BuildTest.php b/tests/unit/Usage/BuildTest.php new file mode 100644 index 0000000000..2e8652e478 --- /dev/null +++ b/tests/unit/Usage/BuildTest.php @@ -0,0 +1,123 @@ + $deployment + * @return array metric key => value + */ + private function publish(array $deployment): array + { + $publisher = new MockPublisher(); + + Build::publish( + new Context(), + new Document([ + '$id' => 'function', + '$sequence' => '1', + 'buildSpecification' => Specification::S_2VCPU_2GB, + ]), + new Document($deployment), + new Document(['$id' => 'project', '$sequence' => '1']), + new UsagePublisher($publisher, new Queue('usage')), + ); + + $events = $publisher->getEvents('usage'); + $this->assertCount(1, $events); + + $metrics = []; + foreach ($events[0]['metrics'] as $metric) { + $metrics[$metric['key']] = $metric['value']; + } + + return $metrics; + } + + /** + * @param array $overrides + * @return array + */ + private function deployment(array $overrides = []): array + { + return \array_merge([ + '$id' => 'deployment', + '$sequence' => '1', + '$createdAt' => '2026-08-31T10:00:00.000+00:00', + 'resourceType' => 'functions', + 'status' => 'ready', + 'buildDuration' => self::DURATION, + 'buildSize' => self::SIZE, + ], $overrides); + } + + public function testBuildStartedAtStampedBillsMemoryTimesDurationTimesCpus(): void + { + $metrics = $this->publish($this->deployment([ + 'buildStartedAt' => '2026-08-31T10:00:05.000+00:00', + ])); + + $expectedMbSeconds = self::MEMORY * self::DURATION * self::CPUS; + $expectedCompute = self::DURATION * 1000; + + $this->assertSame($expectedMbSeconds, $metrics[METRIC_BUILDS_MB_SECONDS]); + $this->assertSame($expectedMbSeconds, $metrics['functions.builds.mbSeconds']); + $this->assertSame($expectedCompute, $metrics[METRIC_BUILDS_COMPUTE]); + $this->assertSame($expectedCompute, $metrics[METRIC_BUILDS_COMPUTE_SUCCESS]); + $this->assertSame($expectedCompute, $metrics['functions.builds.compute']); + $this->assertSame(1, $metrics[METRIC_BUILDS]); + $this->assertSame(self::SIZE, $metrics[METRIC_BUILDS_STORAGE]); + } + + public function testMissingBuildStartedAtDropsComputeButKeepsCounts(): void + { + $metrics = $this->publish($this->deployment()); + + $this->assertSame(0, $metrics[METRIC_BUILDS_MB_SECONDS]); + $this->assertSame(0, $metrics['functions.builds.mbSeconds']); + $this->assertSame(0, $metrics[METRIC_BUILDS_COMPUTE]); + $this->assertSame(0, $metrics[METRIC_BUILDS_COMPUTE_SUCCESS]); + $this->assertSame(0, $metrics['functions.builds.compute']); + $this->assertSame(0, $metrics['functions.builds.compute.success']); + + $this->assertSame(1, $metrics[METRIC_BUILDS]); + $this->assertSame(1, $metrics[METRIC_BUILDS_SUCCESS]); + $this->assertSame(self::SIZE, $metrics[METRIC_BUILDS_STORAGE]); + } + + public function testNullBuildStartedAtDropsComputeOnFailedBuilds(): void + { + $metrics = $this->publish($this->deployment([ + 'status' => 'failed', + 'buildStartedAt' => null, + ])); + + $this->assertSame(0, $metrics[METRIC_BUILDS_MB_SECONDS]); + $this->assertSame(0, $metrics[METRIC_BUILDS_COMPUTE]); + $this->assertSame(0, $metrics[METRIC_BUILDS_COMPUTE_FAILED]); + $this->assertSame(0, $metrics['functions.builds.compute.failed']); + + $this->assertSame(1, $metrics[METRIC_BUILDS]); + $this->assertSame(1, $metrics[METRIC_BUILDS_FAILED]); + $this->assertSame(self::SIZE, $metrics[METRIC_BUILDS_STORAGE]); + } +} From fd8f8e41e54f6d97078fbfcc74a0b10b3460ec67 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Wed, 2 Sep 2026 06:22:34 +0545 Subject: [PATCH 2/2] test(usage): drop the Build usage unit test Requested in review; the guard is a two-line branch and the layer has no other unit coverage. --- tests/unit/Usage/BuildTest.php | 123 --------------------------------- 1 file changed, 123 deletions(-) delete mode 100644 tests/unit/Usage/BuildTest.php diff --git a/tests/unit/Usage/BuildTest.php b/tests/unit/Usage/BuildTest.php deleted file mode 100644 index 2e8652e478..0000000000 --- a/tests/unit/Usage/BuildTest.php +++ /dev/null @@ -1,123 +0,0 @@ - $deployment - * @return array metric key => value - */ - private function publish(array $deployment): array - { - $publisher = new MockPublisher(); - - Build::publish( - new Context(), - new Document([ - '$id' => 'function', - '$sequence' => '1', - 'buildSpecification' => Specification::S_2VCPU_2GB, - ]), - new Document($deployment), - new Document(['$id' => 'project', '$sequence' => '1']), - new UsagePublisher($publisher, new Queue('usage')), - ); - - $events = $publisher->getEvents('usage'); - $this->assertCount(1, $events); - - $metrics = []; - foreach ($events[0]['metrics'] as $metric) { - $metrics[$metric['key']] = $metric['value']; - } - - return $metrics; - } - - /** - * @param array $overrides - * @return array - */ - private function deployment(array $overrides = []): array - { - return \array_merge([ - '$id' => 'deployment', - '$sequence' => '1', - '$createdAt' => '2026-08-31T10:00:00.000+00:00', - 'resourceType' => 'functions', - 'status' => 'ready', - 'buildDuration' => self::DURATION, - 'buildSize' => self::SIZE, - ], $overrides); - } - - public function testBuildStartedAtStampedBillsMemoryTimesDurationTimesCpus(): void - { - $metrics = $this->publish($this->deployment([ - 'buildStartedAt' => '2026-08-31T10:00:05.000+00:00', - ])); - - $expectedMbSeconds = self::MEMORY * self::DURATION * self::CPUS; - $expectedCompute = self::DURATION * 1000; - - $this->assertSame($expectedMbSeconds, $metrics[METRIC_BUILDS_MB_SECONDS]); - $this->assertSame($expectedMbSeconds, $metrics['functions.builds.mbSeconds']); - $this->assertSame($expectedCompute, $metrics[METRIC_BUILDS_COMPUTE]); - $this->assertSame($expectedCompute, $metrics[METRIC_BUILDS_COMPUTE_SUCCESS]); - $this->assertSame($expectedCompute, $metrics['functions.builds.compute']); - $this->assertSame(1, $metrics[METRIC_BUILDS]); - $this->assertSame(self::SIZE, $metrics[METRIC_BUILDS_STORAGE]); - } - - public function testMissingBuildStartedAtDropsComputeButKeepsCounts(): void - { - $metrics = $this->publish($this->deployment()); - - $this->assertSame(0, $metrics[METRIC_BUILDS_MB_SECONDS]); - $this->assertSame(0, $metrics['functions.builds.mbSeconds']); - $this->assertSame(0, $metrics[METRIC_BUILDS_COMPUTE]); - $this->assertSame(0, $metrics[METRIC_BUILDS_COMPUTE_SUCCESS]); - $this->assertSame(0, $metrics['functions.builds.compute']); - $this->assertSame(0, $metrics['functions.builds.compute.success']); - - $this->assertSame(1, $metrics[METRIC_BUILDS]); - $this->assertSame(1, $metrics[METRIC_BUILDS_SUCCESS]); - $this->assertSame(self::SIZE, $metrics[METRIC_BUILDS_STORAGE]); - } - - public function testNullBuildStartedAtDropsComputeOnFailedBuilds(): void - { - $metrics = $this->publish($this->deployment([ - 'status' => 'failed', - 'buildStartedAt' => null, - ])); - - $this->assertSame(0, $metrics[METRIC_BUILDS_MB_SECONDS]); - $this->assertSame(0, $metrics[METRIC_BUILDS_COMPUTE]); - $this->assertSame(0, $metrics[METRIC_BUILDS_COMPUTE_FAILED]); - $this->assertSame(0, $metrics['functions.builds.compute.failed']); - - $this->assertSame(1, $metrics[METRIC_BUILDS]); - $this->assertSame(1, $metrics[METRIC_BUILDS_FAILED]); - $this->assertSame(self::SIZE, $metrics[METRIC_BUILDS_STORAGE]); - } -}