From 861d4f33b761b4bacb1756fc4a4345fdef512ad5 Mon Sep 17 00:00:00 2001 From: Thodoris Greasidis Date: Tue, 23 May 2023 19:14:27 +0300 Subject: [PATCH 1/3] ssh,tunnel: Fetch the fleet & devices in one request Change-type: patch --- lib/utils/patterns.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/utils/patterns.ts b/lib/utils/patterns.ts index 409d8276..7118dc13 100644 --- a/lib/utils/patterns.ts +++ b/lib/utils/patterns.ts @@ -317,6 +317,12 @@ export async function getOnlineTargetDeviceUuid( const { getApplication } = await import('./sdk'); return await getApplication(sdk, fleetOrDevice, { $select: ['id', 'slug'], + $expand: { + owns__device: { + $select: ['device_name', 'uuid'], + $filter: { is_online: true }, + }, + }, }); } catch (err) { const { BalenaApplicationNotFound } = await import('balena-errors'); @@ -329,10 +335,7 @@ export async function getOnlineTargetDeviceUuid( })(); // App found, load its devices - const devices = await sdk.models.device.getAllByApplication(application.id, { - $select: ['device_name', 'uuid'], - $filter: { is_online: true }, - }); + const devices = application.owns__device; // Throw if no devices online if (_.isEmpty(devices)) { From 2b58143164f7933c5910c1e99d2d651841eb29b8 Mon Sep 17 00:00:00 2001 From: Thodoris Greasidis Date: Tue, 23 May 2023 19:42:27 +0300 Subject: [PATCH 2/3] devices: Use a single request when providing the --fleet parameter Reduces the response time when using --fleet from 1.5s to 1s. Change-type: patch --- lib/commands/devices/index.ts | 63 +++++++++++++++-------------------- 1 file changed, 26 insertions(+), 37 deletions(-) diff --git a/lib/commands/devices/index.ts b/lib/commands/devices/index.ts index f6a04b7b..9a028d25 100644 --- a/lib/commands/devices/index.ts +++ b/lib/commands/devices/index.ts @@ -22,13 +22,7 @@ import { expandForAppName } from '../../utils/helpers'; import { getBalenaSdk, getVisuals, stripIndent } from '../../utils/lazy'; import { applicationIdInfo, jsonInfo } from '../../utils/messages'; -import type { Application, Device, PineOptions } from 'balena-sdk'; - -interface ExtendedDevice extends DeviceWithDeviceType { - dashboard_url?: string; - fleet?: string | null; // 'org/name' slug - device_type?: string | null; -} +import type { Device, PineOptions } from 'balena-sdk'; interface FlagsDef { fleet?: string; @@ -88,38 +82,33 @@ export default class DevicesCmd extends Command { $orderby: { device_name: 'asc' }, } satisfies PineOptions; - let devices; + const devices = ( + await (async () => { + if (options.fleet != null) { + const { getApplication } = await import('../../utils/sdk'); + const application = await getApplication(balena, options.fleet, { + $select: 'slug', + $expand: { + owns__device: devicesOptions, + }, + }); + return application.owns__device; + } - if (options.fleet != null) { - const { getApplication } = await import('../../utils/sdk'); - const application = await getApplication(balena, options.fleet, { - $select: 'id', - }); - devices = (await balena.models.device.getAllByApplication( - application.id, - devicesOptions, - )) as ExtendedDevice[]; - } else { - devices = (await balena.pine.get({ - resource: 'device', - options: devicesOptions, - })) as ExtendedDevice[]; - } + return await balena.pine.get({ + resource: 'device', + options: devicesOptions, + }); + })() + ).map((device) => ({ + ...device, + dashboard_url: balena.models.device.getDashboardUrl(device.uuid), + fleet: device.belongs_to__application?.[0]?.slug || null, + uuid: options.json ? device.uuid : device.uuid.slice(0, 7), + device_type: device.is_of__device_type?.[0]?.slug || null, + })); - devices = devices.map(function (device) { - device.dashboard_url = balena.models.device.getDashboardUrl(device.uuid); - - const belongsToApplication = - device.belongs_to__application as Application[]; - device.fleet = belongsToApplication?.[0]?.slug || null; - - device.uuid = options.json ? device.uuid : device.uuid.slice(0, 7); - - device.device_type = device.is_of__device_type?.[0]?.slug || null; - return device; - }); - - const fields = [ + const fields: Array = [ 'id', 'uuid', 'device_name', From 063e9d40f0bb7cf5fd80d04d7989ffb1c5f7d98f Mon Sep 17 00:00:00 2001 From: Thodoris Greasidis Date: Tue, 23 May 2023 11:34:16 +0300 Subject: [PATCH 3/3] device init: Avoid extra request when not providing the --fleet option Change-type: patch --- lib/commands/device/init.ts | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/lib/commands/device/init.ts b/lib/commands/device/init.ts index e22e0a11..d0d861b6 100644 --- a/lib/commands/device/init.ts +++ b/lib/commands/device/init.ts @@ -124,21 +124,16 @@ export default class DeviceInitCmd extends Command { const balena = getBalenaSdk(); // Get application and - const application = await getApplication( - balena, - options.fleet || - ( - await (await import('../../utils/patterns')).selectApplication() - ).slug, - { - $select: ['id', 'slug'], - $expand: { - is_for__device_type: { - $select: 'slug', + const application = options.fleet + ? await getApplication(balena, options.fleet, { + $select: ['id', 'slug'], + $expand: { + is_for__device_type: { + $select: 'slug', + }, }, - }, - }, - ); + }) + : await (await import('../../utils/patterns')).selectApplication(); // Register new device const deviceUuid = balena.models.device.generateUniqueKey();