Skip to content

Commit

Permalink
[ML] Tweak types for failed correlations.
Browse files Browse the repository at this point in the history
  • Loading branch information
walterra committed Sep 6, 2021
1 parent 83c2b74 commit 5f69244
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,48 @@
* 2.0.
*/

import { RawResponseBase, SearchStrategyClientParams } from '../types';
import {
FieldValuePair,
RawResponseBase,
SearchStrategyClientParams,
} from '../types';

import { FAILED_TRANSACTIONS_IMPACT_THRESHOLD } from './constants';

export interface FailedTransactionsCorrelation {
key: string;
export interface FailedTransactionsCorrelation extends FieldValuePair {
doc_count: number;
bg_count: number;
score: number;
pValue: number | null;
fieldName: string;
fieldValue: string;
normalizedScore: number;
failurePercentage: number;
successPercentage: number;
}

// Basic type guard for array of FailedTransactionsCorrelation
// Type guard for populated array of FailedTransactionsCorrelation
export const isFailedTransactionsCorrelations = (
arg: unknown
): arg is FailedTransactionsCorrelation[] => {
return Array.isArray(arg) && arg.length > 0;
return (
Array.isArray(arg) &&
arg.length > 0 &&
arg.every(
(d) =>
typeof d === 'object' &&
d !== null &&
Object.keys(d).length === 9 &&
typeof d.doc_count === 'number' &&
typeof d.bg_count === 'number' &&
typeof d.score === 'number' &&
(typeof d.pValue === 'number' || d.pValue === null) &&
typeof d.fieldName === 'string' &&
(typeof d.fieldValue === 'string' ||
typeof d.fieldValue === 'number') &&
typeof d.normalizedScore === 'number' &&
typeof d.failurePercentage === 'number' &&
typeof d.successPercentage === 'number'
)
);
};

export type FailedTransactionsCorrelationsImpactThreshold = typeof FAILED_TRANSACTIONS_IMPACT_THRESHOLD[keyof typeof FAILED_TRANSACTIONS_IMPACT_THRESHOLD];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,37 @@
*/

import {
FieldValuePair,
HistogramItem,
RawResponseBase,
SearchStrategyClientParams,
} from '../types';

export interface LatencyCorrelation {
export interface LatencyCorrelation extends FieldValuePair {
correlation: number;
fieldName: string;
fieldValue: string;
histogram: HistogramItem[];
ksTest: number;
}

// Basic type guard for array of LatencyCorrelation
// Type guard for populated array of LatencyCorrelation
export function isLatencyCorrelations(
arg: unknown
): arg is LatencyCorrelation[] {
return Array.isArray(arg) && arg.length > 0;
return (
Array.isArray(arg) &&
arg.length > 0 &&
arg.every(
(d) =>
typeof d === 'object' &&
d !== null &&
Object.keys(d).length === 5 &&
typeof d.correlation === 'number' &&
typeof d.fieldName === 'string' &&
typeof d.fieldValue === 'string' &&
Array.isArray(d.histogram) &&
typeof d.ksTest === 'number'
)
);
}

export interface AsyncSearchProviderProgress {
Expand Down
5 changes: 4 additions & 1 deletion x-pack/plugins/apm/common/search_strategies/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@

export interface FieldValuePair {
fieldName: string;
fieldValue: string;
// For dynamic fieldValues we only identify fields as `string`,
// but for example `http.response.status_code` which is part of
// of the list of predefined field candidates is of type long/number.
fieldValue: string | number;
}

export interface HistogramItem {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,12 @@ import type { FieldValuePair } from '../../../../common/search_strategies/types'

const PAGINATION_SIZE_OPTIONS = [5, 10, 20, 50];

export type SelectedCorrelationTerm<T extends FieldValuePair> = Pick<
T,
'fieldName' | 'fieldValue'
>;

interface Props<T> {
interface CorrelationsTableProps<T extends FieldValuePair> {
significantTerms?: T[];
status: FETCH_STATUS;
percentageColumnName?: string;
setSelectedSignificantTerm: (term: T | null) => void;
selectedTerm?: { fieldName: string; fieldValue: string };
selectedTerm?: FieldValuePair;
onFilter?: () => void;
columns: Array<EuiBasicTableColumn<T>>;
onTableChange: (c: Criteria<T>) => void;
Expand All @@ -43,7 +38,7 @@ export function CorrelationsTable<T extends FieldValuePair>({
selectedTerm,
onTableChange,
sorting,
}: Props<T>) {
}: CorrelationsTableProps<T>) {
const euiTheme = useTheme();
const trackApmEvent = useUiTracker({ app: 'apm' });
const trackSelectSignificantCorrelationTerm = useCallback(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ export function FailedTransactionsCorrelations({
sortable: true,
},
{
field: 'key',
field: 'fieldValue',
name: i18n.translate(
'xpack.apm.correlations.failedTransactions.correlationsTable.fieldValueLabel',
{ defaultMessage: 'Field value' }
Expand Down Expand Up @@ -444,7 +444,7 @@ export function FailedTransactionsCorrelations({
<Summary
items={[
<EuiBadge color="hollow">
{`${selectedTerm.fieldName}: ${selectedTerm.key}`}
{`${selectedTerm.fieldName}: ${selectedTerm.fieldValue}`}
</EuiBadge>,
<>{`p-value: ${selectedTerm.pValue.toPrecision(3)}`}</>,
]}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,19 @@ import { i18n } from '@kbn/i18n';
import { useChartTheme } from '../../../../../../observability/public';

import { getDurationFormatter } from '../../../../../common/utils/formatters';
import type { HistogramItem } from '../../../../../common/search_strategies/types';
import type {
FieldValuePair,
HistogramItem,
} from '../../../../../common/search_strategies/types';

import { FETCH_STATUS } from '../../../../hooks/use_fetcher';
import { useTheme } from '../../../../hooks/use_theme';

import { ChartContainer } from '../chart_container';

interface TransactionDistributionChartProps {
fieldName?: string;
fieldValue?: string;
fieldName?: FieldValuePair['fieldName'];
fieldValue?: FieldValuePair['fieldValue'];
hasData: boolean;
histogram?: HistogramItem[];
markerCurrentTransaction?: number;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,26 +74,26 @@ export const fetchFailedTransactionsCorrelationPValues = async (

const overallResult = resp.body.aggregations
.failure_p_value as estypes.AggregationsSignificantTermsAggregate<{
key: string;
key: string | number;
doc_count: number;
bg_count: number;
score: number;
}>;
const result = overallResult.buckets.map((bucket) => {
const score = bucket.score;

// Scale the score into a value from 0 - 1
// using a concave piecewise linear function in -log(p-value)
const normalizedScore =
0.5 * Math.min(Math.max((score - 3.912) / 2.995, 0), 1) +
0.25 * Math.min(Math.max((score - 6.908) / 6.908, 0), 1) +
0.25 * Math.min(Math.max((score - 13.816) / 101.314, 0), 1);
0.5 * Math.min(Math.max((bucket.score - 3.912) / 2.995, 0), 1) +
0.25 * Math.min(Math.max((bucket.score - 6.908) / 6.908, 0), 1) +
0.25 * Math.min(Math.max((bucket.score - 13.816) / 101.314, 0), 1);

return {
...bucket,
fieldName,
fieldValue: bucket.key,
pValue: Math.exp(-score),
doc_count: bucket.doc_count,
bg_count: bucket.doc_count,
score: bucket.score,
pValue: Math.exp(-bucket.score),
normalizedScore,
// Percentage of time the term appears in failed transactions
failurePercentage: bucket.doc_count / overallResult.doc_count,
Expand Down

0 comments on commit 5f69244

Please sign in to comment.