Skip to content

Commit

Permalink
fixing saved searches
Browse files Browse the repository at this point in the history
  • Loading branch information
jgowdyelastic committed Dec 5, 2019
1 parent c366058 commit cc6f298
Show file tree
Hide file tree
Showing 42 changed files with 237 additions and 144 deletions.
11 changes: 11 additions & 0 deletions x-pack/legacy/plugins/ml/common/types/kibana.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

// custom edits or fixes for default kibana types which are incomplete

import { SavedObjectAttributes, SimpleSavedObject } from 'kibana/public';

export type IndexPatternTitle = string;

export type callWithRequestType = (action: string, params?: any) => Promise<any>;
Expand All @@ -14,3 +16,12 @@ export interface Route {
id: string;
k7Breadcrumbs: () => any;
}

export type IndexPatternSavedObject = SimpleSavedObject<SavedObjectAttributes>;
export type SavedSearchSavedObject = SimpleSavedObject<SavedObjectAttributes>;

export function isSavedSearchSavedObject(
ss: SavedSearchSavedObject | null
): ss is SavedSearchSavedObject {
return ss !== null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import { FC } from 'react';

import { IndexPattern } from 'ui/index_patterns';
import { SavedSearch } from 'src/legacy/core_plugins/kibana/public/discover/types';
import { SavedSearchSavedObject } from '../../../../common/types/kibana';

declare const DataRecognizer: FC<{
indexPattern: IndexPattern;
savedSearch?: SavedSearch;
savedSearch?: SavedSearchSavedObject | null;
results: {
count: number;
onChange?: Function;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const RecognizedResult = ({
indexPattern,
savedSearch
}) => {
const id = (savedSearch === undefined || savedSearch.id === undefined) ?
const id = (savedSearch === null) ?
`index=${indexPattern.id}` :
`savedSearchId=${savedSearch.id}`;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,23 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { searchSourceMock } from '../../../../../../../../../src/legacy/ui/public/courier/search_source/mocks';
import { SearchSourceContract } from '../../../../../../../../../src/legacy/ui/public/courier';

export const savedSearchMock = {
export const savedSearchMock: any = {
id: 'the-saved-search-id',
title: 'the-saved-search-title',
searchSource: searchSourceMock as SearchSourceContract,
columns: [],
sort: [],
destroy: () => {},
type: 'search',
attributes: {
title: 'the-saved-search-title',
kibanaSavedObjectMeta: {
searchSourceJSON:
'{"highlightAll":true,"version":true,"query":{"query":"foo : \\"bar\\" ","language":"kuery"},"filter":[],"indexRefName":"kibanaSavedObjectMeta.searchSourceJSON.index"}',
},
},
references: [
{
name: 'kibanaSavedObjectMeta.searchSourceJSON.index',
type: 'index-pattern',
id: 'the-index-pattern-id',
},
],
migrationVersion: { search: '7.5.0' },
error: undefined,
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
import React from 'react';

import { KibanaConfig } from 'src/legacy/server/kbn_server';
import { SavedSearch } from 'src/legacy/core_plugins/kibana/public/discover/types';

import { IndexPattern, IndexPatterns } from 'ui/index_patterns';
import { SavedSearchSavedObject } from '../../../../common/types/kibana';

// set() method is missing in original d.ts
export interface KibanaConfigTypeFix extends KibanaConfig {
Expand All @@ -18,8 +17,8 @@ export interface KibanaConfigTypeFix extends KibanaConfig {

export interface KibanaContextValue {
combinedQuery: any;
currentIndexPattern: IndexPattern;
currentSavedSearch: SavedSearch;
currentIndexPattern: IndexPattern; // TODO this should be or null
currentSavedSearch: SavedSearchSavedObject | null;
indexPatterns: IndexPatterns;
kibanaConfig: KibanaConfigTypeFix;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { timefilter } from 'ui/timefilter';

import { KibanaConfigTypeFix } from '../../contexts/kibana';
import { NavigationMenu } from '../../components/navigation_menu';
import { getFullIndexPatterns } from '../../util/index_utils';
import { getIndexPatternsContract } from '../../util/index_utils';

// @ts-ignore
import { FileDataVisualizerView } from './components/file_datavisualizer_view/index';
Expand All @@ -21,7 +21,7 @@ export interface FileDataVisualizerPageProps {
export const FileDataVisualizerPage: FC<FileDataVisualizerPageProps> = ({ kibanaConfig }) => {
timefilter.disableTimeRangeSelector();
timefilter.disableAutoRefreshSelector();
const indexPatterns = getFullIndexPatterns();
const indexPatterns = getIndexPatternsContract();
return (
<Fragment>
<NavigationMenu tabId="datavisualizer" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import { FullTimeRangeSelector } from '../../components/full_time_range_selector
import { mlTimefilterRefresh$ } from '../../services/timefilter_refresh_service';
import { useKibanaContext, SavedSearchQuery } from '../../contexts/kibana';
import { kbnTypeToMLJobType } from '../../util/field_types_utils';
import { timeBasedIndexCheck } from '../../util/index_utils';
import { timeBasedIndexCheck, getQueryFromSavedSearch } from '../../util/index_utils';
import { TimeBuckets } from '../../util/time_buckets';
import { FieldRequestConfig, FieldVisConfig } from './common';
import { ActionsPanel } from './components/actions_panel';
Expand Down Expand Up @@ -173,9 +173,8 @@ export const Page: FC = () => {

useEffect(() => {
// Check for a saved search being passed in.
const searchSource = currentSavedSearch.searchSource;
const query = searchSource.getField('query');
if (query !== undefined) {
if (currentSavedSearch !== null) {
const { query } = getQueryFromSavedSearch(currentSavedSearch);
const queryLanguage = query.language as SEARCH_QUERY_LANGUAGE;
const qryString = query.query;
let qry;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { SavedSearch } from 'src/legacy/core_plugins/kibana/public/discover/types';
import { IndexPattern } from 'ui/index_patterns';
import { SavedSearchSavedObject } from '../../../../../../common/types/kibana';

import { JobCreator } from './job_creator';
import { Field, Aggregation, SplitField } from '../../../../../../common/types/fields';
Expand All @@ -32,7 +32,11 @@ export class AdvancedJobCreator extends JobCreator {
private _richDetectors: RichDetector[] = [];
private _queryString: string;

constructor(indexPattern: IndexPattern, savedSearch: SavedSearch, query: object) {
constructor(
indexPattern: IndexPattern,
savedSearch: SavedSearchSavedObject | null,
query: object
) {
super(indexPattern, savedSearch, query);

this._queryString = JSON.stringify(this._datafeed_config.query);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { SavedSearch } from 'src/legacy/core_plugins/kibana/public/discover/types';
import { IndexPattern } from 'ui/index_patterns';
import { SavedSearchSavedObject } from '../../../../../../common/types/kibana';
import { UrlConfig } from '../../../../../../common/types/custom_urls';
import { IndexPatternTitle } from '../../../../../../common/types/kibana';
import { ML_JOB_AGGREGATION } from '../../../../../../common/constants/aggregation_types';
Expand All @@ -24,7 +24,7 @@ import { mlCalendarService } from '../../../../services/calendar_service';
export class JobCreator {
protected _type: JOB_TYPE = JOB_TYPE.SINGLE_METRIC;
protected _indexPattern: IndexPattern;
protected _savedSearch: SavedSearch;
protected _savedSearch: SavedSearchSavedObject | null;
protected _indexPatternTitle: IndexPatternTitle = '';
protected _job_config: Job;
protected _calendars: Calendar[];
Expand All @@ -44,7 +44,11 @@ export class JobCreator {
stop: boolean;
} = { stop: false };

constructor(indexPattern: IndexPattern, savedSearch: SavedSearch, query: object) {
constructor(
indexPattern: IndexPattern,
savedSearch: SavedSearchSavedObject | null,
query: object
) {
this._indexPattern = indexPattern;
this._savedSearch = savedSearch;
this._indexPatternTitle = indexPattern.title;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { SavedSearch } from 'src/legacy/core_plugins/kibana/public/discover/types';
import { IndexPattern } from 'ui/index_patterns';
import { SavedSearchSavedObject } from '../../../../../../common/types/kibana';
import { SingleMetricJobCreator } from './single_metric_job_creator';
import { MultiMetricJobCreator } from './multi_metric_job_creator';
import { PopulationJobCreator } from './population_job_creator';
Expand All @@ -15,7 +15,7 @@ import { JOB_TYPE } from './util/constants';

export const jobCreatorFactory = (jobType: JOB_TYPE) => (
indexPattern: IndexPattern,
savedSearch: SavedSearch,
savedSearch: SavedSearchSavedObject | null,
query: object
) => {
let jc;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { SavedSearch } from 'src/legacy/core_plugins/kibana/public/discover/types';
import { IndexPattern } from 'ui/index_patterns';
import { SavedSearchSavedObject } from '../../../../../../common/types/kibana';
import { JobCreator } from './job_creator';
import {
Field,
Expand All @@ -26,7 +26,11 @@ export class MultiMetricJobCreator extends JobCreator {
private _lastEstimatedModelMemoryLimit = DEFAULT_MODEL_MEMORY_LIMIT;
protected _type: JOB_TYPE = JOB_TYPE.MULTI_METRIC;

constructor(indexPattern: IndexPattern, savedSearch: SavedSearch, query: object) {
constructor(
indexPattern: IndexPattern,
savedSearch: SavedSearchSavedObject | null,
query: object
) {
super(indexPattern, savedSearch, query);
this.createdBy = CREATED_BY_LABEL.MULTI_METRIC;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { SavedSearch } from 'src/legacy/core_plugins/kibana/public/discover/types';
import { IndexPattern } from 'ui/index_patterns';
import { SavedSearchSavedObject } from '../../../../../../common/types/kibana';
import { JobCreator } from './job_creator';
import {
Field,
Expand All @@ -25,7 +25,11 @@ export class PopulationJobCreator extends JobCreator {
private _byFields: SplitField[] = [];
protected _type: JOB_TYPE = JOB_TYPE.POPULATION;

constructor(indexPattern: IndexPattern, savedSearch: SavedSearch, query: object) {
constructor(
indexPattern: IndexPattern,
savedSearch: SavedSearchSavedObject | null,
query: object
) {
super(indexPattern, savedSearch, query);
this.createdBy = CREATED_BY_LABEL.POPULATION;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { SavedSearch } from 'src/legacy/core_plugins/kibana/public/discover/types';
import { IndexPattern } from 'ui/index_patterns';
import { SavedSearchSavedObject } from '../../../../../../common/types/kibana';
import { parseInterval } from '../../../../../../common/util/parse_interval';
import { JobCreator } from './job_creator';
import { Field, Aggregation, AggFieldPair } from '../../../../../../common/types/fields';
Expand All @@ -21,7 +21,11 @@ import { getRichDetectors } from './util/general';
export class SingleMetricJobCreator extends JobCreator {
protected _type: JOB_TYPE = JOB_TYPE.SINGLE_METRIC;

constructor(indexPattern: IndexPattern, savedSearch: SavedSearch, query: object) {
constructor(
indexPattern: IndexPattern,
savedSearch: SavedSearchSavedObject | null,
query: object
) {
super(indexPattern, savedSearch, query);
this.createdBy = CREATED_BY_LABEL.SINGLE_METRIC;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
} from '@elastic/eui';
import { FormattedMessage } from '@kbn/i18n/react';
import { useKibanaContext } from '../../../../contexts/kibana';
import { isSavedSearchSavedObject } from '../../../../../../common/types/kibana';
import { DataRecognizer } from '../../../../components/data_recognizer';
import { addItemToRecentlyAccessed } from '../../../../util/recently_accessed';
import { timeBasedIndexCheck } from '../../../../util/index_utils';
Expand All @@ -32,33 +33,33 @@ export const Page: FC = () => {

const isTimeBasedIndex = timeBasedIndexCheck(currentIndexPattern);
const indexWarningTitle =
!isTimeBasedIndex && currentSavedSearch.id === undefined
? i18n.translate('xpack.ml.newJob.wizard.jobType.indexPatternNotTimeBasedMessage', {
defaultMessage: 'Index pattern {indexPatternTitle} is not time based',
values: { indexPatternTitle: currentIndexPattern.title },
})
: i18n.translate(
!isTimeBasedIndex && isSavedSearchSavedObject(currentSavedSearch)
? i18n.translate(
'xpack.ml.newJob.wizard.jobType.indexPatternFromSavedSearchNotTimeBasedMessage',
{
defaultMessage:
'{savedSearchTitle} uses index pattern {indexPatternTitle} which is not time based',
values: {
savedSearchTitle: currentSavedSearch.title,
savedSearchTitle: currentSavedSearch.attributes.title as string,
indexPatternTitle: currentIndexPattern.title,
},
}
);
const pageTitleLabel =
currentSavedSearch.id !== undefined
? i18n.translate('xpack.ml.newJob.wizard.jobType.savedSearchPageTitleLabel', {
defaultMessage: 'saved search {savedSearchTitle}',
values: { savedSearchTitle: currentSavedSearch.title },
})
: i18n.translate('xpack.ml.newJob.wizard.jobType.indexPatternPageTitleLabel', {
defaultMessage: 'index pattern {indexPatternTitle}',
)
: i18n.translate('xpack.ml.newJob.wizard.jobType.indexPatternNotTimeBasedMessage', {
defaultMessage: 'Index pattern {indexPatternTitle} is not time based',
values: { indexPatternTitle: currentIndexPattern.title },
});

const pageTitleLabel = isSavedSearchSavedObject(currentSavedSearch)
? i18n.translate('xpack.ml.newJob.wizard.jobType.savedSearchPageTitleLabel', {
defaultMessage: 'saved search {savedSearchTitle}',
values: { savedSearchTitle: currentSavedSearch.attributes.title as string },
})
: i18n.translate('xpack.ml.newJob.wizard.jobType.indexPatternPageTitleLabel', {
defaultMessage: 'index pattern {indexPatternTitle}',
values: { indexPatternTitle: currentIndexPattern.title },
});

const recognizerResults = {
count: 0,
onChange() {
Expand All @@ -67,14 +68,15 @@ export const Page: FC = () => {
};

const getUrl = (basePath: string) => {
return currentSavedSearch.id === undefined
return !isSavedSearchSavedObject(currentSavedSearch)
? `${basePath}?index=${currentIndexPattern.id}`
: `${basePath}?savedSearchId=${currentSavedSearch.id}`;
};

const addSelectionToRecentlyAccessed = () => {
const title =
currentSavedSearch.id === undefined ? currentIndexPattern.title : currentSavedSearch.title;
const title = !isSavedSearchSavedObject(currentSavedSearch)
? currentIndexPattern.title
: (currentSavedSearch.attributes.title as string);
const url = getUrl('');
addItemToRecentlyAccessed('jobs/new_job/datavisualizer', title, url);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export const Page: FC<PageProps> = ({ existingJobsAndGroups, jobType }) => {
jobCreator.modelPlot = true;
}

if (kibanaContext.currentSavedSearch.id !== undefined) {
if (kibanaContext.currentSavedSearch !== null) {
// Jobs created from saved searches cannot be cloned in the wizard as the
// ML job config holds no reference to the saved search ID.
jobCreator.createdBy = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ export const WizardSteps: FC<Props> = ({ currentStep, setCurrentStep }) => {
const [additionalExpanded, setAdditionalExpanded] = useState(false);

function getSummaryStepTitle() {
if (kibanaContext.currentSavedSearch.id !== undefined) {
if (kibanaContext.currentSavedSearch !== null) {
return i18n.translate('xpack.ml.newJob.wizard.stepComponentWrapper.summaryTitleSavedSearch', {
defaultMessage: 'New job from saved search {title}',
values: { title: kibanaContext.currentSavedSearch.title },
values: { title: kibanaContext.currentSavedSearch.attributes.title as string },
});
} else if (kibanaContext.currentIndexPattern.id !== undefined) {
return i18n.translate(
Expand Down
Loading

0 comments on commit cc6f298

Please sign in to comment.