From abd32e812d8808bc11fecd94c794579cf2348783 Mon Sep 17 00:00:00 2001 From: Thodoris Greasidis Date: Thu, 17 Oct 2024 13:06:40 +0300 Subject: [PATCH] var-update-trigger: Skip querying for affected devices on image env var POSTs of non-successful releases Change-type: patch --- .../vars-schema/hooks/vars-update-trigger.ts | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/features/vars-schema/hooks/vars-update-trigger.ts b/src/features/vars-schema/hooks/vars-update-trigger.ts index 019fe461d..72c2281b8 100644 --- a/src/features/vars-schema/hooks/vars-update-trigger.ts +++ b/src/features/vars-schema/hooks/vars-update-trigger.ts @@ -318,6 +318,39 @@ addEnvHooks('device_service_environment_variable', async (args) => { addEnvHooks('image_environment_variable', async (args) => { if (args.req.body.release_image != null) { + // This is a optimization that allows skipping the following + // device->image_install->...->release_image query in most real world + // cases, since when user permissions are used it creates a complex query + // with high planning time. The idea is that image env vars are created + // while the release is still 'running', in which case we don't expect + // any device to possibly be running that release. + const isPartOfSuccessfulRelease = + ( + await args.api.get({ + resource: 'release', + options: { + $top: 1, + $select: 'id', + $filter: { + status: 'success', + release_image: { + $any: { + $alias: 'ri', + $expr: { + ri: { + id: args.req.body.release_image, + }, + }, + }, + }, + }, + }, + }) + ).length > 0; + if (!isPartOfSuccessfulRelease) { + return; + } + return { image_install: { $any: { @@ -328,6 +361,7 @@ addEnvHooks('image_environment_variable', async (args) => { $alias: 'i', $expr: { i: { + status: 'success', release_image: { $any: { $alias: 'ri', @@ -360,6 +394,7 @@ addEnvHooks('image_environment_variable', async (args) => { $alias: 'i', $expr: { i: { + status: 'success', release_image: { $any: { $alias: 'ri',