Skip to content

Commit

Permalink
[ML] Data Frame Analytics: replace custom types with estypes (#132443)
Browse files Browse the repository at this point in the history
* replace common data_frame_analytics types from server with esclient types

* remove unused ts error ignore commments

* remove generic analyis type and move types to common dir

* [CI] Auto-commit changed files from 'node scripts/precommit_hook.js --ref HEAD~1..HEAD --fix'

* move types to commont folder

Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
alvarezmelissa87 and kibanamachine committed May 20, 2022
1 parent 3b7c7e8 commit f0cb40a
Show file tree
Hide file tree
Showing 30 changed files with 155 additions and 227 deletions.
155 changes: 79 additions & 76 deletions x-pack/plugins/ml/common/types/data_frame_analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@

import Boom from '@hapi/boom';
import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import { RuntimeMappings } from './fields';

import { EsErrorBody } from '../util/errors';
import { ANALYSIS_CONFIG_TYPE } from '../constants/data_frame_analytics';
import { DATA_FRAME_TASK_STATE } from '../constants/data_frame_analytics';

export interface DeleteDataFrameAnalyticsWithIndexStatus {
success: boolean;
Expand Down Expand Up @@ -57,69 +55,90 @@ export interface ClassificationAnalysis {
classification: Classification;
}

interface GenericAnalysis {
[key: string]: Record<string, any>;
export type AnalysisConfig = estypes.MlDataframeAnalysisContainer;
export interface DataFrameAnalyticsConfig
extends Omit<estypes.MlDataframeAnalyticsSummary, 'analyzed_fields'> {
analyzed_fields?: estypes.MlDataframeAnalysisAnalyzedFields;
}

export type AnalysisConfig =
| OutlierAnalysis
| RegressionAnalysis
| ClassificationAnalysis
| GenericAnalysis;

export interface DataFrameAnalyticsConfig {
id: DataFrameAnalyticsId;
export interface UpdateDataFrameAnalyticsConfig {
allow_lazy_start?: string;
description?: string;
dest: {
index: IndexName;
results_field: string;
};
source: {
index: IndexName | IndexName[];
query?: estypes.QueryDslQueryContainer;
runtime_mappings?: RuntimeMappings;
};
analysis: AnalysisConfig;
analyzed_fields?: {
includes?: string[];
excludes?: string[];
};
model_memory_limit: string;
model_memory_limit?: string;
max_num_threads?: number;
create_time: number;
version: string;
allow_lazy_start?: boolean;
}

export type DataFrameAnalysisConfigType =
typeof ANALYSIS_CONFIG_TYPE[keyof typeof ANALYSIS_CONFIG_TYPE];

export type DataFrameTaskStateType =
typeof DATA_FRAME_TASK_STATE[keyof typeof DATA_FRAME_TASK_STATE];
export type DataFrameTaskStateType = estypes.MlDataframeState | 'analyzing' | 'reindexing';

export interface DataFrameAnalyticsStats extends Omit<estypes.MlDataframeAnalytics, 'state'> {
failure_reason?: string;
state: DataFrameTaskStateType;
}

export type DfAnalyticsExplainResponse = estypes.MlExplainDataFrameAnalyticsResponse;

export interface PredictedClass {
predicted_class: string;
count: number;
}
export interface ConfusionMatrix {
actual_class: string;
actual_class_doc_count: number;
predicted_classes: PredictedClass[];
other_predicted_class_doc_count: number;
}

export interface RocCurveItem {
fpr: number;
threshold: number;
tpr: number;
}

interface ProgressSection {
phase: string;
progress_percent: number;
interface EvalClass {
class_name: string;
value: number;
}
export interface ClassificationEvaluateResponse {
classification: {
multiclass_confusion_matrix?: {
confusion_matrix: ConfusionMatrix[];
};
recall?: {
classes: EvalClass[];
avg_recall: number;
};
accuracy?: {
classes: EvalClass[];
overall_accuracy: number;
};
auc_roc?: {
curve?: RocCurveItem[];
value: number;
};
};
}

export interface DataFrameAnalyticsStats {
assignment_explanation?: string;
id: DataFrameAnalyticsId;
memory_usage?: {
timestamp?: string;
peak_usage_bytes: number;
status: string;
export interface EvaluateMetrics {
classification: {
accuracy?: object;
recall?: object;
multiclass_confusion_matrix?: object;
auc_roc?: { include_curve: boolean; class_name: string };
};
node?: {
attributes: Record<string, any>;
ephemeral_id: string;
id: string;
name: string;
transport_address: string;
regression: {
r_squared: object;
mse: object;
msle: object;
huber: object;
};
progress: ProgressSection[];
failure_reason?: string;
state: DataFrameTaskStateType;
}

export interface FieldSelectionItem
extends Omit<estypes.MlDataframeAnalyticsFieldSelection, 'mapping_types'> {
mapping_types?: string[];
}

export interface AnalyticsMapNodeElement {
Expand All @@ -146,30 +165,14 @@ export interface AnalyticsMapReturnType {
error: null | any;
}

export interface FeatureProcessor {
frequency_encoding: {
feature_name: string;
field: string;
frequency_map: Record<string, any>;
};
multi_encoding: {
processors: any[];
};
n_gram_encoding: {
feature_prefix?: string;
field: string;
length?: number;
n_grams: number[];
start?: number;
};
one_hot_encoding: {
field: string;
hot_map: string;
};
target_mean_encoding: {
default_value: number;
feature_name: string;
field: string;
target_map: Record<string, any>;
export type FeatureProcessor = estypes.MlDataframeAnalysisFeatureProcessor;

export interface TrackTotalHitsSearchResponse {
hits: {
total: {
value: number;
relation: string;
};
hits: any[];
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,18 @@ describe('Data Frame Analytics: Analytics utils', () => {
expect(getAnalysisType(outlierAnalysis)).toBe('outlier_detection');

const regressionAnalysis = { regression: {} };
// @ts-expect-error incomplete regression analysis
expect(getAnalysisType(regressionAnalysis)).toBe('regression');

// test against a job type that does not exist yet.
const otherAnalysis = { other: {} };
// @ts-expect-error unkown analysis type
expect(getAnalysisType(otherAnalysis)).toBe('other');

// if the analysis object has a shape that is not just a single property,
// the job type will be returned as 'unknown'.
const unknownAnalysis = { outlier_detection: {}, regression: {} };
// @ts-expect-error unkown analysis type
expect(getAnalysisType(unknownAnalysis)).toBe('unknown');
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ import { cloneDeep } from 'lodash';
import { ml } from '../../services/ml_api_service';
import { Dictionary } from '../../../../common/types/common';
import { extractErrorMessage } from '../../../../common/util/errors';
import {
ClassificationEvaluateResponse,
EvaluateMetrics,
TrackTotalHitsSearchResponse,
} from '../../../../common/types/data_frame_analytics';
import { SavedSearchQuery } from '../../contexts/ml';
import {
AnalysisConfig,
Expand Down Expand Up @@ -106,23 +111,6 @@ export enum INDEX_STATUS {
ERROR,
}

export interface FieldSelectionItem {
name: string;
mappings_types?: string[];
is_included: boolean;
is_required: boolean;
feature_type?: string;
reason?: string;
}

export interface DfAnalyticsExplainResponse {
field_selection?: FieldSelectionItem[];
memory_estimation: {
expected_memory_without_disk: string;
expected_memory_with_disk: string;
};
}

export interface Eval {
mse: number | string;
msle: number | string;
Expand All @@ -148,49 +136,6 @@ export interface RegressionEvaluateResponse {
};
}

export interface PredictedClass {
predicted_class: string;
count: number;
}

export interface ConfusionMatrix {
actual_class: string;
actual_class_doc_count: number;
predicted_classes: PredictedClass[];
other_predicted_class_doc_count: number;
}

export interface RocCurveItem {
fpr: number;
threshold: number;
tpr: number;
}

interface EvalClass {
class_name: string;
value: number;
}

export interface ClassificationEvaluateResponse {
classification: {
multiclass_confusion_matrix?: {
confusion_matrix: ConfusionMatrix[];
};
recall?: {
classes: EvalClass[];
avg_recall: number;
};
accuracy?: {
classes: EvalClass[];
overall_accuracy: number;
};
auc_roc?: {
curve?: RocCurveItem[];
value: number;
};
};
}

interface LoadEvaluateResult {
success: boolean;
eval: RegressionEvaluateResponse | ClassificationEvaluateResponse | null;
Expand Down Expand Up @@ -279,13 +224,6 @@ export const isClassificationEvaluateResponse = (
);
};

export interface UpdateDataFrameAnalyticsConfig {
allow_lazy_start?: string;
description?: string;
model_memory_limit?: string;
max_num_threads?: number;
}

export enum REFRESH_ANALYTICS_LIST_STATE {
ERROR = 'error',
IDLE = 'idle',
Expand Down Expand Up @@ -451,21 +389,6 @@ export enum REGRESSION_STATS {
HUBER = 'huber',
}

interface EvaluateMetrics {
classification: {
accuracy?: object;
recall?: object;
multiclass_confusion_matrix?: object;
auc_roc?: { include_curve: boolean; class_name: string };
};
regression: {
r_squared: object;
mse: object;
msle: object;
huber: object;
};
}

interface LoadEvalDataConfig {
isTraining?: boolean;
index: string;
Expand Down Expand Up @@ -548,16 +471,6 @@ export const loadEvalData = async ({
}
};

interface TrackTotalHitsSearchResponse {
hits: {
total: {
value: number;
relation: string;
};
hits: any[];
};
}

interface LoadDocsCountConfig {
ignoreDefaultQuery?: boolean;
isTraining?: boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export const sortExplorationResultsFields = (

if (isClassificationAnalysis(jobConfig.analysis) || isRegressionAnalysis(jobConfig.analysis)) {
const dependentVariable = getDependentVar(jobConfig.analysis);
const predictedField = getPredictedFieldName(resultsField, jobConfig.analysis, true);
const predictedField = getPredictedFieldName(resultsField!, jobConfig.analysis, true);

if (a === `${resultsField}.is_training`) {
return -1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,7 @@
* 2.0.
*/

export type {
UpdateDataFrameAnalyticsConfig,
IndexPattern,
RegressionEvaluateResponse,
Eval,
SearchQuery,
} from './analytics';
export type { IndexPattern, RegressionEvaluateResponse, Eval, SearchQuery } from './analytics';
export {
getAnalysisType,
getDependentVar,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { isEqual } from 'lodash';
import { LEFT_ALIGNMENT, SortableProperties } from '@elastic/eui/lib/services';
import { i18n } from '@kbn/i18n';
import { FormattedMessage } from '@kbn/i18n-react';
import { FieldSelectionItem } from '../../../../common/analytics';
import { FieldSelectionItem } from '../../../../../../../common/types/data_frame_analytics';
// @ts-ignore could not find declaration file
import { CustomSelectionTable } from '../../../../../components/custom_selection_table';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ import {
ANALYSIS_CONFIG_TYPE,
TRAINING_PERCENT_MIN,
TRAINING_PERCENT_MAX,
FieldSelectionItem,
} from '../../../../common/analytics';
import { getScatterplotMatrixLegendType } from '../../../../common/get_scatterplot_matrix_legend_type';
import { RuntimeMappings as RuntimeMappingsType } from '../../../../../../../common/types/fields';
import { FieldSelectionItem } from '../../../../../../../common/types/data_frame_analytics';
import {
isRuntimeMappings,
isRuntimeField,
Expand Down
Loading

0 comments on commit f0cb40a

Please sign in to comment.