mirror of
https://github.com/balena-os/balena-supervisor.git
synced 2025-02-21 18:06:47 +00:00
Merge pull request #2061 from balena-os/unnecessary-reduce
Remove some unnecessary usage of `.reduce`
This commit is contained in:
commit
13ca0ebb25
@ -444,10 +444,8 @@ export class App {
|
|||||||
// FIXME: We should add to the changingServices array, as we could emit several
|
// FIXME: We should add to the changingServices array, as we could emit several
|
||||||
// kill steps for a service
|
// kill steps for a service
|
||||||
steps = steps.concat(
|
steps = steps.concat(
|
||||||
dependencies.reduce(
|
dependencies.flatMap((svc) =>
|
||||||
(acc, svc) =>
|
this.generateKillStep(svc, changingServices),
|
||||||
acc.concat(this.generateKillStep(svc, changingServices)),
|
|
||||||
[] as CompositionStep[],
|
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
|
@ -1081,37 +1081,41 @@ export async function getState() {
|
|||||||
.concat(stateFromServices);
|
.concat(stateFromServices);
|
||||||
|
|
||||||
// Get the list of commits for all appIds from the database
|
// Get the list of commits for all appIds from the database
|
||||||
const commitsForApp = (
|
const commitsForApp: Dictionary<string | undefined> = {};
|
||||||
await Promise.all(
|
// Deduplicate appIds first
|
||||||
// Deduplicate appIds first
|
await Promise.all(
|
||||||
[...new Set(servicesToReport.map((svc) => svc.appId))].map(
|
[...new Set(servicesToReport.map((svc) => svc.appId))].map(
|
||||||
async (appId) => ({
|
async (appId) => {
|
||||||
[appId]: await commitStore.getCommitForApp(appId),
|
commitsForApp[appId] = await commitStore.getCommitForApp(appId);
|
||||||
}),
|
},
|
||||||
),
|
),
|
||||||
)
|
);
|
||||||
).reduce((commits, c) => ({ ...commits, ...c }), {});
|
|
||||||
|
|
||||||
// Assemble the state of apps
|
// Assemble the state of apps
|
||||||
return servicesToReport.reduce(
|
const state: { [appUuid: string]: AppState } = {};
|
||||||
(apps, { appId, appUuid, commit, serviceName, createdAt, ...svc }) => ({
|
for (const {
|
||||||
...apps,
|
appId,
|
||||||
[appUuid]: {
|
appUuid,
|
||||||
...(apps[appUuid] ?? {}),
|
commit,
|
||||||
// Add the release_uuid if the commit has been stored in the database
|
serviceName,
|
||||||
...(commitsForApp[appId] && { release_uuid: commitsForApp[appId] }),
|
createdAt,
|
||||||
releases: {
|
...svc
|
||||||
...(apps[appUuid]?.releases ?? {}),
|
} of servicesToReport) {
|
||||||
[commit]: {
|
state[appUuid] = {
|
||||||
...(apps[appUuid]?.releases[commit] ?? {}),
|
...state[appUuid],
|
||||||
services: {
|
// Add the release_uuid if the commit has been stored in the database
|
||||||
...(apps[appUuid]?.releases[commit]?.services ?? {}),
|
...(commitsForApp[appId] && { release_uuid: commitsForApp[appId] }),
|
||||||
[serviceName]: svc,
|
releases: {
|
||||||
},
|
...state[appUuid]?.releases,
|
||||||
|
[commit]: {
|
||||||
|
...state[appUuid]?.releases[commit],
|
||||||
|
services: {
|
||||||
|
...state[appUuid]?.releases[commit]?.services,
|
||||||
|
[serviceName]: svc,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}),
|
};
|
||||||
{} as { [appUuid: string]: AppState },
|
}
|
||||||
);
|
return state;
|
||||||
}
|
}
|
||||||
|
@ -597,12 +597,15 @@ export async function inspectByName(imageName: string) {
|
|||||||
|
|
||||||
// Run the queries in sequence, return the first one that matches or
|
// Run the queries in sequence, return the first one that matches or
|
||||||
// the error from the last query
|
// the error from the last query
|
||||||
return await [inspectByURI, inspectByReference, inspectByDigest].reduce(
|
let err;
|
||||||
(promise, query) => promise.catch(() => query(imageName)),
|
for (const query of [inspectByURI, inspectByReference, inspectByDigest]) {
|
||||||
Promise.reject(
|
try {
|
||||||
'Promise sequence in inspectByName is broken. This is a bug.',
|
return await query(imageName);
|
||||||
),
|
} catch ($err) {
|
||||||
);
|
err = $err;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw err;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function cleanup() {
|
export async function cleanup() {
|
||||||
@ -681,10 +684,7 @@ async function removeImageIfNotNeeded(image: Image): Promise<void> {
|
|||||||
digests: true,
|
digests: true,
|
||||||
filters: { reference: [reference] },
|
filters: { reference: [reference] },
|
||||||
})
|
})
|
||||||
).reduce(
|
).flatMap((imgInfo) => imgInfo.RepoTags || []);
|
||||||
(tagList, imgInfo) => tagList.concat(imgInfo.RepoTags || []),
|
|
||||||
[] as string[],
|
|
||||||
);
|
|
||||||
|
|
||||||
reportEvent('start', { ...image, status: 'Deleting' });
|
reportEvent('start', { ...image, status: 'Deleting' });
|
||||||
logger.logSystemEvent(LogTypes.deleteImage, { image });
|
logger.logSystemEvent(LogTypes.deleteImage, { image });
|
||||||
@ -700,10 +700,9 @@ async function removeImageIfNotNeeded(image: Image): Promise<void> {
|
|||||||
// Remove all matching tags in sequence
|
// Remove all matching tags in sequence
|
||||||
// as removing in parallel causes some engine weirdness (see above)
|
// as removing in parallel causes some engine weirdness (see above)
|
||||||
// this stops on the first error
|
// this stops on the first error
|
||||||
await tags.reduce(
|
for (const tag of tags) {
|
||||||
(promise, tag) => promise.then(() => docker.getImage(tag).remove()),
|
await docker.getImage(tag).remove();
|
||||||
Promise.resolve(),
|
}
|
||||||
);
|
|
||||||
|
|
||||||
// Check for any remaining digests.
|
// Check for any remaining digests.
|
||||||
const digests = (
|
const digests = (
|
||||||
@ -711,16 +710,12 @@ async function removeImageIfNotNeeded(image: Image): Promise<void> {
|
|||||||
digests: true,
|
digests: true,
|
||||||
filters: { reference: [reference] },
|
filters: { reference: [reference] },
|
||||||
})
|
})
|
||||||
).reduce(
|
).flatMap((imgInfo) => imgInfo.RepoDigests || []);
|
||||||
(digestList, imgInfo) => digestList.concat(imgInfo.RepoDigests || []),
|
|
||||||
[] as string[],
|
|
||||||
);
|
|
||||||
|
|
||||||
// Remove all remaining digests
|
// Remove all remaining digests
|
||||||
await digests.reduce(
|
for (const digest of digests) {
|
||||||
(promise, digest) => promise.then(() => docker.getImage(digest).remove()),
|
await docker.getImage(digest).remove();
|
||||||
Promise.resolve(),
|
}
|
||||||
);
|
|
||||||
|
|
||||||
// Mark the image as removed
|
// Mark the image as removed
|
||||||
removed = true;
|
removed = true;
|
||||||
|
@ -223,27 +223,26 @@ export class ExtraUEnv extends ConfigBackend {
|
|||||||
|
|
||||||
private static configToMap(configs: ConfigOptions): Map<string, string> {
|
private static configToMap(configs: ConfigOptions): Map<string, string> {
|
||||||
// Reduce ConfigOptions into a Map that joins collections
|
// Reduce ConfigOptions into a Map that joins collections
|
||||||
return Object.entries(configs).reduce(
|
const configMap = new Map();
|
||||||
(configMap: Map<string, string>, [configKey, configValue]) => {
|
for (const [configKey, configValue] of Object.entries(configs)) {
|
||||||
const { key: ENTRY_KEY, collection: ENTRY_IS_COLLECTION } =
|
const { key: ENTRY_KEY, collection: ENTRY_IS_COLLECTION } =
|
||||||
ExtraUEnv.supportedConfigs[configKey];
|
ExtraUEnv.supportedConfigs[configKey];
|
||||||
// Check if we have to build the value for the entry
|
// Check if we have to build the value for the entry
|
||||||
if (ENTRY_IS_COLLECTION) {
|
if (ENTRY_IS_COLLECTION) {
|
||||||
return configMap.set(
|
configMap.set(
|
||||||
ENTRY_KEY,
|
ENTRY_KEY,
|
||||||
ExtraUEnv.appendToCollection(
|
ExtraUEnv.appendToCollection(
|
||||||
configMap.get(ENTRY_KEY),
|
configMap.get(ENTRY_KEY),
|
||||||
configKey,
|
configKey,
|
||||||
configValue,
|
configValue,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
} else {
|
||||||
// Set the value of this config
|
// Set the value of this config
|
||||||
return configMap.set(ENTRY_KEY, `${configValue}`);
|
configMap.set(ENTRY_KEY, `${configValue}`);
|
||||||
},
|
}
|
||||||
// Start with empty Map
|
}
|
||||||
new Map(),
|
return configMap;
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static appendToCollection(
|
private static appendToCollection(
|
||||||
|
@ -127,19 +127,15 @@ export class Odmdata extends ConfigBackend {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
// Find the configuration given the optionsBuffer
|
// Find the configuration given the optionsBuffer
|
||||||
const configIndex = this.CONFIG_BYTES.reduce(
|
const configIndex = this.CONFIG_BYTES.findIndex(
|
||||||
(currentIndex: number, _config: number, index: number) => {
|
(_config: number, index: number) => {
|
||||||
if (
|
return (
|
||||||
this.CONFIG_BUFFER.readUInt8(index) === optionsBuffer.readUInt8(0)
|
this.CONFIG_BUFFER.readUInt8(index) === optionsBuffer.readUInt8(0)
|
||||||
) {
|
);
|
||||||
return index;
|
|
||||||
}
|
|
||||||
return currentIndex;
|
|
||||||
},
|
},
|
||||||
null,
|
|
||||||
);
|
);
|
||||||
// Check if we found a configuration we support
|
// Check if we found a configuration we support
|
||||||
if (configIndex === null) {
|
if (configIndex === -1) {
|
||||||
log.error(
|
log.error(
|
||||||
`ODMDATA is set with an unsupported byte: 0x${optionsBuffer.readUInt8(
|
`ODMDATA is set with an unsupported byte: 0x${optionsBuffer.readUInt8(
|
||||||
0,
|
0,
|
||||||
|
@ -117,7 +117,7 @@ export async function loadTargetFromFile(appsPath: string): Promise<boolean> {
|
|||||||
return imageFromService(svc);
|
return imageFromService(svc);
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
.reduce((res, images) => res.concat(images), []);
|
.flat();
|
||||||
|
|
||||||
for (const image of imgs) {
|
for (const image of imgs) {
|
||||||
const name = imageManager.normalise(image.name);
|
const name = imageManager.normalise(image.name);
|
||||||
|
@ -11,9 +11,9 @@ import { exec } from './fs-utils';
|
|||||||
|
|
||||||
export async function getCpuUsage(): Promise<number> {
|
export async function getCpuUsage(): Promise<number> {
|
||||||
const cpuData = await systeminformation.currentLoad();
|
const cpuData = await systeminformation.currentLoad();
|
||||||
const totalLoad = cpuData.cpus.reduce((load, cpuLoad) => {
|
const totalLoad = _.sumBy(cpuData.cpus, ({ load }) => {
|
||||||
return load + cpuLoad.load;
|
return load;
|
||||||
}, 0);
|
});
|
||||||
return Math.round(totalLoad / cpuData.cpus.length);
|
return Math.round(totalLoad / cpuData.cpus.length);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user