mirror of
https://github.com/balena-os/balena-supervisor.git
synced 2025-04-16 23:38:52 +00:00
Simplify/optimize filtering non-significant sys info changes
Change-type: patch
This commit is contained in:
parent
466ff58871
commit
74ae31fcfd
@ -112,7 +112,12 @@ const getStateDiff = (): DeviceStatus => {
|
||||
stateForReport.local,
|
||||
(val, key: keyof NonNullable<DeviceStatus['local']>) =>
|
||||
INTERNAL_STATE_KEYS.includes(key) ||
|
||||
_.isEqual(lastReportedLocal[key], val),
|
||||
_.isEqual(lastReportedLocal[key], val) ||
|
||||
!sysInfo.isSignificantChange(
|
||||
key,
|
||||
lastReportedLocal[key] as number,
|
||||
val as number,
|
||||
),
|
||||
),
|
||||
dependent: _.omitBy(
|
||||
stateForReport.dependent,
|
||||
@ -122,11 +127,6 @@ const getStateDiff = (): DeviceStatus => {
|
||||
),
|
||||
};
|
||||
|
||||
const toOmit: string[] = sysInfo.filterNonSignificantChanges(
|
||||
lastReportedLocal as sysInfo.SystemInfo,
|
||||
stateForReport.local as sysInfo.SystemInfo,
|
||||
);
|
||||
diff.local = _.omit(diff.local, toOmit);
|
||||
return _.omitBy(diff, _.isEmpty);
|
||||
};
|
||||
|
||||
|
@ -120,28 +120,22 @@ const significantChange: { [key in keyof SystemInfo]?: number } = {
|
||||
memory_usage: 10,
|
||||
};
|
||||
|
||||
export function filterNonSignificantChanges(
|
||||
past: Partial<SystemInfo>,
|
||||
current: SystemInfo,
|
||||
): Array<keyof SystemInfo> {
|
||||
return Object.keys(
|
||||
_.omitBy(current, (value, key: keyof SystemInfo) => {
|
||||
// If we didn't have a value for this in the past, include it
|
||||
if (past[key] == null) {
|
||||
return true;
|
||||
}
|
||||
const bucketSize = significantChange[key];
|
||||
// If we don't have any requirements on this value, include it
|
||||
if (bucketSize == null) {
|
||||
return true;
|
||||
}
|
||||
export function isSignificantChange(
|
||||
key: string,
|
||||
past: number | undefined,
|
||||
current: number,
|
||||
): boolean {
|
||||
// If we didn't have a value for this in the past, include it
|
||||
if (past == null) {
|
||||
return true;
|
||||
}
|
||||
const bucketSize = significantChange[key as keyof SystemInfo];
|
||||
// If we don't have any requirements on this value, include it
|
||||
if (bucketSize == null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return (
|
||||
Math.floor((value as number) / bucketSize) !==
|
||||
Math.floor((past[key] as number) / bucketSize)
|
||||
);
|
||||
}),
|
||||
) as Array<keyof SystemInfo>;
|
||||
return Math.floor(current / bucketSize) !== Math.floor(past / bucketSize);
|
||||
}
|
||||
|
||||
function bytesToMb(bytes: number) {
|
||||
|
@ -24,55 +24,39 @@ describe('System information', async () => {
|
||||
|
||||
describe('Delta-based filtering', () => {
|
||||
it('should correctly filter cpu usage', () => {
|
||||
expect(
|
||||
sysInfo.filterNonSignificantChanges({ cpu_usage: 21 }, {
|
||||
cpu_usage: 20,
|
||||
} as sysInfo.SystemInfo),
|
||||
).to.deep.equal(['cpu_usage']);
|
||||
expect(sysInfo.isSignificantChange('cpu_usage', 21, 20)).to.equal(false);
|
||||
|
||||
expect(
|
||||
sysInfo.filterNonSignificantChanges({ cpu_usage: 10 }, {
|
||||
cpu_usage: 20,
|
||||
} as sysInfo.SystemInfo),
|
||||
).to.deep.equal([]);
|
||||
expect(sysInfo.isSignificantChange('cpu_usage', 10, 20)).to.equal(true);
|
||||
});
|
||||
|
||||
it('should correctly filter cpu temperature', () => {
|
||||
expect(
|
||||
sysInfo.filterNonSignificantChanges({ cpu_temp: 21 }, {
|
||||
cpu_temp: 22,
|
||||
} as sysInfo.SystemInfo),
|
||||
).to.deep.equal(['cpu_temp']);
|
||||
expect(sysInfo.isSignificantChange('cpu_temp', 21, 22)).to.equal(false);
|
||||
|
||||
expect(
|
||||
sysInfo.filterNonSignificantChanges({ cpu_temp: 10 }, {
|
||||
cpu_temp: 20,
|
||||
} as sysInfo.SystemInfo),
|
||||
).to.deep.equal([]);
|
||||
expect(sysInfo.isSignificantChange('cpu_temp', 10, 20)).to.equal(true);
|
||||
});
|
||||
|
||||
it('should correctly filter memory usage', () => {
|
||||
expect(
|
||||
sysInfo.filterNonSignificantChanges({ memory_usage: 21 }, {
|
||||
memory_usage: 22,
|
||||
} as sysInfo.SystemInfo),
|
||||
).to.deep.equal(['memory_usage']);
|
||||
expect(sysInfo.isSignificantChange('memory_usage', 21, 22)).to.equal(
|
||||
false,
|
||||
);
|
||||
|
||||
expect(
|
||||
sysInfo.filterNonSignificantChanges({ memory_usage: 10 }, {
|
||||
memory_usage: 20,
|
||||
} as sysInfo.SystemInfo),
|
||||
).to.deep.equal([]);
|
||||
expect(sysInfo.isSignificantChange('memory_usage', 10, 20)).to.equal(
|
||||
true,
|
||||
);
|
||||
});
|
||||
|
||||
it('should not filter if we didnt have a past value', () => {
|
||||
expect(sysInfo.isSignificantChange('cpu_usage', undefined, 22)).to.equal(
|
||||
true,
|
||||
);
|
||||
|
||||
expect(sysInfo.isSignificantChange('cpu_temp', undefined, 10)).to.equal(
|
||||
true,
|
||||
);
|
||||
|
||||
expect(
|
||||
sysInfo.filterNonSignificantChanges({}, {
|
||||
memory_usage: 22,
|
||||
cpu_usage: 10,
|
||||
cpu_temp: 5,
|
||||
} as sysInfo.SystemInfo),
|
||||
).to.deep.equal([]);
|
||||
sysInfo.isSignificantChange('memory_usage', undefined, 5),
|
||||
).to.equal(true);
|
||||
});
|
||||
});
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user