Skip to content

Commit

Permalink
[ML] Fixing route handler types and schemas (elastic#98636) (elastic#…
Browse files Browse the repository at this point in the history
…99570)

* [ML] Fixing route handler types and schemas

* commenting out type error

* updates to fix functional tests

* updating jest test

* updating test expect

* fixing tests

* fixing test expect

* changes based on review

Co-authored-by: Kibana Machine <[email protected]>

Co-authored-by: James Gowdy <[email protected]>
  • Loading branch information
kibanamachine and jgowdyelastic authored May 7, 2021
1 parent e3824ae commit a4adbaa
Show file tree
Hide file tree
Showing 33 changed files with 266 additions and 208 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@ export type ChunkingConfig = estypes.ChunkingConfig;

export type Aggregation = Record<string, estypes.AggregationContainer>;

export type IndicesOptions = estypes.IndicesOptions;
// export type IndicesOptions = estypes.IndicesOptions;
export interface IndicesOptions {
allow_no_indices?: boolean;
expand_wildcards?: estypes.ExpandWildcards;
ignore_unavailable?: boolean;
}
10 changes: 5 additions & 5 deletions x-pack/plugins/ml/common/types/job_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ export interface BucketSpanEstimatorData {
};
fields: Array<string | null>;
index: string;
query: any;
splitField: string | undefined;
timeField: string | undefined;
runtimeMappings: RuntimeMappings | undefined;
indicesOptions: IndicesOptions | undefined;
query?: any;
splitField?: string;
timeField?: string;
runtimeMappings?: RuntimeMappings;
indicesOptions?: IndicesOptions;
}
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ export class GroupSelector extends Component {
}
}

const tempJobs = newJobs.map((j) => ({ job_id: j.id, groups: j.newGroups }));
const tempJobs = newJobs.map((j) => ({ jobId: j.id, groups: j.newGroups }));
ml.jobs
.updateGroups(tempJobs)
.then((resp) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export const jobsApiProvider = (httpService: HttpService) => ({
});
},

updateGroups(updatedJobs: string[]) {
updateGroups(updatedJobs: Array<{ jobId: string; groups: string[] }>) {
const body = JSON.stringify({ jobs: updatedJobs });
return httpService.http<any>({
path: `${ML_BASE_PATH}/jobs/update_groups`,
Expand Down
14 changes: 7 additions & 7 deletions x-pack/plugins/ml/server/lib/route_guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ type MLRequestHandlerContext = RequestHandlerContext & {
alerting?: AlertingApiRequestHandlerContext;
};

type Handler = (handlerParams: {
type Handler<P = unknown, Q = unknown, B = unknown> = (handlerParams: {
client: IScopedClusterClient;
request: KibanaRequest<any, any, any, any>;
request: KibanaRequest<P, Q, B>;
response: KibanaResponseFactory;
context: MLRequestHandlerContext;
jobSavedObjectService: JobSavedObjectService;
mlClient: MlClient;
}) => ReturnType<RequestHandler>;
}) => ReturnType<RequestHandler<P, Q, B>>;

type GetMlSavedObjectClient = (request: KibanaRequest) => SavedObjectsClientContract | null;
type GetInternalSavedObjectClient = () => SavedObjectsClientContract | null;
Expand Down Expand Up @@ -62,17 +62,17 @@ export class RouteGuard {
this._isMlReady = isMlReady;
}

public fullLicenseAPIGuard(handler: Handler) {
public fullLicenseAPIGuard<P, Q, B>(handler: Handler<P, Q, B>) {
return this._guard(() => this._mlLicense.isFullLicense(), handler);
}
public basicLicenseAPIGuard(handler: Handler) {
public basicLicenseAPIGuard<P, Q, B>(handler: Handler<P, Q, B>) {
return this._guard(() => this._mlLicense.isMinimumLicense(), handler);
}

private _guard(check: () => boolean, handler: Handler) {
private _guard<P, Q, B>(check: () => boolean, handler: Handler<P, Q, B>) {
return (
context: MLRequestHandlerContext,
request: KibanaRequest<any, any, any, any>,
request: KibanaRequest<P, Q, B>,
response: KibanaResponseFactory
) => {
if (check() === false) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ export interface FieldToBucket {

export interface IndexAnnotationArgs {
jobIds: string[];
earliestMs: number;
latestMs: number;
earliestMs: number | null;
latestMs: number | null;
maxAnnotations: number;
fields?: FieldToBucket[];
detectorIndex?: number;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export class CalendarManager {
* @param calendarIds
* @returns {Promise<*>}
*/
async getCalendarsByIds(calendarIds: string) {
async getCalendarsByIds(calendarIds: string[]) {
const calendars: Calendar[] = await this.getAllCalendars();
return calendars.filter((calendar) => calendarIds.includes(calendar.calendar_id));
}
Expand Down
80 changes: 41 additions & 39 deletions x-pack/plugins/ml/server/models/data_visualizer/data_visualizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -367,9 +367,9 @@ export class DataVisualizer {
aggregatableFields: string[],
nonAggregatableFields: string[],
samplerShardSize: number,
timeFieldName: string,
earliestMs: number,
latestMs: number,
timeFieldName: string | undefined,
earliestMs: number | undefined,
latestMs: number | undefined,
runtimeMappings?: RuntimeMappings
) {
const stats = {
Expand Down Expand Up @@ -472,10 +472,10 @@ export class DataVisualizer {
query: any,
fields: Field[],
samplerShardSize: number,
timeFieldName: string,
earliestMs: number,
latestMs: number,
intervalMs: number,
timeFieldName: string | undefined,
earliestMs: number | undefined,
latestMs: number | undefined,
intervalMs: number | undefined,
maxExamples: number,
runtimeMappings: RuntimeMappings
): Promise<BatchStats[]> {
Expand Down Expand Up @@ -529,16 +529,18 @@ export class DataVisualizer {
} else {
// Will only ever be one document count card,
// so no value in batching up the single request.
const stats = await this.getDocumentCountStats(
indexPatternTitle,
query,
timeFieldName,
earliestMs,
latestMs,
intervalMs,
runtimeMappings
);
batchStats.push(stats);
if (intervalMs !== undefined) {
const stats = await this.getDocumentCountStats(
indexPatternTitle,
query,
timeFieldName,
earliestMs,
latestMs,
intervalMs,
runtimeMappings
);
batchStats.push(stats);
}
}
break;
case ML_JOB_FIELD_TYPES.KEYWORD:
Expand Down Expand Up @@ -612,7 +614,7 @@ export class DataVisualizer {
query: any,
aggregatableFields: string[],
samplerShardSize: number,
timeFieldName: string,
timeFieldName: string | undefined,
earliestMs?: number,
latestMs?: number,
datafeedConfig?: Datafeed,
Expand Down Expand Up @@ -738,9 +740,9 @@ export class DataVisualizer {
indexPatternTitle: string,
query: any,
field: string,
timeFieldName: string,
earliestMs: number,
latestMs: number,
timeFieldName: string | undefined,
earliestMs: number | undefined,
latestMs: number | undefined,
runtimeMappings?: RuntimeMappings
) {
const index = indexPatternTitle;
Expand Down Expand Up @@ -769,9 +771,9 @@ export class DataVisualizer {
async getDocumentCountStats(
indexPatternTitle: string,
query: any,
timeFieldName: string,
earliestMs: number,
latestMs: number,
timeFieldName: string | undefined,
earliestMs: number | undefined,
latestMs: number | undefined,
intervalMs: number,
runtimeMappings: RuntimeMappings
): Promise<DocumentCountStats> {
Expand Down Expand Up @@ -832,9 +834,9 @@ export class DataVisualizer {
query: object,
fields: Field[],
samplerShardSize: number,
timeFieldName: string,
earliestMs: number,
latestMs: number,
timeFieldName: string | undefined,
earliestMs: number | undefined,
latestMs: number | undefined,
runtimeMappings?: RuntimeMappings
) {
const index = indexPatternTitle;
Expand Down Expand Up @@ -982,9 +984,9 @@ export class DataVisualizer {
query: object,
fields: Field[],
samplerShardSize: number,
timeFieldName: string,
earliestMs: number,
latestMs: number,
timeFieldName: string | undefined,
earliestMs: number | undefined,
latestMs: number | undefined,
runtimeMappings?: RuntimeMappings
) {
const index = indexPatternTitle;
Expand Down Expand Up @@ -1074,9 +1076,9 @@ export class DataVisualizer {
query: object,
fields: Field[],
samplerShardSize: number,
timeFieldName: string,
earliestMs: number,
latestMs: number,
timeFieldName: string | undefined,
earliestMs: number | undefined,
latestMs: number | undefined,
runtimeMappings?: RuntimeMappings
) {
const index = indexPatternTitle;
Expand Down Expand Up @@ -1142,9 +1144,9 @@ export class DataVisualizer {
query: object,
fields: Field[],
samplerShardSize: number,
timeFieldName: string,
earliestMs: number,
latestMs: number,
timeFieldName: string | undefined,
earliestMs: number | undefined,
latestMs: number | undefined,
runtimeMappings?: RuntimeMappings
) {
const index = indexPatternTitle;
Expand Down Expand Up @@ -1211,9 +1213,9 @@ export class DataVisualizer {
indexPatternTitle: string,
query: any,
field: string,
timeFieldName: string,
earliestMs: number,
latestMs: number,
timeFieldName: string | undefined,
earliestMs: number | undefined,
latestMs: number | undefined,
maxExamples: number,
runtimeMappings?: RuntimeMappings
): Promise<FieldExamples> {
Expand Down
8 changes: 7 additions & 1 deletion x-pack/plugins/ml/server/models/filter/filter_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ export interface FormFilter {
removeItems?: string[];
}

export interface UpdateFilter {
description?: string;
addItems?: string[];
removeItems?: string[];
}

export interface FilterRequest {
filter_id: string;
description?: string;
Expand Down Expand Up @@ -154,7 +160,7 @@ export class FilterManager {
}
}

async updateFilter(filterId: string, filter: FormFilter) {
async updateFilter(filterId: string, filter: UpdateFilter) {
try {
const body: FilterRequest = { filter_id: filterId };
if (filter.description !== undefined) {
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/ml/server/models/filter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
* 2.0.
*/

export { FilterManager, Filter, FormFilter } from './filter_manager';
export { FilterManager, Filter, FormFilter, UpdateFilter } from './filter_manager';
5 changes: 2 additions & 3 deletions x-pack/plugins/ml/server/models/job_service/groups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import { CalendarManager } from '../calendar';
import { GLOBAL_CALENDAR } from '../../../common/constants/calendars';
import { Job } from '../../../common/types/anomaly_detection_jobs';
import { MlJobsResponse } from '../../../common/types/job_service';
import type { MlClient } from '../../lib/ml_client';

Expand Down Expand Up @@ -78,10 +77,10 @@ export function groupsProvider(mlClient: MlClient) {
.map((g) => groups[g]);
}

async function updateGroups(jobs: Job[]) {
async function updateGroups(jobs: Array<{ jobId: string; groups: string[] }>) {
const results: Results = {};
for (const job of jobs) {
const { job_id: jobId, groups } = job;
const { jobId, groups } = job;
try {
await mlClient.updateJob({ job_id: jobId, body: { groups } });
results[jobId] = { success: true };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function modelSnapshotProvider(client: IScopedClusterClient, mlClient: Ml
replay: boolean,
end?: number,
deleteInterveningResults: boolean = true,
calendarEvents?: [{ start: number; end: number; description: string }]
calendarEvents?: Array<{ start: number; end: number; description: string }>
) {
let datafeedId = `datafeed-${jobId}`;
// ensure job exists
Expand Down
18 changes: 11 additions & 7 deletions x-pack/plugins/ml/server/routes/anomaly_detectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import {
forecastAnomalyDetector,
getBucketParamsSchema,
getModelSnapshotsSchema,
updateModelSnapshotSchema,
updateModelSnapshotsSchema,
updateModelSnapshotBodySchema,
} from './schemas/anomaly_detectors_schema';

/**
Expand Down Expand Up @@ -179,6 +180,7 @@ export function jobRoutes({ router, routeGuard }: RouteInitialization) {
const { jobId } = request.params;
const { body } = await mlClient.putJob({
job_id: jobId,
// @ts-expect-error job type custom_rules is incorrect
body: request.body,
});

Expand Down Expand Up @@ -274,6 +276,7 @@ export function jobRoutes({ router, routeGuard }: RouteInitialization) {
path: '/api/ml/anomaly_detectors/{jobId}/_close',
validate: {
params: jobIdSchema,
query: schema.object({ force: schema.maybe(schema.boolean()) }),
},
options: {
tags: ['access:ml:canCloseJob'],
Expand Down Expand Up @@ -312,6 +315,7 @@ export function jobRoutes({ router, routeGuard }: RouteInitialization) {
path: '/api/ml/anomaly_detectors/{jobId}',
validate: {
params: jobIdSchema,
query: schema.object({ force: schema.maybe(schema.boolean()) }),
},
options: {
tags: ['access:ml:canDeleteJob'],
Expand Down Expand Up @@ -637,15 +641,15 @@ export function jobRoutes({ router, routeGuard }: RouteInitialization) {
* @apiName UpdateModelSnapshotsById
* @apiDescription Updates the model snapshot for the specified snapshot ID
*
* @apiSchema (params) getModelSnapshotsSchema
* @apiSchema (body) updateModelSnapshotSchema
* @apiSchema (params) updateModelSnapshotsSchema
* @apiSchema (body) updateModelSnapshotBodySchema
*/
router.post(
{
path: '/api/ml/anomaly_detectors/{jobId}/model_snapshots/{snapshotId}/_update',
validate: {
params: getModelSnapshotsSchema,
body: updateModelSnapshotSchema,
params: updateModelSnapshotsSchema,
body: updateModelSnapshotBodySchema,
},
options: {
tags: ['access:ml:canCreateJob'],
Expand Down Expand Up @@ -674,13 +678,13 @@ export function jobRoutes({ router, routeGuard }: RouteInitialization) {
* @apiName GetModelSnapshotsById
* @apiDescription Deletes the model snapshot for the specified snapshot ID
*
* @apiSchema (params) getModelSnapshotsSchema
* @apiSchema (params) updateModelSnapshotsSchema
*/
router.delete(
{
path: '/api/ml/anomaly_detectors/{jobId}/model_snapshots/{snapshotId}',
validate: {
params: getModelSnapshotsSchema,
params: updateModelSnapshotsSchema,
},
options: {
tags: ['access:ml:canCreateJob'],
Expand Down
Loading

0 comments on commit a4adbaa

Please sign in to comment.