Skip to content

Commit

Permalink
fix handling of params.
Browse files Browse the repository at this point in the history
  • Loading branch information
walterra committed Jul 7, 2021
1 parent 48074a4 commit 8250dd0
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,16 @@ export function MlLatencyCorrelations({ onClose }: Props) {
} = useApmPluginContext();

const { serviceName } = useParams<{ serviceName: string }>();
const { urlParams } = useUrlParams();

const fetchOptions = useMemo(
() => ({
...{
serviceName,
...urlParams,
},
}),
[serviceName, urlParams]
);
const {
urlParams: {
environment,
kuery,
transactionName,
transactionType,
start,
end,
},
} = useUrlParams();

const {
error,
Expand All @@ -84,7 +83,15 @@ export function MlLatencyCorrelations({ onClose }: Props) {
} = useCorrelations({
index: 'apm-*',
...{
...fetchOptions,
...{
environment,
kuery,
serviceName,
transactionName,
transactionType,
start,
end,
},
percentileThreshold: DEFAULT_PERCENTILE_THRESHOLD,
},
});
Expand Down Expand Up @@ -332,8 +339,7 @@ export function MlLatencyCorrelations({ onClose }: Props) {
{
defaultMessage: 'Latency distribution for {name}',
values: {
name:
fetchOptions.transactionName ?? fetchOptions.serviceName,
name: transactionName ?? serviceName,
},
}
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@
* 2.0.
*/

import { pipe } from 'fp-ts/lib/pipeable';
import { getOrElse } from 'fp-ts/lib/Either';
import { failure } from 'io-ts/lib/PathReporter';
import * as t from 'io-ts';

import type { estypes } from '@elastic/elasticsearch';
import {
PROCESSOR_EVENT,
SERVICE_NAME,
TRANSACTION_DURATION,
TRANSACTION_NAME,
} from '../../../../common/elasticsearch_fieldnames';
import { TRANSACTION_DURATION } from '../../../../common/elasticsearch_fieldnames';
import type { SearchServiceParams } from '../../../../common/search_strategies/correlations/types';
import { environmentQuery as getEnvironmentQuery } from '../../../utils/queries';
import { ProcessorEvent } from '../../../../common/processor_event';
import { rangeRt } from '../../../routes/default_api_types';

import { Setup, SetupTimeRange } from '../../helpers/setup_request';

import { getCorrelationsFilters } from '../../correlations/get_filters';

const getPercentileThresholdValueQuery = (
percentileThresholdValue: number | undefined
Expand All @@ -39,26 +42,6 @@ export const getTermsQuery = (
return fieldName && fieldValue ? [{ term: { [fieldName]: fieldValue } }] : [];
};

const getRangeQuery = (
start?: string,
end?: string
): estypes.QueryDslQueryContainer[] => {
if (start === undefined && end === undefined) {
return [];
}

return [
{
range: {
'@timestamp': {
...(start !== undefined ? { gte: start } : {}),
...(end !== undefined ? { lte: end } : {}),
},
},
},
];
};

interface QueryParams {
params: SearchServiceParams;
fieldName?: string;
Expand All @@ -71,21 +54,37 @@ export const getQueryWithParams = ({
}: QueryParams) => {
const {
environment,
kuery,
serviceName,
start,
end,
percentileThresholdValue,
transactionType,
transactionName,
} = params;

// converts string based start/end to epochmillis
const setup = pipe(
rangeRt.decode({ start, end }),
getOrElse<t.Errors, { start: number; end: number }>((errors) => {
throw new Error(failure(errors).join('\n'));
})
) as Setup & SetupTimeRange;

const filters = getCorrelationsFilters({
setup,
environment,
kuery,
serviceName,
transactionType,
transactionName,
});

return {
bool: {
filter: [
...getTermsQuery(PROCESSOR_EVENT, ProcessorEvent.transaction),
...getTermsQuery(SERVICE_NAME, serviceName),
...getTermsQuery(TRANSACTION_NAME, transactionName),
...filters,
...getTermsQuery(fieldName, fieldValue),
...getRangeQuery(start, end),
...getEnvironmentQuery(environment),
...getPercentileThresholdValueQuery(percentileThresholdValue),
] as estypes.QueryDslQueryContainer[],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ describe('APM Correlations search strategy', () => {
range: {
'@timestamp': {
format: 'epoch_millis',
gte: '2020',
lte: '2021',
gte: 1577836800000,
lte: 1609459200000,
},
},
},
Expand Down

0 comments on commit 8250dd0

Please sign in to comment.