Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize the API request to fetch only the uuid & of only the accessible devices #368

Merged
merged 1 commit into from
Sep 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ interface PublishedHosts {
/** List of published hosts */
const publishedHosts: PublishedHosts[] = [];
/** List of devices with accessible public URLs */
let accessibleDevices: BalenaSdk.Device[] = [];
let accessibleDevices: Array<Pick<BalenaSdk.Device, 'uuid'>> = [];

/** DBus controller */
const dbus = systemBus();
Expand Down Expand Up @@ -143,22 +143,22 @@ const reapDevices = async (addresses: string[], deviceTld?: string) => {
for (const address of addresses) {
// Query the SDK using the Proxy service key for *all* current devices
try {
const devices = await balena.pine.get({
resource: 'device',
options: {$orderby: 'device_name asc'}
}) as BalenaSdk.Device[];

// Get list of all accessible devices
const newAccessible = _.filter(
devices,
(device) => device.is_web_accessible,
);
const newAccessible = await balena.pine.get({
resource: 'device',
options: {
$select: 'uuid',
$filter: {
is_web_accessible: true,
},
},
});

// Get all devices that are not in both lists
const xorList = _.xorBy(accessibleDevices, newAccessible, 'uuid');

// Get all new devices to be published and old to be unpublished
const toUnpublish: BalenaSdk.Device[] = [];
const toUnpublish: typeof newAccessible = [];
const toPublish = _.filter(xorList, (device) => {
const filter = _.find(newAccessible, { uuid: device.uuid })
? true
Expand Down
Loading