-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
Copy pathtask.ts
144 lines (133 loc) · 4.6 KB
/
task.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { Logger, CoreSetup } from '@kbn/core/server';
import moment from 'moment';
import {
RunContext,
TaskManagerSetupContract,
TaskManagerStartContract,
} from '@kbn/task-manager-plugin/server';
import { PreConfiguredAction } from '../types';
import { getTotalCount, getInUseTotalCount, getExecutionsPerDayCount } from './actions_telemetry';
export const TELEMETRY_TASK_TYPE = 'actions_telemetry';
export const TASK_ID = `Actions-${TELEMETRY_TASK_TYPE}`;
export function initializeActionsTelemetry(
logger: Logger,
taskManager: TaskManagerSetupContract,
core: CoreSetup,
kibanaIndex: string,
preconfiguredActions: PreConfiguredAction[],
eventLogIndex: string
) {
registerActionsTelemetryTask(
logger,
taskManager,
core,
kibanaIndex,
preconfiguredActions,
eventLogIndex
);
}
export function scheduleActionsTelemetry(logger: Logger, taskManager: TaskManagerStartContract) {
scheduleTasks(logger, taskManager);
}
function registerActionsTelemetryTask(
logger: Logger,
taskManager: TaskManagerSetupContract,
core: CoreSetup,
kibanaIndex: string,
preconfiguredActions: PreConfiguredAction[],
eventLogIndex: string
) {
taskManager.registerTaskDefinitions({
[TELEMETRY_TASK_TYPE]: {
title: 'Actions usage fetch task',
timeout: '5m',
createTaskRunner: telemetryTaskRunner(
logger,
core,
kibanaIndex,
preconfiguredActions,
eventLogIndex
),
},
});
}
async function scheduleTasks(logger: Logger, taskManager: TaskManagerStartContract) {
try {
await taskManager.ensureScheduled({
id: TASK_ID,
taskType: TELEMETRY_TASK_TYPE,
state: {},
params: {},
});
} catch (e) {
logger.debug(`Error scheduling task, received ${e.message}`);
}
}
export function telemetryTaskRunner(
logger: Logger,
core: CoreSetup,
kibanaIndex: string,
preconfiguredActions: PreConfiguredAction[],
eventLogIndex: string
) {
return ({ taskInstance }: RunContext) => {
const { state } = taskInstance;
const getEsClient = () =>
core.getStartServices().then(
([
{
elasticsearch: { client },
},
]) => client.asInternalUser
);
return {
async run() {
const esClient = await getEsClient();
return Promise.all([
getTotalCount(esClient, kibanaIndex, logger, preconfiguredActions),
getInUseTotalCount(esClient, kibanaIndex, logger, undefined, preconfiguredActions),
getExecutionsPerDayCount(esClient, eventLogIndex, logger),
]).then(([totalAggegations, totalInUse, totalExecutionsPerDay]) => {
const hasErrors =
totalAggegations.hasErrors || totalInUse.hasErrors || totalExecutionsPerDay.hasErrors;
const errorMessages = [
totalAggegations.errorMessage,
totalInUse.errorMessage,
totalExecutionsPerDay.errorMessage,
].filter((message) => message !== undefined);
return {
state: {
has_errors: hasErrors,
...(errorMessages.length > 0 && { error_messages: errorMessages }),
runs: (state.runs || 0) + 1,
count_total: totalAggegations.countTotal,
count_by_type: totalAggegations.countByType,
count_active_total: totalInUse.countTotal,
count_active_by_type: totalInUse.countByType,
count_active_alert_history_connectors: totalInUse.countByAlertHistoryConnectorType,
count_active_email_connectors_by_service_type: totalInUse.countEmailByService,
count_actions_namespaces: totalInUse.countNamespaces,
count_actions_executions_per_day: totalExecutionsPerDay.countTotal,
count_actions_executions_by_type_per_day: totalExecutionsPerDay.countByType,
count_actions_executions_failed_per_day: totalExecutionsPerDay.countFailed,
count_actions_executions_failed_by_type_per_day:
totalExecutionsPerDay.countFailedByType,
avg_execution_time_per_day: totalExecutionsPerDay.avgExecutionTime,
avg_execution_time_by_type_per_day: totalExecutionsPerDay.avgExecutionTimeByType,
},
runAt: getNextMidnight(),
};
});
},
};
};
}
function getNextMidnight() {
return moment().add(1, 'd').startOf('d').toDate();
}