Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move UI Filters conversion to backend #14

Merged
merged 1 commit into from
Jul 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/legacy/ui/public/index_patterns/_index_pattern.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export interface StaticIndexPatternField {
}

export interface StaticIndexPattern {
id: string;
fields: StaticIndexPatternField[];
title: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,25 @@ import chrome from 'ui/chrome';
import styled from 'styled-components';
import { FormattedMessage } from '@kbn/i18n/react';
import { i18n } from '@kbn/i18n';
import { AutocompleteSuggestion } from 'ui/autocomplete_providers';
import {
AutocompleteSuggestion,
getAutocompleteProvider
} from 'ui/autocomplete_providers';
import { StaticIndexPattern } from 'ui/index_patterns';
import { fromKueryExpression, toElasticsearchQuery } from '@kbn/es-query';
import { getFromSavedObject } from 'ui/index_patterns/static_utils';
import { fromQuery, toQuery } from '../Links/url_helpers';
import { KibanaLink } from '../Links/KibanaLink';
// @ts-ignore
import { Typeahead } from './Typeahead';
import {
convertKueryToEsQuery,
getSuggestions,
getAPMIndexPatternForKuery
} from '../../../services/kuery';
// @ts-ignore
import { getBoolFilter } from './get_bool_filter';
import { useLocation } from '../../../hooks/useLocation';
import { useUrlParams } from '../../../hooks/useUrlParams';
import { history } from '../../../utils/history';
import { useMatchedRoutes } from '../../../hooks/useMatchedRoutes';
import { RouteName } from '../../app/Main/route_config/route_names';
import { getAPMIndexPattern } from '../../../services/rest/savedObjects';

const Container = styled.div`
margin-bottom: 10px;
Expand All @@ -41,6 +42,50 @@ interface State {
isLoadingSuggestions: boolean;
}

function convertKueryToEsQuery(
kuery: string,
indexPattern: StaticIndexPattern
) {
const ast = fromKueryExpression(kuery);
return toElasticsearchQuery(ast, indexPattern);
}

async function getAPMIndexPatternForKuery(): Promise<
StaticIndexPattern | undefined
> {
const apmIndexPattern = await getAPMIndexPattern();
if (!apmIndexPattern) {
return;
}
return getFromSavedObject(apmIndexPattern);
}

function getSuggestions(
query: string,
selectionStart: number,
apmIndexPattern: StaticIndexPattern,
boolFilter: unknown
) {
const autocompleteProvider = getAutocompleteProvider('kuery');
if (!autocompleteProvider) {
return [];
}
const config = {
get: () => true
};

const getAutocompleteSuggestions = autocompleteProvider({
config,
indexPatterns: [apmIndexPattern],
boolFilter
});
return getAutocompleteSuggestions({
query,
selectionStart,
selectionEnd: selectionStart
});
}

export function KueryBar() {
const [state, setState] = useState<State>({
indexPattern: null,
Expand Down
55 changes: 0 additions & 55 deletions x-pack/legacy/plugins/apm/public/services/kuery.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { ErrorDistributionAPIResponse } from '../../../../server/lib/errors/dist
import { ErrorGroupAPIResponse } from '../../../../server/lib/errors/get_error_group';
import { ErrorGroupListAPIResponse } from '../../../../server/lib/errors/get_error_groups';
import { callApi } from '../callApi';
import { getUiFiltersES } from '../../ui_filters/get_ui_filters_es';
import { UIFilters } from '../../../../typings/ui-filters';

export async function loadErrorGroupList({
Expand All @@ -33,7 +32,7 @@ export async function loadErrorGroupList({
end,
sortField,
sortDirection,
uiFiltersES: await getUiFiltersES(uiFilters)
uiFilters: JSON.stringify(uiFilters)
}
});
}
Expand All @@ -56,7 +55,7 @@ export async function loadErrorGroupDetails({
query: {
start,
end,
uiFiltersES: await getUiFiltersES(uiFilters)
uiFilters: JSON.stringify(uiFilters)
}
});
}
Expand All @@ -80,7 +79,7 @@ export async function loadErrorDistribution({
start,
end,
groupId: errorGroupId,
uiFiltersES: await getUiFiltersES(uiFilters)
uiFilters: JSON.stringify(uiFilters)
}
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import { MetricsChartsByAgentAPIResponse } from '../../../../server/lib/metrics/get_metrics_chart_data_by_agent';
import { callApi } from '../callApi';
import { getUiFiltersES } from '../../ui_filters/get_ui_filters_es';
import { UIFilters } from '../../../../typings/ui-filters';

export async function loadMetricsChartData({
Expand All @@ -28,7 +27,7 @@ export async function loadMetricsChartData({
start,
end,
agentName,
uiFiltersES: await getUiFiltersES(uiFilters)
uiFilters: JSON.stringify(uiFilters)
}
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import { ServiceAgentNameAPIResponse } from '../../../../server/lib/services/get_service_agent_name';
import { ServiceListAPIResponse } from '../../../../server/lib/services/get_services';
import { callApi } from '../callApi';
import { getUiFiltersES } from '../../ui_filters/get_ui_filters_es';
import { UIFilters } from '../../../../typings/ui-filters';
import { ServiceTransactionTypesAPIResponse } from '../../../../server/lib/services/get_service_transaction_types';

Expand All @@ -25,7 +24,7 @@ export async function loadServiceList({
query: {
start,
end,
uiFiltersES: await getUiFiltersES(uiFilters)
uiFilters: JSON.stringify(uiFilters)
}
});
}
Expand Down
3 changes: 1 addition & 2 deletions x-pack/legacy/plugins/apm/public/services/rest/apm/traces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import { TraceListAPIResponse } from '../../../../server/lib/traces/get_top_traces';
import { TraceAPIResponse } from '../../../../server/lib/traces/get_trace';
import { callApi } from '../callApi';
import { getUiFiltersES } from '../../ui_filters/get_ui_filters_es';
import { UIFilters } from '../../../../typings/ui-filters';

export async function loadTrace({
Expand Down Expand Up @@ -42,7 +41,7 @@ export async function loadTraceList({
query: {
start,
end,
uiFiltersES: await getUiFiltersES(uiFilters)
uiFilters: JSON.stringify(uiFilters)
}
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { TimeSeriesAPIResponse } from '../../../../server/lib/transactions/chart
import { TransactionDistributionAPIResponse } from '../../../../server/lib/transactions/distribution';
import { TransactionListAPIResponse } from '../../../../server/lib/transactions/get_top_transactions';
import { callApi } from '../callApi';
import { getUiFiltersES } from '../../ui_filters/get_ui_filters_es';
import { UIFilters } from '../../../../typings/ui-filters';

export async function loadTransactionList({
Expand All @@ -31,7 +30,7 @@ export async function loadTransactionList({
start,
end,
transactionType,
uiFiltersES: await getUiFiltersES(uiFilters)
uiFilters: JSON.stringify(uiFilters)
}
});
}
Expand Down Expand Up @@ -64,7 +63,7 @@ export async function loadTransactionDistribution({
transactionName,
transactionId,
traceId,
uiFiltersES: await getUiFiltersES(uiFilters)
uiFilters: JSON.stringify(uiFilters)
}
});
}
Expand All @@ -91,7 +90,7 @@ export async function loadTransactionCharts({
end,
transactionType,
transactionName,
uiFiltersES: await getUiFiltersES(uiFilters)
uiFilters: JSON.stringify(uiFilters)
}
});
}
Expand All @@ -118,7 +117,7 @@ export async function loadTransactionBreakdown({
end,
transactionName,
transactionType,
uiFiltersES: await getUiFiltersES(uiFilters)
uiFilters: JSON.stringify(uiFilters)
}
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { callApi } from './callApi';
export interface ISavedObject {
attributes: {
title: string;
fields: string;
};
id: string;
type: string;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

import { ESFilter } from 'elasticsearch';
import { getEnvironmentUiFilterES } from '../get_environment_ui_filter_es';
import { ENVIRONMENT_NOT_DEFINED } from '../../../../common/environment_filter_values';
import { SERVICE_ENVIRONMENT } from '../../../../common/elasticsearch_fieldnames';
import { ENVIRONMENT_NOT_DEFINED } from '../../../../../common/environment_filter_values';
import { SERVICE_ENVIRONMENT } from '../../../../../common/elasticsearch_fieldnames';

describe('getEnvironmentUiFilterES', () => {
it('should return undefined, when environment is undefined', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
*/

import { ESFilter } from 'elasticsearch';
import { ENVIRONMENT_NOT_DEFINED } from '../../../common/environment_filter_values';
import { SERVICE_ENVIRONMENT } from '../../../common/elasticsearch_fieldnames';
import { ENVIRONMENT_NOT_DEFINED } from '../../../../common/environment_filter_values';
import { SERVICE_ENVIRONMENT } from '../../../../common/elasticsearch_fieldnames';

export function getEnvironmentUiFilterES(
environment?: string
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { ESFilter } from 'elasticsearch';
import { Server } from 'hapi';
import { idx } from '@kbn/elastic-idx/target';
import { toElasticsearchQuery, fromKueryExpression } from '@kbn/es-query';
import { ISavedObject } from '../../../../public/services/rest/savedObjects';
import { StaticIndexPattern } from '../../../../../../../../src/legacy/core_plugins/data/public';
import { getAPMIndexPattern } from '../../../lib/index_pattern';

export async function getKueryUiFilterES(
server: Server,
kuery?: string
): Promise<ESFilter | undefined> {
if (!kuery) {
return;
}

const apmIndexPattern = await getAPMIndexPattern(server);
const formattedIndexPattern = getFromSavedObject(apmIndexPattern);

if (!formattedIndexPattern) {
return;
}

return convertKueryToEsQuery(kuery, formattedIndexPattern) as ESFilter;
}

// lifted from src/legacy/ui/public/index_patterns/static_utils/index.js
export function getFromSavedObject(apmIndexPattern: ISavedObject) {
if (idx(apmIndexPattern, _ => _.attributes.fields) === undefined) {
return;
}

return {
id: apmIndexPattern.id,
fields: JSON.parse(apmIndexPattern.attributes.fields),
title: apmIndexPattern.attributes.title
};
}

function convertKueryToEsQuery(
kuery: string,
indexPattern: StaticIndexPattern
) {
const ast = fromKueryExpression(kuery);
return toElasticsearchQuery(ast, indexPattern);
}
Loading