UNPKG

balena-sdk

Version:
76 lines (75 loc) 3.57 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.generateCurrentServiceDetails = exports.getCurrentServiceDetailsPineExpand = void 0; const tslib_1 = require("tslib"); // Pine expand options necessary for getting raw service data for a device exports.getCurrentServiceDetailsPineExpand = { image_install: { $select: ['id', 'download_progress', 'status', 'install_date'], $filter: { status: { $ne: 'deleted', }, }, $expand: { installs__image: { $select: ['id'], $expand: { is_a_build_of__service: { $select: ['id', 'service_name'], }, }, }, is_provided_by__release: { $select: ['id', 'commit', 'raw_version'], $expand: { belongs_to__application: { $select: ['slug'], }, }, }, }, }, }; function getSingleInstallSummary(rawData) { const image = rawData.installs__image[0]; const service = image.is_a_build_of__service[0]; const release = rawData.is_provided_by__release[0]; const releaseInfo = { commit: release === null || release === void 0 ? void 0 : release.commit, raw_version: release === null || release === void 0 ? void 0 : release.raw_version, release_id: release === null || release === void 0 ? void 0 : release.id, }; const result = Object.assign(Object.assign(Object.assign({}, rawData), { service_id: service.id, // add this extra property to make grouping the services easier service_name: service.service_name, image_id: image.id }), releaseInfo); if ('installs__image' in result) { delete result.installs__image; } if ('is_provided_by__release' in result) { delete result.is_provided_by__release; } return result; } const generateCurrentServiceDetails = (rawDevice) => { // Essentially a groupBy operation, but try making it a bit faster for the sake of large fleets. // Uses Object.create(null) so that there are no inherited properties // which could match service names var _a, _b, _c, _d, _e, _f, _g; const byService = Object.create(null); const byApp = Object.create(null); for (const ii of rawDevice.image_install.sort((a, b) => b.install_date.localeCompare(a.install_date))) { const appSlug = (_d = (_c = (_b = (_a = ii.is_provided_by__release) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.belongs_to__application) === null || _c === void 0 ? void 0 : _c[0]) === null || _d === void 0 ? void 0 : _d.slug; const summary = getSingleInstallSummary(ii); const { service_name } = summary, container = tslib_1.__rest(summary, ["service_name"]); ((_e = byService[service_name]) !== null && _e !== void 0 ? _e : (byService[service_name] = [])).push(container); const appGroup = ((_f = byApp[appSlug]) !== null && _f !== void 0 ? _f : (byApp[appSlug] = Object.create(null))); ((_g = appGroup[service_name]) !== null && _g !== void 0 ? _g : (appGroup[service_name] = [])).push(container); } const device = rawDevice; // TODO: Drop this in the next major device.current_services = byService; device.current_services_by_app = byApp; return device; }; exports.generateCurrentServiceDetails = generateCurrentServiceDetails;