Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Usage collection] Usage counters #96696

Merged
merged 17 commits into from
Apr 14, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@ export {
registerUiCounterSavedObjectType,
registerUiCountersRollups,
} from './ui_counters';
export {
registerUsageCountersRollups,
registerUsageCountersUsageCollector,
} from './usage_counters';
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import type { UICounterSavedObject } from '../ui_counter_saved_object_type';
export const rawUiCounters: UICounterSavedObject[] = [
{
type: 'ui-counter',
id: 'Kibana_home:24112020:click:ingest_data_card_home_tutorial_directory',
attributes: {
count: 3,
},
references: [],
updated_at: '2020-11-24T11:27:57.067Z',
version: 'WzI5LDFd',
},
{
type: 'ui-counter',
id: 'Kibana_home:24112020:click:home_tutorial_directory',
attributes: {
count: 1,
},
references: [],
updated_at: '2020-11-24T11:27:57.067Z',
version: 'WzI5NDRd',
},
{
type: 'ui-counter',
id: 'Kibana_home:24112020:loaded:home_tutorial_directory',
attributes: {
count: 3,
},
references: [],
updated_at: '2020-10-23T11:27:57.067Z',
version: 'WzI5NDRd',
},
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* 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 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import type { UsageCountersSavedObject } from '../../../../../usage_collection/server';

export const rawUsageCounters: UsageCountersSavedObject[] = [
{
type: 'usage-counters',
id: 'uiCounter:09042021:count:myApp:my_event',
attributes: { count: 1 },
references: [],
coreMigrationVersion: '8.0.0',
updated_at: '2021-04-09T08:17:57.693Z',
version: 'WzAsMV0=',
score: 0,
},
{
type: 'usage-counters',
id: 'uiCounter:09042021:loaded:Kibana_home:home_tutorial_directory',
attributes: { count: 60 },
references: [],
coreMigrationVersion: '8.0.0',
updated_at: '2020-10-23T11:27:57.067Z',
version: 'WzUsMV0=',
score: 0,
},
{
type: 'usage-counters',
id: 'uiCounter:09042021:count:myApp:my_event_4457914848544',
attributes: { count: 0 },
references: [],
coreMigrationVersion: '8.0.0',
updated_at: '2021-04-09T08:18:03.030Z',
version: 'WzUsMV0=',
score: 0,
},
{
type: 'usage-counters',
id: 'uiCounter:09042021:count:myApp:my_event_malformed',
attributes: { count: 'malformed' },
references: [],
coreMigrationVersion: '8.0.0',
updated_at: '2021-04-09T08:18:03.030Z',
version: 'WzUsMV0=',
score: 0,
},
{
type: 'usage-counters',
id: 'anotherDomainId:09042021:count:some_event_name',
attributes: { count: 4 },
references: [],
coreMigrationVersion: '8.0.0',
updated_at: '2021-04-09T08:18:03.030Z',
version: 'WzUsMV0=',
score: 0,
},
{
type: 'usage-counters',
id: 'uiCounter:09042021:count:myApp:my_event_4457914848544_2',
attributes: { count: 8 },
references: [],
coreMigrationVersion: '8.0.0',
updated_at: '2021-04-09T08:18:03.031Z',
version: 'WzcsMV0=',
score: 0,
},
];
Original file line number Diff line number Diff line change
Expand Up @@ -6,70 +6,136 @@
* Side Public License, v 1.
*/

import { transformRawCounter } from './register_ui_counters_collector';
import { UICounterSavedObject } from './ui_counter_saved_object_type';
import {
transformRawUiCounterObject,
transformRawUsageCounterObject,
fetchUiCounters,
} from './register_ui_counters_collector';
import { rawUiCounters } from './__fixtures__/ui_counter_saved_objects';
import { rawUsageCounters } from './__fixtures__/usage_counter_saved_objects';
import { savedObjectsClientMock } from '../../../../../core/server/mocks';
import { UI_COUNTER_SAVED_OBJECT_TYPE } from './ui_counter_saved_object_type';
import { USAGE_COUNTERS_SAVED_OBJECT_TYPE } from '../../../../usage_collection/server';

describe('transformRawCounter', () => {
const mockRawUiCounters = [
{
type: 'ui-counter',
id: 'Kibana_home:24112020:click:ingest_data_card_home_tutorial_directory',
attributes: {
count: 3,
},
references: [],
updated_at: '2020-11-24T11:27:57.067Z',
version: 'WzI5LDFd',
},
{
type: 'ui-counter',
id: 'Kibana_home:24112020:click:home_tutorial_directory',
attributes: {
count: 1,
},
references: [],
updated_at: '2020-11-24T11:27:57.067Z',
version: 'WzI5NDRd',
},
{
type: 'ui-counter',
id: 'Kibana_home:24112020:loaded:home_tutorial_directory',
attributes: {
count: 3,
},
references: [],
updated_at: '2020-10-23T11:27:57.067Z',
version: 'WzI5NDRd',
},
] as UICounterSavedObject[];
describe('transformRawUsageCounterObject', () => {
it('transforms usage counters savedObject raw entries', () => {
const result = rawUsageCounters.map(transformRawUsageCounterObject);
expect(result).toMatchInlineSnapshot(`
Array [
Object {
"appName": "myApp",
"counterType": "count",
"eventName": "my_event",
"fromTimestamp": "2021-04-09T00:00:00Z",
"lastUpdatedAt": "2021-04-09T08:17:57.693Z",
"total": 1,
},
Object {
"appName": "Kibana_home",
"counterType": "loaded",
"eventName": "home_tutorial_directory",
"fromTimestamp": "2020-10-23T00:00:00Z",
"lastUpdatedAt": "2020-10-23T11:27:57.067Z",
"total": 60,
},
undefined,
undefined,
undefined,
Object {
"appName": "myApp",
"counterType": "count",
"eventName": "my_event_4457914848544_2",
"fromTimestamp": "2021-04-09T00:00:00Z",
"lastUpdatedAt": "2021-04-09T08:18:03.031Z",
"total": 8,
},
]
`);
});
});

describe('transformRawUiCounterObject', () => {
it('transforms ui counters savedObject raw entries', () => {
const result = rawUiCounters.map(transformRawUiCounterObject);
expect(result).toMatchInlineSnapshot(`
Array [
Object {
"appName": "Kibana_home",
"counterType": "click",
"eventName": "ingest_data_card_home_tutorial_directory",
"fromTimestamp": "2020-11-24T00:00:00Z",
"lastUpdatedAt": "2020-11-24T11:27:57.067Z",
"total": 3,
},
Object {
"appName": "Kibana_home",
"counterType": "click",
"eventName": "home_tutorial_directory",
"fromTimestamp": "2020-11-24T00:00:00Z",
"lastUpdatedAt": "2020-11-24T11:27:57.067Z",
"total": 1,
},
Object {
"appName": "Kibana_home",
"counterType": "loaded",
"eventName": "home_tutorial_directory",
"fromTimestamp": "2020-10-23T00:00:00Z",
"lastUpdatedAt": "2020-10-23T11:27:57.067Z",
"total": 3,
},
]
`);
});
});

describe('fetchUiCounters', () => {
const soClientMock = savedObjectsClientMock.create();
beforeEach(() => {
jest.clearAllMocks();
});
it('merges saved objects from both ui_counters and usage_counters saved objects', async () => {
soClientMock.find.mockImplementation(async ({ type }) => {
switch (type) {
case UI_COUNTER_SAVED_OBJECT_TYPE:
return { saved_objects: rawUiCounters };
case USAGE_COUNTERS_SAVED_OBJECT_TYPE:
return { saved_objects: rawUsageCounters };
default:
throw new Error(`unexpected type ${type}`);
}
});

const { dailyEvents } = await fetchUiCounters({ soClient: soClientMock });
expect(dailyEvents).toHaveLength(6);
const intersectingEntry = dailyEvents.find(
(dailyEvent) =>
dailyEvent.eventName === 'home_tutorial_directory' &&
dailyEvent.fromTimestamp === '2020-10-23T00:00:00Z'
);
const invalidCountEntry = dailyEvents.find(
(dailyEvent) => dailyEvent.eventName === 'my_event_malformed'
);

const zeroCountEntry = dailyEvents.find(
(dailyEvent) => dailyEvent.eventName === 'my_event_4457914848544'
);

const nonUiCountersEntry = dailyEvents.find(
(dailyEvent) => dailyEvent.eventName === 'some_event_name'
);

it('transforms saved object raw entries', () => {
const result = mockRawUiCounters.map(transformRawCounter);
expect(result).toEqual([
{
appName: 'Kibana_home',
eventName: 'ingest_data_card_home_tutorial_directory',
lastUpdatedAt: '2020-11-24T11:27:57.067Z',
fromTimestamp: '2020-11-24T00:00:00Z',
counterType: 'click',
total: 3,
},
{
appName: 'Kibana_home',
eventName: 'home_tutorial_directory',
lastUpdatedAt: '2020-11-24T11:27:57.067Z',
fromTimestamp: '2020-11-24T00:00:00Z',
counterType: 'click',
total: 1,
},
{
appName: 'Kibana_home',
eventName: 'home_tutorial_directory',
lastUpdatedAt: '2020-10-23T11:27:57.067Z',
fromTimestamp: '2020-10-23T00:00:00Z',
counterType: 'loaded',
total: 3,
},
]);
expect(invalidCountEntry).toBe(undefined);
expect(nonUiCountersEntry).toBe(undefined);
expect(zeroCountEntry).toBe(undefined);
expect(intersectingEntry).toMatchInlineSnapshot(`
Object {
"appName": "Kibana_home",
"counterType": "loaded",
"eventName": "home_tutorial_directory",
"fromTimestamp": "2020-10-23T00:00:00Z",
"lastUpdatedAt": "2020-10-23T11:27:57.067Z",
"total": 60,
}
`);
Bamieh marked this conversation as resolved.
Show resolved Hide resolved
});
});
Loading