Skip to content

Commit

Permalink
[ML] Fix types.
Browse files Browse the repository at this point in the history
  • Loading branch information
walterra committed Jun 1, 2021
1 parent 9ccb65a commit 220bca7
Show file tree
Hide file tree
Showing 16 changed files with 134 additions and 81 deletions.
50 changes: 50 additions & 0 deletions x-pack/plugins/apm/common/search_strategies/correlations/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

export interface HistogramItem {
key: number;
doc_count: number;
doc_count_full: number;
}

export interface ResponseHitSource {
[s: string]: unknown;
}

export interface ResponseHit {
_source: ResponseHitSource;
}

export interface SearchServiceParams {
index: string;
environment?: string;
kuery?: string;
serviceName?: string;
transactionName?: string;
transactionType?: string;
start?: string;
end?: string;
percentileThreshold?: number;
percentileThresholdValue?: number;
}

export interface SearchServiceValue {
histogram: HistogramItem[];
value: string;
field: string;
correlation: number;
}

export interface AsyncSearchProviderProgress {
started: number;
loadedHistogramStepsize: number;
loadedOverallHistogram: number;
loadedFieldCanditates: number;
loadedFieldValuePairs: number;
loadedHistograms: number;
getOverallProgress: () => number;
}
4 changes: 3 additions & 1 deletion x-pack/plugins/apm/public/application/csmApp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export function CsmAppRoot({
core,
deps,
config,
corePlugins: { embeddable, maps },
corePlugins,
observabilityRuleTypeRegistry,
}: {
appMountParameters: AppMountParameters;
Expand All @@ -83,6 +83,7 @@ export function CsmAppRoot({
corePlugins: ApmPluginStartDeps;
observabilityRuleTypeRegistry: ObservabilityRuleTypeRegistry;
}) {
const { embeddable, maps } = corePlugins;
const { history } = appMountParameters;
const i18nCore = core.i18n;
const plugins = { ...deps, maps };
Expand All @@ -91,6 +92,7 @@ export function CsmAppRoot({
config,
core,
plugins,
pluginsStart: corePlugins,
observabilityRuleTypeRegistry,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ const chartTheme: PartialTheme = {
interface CorrelationsChartProps {
field: string;
value: string;
histogram: Array<{ key: string; doc_count: number; doc_count_full: number }>;
histogram: Array<{ key: number; doc_count: number; doc_count_full: number }>;
markerValue: number;
markerPercentile: number;
}
Expand Down Expand Up @@ -107,7 +107,7 @@ export function CorrelationsChart({
];

const euiTheme = useTheme();
const xMax = max(histogram.map((d) => parseFloat(d.key))) ?? 0;
const xMax = max(histogram.map((d) => d.key)) ?? 0;
const durationFormatter = getDurationFormatter(xMax);

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ export function MlCorrelations({ onClose }: Props) {

<EuiSpacer size="m" />

{histograms.length > 0 && (
{histograms.length > 0 && selectedHistogram !== undefined && (
<>
<EuiTitle size="xxs">
<h4>
Expand All @@ -175,7 +175,7 @@ export function MlCorrelations({ onClose }: Props) {

<CorrelationsChart
markerPercentile={percentileThreshold}
markerValue={percentileThresholdValue}
markerValue={percentileThresholdValue ?? 0}
{...selectedHistogram}
key={`${selectedHistogram.field}:${selectedHistogram.value}`}
/>
Expand All @@ -188,10 +188,13 @@ export function MlCorrelations({ onClose }: Props) {
{ defaultMessage: '% of slow transactions' }
)}
significantTerms={histograms.map((d) => ({
distribution: [],
fieldName: d.field,
fieldValue: d.value,
score: d.correlation,
impact: d.correlation,
percentage: d.correlation,
fieldCount: 0,
valueCount: 0,
}))}
status={FETCH_STATUS.SUCCESS}
setSelectedSignificantTerm={setSelectedSignificantTerm}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ import { useRef, useState } from 'react';
import type { Subscription } from 'rxjs';

import {
IKibanaSearchRequest,
IKibanaSearchResponse,
isCompleteResponse,
isErrorResponse,
} from '../../../../../../../src/plugins/data/public';

import type { SearchServiceValue } from '../../../../common/search_strategies/correlations/types';

import { useApmPluginContext } from '../../../context/apm_plugin/use_apm_plugin_context';
import type { ApmPluginStartDeps } from '../../../plugin';

Expand All @@ -28,6 +31,12 @@ interface CorrelationsOptions {
end?: string;
}

interface RawResponse {
percentileThresholdValue?: number;
took: number;
values: SearchServiceValue[];
}

export const useCorrelations = (params: CorrelationsOptions) => {
const { pluginsStart } = useApmPluginContext();
const data = (pluginsStart.data as unknown) as ApmPluginStartDeps['data'];
Expand All @@ -36,13 +45,13 @@ export const useCorrelations = (params: CorrelationsOptions) => {
const [isComplete, setIsComplete] = useState(false);
const [isRunning, setIsRunning] = useState(false);
const [loaded, setLoaded] = useState<number>(0);
const [rawResponse, setRawResponse] = useState<Record<string, any>>({});
const [rawResponse, setRawResponse] = useState<RawResponse>();
const [timeTook, setTimeTook] = useState<number | undefined>();
const [total, setTotal] = useState<number>(100);
const abortCtrl = useRef(new AbortController());
const searchSubscription$ = useRef<Subscription>();

function setResponse(response: IKibanaSearchResponse) {
function setResponse(response: IKibanaSearchResponse<RawResponse>) {
setIsRunning(response.isRunning || false);
setRawResponse(response.rawResponse);
setLoaded(response.loaded!);
Expand All @@ -61,7 +70,7 @@ export const useCorrelations = (params: CorrelationsOptions) => {

// Submit the search request using the `data.search` service.
searchSubscription$.current = data.search
.search(req, {
.search<IKibanaSearchRequest, IKibanaSearchResponse<RawResponse>>(req, {
strategy: 'apmCorrelationsSearchStrategy',
abortSignal: abortCtrl.current.signal,
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,44 +9,19 @@ import { shuffle } from 'lodash';

import type { ElasticsearchClient } from 'src/core/server';

import type {
AsyncSearchProviderProgress,
SearchServiceParams,
SearchServiceValue,
} from '../../../../common/search_strategies/correlations/types';

import { fetchTransactionDurationFieldCandidates } from './query_field_candidates';
import { fetchTransactionDurationFieldValuePairs } from './query_field_value_pairs';
import { HistogramItem } from './query_histogram';
import { fetchTransactionDurationPecentiles } from './query_percentiles';
import { fetchTransactionDurationCorrelation } from './query_correlation';
import { fetchTransactionDurationHistogramRangesteps } from './query_histogram_rangesteps';
import { fetchTransactionDurationRanges } from './query_ranges';

export interface SearchServiceParams {
index: string;
environment?: string;
kuery?: string;
serviceName?: string;
transactionName?: string;
transactionType?: string;
start?: string;
end?: string;
percentileThreshold?: number;
percentileThresholdValue?: number;
}

export interface SearchServiceValue {
histogram: HistogramItem[];
value: string;
field: string;
correlation: number;
}

export interface AsyncSearchProviderProgress {
started: number;
loadedHistogramStepsize: number;
loadedOverallHistogram: number;
loadedFieldCanditates: number;
loadedFieldValuePairs: number;
loadedHistograms: number;
getOverallProgress: () => number;
}

export const asyncSearchServiceProvider = (
esClient: ElasticsearchClient,
params: SearchServiceParams
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ import { QueryContainer } from '@elastic/elasticsearch/api/types';

import { i18n } from '@kbn/i18n';

import type { SearchServiceParams } from './async_search_service';

import { TRANSACTION_DURATION } from '../../../../common/elasticsearch_fieldnames';
import type { SearchServiceParams } from '../../../../common/search_strategies/correlations/types';

export enum ProcessorEvent {
transaction = 'transaction',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import type { estypes } from '@elastic/elasticsearch';
import type { ElasticsearchClient } from 'src/core/server';

import { TRANSACTION_DURATION } from '../../../../common/elasticsearch_fieldnames';
import type { SearchServiceParams } from '../../../../common/search_strategies/correlations/types';

import type { SearchServiceParams } from './async_search_service';
import { getQueryWithParams } from './get_query_with_params';

export interface HistogramItem {
Expand All @@ -26,6 +26,18 @@ interface ResponseHit {
_source: ResponseHitSource;
}

interface BucketCorrelation {
buckets_path: string;
function: {
count_correlation: {
indicator: {
expectations: number[];
doc_count: number;
};
};
};
}

export const getTransactionDurationCorrelationRequest = (
params: SearchServiceParams,
percentiles: Record<string, number>,
Expand Down Expand Up @@ -64,6 +76,18 @@ export const getTransactionDurationCorrelationRequest = (
expectations.unshift(0);
expectations.push(percentileValues[percentileValues.length - 1]);

const bucketCorrelation: BucketCorrelation = {
buckets_path: 'latency_ranges>_count',
function: {
count_correlation: {
indicator: {
expectations,
doc_count: totalHits,
},
},
},
};

return {
index: params.index,
body: {
Expand All @@ -77,18 +101,8 @@ export const getTransactionDurationCorrelationRequest = (
},
},
transaction_duration_correlation: {
bucket_correlation: {
buckets_path: 'latency_ranges>_count',
function: {
count_correlation: {
indicator: {
expectations,
doc_count: totalHits,
},
},
},
},
},
bucket_correlation: bucketCorrelation,
} as estypes.AggregationContainer,
},
},
};
Expand Down Expand Up @@ -119,7 +133,9 @@ export const fetchTransactionDurationCorrelation = async (
}

return {
ranges: resp.body.aggregations.latency_ranges.buckets,
correlation: resp.body.aggregations.transaction_duration_correlation.value,
ranges: (resp.body.aggregations
.latency_ranges as estypes.MultiBucketAggregate).buckets,
correlation: (resp.body.aggregations
.transaction_duration_correlation as estypes.ValueAggregate).value,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import type { estypes } from '@elastic/elasticsearch';

import type { ElasticsearchClient } from 'src/core/server';

import type { SearchServiceParams } from './async_search_service';
import type { SearchServiceParams } from '../../../../common/search_strategies/correlations/types';

import { getQueryWithParams } from './get_query_with_params';

const fieldCandidatesFilter = ['parent.id', 'trace.id', 'transaction.id'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import type { estypes } from '@elastic/elasticsearch';
import type {
AsyncSearchProviderProgress,
SearchServiceParams,
} from './async_search_service';
} from '../../../../common/search_strategies/correlations/types';

import { getQueryWithParams } from './get_query_with_params';

interface FieldValuePair {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,14 @@ import type { estypes } from '@elastic/elasticsearch';
import type { ElasticsearchClient } from 'src/core/server';

import { TRANSACTION_DURATION } from '../../../../common/elasticsearch_fieldnames';
import type {
HistogramItem,
ResponseHit,
SearchServiceParams,
} from '../../../../common/search_strategies/correlations/types';

import type { SearchServiceParams } from './async_search_service';
import { getQueryWithParams } from './get_query_with_params';

export interface HistogramItem {
key: number;
doc_count: number;
}

interface ResponseHitSource {
[s: string]: unknown;
}
interface ResponseHit {
_source: ResponseHitSource;
}

export const getTransactionDurationHistogramRequest = (
params: SearchServiceParams,
interval: number,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import type { estypes } from '@elastic/elasticsearch';
import type { ElasticsearchClient } from 'src/core/server';

import { TRANSACTION_DURATION } from '../../../../common/elasticsearch_fieldnames';
import type { SearchServiceParams } from '../../../../common/search_strategies/correlations/types';

import type { SearchServiceParams } from './async_search_service';
import { getQueryWithParams } from './get_query_with_params';

const HISTOGRAM_INTERVALS = 1000;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import type { estypes } from '@elastic/elasticsearch';
import type { ElasticsearchClient } from 'src/core/server';

import { TRANSACTION_DURATION } from '../../../../common/elasticsearch_fieldnames';
import type { SearchServiceParams } from '../../../../common/search_strategies/correlations/types';

import type { SearchServiceParams } from './async_search_service';
import { getQueryWithParams } from './get_query_with_params';

export const getHistogramIntervalRequest = (
Expand Down
Loading

0 comments on commit 220bca7

Please sign in to comment.