Skip to content

Commit

Permalink
Updating tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ymao1 committed Apr 14, 2021
1 parent c728d9d commit bc4dbc4
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 12 deletions.
67 changes: 67 additions & 0 deletions x-pack/plugins/actions/server/usage/actions_telemetry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,79 @@ Object {

expect(telemetry).toMatchInlineSnapshot(`
Object {
"countByAlertHistoryConnectorType": 0,
"countByType": Object {
"__server-log": 1,
"__slack": 1,
},
"countTotal": 2,
}
`);
});

test('getInUseTotalCount should count preconfigured alert history connector usage', async () => {
const mockEsClient = elasticsearchClientMock.createClusterClient().asScoped().asInternalUser;
mockEsClient.search.mockReturnValue(
// @ts-expect-error not full search response
elasticsearchClientMock.createSuccessTransportRequestPromise({
aggregations: {
refs: {
actionRefIds: {
value: {
connectorIds: {
'1': 'action_0',
'123': 'action_1',
'preconfigured-alert-history-es-index': 'action_2',
},
total: 3,
},
},
},
hits: {
hits: [],
},
},
})
);
const actionsBulkGet = jest.fn();
actionsBulkGet.mockReturnValue({
saved_objects: [
{
id: '1',
attributes: {
actionTypeId: '.server-log',
},
},
{
id: '123',
attributes: {
actionTypeId: '.slack',
},
},
{
id: 'preconfigured-alert-history-es-index',
error: {
statusCode: 404,
error: 'Not Found',
message: 'Saved object [action/preconfigured-alert-history-es-index] not found',
},
},
],
});
const telemetry = await getInUseTotalCount(mockEsClient, actionsBulkGet, 'test');

expect(mockEsClient.search).toHaveBeenCalledTimes(1);
expect(actionsBulkGet).toHaveBeenCalledTimes(1);

expect(telemetry).toMatchInlineSnapshot(`
Object {
"countByAlertHistoryConnectorType": 1,
"countByType": Object {
"__server-log": 1,
"__slack": 1,
},
"countTotal": 3,
}
`);
});
});
10 changes: 2 additions & 8 deletions x-pack/plugins/actions/server/usage/actions_telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@ export async function getInUseTotalCount(
}));
const actions = await actionsBulkGet(bulkFilter);

// filter out preconfigured connectors, which are not saved objects
// filter out preconfigured connectors, which are not saved objects and return
// an error in the bulk response
const actionsWithActionTypeId = actions.saved_objects.filter(
(action) => action?.attributes?.actionTypeId != null
);
Expand All @@ -192,13 +193,6 @@ export async function getInUseTotalCount(
(action) => action.id === AlertHistoryEsIndexConnectorId
);

console.log(
`actions telemetry ${JSON.stringify({
countTotal: aggs.total,
countByType: countByActionTypeId,
countByAlertHistoryConnectorType: preconfiguredAlertHistoryConnector.length,
})}`
);
return {
countTotal: aggs.total,
countByType: countByActionTypeId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@

import { UsageCollectionSetup } from 'src/plugins/usage_collection/server';
import { registerActionsUsageCollector } from './actions_usage_collector';
import { configSchema, ActionsConfig } from '../config';
import { taskManagerMock } from '../../../task_manager/server/mocks';

const mockTaskManagerStart = taskManagerMock.createStart();

beforeEach(() => jest.resetAllMocks());

describe('registerActionsUsageCollector', () => {
let config: ActionsConfig;
let usageCollectionMock: jest.Mocked<UsageCollectionSetup>;
beforeEach(() => {
config = configSchema.validate({});
usageCollectionMock = ({
makeUsageCollector: jest.fn(),
registerCollector: jest.fn(),
Expand All @@ -25,6 +28,7 @@ describe('registerActionsUsageCollector', () => {
it('should call registerCollector', () => {
registerActionsUsageCollector(
usageCollectionMock as UsageCollectionSetup,
config,
new Promise(() => mockTaskManagerStart)
);
expect(usageCollectionMock.registerCollector).toHaveBeenCalledTimes(1);
Expand All @@ -33,6 +37,7 @@ describe('registerActionsUsageCollector', () => {
it('should call makeUsageCollector with type = actions', () => {
registerActionsUsageCollector(
usageCollectionMock as UsageCollectionSetup,
config,
new Promise(() => mockTaskManagerStart)
);
expect(usageCollectionMock.makeUsageCollector).toHaveBeenCalledTimes(1);
Expand Down
5 changes: 1 addition & 4 deletions x-pack/plugins/actions/server/usage/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,5 @@ export function telemetryTaskRunner(logger: Logger, core: CoreSetup, kibanaIndex
}

function getNextMidnight() {
// return moment().add(1, 'd').startOf('d').toDate();
const runAt = new Date();
runAt.setMilliseconds(new Date().getMilliseconds() + 15000);
return runAt;
return moment().add(1, 'd').startOf('d').toDate();
}

0 comments on commit bc4dbc4

Please sign in to comment.