Skip to content

Commit b98e2e4

Browse files
Zacqaryweltenwortelasticmachine
authored
[Metrics UI] Replace uses of any introduced by Lodash 4 (#75507)
Co-authored-by: Felix Stürmer <weltenwort@users.noreply.github.com> Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
1 parent 8671db1 commit b98e2e4

File tree

19 files changed

+82
-64
lines changed

19 files changed

+82
-64
lines changed

x-pack/plugins/infra/public/alerting/inventory/components/expression.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ interface Props {
8585
nodeType: InventoryItemType;
8686
filterQuery?: string;
8787
filterQueryText?: string;
88-
sourceId?: string;
88+
sourceId: string;
8989
alertOnNoData?: boolean;
9090
};
9191
alertInterval: string;
@@ -379,7 +379,7 @@ export const Expressions: React.FC<Props> = (props) => {
379379
<AlertPreview
380380
alertInterval={alertInterval}
381381
alertType={METRIC_INVENTORY_THRESHOLD_ALERT_TYPE_ID}
382-
alertParams={pick(alertParams as any, 'criteria', 'nodeType', 'sourceId', 'filterQuery')}
382+
alertParams={pick(alertParams, 'criteria', 'nodeType', 'sourceId', 'filterQuery')}
383383
validate={validateMetricThreshold}
384384
fetch={alertsContext.http.fetch}
385385
groupByDisplayName={alertParams.nodeType}

x-pack/plugins/infra/public/alerting/metric_threshold/components/expression.test.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ describe('Expression', () => {
3434
criteria: [],
3535
groupBy: undefined,
3636
filterQueryText: '',
37+
sourceId: 'default',
3738
};
3839

3940
const mocks = coreMock.createSetup();

x-pack/plugins/infra/public/alerting/metric_threshold/components/expression.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ export const Expressions: React.FC<Props> = (props) => {
400400
<AlertPreview
401401
alertInterval={alertInterval}
402402
alertType={METRIC_THRESHOLD_ALERT_TYPE_ID}
403-
alertParams={pick(alertParams as any, 'criteria', 'groupBy', 'filterQuery', 'sourceId')}
403+
alertParams={pick(alertParams, 'criteria', 'groupBy', 'filterQuery', 'sourceId')}
404404
showNoDataResults={alertParams.alertOnNoData}
405405
validate={validateMetricThreshold}
406406
fetch={alertsContext.http.fetch}

x-pack/plugins/infra/public/alerting/metric_threshold/components/expression_chart.tsx

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,16 @@ export const ExpressionChart: React.FC<Props> = ({
8484
};
8585
const isDarkMode = context.uiSettings?.get('theme:darkMode') || false;
8686
const dateFormatter = useMemo(() => {
87-
const firstSeries = data ? first(data.series) : null;
88-
return firstSeries && firstSeries.rows.length > 0
89-
? niceTimeFormatter([
90-
(first(firstSeries.rows) as any).timestamp,
91-
(last(firstSeries.rows) as any).timestamp,
92-
])
93-
: (value: number) => `${value}`;
94-
}, [data]);
87+
const firstSeries = first(data?.series);
88+
const firstTimestamp = first(firstSeries?.rows)?.timestamp;
89+
const lastTimestamp = last(firstSeries?.rows)?.timestamp;
90+
91+
if (firstTimestamp == null || lastTimestamp == null) {
92+
return (value: number) => `${value}`;
93+
}
94+
95+
return niceTimeFormatter([firstTimestamp, lastTimestamp]);
96+
}, [data?.series]);
9597

9698
/* eslint-disable-next-line react-hooks/exhaustive-deps */
9799
const yAxisFormater = useCallback(createFormatterForMetric(metric), [expression]);
@@ -138,8 +140,8 @@ export const ExpressionChart: React.FC<Props> = ({
138140
}),
139141
};
140142

141-
const firstTimestamp = (first(firstSeries.rows) as any).timestamp;
142-
const lastTimestamp = (last(firstSeries.rows) as any).timestamp;
143+
const firstTimestamp = first(firstSeries.rows)!.timestamp;
144+
const lastTimestamp = last(firstSeries.rows)!.timestamp;
143145
const dataDomain = calculateDomain(series, [metric], false);
144146
const domain = {
145147
max: Math.max(dataDomain.max, last(thresholds) || dataDomain.max) * 1.1, // add 10% headroom.

x-pack/plugins/infra/public/alerting/metric_threshold/lib/transform_metrics_explorer_data.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ export const transformMetricsExplorerData = (
1313
data: MetricsExplorerResponse | null
1414
) => {
1515
const { criteria } = params;
16-
if (criteria && data) {
17-
const firstSeries = first(data.series) as any;
18-
const series = firstSeries.rows.reduce((acc: any, row: any) => {
16+
const firstSeries = first(data?.series);
17+
if (criteria && firstSeries) {
18+
const series = firstSeries.rows.reduce((acc, row) => {
1919
const { timestamp } = row;
2020
criteria.forEach((item, index) => {
2121
if (!acc[index]) {

x-pack/plugins/infra/public/alerting/metric_threshold/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export interface AlertParams {
5555
criteria: MetricExpression[];
5656
groupBy?: string[];
5757
filterQuery?: string;
58-
sourceId?: string;
58+
sourceId: string;
5959
filterQueryText?: string;
6060
alertOnNoData?: boolean;
6161
}

x-pack/plugins/infra/public/pages/metrics/inventory_view/components/waffle/legend_controls.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ export const LegendControls = ({
266266
fullWidth
267267
label={
268268
<SwatchLabel
269-
color={first(paletteColors) as any}
269+
color={first(paletteColors)!}
270270
label={i18n.translate('xpack.infra.legendControls.minLabel', {
271271
defaultMessage: 'Minimum',
272272
})}
@@ -294,7 +294,7 @@ export const LegendControls = ({
294294
display="columnCompressed"
295295
label={
296296
<SwatchLabel
297-
color={last(paletteColors) as any}
297+
color={last(paletteColors)!}
298298
label={i18n.translate('xpack.infra.legendControls.maxLabel', {
299299
defaultMessage: 'Maxium',
300300
})}

x-pack/plugins/infra/public/pages/metrics/inventory_view/lib/color_from_value.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,9 @@ export const calculateSteppedGradientColor = (
6868

6969
// Since the stepped legend matches a range we need to ensure anything outside
7070
// the max bounds get's the maximum color.
71-
if (gte(normalizedValue, (last(rules) as any).value)) {
72-
return (last(rules) as any).color;
71+
const lastRule = last(rules);
72+
if (lastRule && gte(normalizedValue, lastRule.value)) {
73+
return lastRule.color;
7374
}
7475

7576
return rules.reduce((color: string, rule) => {
@@ -79,7 +80,7 @@ export const calculateSteppedGradientColor = (
7980
return rule.color;
8081
}
8182
return color;
82-
}, (first(rules) as any).color || defaultColor);
83+
}, first(rules)?.color ?? defaultColor);
8384
};
8485

8586
export const calculateStepColor = (
@@ -106,7 +107,7 @@ export const calculateGradientColor = (
106107
return defaultColor;
107108
}
108109
if (rules.length === 1) {
109-
return (last(rules) as any).color;
110+
return last(rules)!.color;
110111
}
111112
const { min, max } = bounds;
112113
const sortedRules = sortBy(rules, 'value');
@@ -116,10 +117,8 @@ export const calculateGradientColor = (
116117
return rule;
117118
}
118119
return acc;
119-
}, first(sortedRules)) as any;
120-
const endRule = sortedRules
121-
.filter((r) => r !== startRule)
122-
.find((r) => r.value >= normValue) as any;
120+
}, first(sortedRules))!;
121+
const endRule = sortedRules.filter((r) => r !== startRule).find((r) => r.value >= normValue);
123122
if (!endRule) {
124123
return startRule.color;
125124
}

x-pack/plugins/infra/public/pages/metrics/inventory_view/lib/nodes_to_wafflemap.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ function findOrCreateGroupWithNodes(
2929
* look for the full id. Otherwise we need to find the parent group and
3030
* then look for the group in it's sub groups.
3131
*/
32-
if (path.length === 2) {
33-
const parentId = (first(path) as any).value;
32+
const firstPath = first(path);
33+
if (path.length === 2 && firstPath) {
34+
const parentId = firstPath.value;
3435
const existingParentGroup = groups.find((g) => g.id === parentId);
3536
if (isWaffleMapGroupWithGroups(existingParentGroup)) {
3637
const existingSubGroup = existingParentGroup.groups.find((g) => g.id === id);

x-pack/plugins/infra/public/pages/metrics/metrics_explorer/components/chart.tsx

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,13 @@ export const MetricsExplorerChart = ({
7474
const [from, to] = x;
7575
onTimeChange(moment(from).toISOString(), moment(to).toISOString());
7676
};
77-
const dateFormatter = useMemo(
78-
() =>
79-
series.rows.length > 0
80-
? niceTimeFormatter([
81-
(first(series.rows) as any).timestamp,
82-
(last(series.rows) as any).timestamp,
83-
])
84-
: (value: number) => `${value}`,
85-
[series.rows]
86-
);
77+
const dateFormatter = useMemo(() => {
78+
const firstRow = first(series.rows);
79+
const lastRow = last(series.rows);
80+
return firstRow && lastRow
81+
? niceTimeFormatter([firstRow.timestamp, lastRow.timestamp])
82+
: (value: number) => `${value}`;
83+
}, [series.rows]);
8784
const tooltipProps = {
8885
headerFormatter: useCallback(
8986
(data: TooltipValue) => moment(data.value).format(dateFormat || 'Y-MM-DD HH:mm:ss.SSS'),

0 commit comments

Comments
 (0)