From a407ee2502358814187d70183eb0b39e1561dfa7 Mon Sep 17 00:00:00 2001 From: Melissa Date: Fri, 10 Nov 2023 16:56:01 -0700 Subject: [PATCH] disable View Training Data if original job no longer exists --- .../plugins/ml/common/types/trained_models.ts | 1 + .../model_management/model_actions.tsx | 1 + .../model_management/models_list.tsx | 2 ++ .../services/ml_api_service/trained_models.ts | 1 + .../server/routes/schemas/inference_schema.ts | 1 + .../ml/server/routes/trained_models.ts | 21 +++++++++++++++++-- 6 files changed, 25 insertions(+), 2 deletions(-) diff --git a/x-pack/plugins/ml/common/types/trained_models.ts b/x-pack/plugins/ml/common/types/trained_models.ts index 70f588712c351..95815cd3b87af 100644 --- a/x-pack/plugins/ml/common/types/trained_models.ts +++ b/x-pack/plugins/ml/common/types/trained_models.ts @@ -98,6 +98,7 @@ export type TrainedModelConfigResponse = estypes.MlTrainedModelConfig & { * Associated pipelines. Extends response from the ES endpoint. */ pipelines?: Record | null; + origin_job_exists?: boolean; metadata?: { analytics_config: DataFrameAnalyticsConfig; diff --git a/x-pack/plugins/ml/public/application/model_management/model_actions.tsx b/x-pack/plugins/ml/public/application/model_management/model_actions.tsx index 7c39528cf5b4a..77e0895f21429 100644 --- a/x-pack/plugins/ml/public/application/model_management/model_actions.tsx +++ b/x-pack/plugins/ml/public/application/model_management/model_actions.tsx @@ -142,6 +142,7 @@ export function useModelActions({ icon: 'visTable', type: 'icon', available: (item) => !!item.metadata?.analytics_config?.id, + enabled: (item) => item.origin_job_exists === true, onClick: async (item) => { if (item.metadata?.analytics_config === undefined) return; diff --git a/x-pack/plugins/ml/public/application/model_management/models_list.tsx b/x-pack/plugins/ml/public/application/model_management/models_list.tsx index 56486d1bbbd4f..943bcd8ae5ebd 100644 --- a/x-pack/plugins/ml/public/application/model_management/models_list.tsx +++ b/x-pack/plugins/ml/public/application/model_management/models_list.tsx @@ -79,6 +79,7 @@ export type ModelItem = TrainedModelConfigResponse & { type?: string[]; stats?: Stats & { deployment_stats: TrainedModelDeploymentStatsResponse[] }; pipelines?: ModelPipelines['pipelines'] | null; + origin_job_exists?: boolean; deployment_ids: string[]; putModelConfig?: object; state: ModelState; @@ -214,6 +215,7 @@ export const ModelsList: FC = ({ const response = await trainedModelsApiService.getTrainedModels(undefined, { with_pipelines: true, with_indices: true, + with_origin_job_check: true, }); const newItems: ModelItem[] = []; diff --git a/x-pack/plugins/ml/public/application/services/ml_api_service/trained_models.ts b/x-pack/plugins/ml/public/application/services/ml_api_service/trained_models.ts index b886f6f7df8e5..ae93f229a1445 100644 --- a/x-pack/plugins/ml/public/application/services/ml_api_service/trained_models.ts +++ b/x-pack/plugins/ml/public/application/services/ml_api_service/trained_models.ts @@ -33,6 +33,7 @@ export interface InferenceQueryParams { // Custom kibana endpoint query params with_pipelines?: boolean; with_indices?: boolean; + with_origin_job_check?: boolean; include?: 'total_feature_importance' | 'feature_importance_baseline' | string; } diff --git a/x-pack/plugins/ml/server/routes/schemas/inference_schema.ts b/x-pack/plugins/ml/server/routes/schemas/inference_schema.ts index b24451bf755de..7d31f048c07ad 100644 --- a/x-pack/plugins/ml/server/routes/schemas/inference_schema.ts +++ b/x-pack/plugins/ml/server/routes/schemas/inference_schema.ts @@ -49,6 +49,7 @@ export const getInferenceQuerySchema = schema.object({ size: schema.maybe(schema.string()), with_pipelines: schema.maybe(schema.string()), with_indices: schema.maybe(schema.oneOf([schema.string(), schema.boolean()])), + with_origin_job_check: schema.maybe(schema.oneOf([schema.string(), schema.boolean()])), include: schema.maybe(schema.string()), }); diff --git a/x-pack/plugins/ml/server/routes/trained_models.ts b/x-pack/plugins/ml/server/routes/trained_models.ts index 8095411f911e7..308e8361cae37 100644 --- a/x-pack/plugins/ml/server/routes/trained_models.ts +++ b/x-pack/plugins/ml/server/routes/trained_models.ts @@ -90,6 +90,7 @@ export function trainedModelsRoutes( const { with_pipelines: withPipelines, with_indices: withIndicesRaw, + with_origin_job_check: withOriginJobCheck, ...getTrainedModelsRequestParams } = request.query; @@ -191,10 +192,26 @@ export function trainedModelsRoutes( mlLog.debug(e); } - const body = filterForEnabledFeatureModels(result, getEnabledFeatures()); + try { + if (withOriginJobCheck) { + for (const model of result) { + if (typeof model.metadata?.analytics_config?.id === 'string') { + const jobExistsResult = await mlClient.getDataFrameAnalytics({ + id: model.metadata?.analytics_config?.id, + size: 1, + }); + model.origin_job_exists = jobExistsResult.count === 1; + } + } + } + } catch (e) { + // Swallow error to prevent blocking trained models result + } + + const filteredModels = filterForEnabledFeatureModels(result, getEnabledFeatures()); return response.ok({ - body, + body: filteredModels, }); } catch (e) { return response.customError(wrapError(e));