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 4 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 @@ -10,27 +10,27 @@ 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',
id: 'Kibana_home:23102020:click:different_type',
attributes: {
count: 3,
count: 1,
},
references: [],
updated_at: '2020-11-24T11:27:57.067Z',
version: 'WzI5LDFd',
version: 'WzI5NDRd',
},
{
type: 'ui-counter',
id: 'Kibana_home:24112020:click:home_tutorial_directory',
id: 'Kibana_home:25102020:loaded:intersecting_event',
attributes: {
count: 1,
},
references: [],
updated_at: '2020-11-24T11:27:57.067Z',
updated_at: '2020-10-25T11:27:57.067Z',
version: 'WzI5NDRd',
},
{
type: 'ui-counter',
id: 'Kibana_home:24112020:loaded:home_tutorial_directory',
id: 'Kibana_home:23102020:loaded:intersecting_event',
attributes: {
count: 3,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,56 +12,91 @@ export const rawUsageCounters: UsageCountersSavedObject[] = [
{
type: 'usage-counters',
id: 'uiCounter:09042021:count:myApp:my_event',
attributes: { count: 1 },
attributes: {
count: 1,
counterName: 'myApp:my_event',
counterType: 'count',
domainId: 'uiCounter',
},
references: [],
coreMigrationVersion: '8.0.0',
updated_at: '2021-04-09T08:17:57.693Z',
},
{
type: 'usage-counters',
id: 'uiCounter:09042021:loaded:Kibana_home:home_tutorial_directory',
attributes: { count: 60 },
id: 'uiCounter:23102020:loaded:Kibana_home:intersecting_event',
attributes: {
count: 60,
counterName: 'Kibana_home:intersecting_event',
counterType: 'loaded',
domainId: 'uiCounter',
},
references: [],
coreMigrationVersion: '8.0.0',
updated_at: '2020-10-23T11:27:57.067Z',
},
{
type: 'usage-counters',
id: 'uiCounter:09042021:count:myApp:my_event_4457914848544',
attributes: { count: 0 },
attributes: {
count: 0,
counterName: 'myApp:my_event_4457914848544',
counterType: 'count',
domainId: 'uiCounter',
},
references: [],
coreMigrationVersion: '8.0.0',
updated_at: '2021-04-09T08:18:03.030Z',
},
{
type: 'usage-counters',
id: 'uiCounter:09042021:count:myApp:my_event_malformed',
// @ts-expect-error
attributes: { count: 'malformed' },
attributes: {
// @ts-expect-error
count: 'malformed',
counterName: 'myApp:my_event_malformed',
counterType: 'count',
domainId: 'uiCounter',
},
references: [],
coreMigrationVersion: '8.0.0',
updated_at: '2021-04-09T08:18:03.030Z',
},
{
type: 'usage-counters',
id: 'anotherDomainId:09042021:count:some_event_name',
attributes: { count: 4 },
attributes: {
count: 4,
counterName: 'some_event_name',
counterType: 'count',
domainId: 'anotherDomainId',
},
references: [],
coreMigrationVersion: '8.0.0',
updated_at: '2021-04-09T08:18:03.030Z',
},
{
type: 'usage-counters',
id: 'uiCounter:09042021:count:myApp:my_event_4457914848544_2',
attributes: { count: 8 },
attributes: {
count: 8,
counterName: 'myApp:my_event_4457914848544_2',
counterType: 'count',
domainId: 'uiCounter',
},
references: [],
coreMigrationVersion: '8.0.0',
updated_at: '2021-04-09T08:18:03.031Z',
},
{
type: 'usage-counters',
id: 'uiCounter:09042021:count:myApp:only_reported_in_usage_counters',
attributes: { count: 1 },
attributes: {
count: 1,
counterName: 'myApp:only_reported_in_usage_counters',
counterType: 'count',
domainId: 'uiCounter',
},
references: [],
coreMigrationVersion: '8.0.0',
updated_at: '2021-04-09T08:18:03.031Z',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
import {
transformRawUiCounterObject,
transformRawUsageCounterObject,
fetchUiCounters,
createFetchUiCounters,
} from './register_ui_counters_collector';
import { BehaviorSubject } from 'rxjs';
import { rawUiCounters } from './__fixtures__/ui_counter_saved_objects';
import { rawUsageCounters } from './__fixtures__/usage_counter_saved_objects';
import { savedObjectsClientMock } from '../../../../../core/server/mocks';
Expand All @@ -33,7 +34,7 @@ describe('transformRawUsageCounterObject', () => {
Object {
"appName": "Kibana_home",
"counterType": "loaded",
"eventName": "home_tutorial_directory",
"eventName": "intersecting_event",
"fromTimestamp": "2020-10-23T00:00:00Z",
"lastUpdatedAt": "2020-10-23T11:27:57.067Z",
"total": 60,
Expand Down Expand Up @@ -70,23 +71,23 @@ describe('transformRawUiCounterObject', () => {
Object {
"appName": "Kibana_home",
"counterType": "click",
"eventName": "ingest_data_card_home_tutorial_directory",
"eventName": "different_type",
"fromTimestamp": "2020-11-24T00:00:00Z",
"lastUpdatedAt": "2020-11-24T11:27:57.067Z",
"total": 3,
"total": 1,
},
Object {
"appName": "Kibana_home",
"counterType": "click",
"eventName": "home_tutorial_directory",
"fromTimestamp": "2020-11-24T00:00:00Z",
"lastUpdatedAt": "2020-11-24T11:27:57.067Z",
"counterType": "loaded",
"eventName": "intersecting_event",
"fromTimestamp": "2020-10-25T00:00:00Z",
"lastUpdatedAt": "2020-10-25T11:27:57.067Z",
"total": 1,
},
Object {
"appName": "Kibana_home",
"counterType": "loaded",
"eventName": "home_tutorial_directory",
"eventName": "intersecting_event",
"fromTimestamp": "2020-10-23T00:00:00Z",
"lastUpdatedAt": "2020-10-23T11:27:57.067Z",
"total": 3,
Expand All @@ -104,11 +105,36 @@ describe('transformRawUiCounterObject', () => {
});
});

describe('fetchUiCounters', () => {
describe('createFetchUiCounters', () => {
let stopUsingUiCounterIndicies$: BehaviorSubject<boolean>;
const soClientMock = savedObjectsClientMock.create();
beforeEach(() => {
jest.clearAllMocks();
stopUsingUiCounterIndicies$ = new BehaviorSubject<boolean>(false);
});

it('does not query ui_counters saved objects if stopUsingUiCounterIndicies$ is complete', async () => {
// @ts-expect-error incomplete mock implementation
soClientMock.find.mockImplementation(async ({ type }) => {
switch (type) {
case USAGE_COUNTERS_SAVED_OBJECT_TYPE:
return { saved_objects: rawUsageCounters };
default:
throw new Error(`unexpected type ${type}`);
}
});

stopUsingUiCounterIndicies$.complete();
// @ts-expect-error incomplete mock implementation
const { dailyEvents } = await createFetchUiCounters(stopUsingUiCounterIndicies$)({
soClient: soClientMock,
});

const transforemdUsageCounters = rawUsageCounters.map(transformRawUsageCounterObject);
expect(soClientMock.find).toBeCalledTimes(1);
expect(dailyEvents).toEqual(transforemdUsageCounters.filter(Boolean));
});

it('merges saved objects from both ui_counters and usage_counters saved objects', async () => {
// @ts-expect-error incomplete mock implementation
soClientMock.find.mockImplementation(async ({ type }) => {
Expand All @@ -123,11 +149,13 @@ describe('fetchUiCounters', () => {
});

// @ts-expect-error incomplete mock implementation
const { dailyEvents } = await fetchUiCounters({ soClient: soClientMock });
expect(dailyEvents).toHaveLength(8);
const { dailyEvents } = await createFetchUiCounters(stopUsingUiCounterIndicies$)({
soClient: soClientMock,
});
expect(dailyEvents).toHaveLength(7);
const intersectingEntry = dailyEvents.find(
({ eventName, fromTimestamp }) =>
eventName === 'home_tutorial_directory' && fromTimestamp === '2020-10-23T00:00:00Z'
eventName === 'intersecting_event' && fromTimestamp === '2020-10-23T00:00:00Z'
);

const onlyFromUICountersEntry = dailyEvents.find(
Expand Down Expand Up @@ -175,10 +203,10 @@ describe('fetchUiCounters', () => {
Object {
"appName": "Kibana_home",
"counterType": "loaded",
"eventName": "home_tutorial_directory",
"eventName": "intersecting_event",
"fromTimestamp": "2020-10-23T00:00:00Z",
"lastUpdatedAt": "2020-10-23T11:27:57.067Z",
"total": 60,
"total": 63,
}
`);
});
Expand Down
Loading