Skip to content

Commit

Permalink
Refactor the missing service installs query to use $coiunt $filter
Browse files Browse the repository at this point in the history
Resolves: #
Change-type:
  • Loading branch information
thgreasi committed Nov 25, 2024
1 parent e3769fe commit fec6be5
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 47 deletions.
47 changes: 20 additions & 27 deletions src/features/ci-cd/hooks/service-installs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,21 +98,6 @@ const createReleaseServiceInstalls = async (
}
const serviceIds = services.map(({ id }) => id);

const missingServiceFilters = serviceIds.map((serviceId) => ({
$not: {
service_install: {
$any: {
$alias: 'si',
$expr: {
si: {
installs__service: serviceId,
},
},
},
},
},
}));

const devicesToAddServiceInstalls = await api.get({
resource: 'device',
options: {
Expand All @@ -126,18 +111,26 @@ const createReleaseServiceInstalls = async (
},
},
$filter: {
// Pass the device filters instead of IDs, since a using $in errors with `code: '08P01'` for more than 66k IDs.
...(Array.isArray(deviceFilterOrIds)
? { id: { $in: deviceFilterOrIds } }
: deviceFilterOrIds),
// TODO: Once Pine support it, change this with a filtered-count filter like:
// $filter=... service_install/$filter(installs__service $in (...serviceIds))/$count lt ${serviceIds.length}
// See: http://docs.oasis-open.org/odata/odata/v4.01/odata-v4.01-part1-protocol.html#sec_RequestingtheNumberofItemsinaCollect
...(missingServiceFilters.length === 1
? missingServiceFilters[0]
: {
$or: missingServiceFilters,
}),
$and: [
// Pass the device filters instead of IDs, since a using $in errors with `code: '08P01'` for more than 66k IDs.
Array.isArray(deviceFilterOrIds)
? { id: { $in: deviceFilterOrIds } }
: deviceFilterOrIds,
{
$lt: [
{
service_install: {
$count: {
$filter: {
installs__service: { $in: serviceIds },
},
},
},
},
serviceIds.length,
],
},
],
},
},
} as const);
Expand Down
32 changes: 12 additions & 20 deletions src/features/ci-cd/tasks/service-installs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,21 +115,6 @@ const createServiceInstalls = async ({
return 0;
}

const missingServiceFilters = serviceIds.map((serviceId) => ({
$not: {
service_install: {
$any: {
$alias: 'si',
$expr: {
si: {
installs__service: serviceId,
},
},
},
},
},
}));

const devicesToAddServiceInstalls = (
await api.resin.get({
resource: 'device',
Expand All @@ -146,11 +131,18 @@ const createServiceInstalls = async ({
},
$filter: {
id: { $in: devices },
...(missingServiceFilters.length === 1
? missingServiceFilters[0]
: {
$or: missingServiceFilters,
}),
$lt: [
{
service_install: {
$count: {
$filter: {
installs__service: { $in: serviceIds },
},
},
},
},
serviceIds.length,
],
},
},
} as const)
Expand Down

0 comments on commit fec6be5

Please sign in to comment.