Skip to content

Commit

Permalink
Remove getSavedObjectsRepository, use NP internalRepository
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron Caldwell committed Feb 20, 2020
1 parent 8fe9aec commit b462cda
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 101 deletions.
15 changes: 5 additions & 10 deletions x-pack/legacy/plugins/file_upload/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,14 @@ export const fileUpload = kibana => {

init(server) {
const coreSetup = server.newPlatform.setup.core;
const coreStart = server.newPlatform.start.core;
const { usageCollection } = server.newPlatform.setup.plugins;
const pluginsSetup = {
const pluginsStart = {
usageCollection,
};

// legacy dependencies
const __LEGACY = {
savedObjects: {
getSavedObjectsRepository: server.savedObjects.getSavedObjectsRepository,
},
};

new FileUploadPlugin().setup(coreSetup, pluginsSetup, __LEGACY);
const fileUploadPlugin = new FileUploadPlugin();
fileUploadPlugin.setup(coreSetup);
fileUploadPlugin.start(coreStart, pluginsStart);
},
});
};

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
*/

let dataClient;
let adminClient;

export const setElasticsearchClientServices = elasticsearch => {
({ dataClient, adminClient } = elasticsearch);
({ dataClient } = elasticsearch);
};
export const getAdminClient = () => adminClient;
export const getDataClient = () => dataClient;

let internalRepository;
export const setInternalRepository = createInternalRepository => {
internalRepository = createInternalRepository();
};
export const getInternalRepository = () => internalRepository;
20 changes: 12 additions & 8 deletions x-pack/legacy/plugins/file_upload/server/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,23 @@
*/

import { initRoutes } from './routes/file_upload';
import { setElasticsearchClientServices } from './kibana_server_services';
import { setElasticsearchClientServices, setInternalRepository } from './kibana_server_services';
import { registerFileUploadUsageCollector } from './telemetry';

export class FileUploadPlugin {
setup(core, plugins, __LEGACY) {
const getSavedObjectsRepository = __LEGACY.savedObjects.getSavedObjectsRepository;
const router = core.http.createRouter();
constructor() {
this.router = null;
}

setup(core) {
setElasticsearchClientServices(core.elasticsearch);
initRoutes(router, getSavedObjectsRepository);
this.router = core.http.createRouter();
}

start(core, plugins) {
initRoutes(this.router, core.savedObjects.getSavedObjectsRepository);
setInternalRepository(core.savedObjects.createInternalRepository);

registerFileUploadUsageCollector(plugins.usageCollection, {
getSavedObjectsRepository,
});
registerFileUploadUsageCollector(plugins.usageCollection);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export const idConditionalValidation = (body, boolHasId) =>
)
.validate(body);

const finishValidationAndProcessReq = getSavedObjectsRepository => {
const finishValidationAndProcessReq = () => {
return async (con, req, { ok, badRequest }) => {
const {
query: { id },
Expand Down Expand Up @@ -103,7 +103,7 @@ const finishValidationAndProcessReq = getSavedObjectsRepository => {
resp = ok({ body: processedReq });
// If no id's been established then this is a new index, update telemetry
if (!boolHasId) {
await updateTelemetry({ getSavedObjectsRepository });
await updateTelemetry();
}
} else {
resp = badRequest(`Error processing request 1: ${processedReq.error.message}`, ['body']);
Expand All @@ -115,7 +115,7 @@ const finishValidationAndProcessReq = getSavedObjectsRepository => {
};
};

export const initRoutes = (router, getSavedObjectsRepository) => {
export const initRoutes = router => {
router.post(
{
path: `${IMPORT_ROUTE}{id?}`,
Expand All @@ -125,6 +125,6 @@ export const initRoutes = (router, getSavedObjectsRepository) => {
},
options,
},
finishValidationAndProcessReq(getSavedObjectsRepository)
finishValidationAndProcessReq()
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,11 @@ import { getTelemetry, initTelemetry } from './telemetry';

const TELEMETRY_TYPE = 'fileUploadTelemetry';

export function registerFileUploadUsageCollector(
usageCollection: UsageCollectionSetup,
deps: {
getSavedObjectsRepository: any;
}
): void {
const { getSavedObjectsRepository } = deps;
export function registerFileUploadUsageCollector(usageCollection: UsageCollectionSetup): void {
const fileUploadUsageCollector = usageCollection.makeUsageCollector({
type: TELEMETRY_TYPE,
isReady: () => true,
fetch: async () => (await getTelemetry(getSavedObjectsRepository)) || initTelemetry(),
fetch: async () => (await getTelemetry()) || initTelemetry(),
});

usageCollection.registerCollector(fileUploadUsageCollector);
Expand Down
27 changes: 7 additions & 20 deletions x-pack/legacy/plugins/file_upload/server/telemetry/telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
*/

import _ from 'lodash';
import { callWithInternalUserFactory } from '../client/call_with_internal_user_factory';
// @ts-ignore
import { getInternalRepository } from '../kibana_server_services';

export const TELEMETRY_DOC_ID = 'file-upload-telemetry';

Expand All @@ -17,22 +18,14 @@ export interface TelemetrySavedObject {
attributes: Telemetry;
}

export function getInternalRepository(getSavedObjectsRepository: any): any {
const callWithInternalUser = callWithInternalUserFactory();
return getSavedObjectsRepository(callWithInternalUser);
}

export function initTelemetry(): Telemetry {
return {
filesUploadedTotalCount: 0,
};
}

export async function getTelemetry(
getSavedObjectsRepository: any,
internalRepo?: object
): Promise<Telemetry> {
const internalRepository = internalRepo || getInternalRepository(getSavedObjectsRepository);
export async function getTelemetry(internalRepo?: object): Promise<Telemetry> {
const internalRepository = internalRepo || getInternalRepository();
let telemetrySavedObject;

try {
Expand All @@ -44,15 +37,9 @@ export async function getTelemetry(
return telemetrySavedObject ? telemetrySavedObject.attributes : null;
}

export async function updateTelemetry({
getSavedObjectsRepository,
internalRepo,
}: {
getSavedObjectsRepository: any;
internalRepo?: any;
}) {
const internalRepository = internalRepo || getInternalRepository(getSavedObjectsRepository);
let telemetry = await getTelemetry(getSavedObjectsRepository, internalRepository);
export async function updateTelemetry(internalRepo?: any) {
const internalRepository = internalRepo || getInternalRepository();
let telemetry = await getTelemetry(internalRepository);
// Create if doesn't exist
if (!telemetry || _.isEmpty(telemetry)) {
const newTelemetrySavedObject = await internalRepository.create(
Expand Down

0 comments on commit b462cda

Please sign in to comment.