Add run and alert count to task state. (#86776)

This commit is contained in:
Pete Hampton 2020-12-22 19:55:40 +00:00 committed by GitHub
parent 97858d697b
commit 978fa4325c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -36,6 +36,8 @@ export class TelemetryDiagTask {
title: 'Security Solution Telemetry Diagnostics task',
timeout: TelemetryDiagTaskConstants.TIMEOUT,
createTaskRunner: ({ taskInstance }: { taskInstance: ConcreteTaskInstance }) => {
const { state } = taskInstance;
return {
run: async () => {
const executeTo = moment().utc().toISOString();
@ -43,11 +45,13 @@ export class TelemetryDiagTask {
executeTo,
taskInstance.state?.lastExecutionTimestamp
);
await this.runTask(taskInstance.id, executeFrom, executeTo);
const hits = await this.runTask(taskInstance.id, executeFrom, executeTo);
return {
state: {
lastExecutionTimestamp: executeTo,
lastDiagAlertCount: hits,
runs: (state.runs || 0) + 1,
},
};
},
@ -81,7 +85,7 @@ export class TelemetryDiagTask {
schedule: {
interval: TelemetryDiagTaskConstants.INTERVAL,
},
state: {},
state: { runs: 0 },
params: { version: TelemetryDiagTaskConstants.VERSION },
});
} catch (e) {
@ -97,13 +101,13 @@ export class TelemetryDiagTask {
this.logger.debug(`Running task ${taskId}`);
if (taskId !== this.getTaskId()) {
this.logger.debug(`Outdated task running: ${taskId}`);
return;
return 0;
}
const isOptedIn = await this.sender.isTelemetryOptedIn();
if (!isOptedIn) {
this.logger.debug(`Telemetry is not opted-in.`);
return;
return 0;
}
const response = await this.sender.fetchDiagnosticAlerts(searchFrom, searchTo);
@ -111,11 +115,12 @@ export class TelemetryDiagTask {
const hits = response.hits?.hits || [];
if (!Array.isArray(hits) || !hits.length) {
this.logger.debug('no diagnostic alerts retrieved');
return;
return 0;
}
this.logger.debug(`Received ${hits.length} diagnostic alerts`);
const diagAlerts: TelemetryEvent[] = hits.map((h) => h._source);
this.logger.debug(`Received ${diagAlerts.length} diagnostic alerts`);
this.sender.queueTelemetryEvents(diagAlerts);
return diagAlerts.length;
};
}