-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* UI Metrics use findAll to retrieve all Saved Objects * Rename test description Co-Authored-By: Christiane (Tina) Heiligers <[email protected]> Co-authored-by: Elastic Machine <[email protected]> Co-authored-by: Christiane (Tina) Heiligers <[email protected]> Co-authored-by: Elastic Machine <[email protected]> Co-authored-by: Christiane (Tina) Heiligers <[email protected]>
- Loading branch information
1 parent
29c9f00
commit a43a29c
Showing
6 changed files
with
207 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/legacy/core_plugins/telemetry/server/collectors/find_all.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import { savedObjectsRepositoryMock } from '../../../../../core/server/mocks'; | ||
|
||
import { findAll } from './find_all'; | ||
|
||
describe('telemetry_application_usage', () => { | ||
test('when savedObjectClient is initialised, return something', async () => { | ||
const savedObjectClient = savedObjectsRepositoryMock.create(); | ||
savedObjectClient.find.mockImplementation( | ||
async () => | ||
({ | ||
saved_objects: [], | ||
total: 0, | ||
} as any) | ||
); | ||
|
||
expect(await findAll(savedObjectClient, { type: 'test-type' })).toStrictEqual([]); | ||
}); | ||
|
||
test('paging in findAll works', async () => { | ||
const savedObjectClient = savedObjectsRepositoryMock.create(); | ||
let total = 201; | ||
const doc = { id: 'test-id', attributes: { test: 1 } }; | ||
savedObjectClient.find.mockImplementation(async opts => { | ||
if ((opts.page || 1) > 2) { | ||
return { saved_objects: [], total } as any; | ||
} | ||
const savedObjects = new Array(opts.perPage).fill(doc); | ||
total = savedObjects.length * 2 + 1; | ||
return { saved_objects: savedObjects, total }; | ||
}); | ||
|
||
expect(await findAll(savedObjectClient, { type: 'test-type' })).toStrictEqual( | ||
new Array(total - 1).fill(doc) | ||
); | ||
}); | ||
}); |
41 changes: 41 additions & 0 deletions
41
src/legacy/core_plugins/telemetry/server/collectors/find_all.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import { | ||
SavedObjectAttributes, | ||
ISavedObjectsRepository, | ||
SavedObjectsFindOptions, | ||
SavedObject, | ||
} from 'kibana/server'; | ||
|
||
export async function findAll<T extends SavedObjectAttributes>( | ||
savedObjectsClient: ISavedObjectsRepository, | ||
opts: SavedObjectsFindOptions | ||
): Promise<Array<SavedObject<T>>> { | ||
const { page = 1, perPage = 100, ...options } = opts; | ||
const { saved_objects: savedObjects, total } = await savedObjectsClient.find<T>({ | ||
...options, | ||
page, | ||
perPage, | ||
}); | ||
if (page * perPage >= total) { | ||
return savedObjects; | ||
} | ||
return [...savedObjects, ...(await findAll<T>(savedObjectsClient, { ...opts, page: page + 1 }))]; | ||
} |
86 changes: 86 additions & 0 deletions
86
src/legacy/core_plugins/telemetry/server/collectors/ui_metric/index.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import { UsageCollectionSetup } from '../../../../../../plugins/usage_collection/server'; | ||
import { savedObjectsRepositoryMock } from '../../../../../../core/server/mocks'; | ||
// eslint-disable-next-line @kbn/eslint/no-restricted-paths | ||
import { CollectorOptions } from '../../../../../../plugins/usage_collection/server/collector/collector'; | ||
|
||
import { registerUiMetricUsageCollector } from './'; | ||
|
||
describe('telemetry_ui_metric', () => { | ||
let collector: CollectorOptions; | ||
|
||
const usageCollectionMock: jest.Mocked<UsageCollectionSetup> = { | ||
makeUsageCollector: jest.fn().mockImplementation(config => (collector = config)), | ||
registerCollector: jest.fn(), | ||
} as any; | ||
|
||
const getUsageCollector = jest.fn(); | ||
const callCluster = jest.fn(); | ||
|
||
beforeAll(() => registerUiMetricUsageCollector(usageCollectionMock, getUsageCollector)); | ||
|
||
test('registered collector is set', () => { | ||
expect(collector).not.toBeUndefined(); | ||
}); | ||
|
||
test('if no savedObjectClient initialised, return undefined', async () => { | ||
expect(await collector.fetch(callCluster)).toBeUndefined(); | ||
}); | ||
|
||
test('when savedObjectClient is initialised, return something', async () => { | ||
const savedObjectClient = savedObjectsRepositoryMock.create(); | ||
savedObjectClient.find.mockImplementation( | ||
async () => | ||
({ | ||
saved_objects: [], | ||
total: 0, | ||
} as any) | ||
); | ||
getUsageCollector.mockImplementation(() => savedObjectClient); | ||
|
||
expect(await collector.fetch(callCluster)).toStrictEqual({}); | ||
expect(savedObjectClient.bulkCreate).not.toHaveBeenCalled(); | ||
}); | ||
|
||
test('results grouped by appName', async () => { | ||
const savedObjectClient = savedObjectsRepositoryMock.create(); | ||
savedObjectClient.find.mockImplementation(async () => { | ||
return { | ||
saved_objects: [ | ||
{ id: 'testAppName:testKeyName1', attributes: { count: 3 } }, | ||
{ id: 'testAppName:testKeyName2', attributes: { count: 5 } }, | ||
{ id: 'testAppName2:testKeyName3', attributes: { count: 1 } }, | ||
], | ||
total: 3, | ||
} as any; | ||
}); | ||
|
||
getUsageCollector.mockImplementation(() => savedObjectClient); | ||
|
||
expect(await collector.fetch(callCluster)).toStrictEqual({ | ||
testAppName: [ | ||
{ key: 'testKeyName1', value: 3 }, | ||
{ key: 'testKeyName2', value: 5 }, | ||
], | ||
testAppName2: [{ key: 'testKeyName3', value: 1 }], | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters