Skip to content

Commit

Permalink
[ML] Improve FieldValuePair type usage.
Browse files Browse the repository at this point in the history
  • Loading branch information
walterra committed Sep 6, 2021
1 parent 14bdcc1 commit e96aed9
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
FailedTransactionsCorrelation,
} from '../../../../common/search_strategies/failed_transactions_correlations/types';
import { APM_SEARCH_STRATEGIES } from '../../../../common/search_strategies/constants';
import { FieldValuePair } from '../../../../common/search_strategies/types';

import { useApmPluginContext } from '../../../context/apm_plugin/use_apm_plugin_context';
import { FETCH_STATUS } from '../../../hooks/use_fetcher';
Expand Down Expand Up @@ -224,7 +225,8 @@ export function FailedTransactionsCorrelations({
'xpack.apm.correlations.failedTransactions.correlationsTable.fieldValueLabel',
{ defaultMessage: 'Field value' }
),
render: (fieldValue: string) => String(fieldValue).slice(0, 50),
render: (fieldValue: FieldValuePair['fieldValue']) =>
String(fieldValue).slice(0, 50),
sortable: true,
},
...percentageColumns,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
APM_SEARCH_STRATEGIES,
DEFAULT_PERCENTILE_THRESHOLD,
} from '../../../../common/search_strategies/constants';
import { FieldValuePair } from '../../../../common/search_strategies/types';
import {
isLatencyCorrelations,
LatencyCorrelation,
Expand Down Expand Up @@ -173,7 +174,8 @@ export function LatencyCorrelations({ onFilter }: { onFilter: () => void }) {
'xpack.apm.correlations.latencyCorrelations.correlationsTable.fieldValueLabel',
{ defaultMessage: 'Field value' }
),
render: (fieldValue: string) => String(fieldValue).slice(0, 50),
render: (fieldValue: FieldValuePair['fieldValue']) =>
String(fieldValue).slice(0, 50),
sortable: true,
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,25 @@ import { getOrElse } from 'fp-ts/lib/Either';
import { pipe } from 'fp-ts/lib/pipeable';
import * as t from 'io-ts';
import { failure } from 'io-ts/lib/PathReporter';
import type { SearchStrategyParams } from '../../../../common/search_strategies/types';
import type {
FieldValuePair,
SearchStrategyParams,
} from '../../../../common/search_strategies/types';
import { rangeRt } from '../../../routes/default_api_types';
import { getCorrelationsFilters } from '../../correlations/get_filters';
import { Setup, SetupTimeRange } from '../../helpers/setup_request';

export const getTermsQuery = (
fieldName: string | undefined,
fieldValue: string | undefined
fieldName: FieldValuePair['fieldName'] | undefined,
fieldValue: FieldValuePair['fieldValue'] | undefined
) => {
return fieldName && fieldValue ? [{ term: { [fieldName]: fieldValue } }] : [];
};

interface QueryParams {
params: SearchStrategyParams;
fieldName?: string;
fieldValue?: string;
fieldName?: FieldValuePair['fieldName'];
fieldValue?: FieldValuePair['fieldValue'];
}
export const getQueryWithParams = ({
params,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type { ElasticsearchClient } from 'src/core/server';

import { TRANSACTION_DURATION } from '../../../../common/elasticsearch_fieldnames';
import type {
FieldValuePair,
ResponseHit,
SearchStrategyParams,
} from '../../../../common/search_strategies/types';
Expand All @@ -37,8 +38,8 @@ export const getTransactionDurationCorrelationRequest = (
ranges: estypes.AggregationsAggregationRange[],
fractions: number[],
totalDocCount: number,
fieldName?: string,
fieldValue?: string
fieldName?: FieldValuePair['fieldName'],
fieldValue?: FieldValuePair['fieldValue']
): estypes.SearchRequest => {
const query = getQueryWithParams({ params, fieldName, fieldValue });

Expand Down Expand Up @@ -92,8 +93,8 @@ export const fetchTransactionDurationCorrelation = async (
ranges: estypes.AggregationsAggregationRange[],
fractions: number[],
totalDocCount: number,
fieldName?: string,
fieldValue?: string
fieldName?: FieldValuePair['fieldName'],
fieldValue?: FieldValuePair['fieldValue']
): Promise<{
ranges: unknown[];
correlation: number | null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type { ElasticsearchClient } from 'src/core/server';

import { TRANSACTION_DURATION } from '../../../../common/elasticsearch_fieldnames';
import type {
FieldValuePair,
HistogramItem,
ResponseHit,
SearchStrategyParams,
Expand All @@ -22,8 +23,8 @@ import { getRequestBase } from './get_request_base';
export const getTransactionDurationHistogramRequest = (
params: SearchStrategyParams,
interval: number,
fieldName?: string,
fieldValue?: string
fieldName?: FieldValuePair['fieldName'],
fieldValue?: FieldValuePair['fieldValue']
): estypes.SearchRequest => ({
...getRequestBase(params),
body: {
Expand All @@ -41,8 +42,8 @@ export const fetchTransactionDurationHistogram = async (
esClient: ElasticsearchClient,
params: SearchStrategyParams,
interval: number,
fieldName?: string,
fieldValue?: string
fieldName?: FieldValuePair['fieldName'],
fieldValue?: FieldValuePair['fieldValue']
): Promise<HistogramItem[]> => {
const resp = await esClient.search<ResponseHit>(
getTransactionDurationHistogramRequest(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type { ElasticsearchClient } from 'src/core/server';

import { TRANSACTION_DURATION } from '../../../../common/elasticsearch_fieldnames';
import type {
FieldValuePair,
ResponseHit,
SearchStrategyParams,
} from '../../../../common/search_strategies/types';
Expand All @@ -22,8 +23,8 @@ import { SIGNIFICANT_VALUE_DIGITS } from '../constants';
export const getTransactionDurationPercentilesRequest = (
params: SearchStrategyParams,
percents?: number[],
fieldName?: string,
fieldValue?: string
fieldName?: FieldValuePair['fieldName'],
fieldValue?: FieldValuePair['fieldValue']
): estypes.SearchRequest => {
const query = getQueryWithParams({ params, fieldName, fieldValue });

Expand Down Expand Up @@ -52,8 +53,8 @@ export const fetchTransactionDurationPercentiles = async (
esClient: ElasticsearchClient,
params: SearchStrategyParams,
percents?: number[],
fieldName?: string,
fieldValue?: string
fieldName?: FieldValuePair['fieldName'],
fieldValue?: FieldValuePair['fieldValue']
): Promise<{ totalDocs: number; percentiles: Record<string, number> }> => {
const resp = await esClient.search<ResponseHit>(
getTransactionDurationPercentilesRequest(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type { ElasticsearchClient } from 'src/core/server';

import { TRANSACTION_DURATION } from '../../../../common/elasticsearch_fieldnames';
import type {
FieldValuePair,
ResponseHit,
SearchStrategyParams,
} from '../../../../common/search_strategies/types';
Expand All @@ -21,8 +22,8 @@ import { getRequestBase } from './get_request_base';
export const getTransactionDurationRangesRequest = (
params: SearchStrategyParams,
rangesSteps: number[],
fieldName?: string,
fieldValue?: string
fieldName?: FieldValuePair['fieldName'],
fieldValue?: FieldValuePair['fieldValue']
): estypes.SearchRequest => {
const query = getQueryWithParams({ params, fieldName, fieldValue });

Expand Down Expand Up @@ -59,8 +60,8 @@ export const fetchTransactionDurationRanges = async (
esClient: ElasticsearchClient,
params: SearchStrategyParams,
rangesSteps: number[],
fieldName?: string,
fieldValue?: string
fieldName?: FieldValuePair['fieldName'],
fieldValue?: FieldValuePair['fieldValue']
): Promise<Array<{ key: number; doc_count: number }>> => {
const resp = await esClient.search<ResponseHit>(
getTransactionDurationRangesRequest(
Expand Down

0 comments on commit e96aed9

Please sign in to comment.