Skip to content

Commit

Permalink
[ML] remove unused code for filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
darnautov committed Feb 22, 2021
1 parent 728df29 commit 551886a
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,68 +12,6 @@ import {
Value,
DataFrameAnalyticsListRow,
} from '../pages/analytics_management/components/analytics_list/common';
import { ModelItem } from '../pages/analytics_management/components/models_management/models_list';

export function filterAnalyticsModels(
items: ModelItem[],
clauses: Array<TermClause | FieldClause>
) {
if (clauses.length === 0) {
return items;
}

// keep count of the number of matches we make as we're looping over the clauses
// we only want to return items which match all clauses, i.e. each search term is ANDed
const matches: Record<string, any> = items.reduce((p: Record<string, any>, c) => {
p[c.model_id] = {
model: c,
count: 0,
};
return p;
}, {});

clauses.forEach((c) => {
// the search term could be negated with a minus, e.g. -bananas
const bool = c.match === 'must';
let ms = [];

if (c.type === 'term') {
// filter term based clauses, e.g. bananas
// match on model_id and type
// if the term has been negated, AND the matches
if (bool === true) {
ms = items.filter(
(item) =>
stringMatch(item.model_id, c.value) === bool || stringMatch(item.type, c.value) === bool
);
} else {
ms = items.filter(
(item) =>
stringMatch(item.model_id, c.value) === bool && stringMatch(item.type, c.value) === bool
);
}
} else {
// filter other clauses, i.e. the filters for type
if (Array.isArray(c.value)) {
// type value is an array of string(s) e.g. c.value => ['classification']
ms = items.filter((item) => {
return item.type !== undefined && (c.value as Value[]).includes(item.type);
});
} else {
ms = items.filter((item) => item[c.field as keyof typeof item] === c.value);
}
}

ms.forEach((j) => matches[j.model_id].count++);
});

// loop through the matches and return only those items which have match all the clauses
const filtered = Object.values(matches)
.filter((m) => (m && m.count) >= clauses.length)
.map((m) => m.model);

return filtered;
}

export function filterAnalytics(
items: DataFrameAnalyticsListRow[],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ export const ModelsTableToConfigMapping = {
id: 'model_id',
description: 'description',
createdAt: 'create_time',
types: 'types',
type: 'type',
} as const;
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import React, { FC, useState, useCallback, useEffect, useMemo } from 'react';
import React, { FC, useState, useCallback, useMemo } from 'react';
import { i18n } from '@kbn/i18n';
import { FormattedMessage } from '@kbn/i18n/react';
import {
Expand All @@ -14,7 +14,6 @@ import {
EuiFlexItem,
EuiTitle,
EuiButton,
EuiSearchBar,
EuiSpacer,
EuiButtonIcon,
EuiBadge,
Expand Down Expand Up @@ -48,8 +47,6 @@ import {
refreshAnalyticsList$,
useRefreshAnalyticsList,
} from '../../../../common';
import { useTableSettings } from '../analytics_list/use_table_settings';
import { filterAnalyticsModels } from '../../../../common/search_bar_filters';
import { ML_PAGES } from '../../../../../../../common/constants/ml_url_generator';
import { DataFrameAnalysisConfigType } from '../../../../../../../common/types/data_frame_analytics';
import { timeFormatter } from '../../../../../../../common/util/date_utils';
Expand All @@ -60,7 +57,7 @@ import { BUILT_IN_MODEL_TAG } from '../../../../../../../common/constants/data_f
type Stats = Omit<TrainedModelStat, 'model_id'>;

export type ModelItem = TrainedModelConfigResponse & {
types?: string[];
type?: string[];
stats?: Stats;
pipelines?: ModelPipelines['pipelines'] | null;
};
Expand Down Expand Up @@ -105,7 +102,6 @@ export const ModelsList: FC = () => {
const trainedModelsApiService = useTrainedModelsApiService();
const { toasts } = useNotifications();

const [filteredModels, setFilteredModels] = useState<ModelItem[]>([]);
const [isLoading, setIsLoading] = useState(false);
const [items, setItems] = useState<ModelItem[]>([]);
const [selectedModels, setSelectedModels] = useState<ModelItem[]>([]);
Expand All @@ -117,32 +113,6 @@ export const ModelsList: FC = () => {
const mlUrlGenerator = useMlUrlGenerator();
const navigateToPath = useNavigateToPath();

const updateFilteredItems = (queryClauses: any) => {
if (queryClauses.length) {
const filtered = filterAnalyticsModels(items, queryClauses);
setFilteredModels(filtered);
} else {
setFilteredModels(items);
}
};

const filterList = () => {
if (searchQueryText !== '') {
const query = EuiSearchBar.Query.parse(searchQueryText);
let clauses: any = [];
if (query && query.ast !== undefined && query.ast.clauses !== undefined) {
clauses = query.ast.clauses;
}
updateFilteredItems(clauses);
} else {
updateFilteredItems([]);
}
};

useEffect(() => {
filterList();
}, [searchQueryText, items]);

const isBuiltInModel = useCallback(
(item: ModelItem) => item.tags.includes(BUILT_IN_MODEL_TAG),
[]
Expand All @@ -167,7 +137,7 @@ export const ModelsList: FC = () => {
// Extract model types
...(typeof model.inference_config === 'object'
? {
types: [
type: [
...Object.keys(model.inference_config),
...(isBuiltInModel(model) ? [BUILT_IN_MODEL_TYPE] : []),
],
Expand Down Expand Up @@ -445,7 +415,7 @@ export const ModelsList: FC = () => {
truncateText: true,
},
{
field: ModelsTableToConfigMapping.types,
field: ModelsTableToConfigMapping.type,
name: i18n.translate('xpack.ml.trainedModels.modelsList.typeHeader', {
defaultMessage: 'Type',
}),
Expand Down Expand Up @@ -493,12 +463,6 @@ export const ModelsList: FC = () => {
]
: [];

const { onTableChange, pagination, sorting } = useTableSettings<ModelItem>(
filteredModels,
pageState,
updatePageState
);

const toolsLeft = (
<EuiFlexItem grow={false}>
<EuiFlexGroup justifyContent="spaceBetween" alignItems="center">
Expand Down Expand Up @@ -580,6 +544,7 @@ export const ModelsList: FC = () => {
}
: {}),
};

return (
<>
<EuiSpacer size="m" />
Expand All @@ -602,9 +567,6 @@ export const ModelsList: FC = () => {
items={items}
itemId={ModelsTableToConfigMapping.id}
loading={isLoading}
onTableChange={onTableChange}
pagination={pagination}
sorting={sorting}
search={search}
selection={selection}
rowProps={(item) => ({
Expand Down

0 comments on commit 551886a

Please sign in to comment.