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

Adds get all REST API to data views #131683

Merged
merged 12 commits into from
Jun 2, 2022
1 change: 0 additions & 1 deletion src/plugins/data_views/common/data_views/data_views.ts
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,6 @@ export class DataViewsService {
* Get an index pattern by id. Cache optimized
* @param id
*/

get = async (id: string): Promise<DataView> => {
const indexPatternPromise =
this.dataViewCache.get(id) || this.dataViewCache.set(id, this.getSavedObjectAndInit(id));
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/data_views/server/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export const SPECIFIC_SCRIPTED_FIELD_PATH_LEGACY = `${SCRIPTED_FIELD_PATH_LEGACY

export const SERVICE_KEY = 'data_view';
export const SERVICE_KEY_LEGACY = 'index_pattern';
export const SERVICE_KEY_MULTIPLE = 'data_views';
export const SERVICE_KEY_MULTIPLE_LEGACY = 'index_patterns';
export type SERVICE_KEY_TYPE = typeof SERVICE_KEY | typeof SERVICE_KEY_LEGACY;

export const CREATE_DATA_VIEW_COUNTER_NAME = `POST ${DATA_VIEW_PATH}`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* 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 { getDataViews } from './get_data_views';
import { dataViewsService } from '../mocks';
import { getUsageCollection } from './test_utils';

describe('get all data views', () => {
it('call usageCollection', () => {
const usageCollection = getUsageCollection();
getDataViews({
dataViewsService,
counterName: 'GET /path',
usageCollection,
size: 10,
});
expect(usageCollection.incrementCounter).toBeCalledTimes(1);
});
});
102 changes: 102 additions & 0 deletions src/plugins/data_views/server/rest_api_routes/get_data_views.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* 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 { UsageCounter } from '@kbn/usage-collection-plugin/server';
import { schema } from '@kbn/config-schema';
import { IRouter, StartServicesAccessor } from '@kbn/core/server';
import { DataViewsService } from '../../common';
import { handleErrors } from './util/handle_errors';
import type { DataViewsServerPluginStartDependencies, DataViewsServerPluginStart } from '../types';
import {
SERVICE_PATH,
SERVICE_PATH_LEGACY,
SERVICE_KEY_MULTIPLE,
SERVICE_KEY_MULTIPLE_LEGACY,
} from '../constants';

interface GetDataViewsArgs {
dataViewsService: DataViewsService;
usageCollection?: UsageCounter;
counterName: string;
size: number;
}

export const getDataViews = async ({
dataViewsService,
usageCollection,
counterName,
size,
}: GetDataViewsArgs) => {
usageCollection?.incrementCounter({ counterName });
return dataViewsService.find('', size);
mattkime marked this conversation as resolved.
Show resolved Hide resolved
};

const getDataViewsRouteFactory =
(path: string, serviceKey: string) =>
(
router: IRouter,
getStartServices: StartServicesAccessor<
DataViewsServerPluginStartDependencies,
DataViewsServerPluginStart
>,
usageCollection?: UsageCounter
) => {
router.get(
{
path,
validate: {
query: schema.object({
size: schema.number({
defaultValue: 10,
max: 10000,
}),
}),
},
},
router.handleLegacyErrors(
handleErrors(async (ctx, req, res) => {
const core = await ctx.core;
const savedObjectsClient = core.savedObjects.client;
const elasticsearchClient = core.elasticsearch.client.asCurrentUser;
const [, , { dataViewsServiceFactory }] = await getStartServices();
const dataViewsService = await dataViewsServiceFactory(
savedObjectsClient,
elasticsearchClient,
req
);
const { size } = req.query;

const dataViews = await getDataViews({
dataViewsService,
usageCollection,
counterName: `${req.route.method} ${path}`,
size,
});

return res.ok({
headers: {
'content-type': 'application/json',
},
body: {
[serviceKey]: dataViews.map((dataView) => dataView.toSpec()),
},
});
})
)
);
};

export const registerGetDataViewsRoute = getDataViewsRouteFactory(
SERVICE_PATH,
SERVICE_KEY_MULTIPLE
);

export const registerGetDataViewsRouteLegacy = getDataViewsRouteFactory(
mattkime marked this conversation as resolved.
Show resolved Hide resolved
SERVICE_PATH_LEGACY,
SERVICE_KEY_MULTIPLE_LEGACY
);
3 changes: 3 additions & 0 deletions src/plugins/data_views/server/rest_api_routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import * as createRoutes from './create_data_view';
import * as defaultRoutes from './default_data_view';
import * as deleteRoutes from './delete_data_view';
import * as getRoutes from './get_data_view';
import * as getAllRoutes from './get_data_views';
import * as hasRoutes from './has_user_data_view';
import * as updateRoutes from './update_data_view';

Expand All @@ -38,6 +39,8 @@ const routes = [
deleteRoutes.registerDeleteDataViewRouteLegacy,
getRoutes.registerGetDataViewRoute,
getRoutes.registerGetDataViewRouteLegacy,
getAllRoutes.registerGetDataViewsRoute,
getAllRoutes.registerGetDataViewsRouteLegacy,
hasRoutes.registerHasUserDataViewRoute,
hasRoutes.registerHasUserDataViewRouteLegacy,
updateRoutes.registerUpdateDataViewRoute,
Expand Down