From 5b359a90b0892d15a92ec2cb7bfbd199e96e9b97 Mon Sep 17 00:00:00 2001 From: Dario Gieselaar Date: Thu, 9 Dec 2021 11:29:18 +0100 Subject: [PATCH] [APM] TypeScript performance fix (#120754) * [APM] TypeScript performance fix * Clarify use_apm_params split with comments * Remove ts-expect-error for security plugin * Fix type error in searchtypes --- .../src/types/index.ts | 169 ++-- src/core/types/elasticsearch/search.ts | 879 +++++++++--------- .../app/service_inventory/index.tsx | 6 +- .../components/app/service_map/Controls.tsx | 4 +- .../service_map/Popover/backend_contents.tsx | 5 +- .../shared/time_comparison/index.tsx | 5 +- .../apm/public/hooks/use_apm_params.ts | 45 +- .../apm/public/hooks/use_apm_router.ts | 2 + .../get_error_group_main_statistics.ts | 1 - .../service_map/get_service_anomalies.ts | 1 - ...ervice_instances_transaction_statistics.ts | 1 - .../signals/query_signals_route.test.ts | 1 - 12 files changed, 541 insertions(+), 578 deletions(-) diff --git a/packages/kbn-typed-react-router-config/src/types/index.ts b/packages/kbn-typed-react-router-config/src/types/index.ts index c1ae5afd816ee..f15fd99a02a87 100644 --- a/packages/kbn-typed-react-router-config/src/types/index.ts +++ b/packages/kbn-typed-react-router-config/src/types/index.ts @@ -13,97 +13,13 @@ import { RequiredKeys, ValuesType } from 'utility-types'; // import { unconst } from '../unconst'; import { NormalizePath } from './utils'; -type PathsOfRoute = - | TRoute['path'] - | (TRoute extends { children: Route[] } - ? AppendPath | PathsOf - : never); - -export type PathsOf = TRoutes extends [] - ? never - : TRoutes extends [Route] - ? PathsOfRoute - : TRoutes extends [Route, Route] - ? PathsOfRoute | PathsOfRoute - : TRoutes extends [Route, Route, Route] - ? PathsOfRoute | PathsOfRoute | PathsOfRoute - : TRoutes extends [Route, Route, Route, Route] - ? - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - : TRoutes extends [Route, Route, Route, Route, Route] - ? - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - : TRoutes extends [Route, Route, Route, Route, Route, Route] - ? - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - : TRoutes extends [Route, Route, Route, Route, Route, Route, Route] - ? - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - : TRoutes extends [Route, Route, Route, Route, Route, Route, Route, Route] - ? - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - : TRoutes extends [Route, Route, Route, Route, Route, Route, Route, Route, Route] - ? - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - : TRoutes extends [Route, Route, Route, Route, Route, Route, Route, Route, Route, Route] - ? - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - : TRoutes extends [Route, Route, Route, Route, Route, Route, Route, Route, Route, Route, Route] - ? - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - | PathsOfRoute - : string; +// type PathsOfRoute = +// | TRoute['path'] +// | (TRoute extends { children: Route[] } +// ? AppendPath | PathsOf +// : never); + +export type PathsOf = keyof MapRoutes & string; export interface RouteMatch { route: TRoute; @@ -347,6 +263,14 @@ type MapRoutes = TRoutes extends [Route] // const routes = unconst([ // { +// path: '/link-to/transaction/{transactionId}', +// element, +// }, +// { +// path: '/link-to/trace/{traceId}', +// element, +// }, +// { // path: '/', // element, // children: [ @@ -393,6 +317,10 @@ type MapRoutes = TRoutes extends [Route] // element, // }, // { +// path: '/settings/agent-keys', +// element, +// }, +// { // path: '/settings', // element, // }, @@ -430,11 +358,19 @@ type MapRoutes = TRoutes extends [Route] // element, // }, // { +// path: '/services/:serviceName/transactions/view', +// element, +// }, +// { +// path: '/services/:serviceName/dependencies', +// element, +// }, +// { // path: '/services/:serviceName/errors', // element, // children: [ // { -// path: '/:groupId', +// path: '/services/:serviceName/errors/:groupId', // element, // params: t.type({ // path: t.type({ @@ -443,7 +379,7 @@ type MapRoutes = TRoutes extends [Route] // }), // }, // { -// path: '/services/:serviceName', +// path: '/services/:serviceName/errors', // element, // params: t.partial({ // query: t.partial({ @@ -457,15 +393,33 @@ type MapRoutes = TRoutes extends [Route] // ], // }, // { -// path: '/services/:serviceName/foo', +// path: '/services/:serviceName/metrics', +// element, +// }, +// { +// path: '/services/:serviceName/nodes', +// element, +// children: [ +// { +// path: '/services/{serviceName}/nodes/{serviceNodeName}/metrics', +// element, +// }, +// { +// path: '/services/:serviceName/nodes', +// element, +// }, +// ], +// }, +// { +// path: '/services/:serviceName/service-map', // element, // }, // { -// path: '/services/:serviceName/bar', +// path: '/services/:serviceName/logs', // element, // }, // { -// path: '/services/:serviceName/baz', +// path: '/services/:serviceName/profiling', // element, // }, // { @@ -497,6 +451,24 @@ type MapRoutes = TRoutes extends [Route] // element, // }, // { +// path: '/backends', +// element, +// children: [ +// { +// path: '/backends/{backendName}/overview', +// element, +// }, +// { +// path: '/backends/overview', +// element, +// }, +// { +// path: '/backends', +// element, +// }, +// ], +// }, +// { // path: '/', // element, // }, @@ -509,10 +481,11 @@ type MapRoutes = TRoutes extends [Route] // type Routes = typeof routes; // type Mapped = keyof MapRoutes; +// type Paths = PathsOf; // type Bar = ValuesType>['route']['path']; // type Foo = OutputOf; -// type Baz = OutputOf; +// // type Baz = OutputOf; // const { path }: Foo = {} as any; @@ -520,4 +493,4 @@ type MapRoutes = TRoutes extends [Route] // return {} as any; // } -// const params = _useApmParams('/*'); +// // const params = _useApmParams('/services/:serviceName/nodes/*'); diff --git a/src/core/types/elasticsearch/search.ts b/src/core/types/elasticsearch/search.ts index c28bf3c258f77..ac93a45da3258 100644 --- a/src/core/types/elasticsearch/search.ts +++ b/src/core/types/elasticsearch/search.ts @@ -9,6 +9,11 @@ import { ValuesType, UnionToIntersection } from 'utility-types'; import * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey'; +interface AggregationsAggregationContainer extends Record { + aggs?: any; + aggregations?: any; +} + type InvalidAggregationRequest = unknown; // ensures aggregations work with requests where aggregation options are a union type, @@ -31,7 +36,7 @@ type KeysOfSources = T extends [any] ? KeyOfSource & KeyOfSource & KeyOfSource & KeyOfSource : Record; -type CompositeKeysOf = +type CompositeKeysOf = TAggregationContainer extends { composite: { sources: [...infer TSource] }; } @@ -40,7 +45,7 @@ type CompositeKeysOf = +type TopMetricKeysOf = TAggregationContainer extends { top_metrics: { metrics: { field: infer TField } } } ? TField : TAggregationContainer extends { top_metrics: { metrics: Array<{ field: infer TField }> } } @@ -92,17 +97,9 @@ type HitsOf< > >; -type AggregationTypeName = Exclude< - keyof estypes.AggregationsAggregationContainer, - 'aggs' | 'aggregations' ->; +type AggregationMap = Partial>; -type AggregationMap = Partial>; - -type TopLevelAggregationRequest = Pick< - estypes.AggregationsAggregationContainer, - 'aggs' | 'aggregations' ->; +type TopLevelAggregationRequest = Pick; type MaybeKeyed< TAggregationContainer, @@ -113,448 +110,460 @@ type MaybeKeyed< : { buckets: TBucket[] }; export type AggregateOf< - TAggregationContainer extends estypes.AggregationsAggregationContainer, + TAggregationContainer extends AggregationsAggregationContainer, TDocument -> = (Record & { - adjacency_matrix: { - buckets: Array< - { - key: string; - doc_count: number; - } & SubAggregateOf - >; - }; - auto_date_histogram: { - interval: string; - buckets: Array< - { - key: number; - key_as_string: string; - doc_count: number; - } & SubAggregateOf - >; - }; - avg: { - value: number | null; - value_as_string?: string; - }; - avg_bucket: { - value: number | null; - }; - boxplot: { - min: number | null; - max: number | null; - q1: number | null; - q2: number | null; - q3: number | null; - }; - bucket_script: { - value: unknown; - }; - cardinality: { - value: number; - }; - children: { - doc_count: number; - } & SubAggregateOf; - composite: { - after_key: CompositeKeysOf; - buckets: Array< - { +> = ValuesType< + Pick< + Record & { + adjacency_matrix: { + buckets: Array< + { + key: string; + doc_count: number; + } & SubAggregateOf + >; + }; + auto_date_histogram: { + interval: string; + buckets: Array< + { + key: number; + key_as_string: string; + doc_count: number; + } & SubAggregateOf + >; + }; + avg: { + value: number | null; + value_as_string?: string; + }; + avg_bucket: { + value: number | null; + }; + boxplot: { + min: number | null; + max: number | null; + q1: number | null; + q2: number | null; + q3: number | null; + }; + bucket_script: { + value: unknown; + }; + cardinality: { + value: number; + }; + children: { doc_count: number; - key: CompositeKeysOf; - } & SubAggregateOf - >; - }; - cumulative_cardinality: { - value: number; - }; - cumulative_sum: { - value: number; - }; - date_histogram: MaybeKeyed< - TAggregationContainer, - { - key: number; - key_as_string: string; - doc_count: number; - } & SubAggregateOf - >; - date_range: MaybeKeyed< - TAggregationContainer, - Partial<{ from: string | number; from_as_string: string }> & - Partial<{ to: string | number; to_as_string: string }> & { + } & SubAggregateOf; + composite: { + after_key: CompositeKeysOf; + buckets: Array< + { + doc_count: number; + key: CompositeKeysOf; + } & SubAggregateOf + >; + }; + cumulative_cardinality: { + value: number; + }; + cumulative_sum: { + value: number; + }; + date_histogram: MaybeKeyed< + TAggregationContainer, + { + key: number; + key_as_string: string; + doc_count: number; + } & SubAggregateOf + >; + date_range: MaybeKeyed< + TAggregationContainer, + Partial<{ from: string | number; from_as_string: string }> & + Partial<{ to: string | number; to_as_string: string }> & { + doc_count: number; + key: string; + } + >; + derivative: + | { + value: number | null; + } + | undefined; + extended_stats: { + count: number; + min: number | null; + max: number | null; + avg: number | null; + sum: number; + sum_of_squares: number | null; + variance: number | null; + variance_population: number | null; + variance_sampling: number | null; + std_deviation: number | null; + std_deviation_population: number | null; + std_deviation_sampling: number | null; + std_deviation_bounds: { + upper: number | null; + lower: number | null; + upper_population: number | null; + lower_population: number | null; + upper_sampling: number | null; + lower_sampling: number | null; + }; + } & ( + | { + min_as_string: string; + max_as_string: string; + avg_as_string: string; + sum_of_squares_as_string: string; + variance_population_as_string: string; + variance_sampling_as_string: string; + std_deviation_as_string: string; + std_deviation_population_as_string: string; + std_deviation_sampling_as_string: string; + std_deviation_bounds_as_string: { + upper: string; + lower: string; + upper_population: string; + lower_population: string; + upper_sampling: string; + lower_sampling: string; + }; + } + | {} + ); + extended_stats_bucket: { + count: number; + min: number | null; + max: number | null; + avg: number | null; + sum: number | null; + sum_of_squares: number | null; + variance: number | null; + variance_population: number | null; + variance_sampling: number | null; + std_deviation: number | null; + std_deviation_population: number | null; + std_deviation_sampling: number | null; + std_deviation_bounds: { + upper: number | null; + lower: number | null; + upper_population: number | null; + lower_population: number | null; + upper_sampling: number | null; + lower_sampling: number | null; + }; + }; + filter: { doc_count: number; - key: string; - } - >; - derivative: - | { - value: number | null; - } - | undefined; - extended_stats: { - count: number; - min: number | null; - max: number | null; - avg: number | null; - sum: number; - sum_of_squares: number | null; - variance: number | null; - variance_population: number | null; - variance_sampling: number | null; - std_deviation: number | null; - std_deviation_population: number | null; - std_deviation_sampling: number | null; - std_deviation_bounds: { - upper: number | null; - lower: number | null; - upper_population: number | null; - lower_population: number | null; - upper_sampling: number | null; - lower_sampling: number | null; - }; - } & ( - | { - min_as_string: string; - max_as_string: string; - avg_as_string: string; - sum_of_squares_as_string: string; - variance_population_as_string: string; - variance_sampling_as_string: string; - std_deviation_as_string: string; - std_deviation_population_as_string: string; - std_deviation_sampling_as_string: string; - std_deviation_bounds_as_string: { - upper: string; - lower: string; - upper_population: string; - lower_population: string; - upper_sampling: string; - lower_sampling: string; + } & SubAggregateOf; + filters: { + buckets: TAggregationContainer extends { filters: { filters: any[] } } + ? Array< + { + doc_count: number; + } & SubAggregateOf + > + : TAggregationContainer extends { filters: { filters: Record } } + ? { + [key in keyof TAggregationContainer['filters']['filters']]: { + doc_count: number; + } & SubAggregateOf; + } & (TAggregationContainer extends { + filters: { other_bucket_key: infer TOtherBucketKey }; + } + ? Record< + TOtherBucketKey & string, + { doc_count: number } & SubAggregateOf + > + : unknown) & + (TAggregationContainer extends { filters: { other_bucket: true } } + ? { + _other: { doc_count: number } & SubAggregateOf< + TAggregationContainer, + TDocument + >; + } + : unknown) + : unknown; + }; + geo_bounds: { + top_left: { + lat: number | null; + lon: number | null; }; - } - | {} - ); - extended_stats_bucket: { - count: number; - min: number | null; - max: number | null; - avg: number | null; - sum: number | null; - sum_of_squares: number | null; - variance: number | null; - variance_population: number | null; - variance_sampling: number | null; - std_deviation: number | null; - std_deviation_population: number | null; - std_deviation_sampling: number | null; - std_deviation_bounds: { - upper: number | null; - lower: number | null; - upper_population: number | null; - lower_population: number | null; - upper_sampling: number | null; - lower_sampling: number | null; - }; - }; - filter: { - doc_count: number; - } & SubAggregateOf; - filters: { - buckets: TAggregationContainer extends { filters: { filters: any[] } } - ? Array< + bottom_right: { + lat: number | null; + lon: number | null; + }; + }; + geo_centroid: { + count: number; + location: { + lat: number; + lon: number; + }; + }; + geo_distance: MaybeKeyed< + TAggregationContainer, + { + from: number; + to?: number; + doc_count: number; + } & SubAggregateOf + >; + geo_hash: { + buckets: Array< { doc_count: number; + key: string; } & SubAggregateOf - > - : TAggregationContainer extends { filters: { filters: Record } } - ? { - [key in keyof TAggregationContainer['filters']['filters']]: { + >; + }; + geotile_grid: { + buckets: Array< + { doc_count: number; - } & SubAggregateOf; - } & (TAggregationContainer extends { filters: { other_bucket_key: infer TOtherBucketKey } } - ? Record< - TOtherBucketKey & string, - { doc_count: number } & SubAggregateOf - > - : unknown) & - (TAggregationContainer extends { filters: { other_bucket: true } } - ? { _other: { doc_count: number } & SubAggregateOf } - : unknown) - : unknown; - }; - geo_bounds: { - top_left: { - lat: number | null; - lon: number | null; - }; - bottom_right: { - lat: number | null; - lon: number | null; - }; - }; - geo_centroid: { - count: number; - location: { - lat: number; - lon: number; - }; - }; - geo_distance: MaybeKeyed< - TAggregationContainer, - { - from: number; - to?: number; - doc_count: number; - } & SubAggregateOf - >; - geo_hash: { - buckets: Array< - { + key: string; + } & SubAggregateOf + >; + }; + global: { doc_count: number; - key: string; - } & SubAggregateOf - >; - }; - geotile_grid: { - buckets: Array< - { + } & SubAggregateOf; + histogram: MaybeKeyed< + TAggregationContainer, + { + key: number; + doc_count: number; + } & SubAggregateOf + >; + ip_range: MaybeKeyed< + TAggregationContainer, + { + key: string; + from?: string; + to?: string; + doc_count: number; + }, + TAggregationContainer extends { ip_range: { ranges: Array } } + ? TRangeType extends { key: infer TKeys } + ? TKeys + : string + : string + >; + inference: { + value: number; + prediction_probability: number; + prediction_score: number; + }; + max: { + value: number | null; + value_as_string?: string; + }; + max_bucket: { + value: number | null; + }; + min: { + value: number | null; + value_as_string?: string; + }; + min_bucket: { + value: number | null; + }; + median_absolute_deviation: { + value: number | null; + }; + moving_avg: + | { + value: number | null; + } + | undefined; + moving_fn: { + value: number | null; + }; + moving_percentiles: TAggregationContainer extends Record + ? Array<{ key: number; value: number | null }> + : Record | undefined; + missing: { doc_count: number; - key: string; - } & SubAggregateOf - >; - }; - global: { - doc_count: number; - } & SubAggregateOf; - histogram: MaybeKeyed< - TAggregationContainer, - { - key: number; - doc_count: number; - } & SubAggregateOf - >; - ip_range: MaybeKeyed< - TAggregationContainer, - { - key: string; - from?: string; - to?: string; - doc_count: number; - }, - TAggregationContainer extends { ip_range: { ranges: Array } } - ? TRangeType extends { key: infer TKeys } - ? TKeys - : string - : string - >; - inference: { - value: number; - prediction_probability: number; - prediction_score: number; - }; - max: { - value: number | null; - value_as_string?: string; - }; - max_bucket: { - value: number | null; - }; - min: { - value: number | null; - value_as_string?: string; - }; - min_bucket: { - value: number | null; - }; - median_absolute_deviation: { - value: number | null; - }; - moving_avg: - | { + } & SubAggregateOf; + multi_terms: { + doc_count_error_upper_bound: number; + sum_other_doc_count: number; + buckets: Array< + { + doc_count: number; + key: string[]; + } & SubAggregateOf + >; + }; + nested: { + doc_count: number; + } & SubAggregateOf; + normalize: { value: number | null; - } - | undefined; - moving_fn: { - value: number | null; - }; - moving_percentiles: TAggregationContainer extends Record - ? Array<{ key: number; value: number | null }> - : Record | undefined; - missing: { - doc_count: number; - } & SubAggregateOf; - multi_terms: { - doc_count_error_upper_bound: number; - sum_other_doc_count: number; - buckets: Array< - { + // TODO: should be perhaps based on input? ie when `format` is specified + value_as_string?: string; + }; + parent: { doc_count: number; - key: string[]; - } & SubAggregateOf - >; - }; - nested: { - doc_count: number; - } & SubAggregateOf; - normalize: { - value: number | null; - // TODO: should be perhaps based on input? ie when `format` is specified - value_as_string?: string; - }; - parent: { - doc_count: number; - } & SubAggregateOf; - percentiles: { - values: TAggregationContainer extends Record - ? Array<{ key: number; value: number | null }> - : Record; - }; - percentile_ranks: { - values: TAggregationContainer extends Record - ? Array<{ key: number; value: number | null }> - : Record; - }; - percentiles_bucket: { - values: TAggregationContainer extends Record - ? Array<{ key: number; value: number | null }> - : Record; - }; - range: MaybeKeyed< - TAggregationContainer, - { - key: string; - from?: number; - from_as_string?: string; - to?: number; - to_as_string?: string; - doc_count: number; - }, - TAggregationContainer extends { range: { ranges: Array } } - ? TRangeType extends { key: infer TKeys } - ? TKeys - : string - : string - >; - rare_terms: Array< - { - key: string | number; - doc_count: number; - } & SubAggregateOf - >; - rate: { - value: number | null; - }; - reverse_nested: { - doc_count: number; - } & SubAggregateOf; - sampler: { - doc_count: number; - } & SubAggregateOf; - scripted_metric: { - value: unknown; - }; - serial_diff: { - value: number | null; - // TODO: should be perhaps based on input? ie when `format` is specified - value_as_string?: string; - }; - significant_terms: { - doc_count: number; - bg_count: number; - buckets: Array< - { - key: string | number; - score: number; + } & SubAggregateOf; + percentiles: { + values: TAggregationContainer extends Record + ? Array<{ key: number; value: number | null }> + : Record; + }; + percentile_ranks: { + values: TAggregationContainer extends Record + ? Array<{ key: number; value: number | null }> + : Record; + }; + percentiles_bucket: { + values: TAggregationContainer extends Record + ? Array<{ key: number; value: number | null }> + : Record; + }; + range: MaybeKeyed< + TAggregationContainer, + { + key: string; + from?: number; + from_as_string?: string; + to?: number; + to_as_string?: string; + doc_count: number; + }, + TAggregationContainer extends { range: { ranges: Array } } + ? TRangeType extends { key: infer TKeys } + ? TKeys + : string + : string + >; + rare_terms: Array< + { + key: string | number; + doc_count: number; + } & SubAggregateOf + >; + rate: { + value: number | null; + }; + reverse_nested: { + doc_count: number; + } & SubAggregateOf; + sampler: { + doc_count: number; + } & SubAggregateOf; + scripted_metric: { + value: unknown; + }; + serial_diff: { + value: number | null; + // TODO: should be perhaps based on input? ie when `format` is specified + value_as_string?: string; + }; + significant_terms: { doc_count: number; bg_count: number; - } & SubAggregateOf - >; - }; - significant_text: { - doc_count: number; - buckets: Array<{ - key: string; - doc_count: number; - score: number; - bg_count: number; - }>; - }; - stats: { - count: number; - min: number | null; - max: number | null; - avg: number | null; - sum: number; - } & ( - | { - min_as_string: string; - max_as_string: string; - avg_as_string: string; - sum_as_string: string; - } - | {} - ); - stats_bucket: { - count: number; - min: number | null; - max: number | null; - avg: number | null; - sum: number; - }; - string_stats: { - count: number; - min_length: number | null; - max_length: number | null; - avg_length: number | null; - entropy: number | null; - distribution: Record; - }; - sum: { - value: number | null; - value_as_string?: string; - }; - sum_bucket: { - value: number | null; - }; - terms: { - doc_count_error_upper_bound: number; - sum_other_doc_count: number; - buckets: Array< - { + buckets: Array< + { + key: string | number; + score: number; + doc_count: number; + bg_count: number; + } & SubAggregateOf + >; + }; + significant_text: { doc_count: number; - key: string | number; - } & SubAggregateOf - >; - }; - top_hits: { - hits: { - total: { + buckets: Array<{ + key: string; + doc_count: number; + score: number; + bg_count: number; + }>; + }; + stats: { + count: number; + min: number | null; + max: number | null; + avg: number | null; + sum: number; + } & ( + | { + min_as_string: string; + max_as_string: string; + avg_as_string: string; + sum_as_string: string; + } + | {} + ); + stats_bucket: { + count: number; + min: number | null; + max: number | null; + avg: number | null; + sum: number; + }; + string_stats: { + count: number; + min_length: number | null; + max_length: number | null; + avg_length: number | null; + entropy: number | null; + distribution: Record; + }; + sum: { + value: number | null; + value_as_string?: string; + }; + sum_bucket: { + value: number | null; + }; + terms: { + doc_count_error_upper_bound: number; + sum_other_doc_count: number; + buckets: Array< + { + doc_count: number; + key: string | number; + } & SubAggregateOf + >; + }; + top_hits: { + hits: { + total: { + value: number; + relation: 'eq' | 'gte'; + }; + max_score: number | null; + hits: TAggregationContainer extends { top_hits: estypes.AggregationsTopHitsAggregation } + ? HitsOf + : estypes.SearchHitsMetadata; + }; + }; + top_metrics: { + top: Array<{ + sort: number[] | string[]; + metrics: Record, string | number | null>; + }>; + }; + weighted_avg: { value: number | null }; + value_count: { value: number; - relation: 'eq' | 'gte'; }; - max_score: number | null; - hits: TAggregationContainer extends { top_hits: estypes.AggregationsTopHitsAggregation } - ? HitsOf - : estypes.SearchHitsMetadata; - }; - }; - top_metrics: { - top: Array<{ - sort: number[] | string[]; - metrics: Record, string | number | null>; - }>; - }; - weighted_avg: { value: number | null }; - value_count: { - value: number; - }; - // t_test: {} not defined -})[ValidAggregationKeysOf & AggregationTypeName]; + // t_test: {} not defined + }, + Exclude, 'aggs' | 'aggregations'> & string + > +>; type AggregateOfMap = { - [TAggregationName in keyof TAggregationMap]: Required[TAggregationName] extends estypes.AggregationsAggregationContainer + [TAggregationName in keyof TAggregationMap]: Required[TAggregationName] extends AggregationsAggregationContainer ? AggregateOf : never; // using never means we effectively ignore optional keys, using {} creates a union type of { ... } | {} }; diff --git a/x-pack/plugins/apm/public/components/app/service_inventory/index.tsx b/x-pack/plugins/apm/public/components/app/service_inventory/index.tsx index 1994d3641ee53..163082cf044cd 100644 --- a/x-pack/plugins/apm/public/components/app/service_inventory/index.tsx +++ b/x-pack/plugins/apm/public/components/app/service_inventory/index.tsx @@ -19,7 +19,7 @@ import { useAnomalyDetectionJobsContext } from '../../../context/anomaly_detecti import { useApmPluginContext } from '../../../context/apm_plugin/use_apm_plugin_context'; import { useLegacyUrlParams } from '../../../context/url_params_context/use_url_params'; import { useLocalStorage } from '../../../hooks/useLocalStorage'; -import { useApmParams } from '../../../hooks/use_apm_params'; +import { useAnyOfApmParams } from '../../../hooks/use_apm_params'; import { FETCH_STATUS, useFetcher } from '../../../hooks/use_fetcher'; import { useTimeRange } from '../../../hooks/use_time_range'; import { useUpgradeAssistantHref } from '../../shared/Links/kibana'; @@ -46,9 +46,7 @@ function useServicesFetcher() { const { query: { rangeFrom, rangeTo, environment, kuery }, - } = - // @ts-ignore 4.3.5 upgrade - Type instantiation is excessively deep and possibly infinite. - useApmParams('/services/{serviceName}', '/services'); + } = useAnyOfApmParams('/services/{serviceName}', '/services'); const { start, end } = useTimeRange({ rangeFrom, rangeTo }); diff --git a/x-pack/plugins/apm/public/components/app/service_map/Controls.tsx b/x-pack/plugins/apm/public/components/app/service_map/Controls.tsx index 4605952a6f396..a48fb77b45585 100644 --- a/x-pack/plugins/apm/public/components/app/service_map/Controls.tsx +++ b/x-pack/plugins/apm/public/components/app/service_map/Controls.tsx @@ -16,7 +16,7 @@ import { useLegacyUrlParams } from '../../../context/url_params_context/use_url_ import { APMQueryParams } from '../../shared/Links/url_helpers'; import { CytoscapeContext } from './Cytoscape'; import { getAnimationOptions, getNodeHeight } from './cytoscape_options'; -import { useApmParams } from '../../../hooks/use_apm_params'; +import { useAnyOfApmParams } from '../../../hooks/use_apm_params'; const ControlsContainer = euiStyled('div')` left: ${({ theme }) => theme.eui.gutterTypes.gutterMedium}; @@ -107,7 +107,7 @@ export function Controls() { const { query: { kuery }, - } = useApmParams('/service-map', '/services/{serviceName}/service-map'); + } = useAnyOfApmParams('/service-map', '/services/{serviceName}/service-map'); const [zoom, setZoom] = useState((cy && cy.zoom()) || 1); const duration = parseInt(theme.eui.euiAnimSpeedFast, 10); diff --git a/x-pack/plugins/apm/public/components/app/service_map/Popover/backend_contents.tsx b/x-pack/plugins/apm/public/components/app/service_map/Popover/backend_contents.tsx index a862ff872f61a..8fa93e22a90fe 100644 --- a/x-pack/plugins/apm/public/components/app/service_map/Popover/backend_contents.tsx +++ b/x-pack/plugins/apm/public/components/app/service_map/Popover/backend_contents.tsx @@ -13,7 +13,7 @@ import React from 'react'; import { useUiTracker } from '../../../../../../observability/public'; import { ContentsProps } from '.'; import { NodeStats } from '../../../../../common/service_map'; -import { useApmParams } from '../../../../hooks/use_apm_params'; +import { useAnyOfApmParams } from '../../../../hooks/use_apm_params'; import { useApmRouter } from '../../../../hooks/use_apm_router'; import { FETCH_STATUS, useFetcher } from '../../../../hooks/use_fetcher'; import { ApmRoutes } from '../../../routing/apm_route_config'; @@ -25,8 +25,7 @@ export function BackendContents({ start, end, }: ContentsProps) { - // @ts-ignore 4.3.5 upgrade - Type instantiation is excessively deep and possibly infinite. - const { query } = useApmParams( + const { query } = useAnyOfApmParams( '/service-map', '/services/{serviceName}/service-map' ); diff --git a/x-pack/plugins/apm/public/components/shared/time_comparison/index.tsx b/x-pack/plugins/apm/public/components/shared/time_comparison/index.tsx index aa6d69c03d8f6..2cb4e0964686f 100644 --- a/x-pack/plugins/apm/public/components/shared/time_comparison/index.tsx +++ b/x-pack/plugins/apm/public/components/shared/time_comparison/index.tsx @@ -15,7 +15,7 @@ import { useUiTracker } from '../../../../../observability/public'; import { TimeRangeComparisonEnum } from '../../../../common/runtime_types/comparison_type_rt'; import { useLegacyUrlParams } from '../../../context/url_params_context/use_url_params'; import { useApmPluginContext } from '../../../context/apm_plugin/use_apm_plugin_context'; -import { useApmParams } from '../../../hooks/use_apm_params'; +import { useAnyOfApmParams } from '../../../hooks/use_apm_params'; import { useBreakpoints } from '../../../hooks/use_breakpoints'; import { useTimeRange } from '../../../hooks/use_time_range'; import * as urlHelpers from '../../shared/Links/url_helpers'; @@ -121,8 +121,7 @@ export function TimeComparison() { const { isSmall } = useBreakpoints(); const { query: { rangeFrom, rangeTo }, - // @ts-expect-error Type instantiation is excessively deep and possibly infinite. - } = useApmParams('/services', '/backends/*', '/services/{serviceName}'); + } = useAnyOfApmParams('/services', '/backends/*', '/services/{serviceName}'); const { exactStart, exactEnd } = useTimeRange({ rangeFrom, diff --git a/x-pack/plugins/apm/public/hooks/use_apm_params.ts b/x-pack/plugins/apm/public/hooks/use_apm_params.ts index 12b79ec7c90ae..b4c17c1b329ae 100644 --- a/x-pack/plugins/apm/public/hooks/use_apm_params.ts +++ b/x-pack/plugins/apm/public/hooks/use_apm_params.ts @@ -4,42 +4,29 @@ * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ - +import { ValuesType } from 'utility-types'; import { TypeOf, PathsOf, useParams } from '@kbn/typed-react-router-config'; import { ApmRoutes } from '../components/routing/apm_route_config'; -export function useApmParams>( +// these three different functions exist purely to speed up completions from +// TypeScript. One overloaded function is expensive because of the size of the +// union type that is created. + +export function useMaybeApmParams>( path: TPath, optional: true -): TypeOf | undefined; +): TypeOf | undefined { + return useParams(path, optional); +} export function useApmParams>( path: TPath -): TypeOf; - -export function useApmParams< - TPath1 extends PathsOf, - TPath2 extends PathsOf ->( - path1: TPath1, - path2: TPath2 -): TypeOf | TypeOf; - -export function useApmParams< - TPath1 extends PathsOf, - TPath2 extends PathsOf, - TPath3 extends PathsOf ->( - path1: TPath1, - path2: TPath2, - path3: TPath3 -): - | TypeOf - | TypeOf - | TypeOf; +): TypeOf { + return useParams(path)!; +} -export function useApmParams( - ...args: any[] -): TypeOf> | undefined { - return useParams(...args); +export function useAnyOfApmParams>>( + ...paths: TPaths +): TypeOf> { + return useParams(...paths)!; } diff --git a/x-pack/plugins/apm/public/hooks/use_apm_router.ts b/x-pack/plugins/apm/public/hooks/use_apm_router.ts index d10b6da857802..dea66d7b2e1c8 100644 --- a/x-pack/plugins/apm/public/hooks/use_apm_router.ts +++ b/x-pack/plugins/apm/public/hooks/use_apm_router.ts @@ -14,6 +14,8 @@ export function useApmRouter() { const { core } = useApmPluginContext(); const link = (...args: [any]) => { + // @ts-expect-error router.link() expects never type, because + // no routes are specified. that's okay. return core.http.basePath.prepend('/app/apm' + router.link(...args)); }; diff --git a/x-pack/plugins/apm/server/routes/errors/get_error_groups/get_error_group_main_statistics.ts b/x-pack/plugins/apm/server/routes/errors/get_error_groups/get_error_group_main_statistics.ts index d32e751a6ca99..e460991029915 100644 --- a/x-pack/plugins/apm/server/routes/errors/get_error_groups/get_error_group_main_statistics.ts +++ b/x-pack/plugins/apm/server/routes/errors/get_error_groups/get_error_group_main_statistics.ts @@ -110,7 +110,6 @@ export async function getErrorGroupMainStatistics({ ); return ( - // @ts-ignore 4.3.5 upgrade - Expression produces a union type that is too complex to represent. ts(2590) response.aggregations?.error_groups.buckets.map((bucket) => ({ groupId: bucket.key as string, name: getErrorName(bucket.sample.hits.hits[0]._source), diff --git a/x-pack/plugins/apm/server/routes/service_map/get_service_anomalies.ts b/x-pack/plugins/apm/server/routes/service_map/get_service_anomalies.ts index 792bc0463aa15..a5fcececad1cc 100644 --- a/x-pack/plugins/apm/server/routes/service_map/get_service_anomalies.ts +++ b/x-pack/plugins/apm/server/routes/service_map/get_service_anomalies.ts @@ -120,7 +120,6 @@ export async function getServiceAnomalies({ const relevantBuckets = uniqBy( sortBy( // make sure we only return data for jobs that are available in this space - // @ts-ignore 4.3.5 upgrade typedAnomalyResponse.aggregations?.services.buckets.filter((bucket) => jobIds.includes(bucket.key.jobId as string) ) ?? [], diff --git a/x-pack/plugins/apm/server/routes/services/get_service_instances/get_service_instances_transaction_statistics.ts b/x-pack/plugins/apm/server/routes/services/get_service_instances/get_service_instances_transaction_statistics.ts index a9f5615acb1c0..ec081916f455d 100644 --- a/x-pack/plugins/apm/server/routes/services/get_service_instances/get_service_instances_transaction_statistics.ts +++ b/x-pack/plugins/apm/server/routes/services/get_service_instances/get_service_instances_transaction_statistics.ts @@ -168,7 +168,6 @@ export async function getServiceInstancesTransactionStatistics< const { timeseries } = serviceNodeBucket; return { serviceNodeName, - // @ts-ignore 4.3.5 upgrade - Expression produces a union type that is too complex to represent. errorRate: timeseries.buckets.map((dateBucket) => ({ x: dateBucket.key, y: dateBucket.failures.doc_count / dateBucket.doc_count, diff --git a/x-pack/plugins/security_solution/server/lib/detection_engine/routes/signals/query_signals_route.test.ts b/x-pack/plugins/security_solution/server/lib/detection_engine/routes/signals/query_signals_route.test.ts index 0af6bceab26a5..0e436760a88ee 100644 --- a/x-pack/plugins/security_solution/server/lib/detection_engine/routes/signals/query_signals_route.test.ts +++ b/x-pack/plugins/security_solution/server/lib/detection_engine/routes/signals/query_signals_route.test.ts @@ -27,7 +27,6 @@ describe('query for signal', () => { server = serverMock.create(); ({ context } = requestContextMock.createTools()); - // @ts-expect-error 4.3.5 upgrade // eslint-disable-next-line @typescript-eslint/no-explicit-any ruleDataClient.getReader().search.mockResolvedValue(getEmptySignalsResponse() as any);