Merge pull request #10500 from appwrite/copilot/fix-3b5374b7-acb3-4c22-9573-00c1feec5bc1

Fix hardcoded specification defaults to use dynamic plan-based logic
This commit is contained in:
Steven Nguyen 2025-10-22 15:41:11 -07:00 committed by GitHub
commit aec26277c7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 112 additions and 4 deletions

View file

@ -5,6 +5,8 @@ namespace Appwrite\Platform\Modules\Compute;
use Appwrite\Event\Build;
use Appwrite\Extend\Exception;
use Appwrite\Platform\Action;
use Appwrite\Platform\Modules\Compute\Validator\Specification as SpecificationValidator;
use Utopia\Config\Config;
use Utopia\Database\Database;
use Utopia\Database\Document;
use Utopia\Database\Exception\Duplicate;
@ -19,6 +21,37 @@ use Utopia\VCS\Exception\RepositoryNotFound;
class Base extends Action
{
/**
* Get default specification based on plan and available specifications.
*
* @param array $plan The billing plan configuration
* @return string The appropriate default specification
*/
protected function getDefaultSpecification(array $plan): string
{
$specifications = Config::getParam('specifications', []);
if (empty($specifications)) {
return APP_COMPUTE_SPECIFICATION_DEFAULT;
}
$specificationValidator = new SpecificationValidator(
$plan,
$specifications,
System::getEnv('_APP_COMPUTE_CPUS', 0),
System::getEnv('_APP_COMPUTE_MEMORY', 0)
);
$allowedSpecifications = $specificationValidator->getAllowedSpecifications();
// If there is no plan use the highest specification
if (empty($plan)) {
return end($allowedSpecifications) ?? APP_COMPUTE_SPECIFICATION_DEFAULT;
}
// Otherwise, use the lowest specification available in the plan
return $allowedSpecifications[0] ?? APP_COMPUTE_SPECIFICATION_DEFAULT;
}
public function redeployVcsFunction(Request $request, Document $function, Document $project, Document $installation, Database $dbForProject, Build $queueForBuilds, Document $template, GitHub $github, bool $activate, string $referenceType = 'branch', string $reference = ''): Document
{
$deploymentId = ID::unique();

View file

@ -93,7 +93,7 @@ class Create extends Base
->param('providerBranch', '', new Text(128, 0), 'Production branch for the repo linked to the function.', true)
->param('providerSilentMode', false, new Boolean(), 'Is the VCS (Version Control System) connection in silent mode for the repo linked to the function? In silent mode, comments will not be made on commits and pull requests.', true)
->param('providerRootDirectory', '', new Text(128, 0), 'Path to function code in the linked repo.', true)
->param('specification', APP_COMPUTE_SPECIFICATION_DEFAULT, fn (array $plan) => new Specification(
->param('specification', fn (array $plan) => $this->getDefaultSpecification($plan), fn (array $plan) => new Specification(
$plan,
Config::getParam('specifications', []),
System::getEnv('_APP_COMPUTE_CPUS', 0),

View file

@ -89,7 +89,7 @@ class Update extends Base
->param('providerBranch', '', new Text(128, 0), 'Production branch for the repo linked to the function', true)
->param('providerSilentMode', false, new Boolean(), 'Is the VCS (Version Control System) connection in silent mode for the repo linked to the function? In silent mode, comments will not be made on commits and pull requests.', true)
->param('providerRootDirectory', '', new Text(128, 0), 'Path to function code in the linked repo.', true)
->param('specification', APP_COMPUTE_SPECIFICATION_DEFAULT, fn (array $plan) => new Specification(
->param('specification', fn (array $plan) => $this->getDefaultSpecification($plan), fn (array $plan) => new Specification(
$plan,
Config::getParam('specifications', []),
System::getEnv('_APP_COMPUTE_CPUS', 0),

View file

@ -78,7 +78,7 @@ class Create extends Base
->param('providerBranch', '', new Text(128, 0), 'Production branch for the repo linked to the site.', true)
->param('providerSilentMode', false, new Boolean(), 'Is the VCS (Version Control System) connection in silent mode for the repo linked to the site? In silent mode, comments will not be made on commits and pull requests.', true)
->param('providerRootDirectory', '', new Text(128, 0), 'Path to site code in the linked repo.', true)
->param('specification', APP_COMPUTE_SPECIFICATION_DEFAULT, fn (array $plan) => new Specification(
->param('specification', fn (array $plan) => $this->getDefaultSpecification($plan), fn (array $plan) => new Specification(
$plan,
Config::getParam('specifications', []),
System::getEnv('_APP_COMPUTE_CPUS', 0),

View file

@ -82,7 +82,7 @@ class Update extends Base
->param('providerBranch', '', new Text(128, 0), 'Production branch for the repo linked to the site.', true)
->param('providerSilentMode', false, new Boolean(), 'Is the VCS (Version Control System) connection in silent mode for the repo linked to the site? In silent mode, comments will not be made on commits and pull requests.', true)
->param('providerRootDirectory', '', new Text(128, 0), 'Path to site code in the linked repo.', true)
->param('specification', APP_COMPUTE_SPECIFICATION_DEFAULT, fn (array $plan) => new Specification(
->param('specification', fn (array $plan) => $this->getDefaultSpecification($plan), fn (array $plan) => new Specification(
$plan,
Config::getParam('specifications', []),
System::getEnv('_APP_COMPUTE_CPUS', 0),

View file

@ -0,0 +1,75 @@
<?php
namespace Tests\Unit\Platform\Modules\Compute\Validator;
use Appwrite\Platform\Modules\Compute\Specification as SpecificationConstants;
use Appwrite\Platform\Modules\Compute\Validator\Specification;
use PHPUnit\Framework\TestCase;
use Utopia\Config\Config;
class SpecificationTest extends TestCase
{
private array $specifications;
protected function setUp(): void
{
parent::setUp();
$this->specifications = Config::getParam('specifications', []);
}
public function testGetAllowedSpecificationsNoLimits(): void
{
$validator = new Specification(
plan: [],
specifications: $this->specifications,
maxCpus: 0,
maxMemory: 0
);
$allowed = $validator->getAllowedSpecifications();
$this->assertCount(count($this->specifications), $allowed);
$this->assertEquals(
$this->specifications[array_key_last($this->specifications)]['slug'],
$allowed[array_key_last($allowed)]
);
}
public function testGetAllowedSpecificationsWithMaxCpusAndMemory(): void
{
$validator = new Specification(
plan: [],
specifications: $this->specifications,
maxCpus: 2,
maxMemory: 2048
);
$allowed = $validator->getAllowedSpecifications();
$this->assertCount(4, $allowed);
$this->assertEquals(
SpecificationConstants::S_2VCPU_2GB,
$allowed[array_key_last($allowed)]
);
}
public function testGetAllowedSpecificationsWithPlanLimits(): void
{
$plan = [
'runtimeSpecifications' => [
SpecificationConstants::S_05VCPU_512MB,
SpecificationConstants::S_1VCPU_512MB
]
];
$validator = new Specification(
plan: $plan,
specifications: $this->specifications,
maxCpus: 0,
maxMemory: 0
);
$allowed = $validator->getAllowedSpecifications();
$this->assertCount(2, $allowed);
$this->assertContains(SpecificationConstants::S_05VCPU_512MB, $allowed);
$this->assertContains(SpecificationConstants::S_1VCPU_512MB, $allowed);
}
}