Skip to content

Commit

Permalink
Logging UI now runs on top of new platform shim
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonrhodes committed Oct 10, 2019
1 parent 4de9d17 commit a7429cd
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export interface InfraServerPluginDeps {
}

/* eslint-disable @typescript-eslint/unified-signatures */
export interface InfraBackendFrameworkAdapter<R> {
export interface InfraBackendFrameworkAdapter<R = unknown> {
registerGraphQLEndpoint(routePath: string, schema: GraphQLSchema): void;
registerRoute<RouteRequest extends InfraWrappableRequest, RouteResponse extends InfraResponse>(
route: R
Expand Down Expand Up @@ -66,10 +66,10 @@ export interface InfraBackendFrameworkAdapter<R> {
// NP_TODO: using Promise<unknown> here until new platform callAsCurrentUser can return types
// callWithRequest(req: KibanaRequest, method: string, options?: object): Promise<unknown>;

getIndexPatternsService(req: InfraFrameworkRequest<any>): Legacy.IndexPatternsService;
getSpaceId(request: InfraFrameworkRequest<any>): string;
getIndexPatternsService(req: KibanaRequest): Legacy.IndexPatternsService;
getSpaceId(request: KibanaRequest): string;
makeTSVBRequest(
req: InfraFrameworkRequest,
req: KibanaRequest,
model: InfraMetricModel,
timerange: { min: number; max: number },
filters: JsonObject[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,24 @@ export class InfraKibanaBackendFrameworkAdapter
const body = schema.object({
operationName: schema.string(),
query: schema.string(),
variables: schema.object({
sourceId: schema.string(),
}),
// NP_TODO: we short circuit validation here, need to understand the best way forward
// whether it's using io-ts and skipping ALL config-schema validation or figuring out
// why the nested maybe stuff inside variables didn't work well here
variables: schema.object(
{
// sourceId: schema.string(),
// countBefore: schema.maybe(schema.number()),
// countAfter: schema.maybe(schema.number()),
// filterQuery: schema.maybe(schema.string()),
// timeKey: schema.maybe(
// schema.object({
// time: schema.maybe(schema.number()),
// tiebreaker: schema.maybe(schema.number()),
// })
// ),
},
{ allowUnknowns: true }
),
});
type Body = TypeOf<typeof body>;

Expand All @@ -101,7 +116,7 @@ export class InfraKibanaBackendFrameworkAdapter
const gqlResponse = await runHttpQuery([request], {
method: request.route.method.toUpperCase(),
options: (req: Legacy.Request) => ({
context: { req: wrapRequest(req) },
context: { req },
schema: gqlSchema,
}),
query,
Expand All @@ -114,7 +129,9 @@ export class InfraKibanaBackendFrameworkAdapter
},
});
} catch (error) {
return response.badRequest({ body: error });
return response.internalError({
body: { ...error, temporary: 'this is just a temporary error catch' },
});
// NP_TODO handle errors! (see below for previously handled error cases)
}

Expand Down Expand Up @@ -238,19 +255,24 @@ export class InfraKibanaBackendFrameworkAdapter
{
...params,
...frozenIndicesParams,
// NP_TODO not sure how to use config auth automatically??
headers: {
Authorization: 'Basic ZWxhc3RpYzpjaGFuZ2VtZQ==', // 8.0 shared 'Basic YWRtaW46dkw0MVpiREpoNWtuUUE=',
},
},
...rest
// NP_TODO: not sure we need this?
{
wrap401Errors: true,
}
);
return fields as Promise<InfraDatabaseSearchResponse<Hit, Aggregation>>;
}

public getIndexPatternsService(
request: InfraFrameworkRequest<Legacy.Request>
): Legacy.IndexPatternsService {
public getIndexPatternsService(request: KibanaRequest): Legacy.IndexPatternsService {
return this.plugins.indexPatterns.indexPatternsServiceFactory({
callCluster: async (method: string, args: [GenericParams], ...rest: any[]) => {
const fieldCaps = await this.callWithRequest(
request[internalInfraFrameworkRequest],
request,
method,
{ ...args, allowNoIndices: true } as GenericParams,
...rest
Expand All @@ -260,63 +282,60 @@ export class InfraKibanaBackendFrameworkAdapter
});
}

public getSpaceId(request: InfraFrameworkRequest): string {
public getSpaceId(request: KibanaRequest): string {
const spacesPlugin = this.plugins.spaces;

if (spacesPlugin && typeof spacesPlugin.getSpaceId === 'function') {
return spacesPlugin.getSpaceId(request[internalInfraFrameworkRequest]);
return spacesPlugin.getSpaceId(request);
} else {
return 'default';
}
}

// NP_TODO: this function still needs NP migration for the metrics plugin
// and for the getBasePath
public async makeTSVBRequest(
req: InfraFrameworkRequest<Legacy.Request>,
request: KibanaRequest,
model: InfraMetricModel,
timerange: { min: number; max: number },
filters: any[]
) {
const internalRequest = req[internalInfraFrameworkRequest];
const server = internalRequest.server;
const server = request.server;
const getVisData = get(server, 'plugins.metrics.getVisData');
if (typeof getVisData !== 'function') {
throw new Error('TSVB is not available');
}

// getBasePath returns randomized base path AND spaces path
const basePath = internalRequest.getBasePath();
const basePath = request.getBasePath();
const url = `${basePath}/api/metrics/vis/data`;

// For the following request we need a copy of the instnace of the internal request
// but modified for our TSVB request. This will ensure all the instance methods
// are available along with our overriden values
const request = Object.assign(
Object.create(Object.getPrototypeOf(internalRequest)),
internalRequest,
{
url,
method: 'POST',
payload: {
timerange,
panels: [model],
filters,
},
}
);
const result = await getVisData(request);
const requestCopy = Object.assign(Object.create(Object.getPrototypeOf(request)), request, {
url,
method: 'POST',
payload: {
timerange,
panels: [model],
filters,
},
});
const result = await getVisData(requestCopy);
return result as InfraTSVBResponse;
}
}

export function wrapRequest<InternalRequest extends InfraWrappableRequest>(
req: InternalRequest
): InfraFrameworkRequest<InternalRequest> {
const { params, payload, query } = req;

return {
[internalInfraFrameworkRequest]: req,
params,
payload,
query,
};
}
// export function wrapRequest<InternalRequest extends InfraWrappableRequest>(
// req: InternalRequest
// ): InfraFrameworkRequest<InternalRequest> {
// const { params, payload, query } = req;

// return {
// [internalInfraFrameworkRequest]: req,
// params,
// payload,
// query,
// };
// }
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import zip from 'lodash/fp/zip';
import { pipe } from 'fp-ts/lib/pipeable';
import { map, fold } from 'fp-ts/lib/Either';
import { identity, constant } from 'fp-ts/lib/function';
import { KibanaRequest } from 'src/core/server';
import { compareTimeKeys, isTimeKey, TimeKey } from '../../../../common/time';
import { JsonObject } from '../../../../common/typed_json';
import {
Expand Down Expand Up @@ -212,7 +213,7 @@ export class InfraKibanaLogEntriesAdapter implements LogEntriesAdapter {
}

private async getLogEntryDocumentsBetween(
request: InfraFrameworkRequest,
request: KibanaRequest,
sourceConfiguration: InfraSourceConfiguration,
fields: string[],
start: number,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { map, fold } from 'fp-ts/lib/Either';
import { identity } from 'fp-ts/lib/function';
import { getJobId } from '../../../common/log_analysis';
import { throwErrors, createPlainError } from '../../../common/runtime_types';
import { InfraBackendFrameworkAdapter, InfraFrameworkRequest } from '../adapters/framework';
import { InfraBackendFrameworkAdapter } from '../adapters/framework';
import { NoLogRateResultsIndexError } from './errors';
import {
logRateModelPlotResponseRT,
Expand Down
14 changes: 5 additions & 9 deletions x-pack/legacy/plugins/infra/server/lib/sources/sources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export class InfraSources {

const createdSourceConfiguration = convertSavedObjectToSavedSourceConfiguration(
await this.libs.savedObjects
.getScopedSavedObjectsClient(request[internalInfraFrameworkRequest])
.getScopedSavedObjectsClient(request)
.create(
infraSourceConfigurationSavedObjectType,
pickSavedSourceConfiguration(newSourceConfiguration) as any,
Expand All @@ -127,7 +127,7 @@ export class InfraSources {

public async deleteSourceConfiguration(request: InfraFrameworkRequest, sourceId: string) {
await this.libs.savedObjects
.getScopedSavedObjectsClient(request[internalInfraFrameworkRequest])
.getScopedSavedObjectsClient(request)
.delete(infraSourceConfigurationSavedObjectType, sourceId);
}

Expand All @@ -147,7 +147,7 @@ export class InfraSources {

const updatedSourceConfiguration = convertSavedObjectToSavedSourceConfiguration(
await this.libs.savedObjects
.getScopedSavedObjectsClient(request[internalInfraFrameworkRequest])
.getScopedSavedObjectsClient(request)
.update(
infraSourceConfigurationSavedObjectType,
sourceId,
Expand Down Expand Up @@ -203,9 +203,7 @@ export class InfraSources {
}

private async getSavedSourceConfiguration(request: InfraFrameworkRequest, sourceId: string) {
const savedObjectsClient = this.libs.savedObjects.getScopedSavedObjectsClient(
request[internalInfraFrameworkRequest]
);
const savedObjectsClient = this.libs.savedObjects.getScopedSavedObjectsClient(request);

const savedObject = await savedObjectsClient.get(
infraSourceConfigurationSavedObjectType,
Expand All @@ -216,9 +214,7 @@ export class InfraSources {
}

private async getAllSavedSourceConfigurations(request: InfraFrameworkRequest) {
const savedObjectsClient = this.libs.savedObjects.getScopedSavedObjectsClient(
request[internalInfraFrameworkRequest]
);
const savedObjectsClient = this.libs.savedObjects.getScopedSavedObjectsClient(request);

const savedObjects = await savedObjectsClient.find({
type: infraSourceConfigurationSavedObjectType,
Expand Down

0 comments on commit a7429cd

Please sign in to comment.