From 49bf1198b293b35f0d89c4d23886175281dd280f Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 11 May 2026 02:08:29 +0000 Subject: [PATCH] refactor(advisor): polish constants aliases and insight lookup helper Agent-Logs-Url: https://github.com/appwrite/appwrite/sessions/517a4586-d3e8-40b3-a3a9-f2d2ca82b0a0 Co-authored-by: abnegate <5857008+abnegate@users.noreply.github.com> --- app/config/collections/platform.php | 2 +- app/init/constants.php | 68 +++++++++---------- .../Modules/Insights/Http/Insights/Get.php | 20 ++++-- .../Advisor/AdvisorCustomServerTest.php | 1 + 4 files changed, 52 insertions(+), 39 deletions(-) diff --git a/app/config/collections/platform.php b/app/config/collections/platform.php index 4cc3f5db0b..c8f26d6a65 100644 --- a/app/config/collections/platform.php +++ b/app/config/collections/platform.php @@ -2103,7 +2103,7 @@ $platformCollections = [ 'type' => Database::INDEX_KEY, 'attributes' => ['projectInternalId', 'targetType', 'target'], // `projectInternalId` and `targetType` do not need explicit prefix lengths. - // Keep a prefix on the free-form `target` string to stay under index limits. + // Keep `target` at 700 chars so the combined index stays under legacy 768-byte limits. 'lengths' => [null, null, 700], 'orders' => [], ], diff --git a/app/init/constants.php b/app/init/constants.php index 7ff63d7380..00943cb462 100644 --- a/app/init/constants.php +++ b/app/init/constants.php @@ -1,12 +1,12 @@ value, // legacy databases.createIndex - Type::TABLES_DB_INDEX->value, // tablesDB.createIndex - Type::DOCUMENTS_DB_INDEX->value, // documentsDB.createIndex - Type::VECTORS_DB_INDEX->value, // vectorsDB.createIndex - Type::DATABASE_PERFORMANCE->value, - Type::SITE_PERFORMANCE->value, - Type::SITE_ACCESSIBILITY->value, - Type::SITE_SEO->value, - Type::FUNCTION_PERFORMANCE->value, +const ADVISOR_INSIGHT_TYPES = [ + AdvisorType::DATABASE_INDEX->value, // legacy databases.createIndex + AdvisorType::TABLES_DB_INDEX->value, // tablesDB.createIndex + AdvisorType::DOCUMENTS_DB_INDEX->value, // documentsDB.createIndex + AdvisorType::VECTORS_DB_INDEX->value, // vectorsDB.createIndex + AdvisorType::DATABASE_PERFORMANCE->value, + AdvisorType::SITE_PERFORMANCE->value, + AdvisorType::SITE_ACCESSIBILITY->value, + AdvisorType::SITE_SEO->value, + AdvisorType::FUNCTION_PERFORMANCE->value, ]; // Public API services (SDK namespaces) that an insight CTA's `service` can reference. // Analyzers must pick the one matching the engine the resource lives in. -const INSIGHT_CTA_SERVICES = [ - CTAService::DATABASES->value, // legacy - CTAService::TABLES_DB->value, - CTAService::DOCUMENTS_DB->value, - CTAService::VECTORS_DB->value, +const ADVISOR_CTA_SERVICES = [ + AdvisorCTAService::DATABASES->value, // legacy + AdvisorCTAService::TABLES_DB->value, + AdvisorCTAService::DOCUMENTS_DB->value, + AdvisorCTAService::VECTORS_DB->value, ]; // Public API method names that an insight CTA's `method` can reference for index suggestions. -const INSIGHT_CTA_METHODS = [ - CTAMethod::CREATE_INDEX->value, +const ADVISOR_CTA_METHODS = [ + AdvisorCTAMethod::CREATE_INDEX->value, ]; // Insight severities -const INSIGHT_SEVERITIES = [ - Severity::INFO->value, - Severity::WARNING->value, - Severity::CRITICAL->value, +const ADVISOR_SEVERITIES = [ + AdvisorSeverity::INFO->value, + AdvisorSeverity::WARNING->value, + AdvisorSeverity::CRITICAL->value, ]; // Insight statuses -const INSIGHT_STATUSES = [ - Status::ACTIVE->value, - Status::DISMISSED->value, +const ADVISOR_STATUSES = [ + AdvisorStatus::ACTIVE->value, + AdvisorStatus::DISMISSED->value, ]; // Report types -const REPORT_TYPES = [ - Report::LIGHTHOUSE->value, - Report::AUDIT->value, - Report::DATABASE_ANALYZER->value, +const ADVISOR_REPORT_TYPES = [ + AdvisorReportType::LIGHTHOUSE->value, + AdvisorReportType::AUDIT->value, + AdvisorReportType::DATABASE_ANALYZER->value, ]; // Resource types for Tokens diff --git a/src/Appwrite/Platform/Modules/Insights/Http/Insights/Get.php b/src/Appwrite/Platform/Modules/Insights/Http/Insights/Get.php index 2c986c1feb..a5e24caa01 100644 --- a/src/Appwrite/Platform/Modules/Insights/Http/Insights/Get.php +++ b/src/Appwrite/Platform/Modules/Insights/Http/Insights/Get.php @@ -67,14 +67,26 @@ class Get extends Action throw new Exception(Exception::REPORT_NOT_FOUND); } - $insight = $report->find('$id', $insightId, 'insights'); + $insight = $this->getInsightFromReport($report, $insightId); - if (empty($insight)) { + if ($insight === null) { throw new Exception(Exception::INSIGHT_NOT_FOUND); } - $insight = $insight instanceof Document ? $insight : new Document($insight); - $response->dynamic($insight, Response::MODEL_INSIGHT); } + + /** + * Resolve a nested insight document from a report's subquery payload. + */ + private function getInsightFromReport(Document $report, string $insightId): ?Document + { + $insight = $report->find('$id', $insightId, 'insights'); + + if (empty($insight)) { + return null; + } + + return $insight instanceof Document ? $insight : new Document($insight); + } } diff --git a/tests/e2e/Services/Advisor/AdvisorCustomServerTest.php b/tests/e2e/Services/Advisor/AdvisorCustomServerTest.php index ef84afd2e2..d91f95035e 100644 --- a/tests/e2e/Services/Advisor/AdvisorCustomServerTest.php +++ b/tests/e2e/Services/Advisor/AdvisorCustomServerTest.php @@ -19,6 +19,7 @@ class AdvisorCustomServerTest extends Scope $projectId = $this->getProject()['$id']; $userKey = $this->getNewKey([ + // Advisor read APIs are protected by the underlying report/insight resource scopes. 'insights.read', 'reports.read', ]);