Report must include a state change if sooner than metrics frequency

Export list of metrics properties from device state module for this
determination.

Change-type: patch
Signed-off-by: Ken Bannister <kb2ma@runbox.com>
This commit is contained in:
Ken Bannister 2022-11-16 21:19:22 -05:00
parent d1dd53b5b6
commit b1d4aa5159
2 changed files with 34 additions and 2 deletions

View File

@ -82,7 +82,7 @@ async function report({ body, opts }: StateReport) {
*
* Does *not* validate time elapsed since last report.
*/
async function reportCurrentState(opts: StateReportOpts) {
async function reportCurrentState(opts: StateReportOpts, uuid: string) {
const getStateAndReport = async () => {
const currentState = await deviceState.getCurrentForReport(lastReport);
const stateDiff = prune(shallowDiff(lastReport, currentState, 2));
@ -91,6 +91,21 @@ async function reportCurrentState(opts: StateReportOpts) {
return;
}
// If metrics not yet scheduled, report must include a state change to
// qualify for sending.
const metricsScheduled =
performance.now() - lastReportTime > maxMetricsFrequency;
if (!metricsScheduled) {
const uuidMap = stateDiff[uuid] as { [k: string]: any };
if (
Object.keys(uuidMap).every((n) =>
deviceState.sysInfoPropertyNames.includes(n),
)
) {
return;
}
}
await report({ body: stateDiff, opts });
lastReportTime = performance.now();
lastReport = currentState;
@ -145,6 +160,11 @@ export async function startReporting() {
'deviceApiKey',
'appUpdatePollInterval',
])) as StateReportOpts;
// Pass uuid to report separately to guarantee it exists.
const uuid = await config.get('uuid');
if (!uuid) {
throw new InternalInconsistencyError('No uuid found for local device');
}
let reportPending = false;
// Reports current state if not already sending and does not exceed report
@ -153,7 +173,7 @@ export async function startReporting() {
if (!reportPending) {
if (performance.now() - lastReportTime > maxReportFrequency) {
reportPending = true;
await reportCurrentState(reportConfigs);
await reportCurrentState(reportConfigs, uuid);
reportPending = false;
return true;
} else {

View File

@ -447,6 +447,18 @@ async function getSysInfo(
);
}
/** SysInfo (metrics) property names used in report. */
export const sysInfoPropertyNames = [
'cpu_usage',
'memory_usage',
'memory_total',
'storage_usage',
'storage_total',
'storage_block_device',
'cpu_temp',
'cpu_id',
];
// Return current state in a way that the API understands
export async function getCurrentForReport(
lastReport = {} as DeviceState,