Skip to content

Commit

Permalink
Create new getDefaultQuery in data plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
kertal committed Mar 5, 2020
1 parent b682c8d commit 47c83fd
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ import {
indexPatterns as indexPatternsUtils,
connectToQueryState,
syncQueryStateWithUrl,
getDefaultQuery,
} from '../../../../../../../plugins/data/public';
import { getIndexPatternId } from '../helpers/get_index_pattern_id';

Expand Down Expand Up @@ -182,6 +183,7 @@ function discoverController(
localStorage,
uiCapabilities
) {
const { isDefault: isDefaultType } = indexPatternsUtils;
const subscriptions = new Subscription();
const $fetchObservable = new Subject();
let inspectorRequest;
Expand All @@ -190,9 +192,7 @@ function discoverController(
$scope.indexPattern = resolveIndexPatternLoading();

const getTimeField = () => {
return indexPatternsUtils.isDefault($scope.indexPattern)
? $scope.indexPattern.timeFieldName
: undefined;
return isDefaultType($scope.indexPattern) ? $scope.indexPattern.timeFieldName : undefined;
};

const {
Expand Down Expand Up @@ -471,7 +471,7 @@ function discoverController(
// searchSource which applies time range
const timeRangeSearchSource = savedSearch.searchSource.create();

if (indexPatternsUtils.isDefault($scope.indexPattern)) {
if (isDefaultType($scope.indexPattern)) {
timeRangeSearchSource.setField('filter', () => {
return timefilter.createFilter($scope.indexPattern);
});
Expand Down Expand Up @@ -574,16 +574,14 @@ function discoverController(
};
};

function getDefaultQuery() {
return {
query: '',
language: localStorage.get('kibana.userQueryLanguage') || config.get('search:queryLanguage'),
};
}

function getStateDefaults() {
const query =
$scope.searchSource.getField('query') ||
getDefaultQuery(
localStorage.get('kibana.userQueryLanguage') || config.get('search:queryLanguage')
);
return {
query: $scope.searchSource.getField('query') || getDefaultQuery(),
query,
sort: getSortArray(savedSearch.sort, $scope.indexPattern),
columns:
savedSearch.columns.length > 0 ? savedSearch.columns : config.get('defaultColumns').slice(),
Expand Down Expand Up @@ -653,7 +651,7 @@ function discoverController(
},
})
);

//Handling change oft the histogram interval
$scope.$watch('state.interval', function(newInterval, oldInterval) {
if (newInterval !== oldInterval) {
setAppState({ interval: newInterval });
Expand Down Expand Up @@ -825,7 +823,9 @@ function discoverController(
//reset filters and query string, remove savedQuery from state
const state = {
...appStateContainer.getState(),
query: getDefaultQuery(),
query: getDefaultQuery(
localStorage.get('kibana.userQueryLanguage') || config.get('search:queryLanguage')
),
filters: [],
};
delete state.savedQuery;
Expand Down Expand Up @@ -970,7 +970,8 @@ function discoverController(

async function setupVisualization() {
// If no timefield has been specified we don't create a histogram of messages
if (!$scope.opts.timefield) return;
if (!getTimeField()) return;
const { interval: histogramInterval } = $scope.state;

const visStateAggs = [
{
Expand All @@ -981,8 +982,8 @@ function discoverController(
type: 'date_histogram',
schema: 'segment',
params: {
field: $scope.opts.timefield,
interval: $scope.state.interval,
field: getTimeField(),
interval: histogramInterval,
timeRange: timefilter.getTime(),
},
},
Expand Down Expand Up @@ -1085,7 +1086,7 @@ function discoverController(
// Block the UI from loading if the user has loaded a rollup index pattern but it isn't
// supported.
$scope.isUnsupportedIndexPattern =
!indexPatternsUtils.isDefault($route.current.locals.savedObjects.ip.loaded) &&
!isDefaultType($route.current.locals.savedObjects.ip.loaded) &&
!hasSearchStategyForIndexPattern($route.current.locals.savedObjects.ip.loaded);

if ($scope.isUnsupportedIndexPattern) {
Expand Down
1 change: 1 addition & 0 deletions src/plugins/data/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ export {
QueryState,
getTime,
getQueryLog,
getDefaultQuery,
FilterManager,
SavedQuery,
SavedQueryService,
Expand Down
27 changes: 27 additions & 0 deletions src/plugins/data/public/query/lib/get_default_query.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

export type QueryLanguage = 'kuery' | 'lucene';

export function getDefaultQuery(language: QueryLanguage = 'kuery') {
return {
query: '',
language,
};
}
1 change: 1 addition & 0 deletions src/plugins/data/public/query/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ export * from './from_user';
export * from './to_user';
export * from './to_user';
export * from './get_query_log';
export * from './get_default_query';

0 comments on commit 47c83fd

Please sign in to comment.