From 3acc318d728cd147971ef1b871bd85237c57a5de Mon Sep 17 00:00:00 2001 From: Stephanie Closson Date: Thu, 9 Apr 2020 00:29:36 -0600 Subject: [PATCH] Chore: reduce null check errors to 788 (currently over 798) (#23449) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fixed ts errors so build will succeed * Update packages/grafana-data/src/types/graph.ts Co-Authored-By: Ryan McKinley * Feedback from code review * Leaving out trivial typing's * Fix error with color being undefined now. * fix test with timezone issue * Fixed test Co-authored-by: Ryan McKinley Co-authored-by: Torkel Ödegaard --- packages/grafana-data/src/types/graph.ts | 2 +- packages/grafana-data/src/utils/series.ts | 15 +++++++------ .../src/components/Graph/GraphWithLegend.tsx | 2 +- public/app/core/time_series2.ts | 2 +- public/app/core/utils/richHistory.test.ts | 3 +++ .../explore/utils/ResultProcessor.test.ts | 4 ++-- public/app/plugins/panel/graph/align_yaxes.ts | 22 +++++++++---------- public/app/plugins/panel/graph/graph.ts | 4 ++-- .../panel/graph/time_region_manager.ts | 22 ++++++++++--------- .../panel/graph2/getGraphSeriesModel.ts | 4 ++-- .../app/plugins/panel/heatmap/color_scale.ts | 2 +- .../app/plugins/panel/heatmap/heatmap_ctrl.ts | 2 +- .../panel/heatmap/heatmap_data_converter.ts | 2 +- public/app/plugins/panel/table/module.ts | 4 +++- 14 files changed, 49 insertions(+), 41 deletions(-) diff --git a/packages/grafana-data/src/types/graph.ts b/packages/grafana-data/src/types/graph.ts index adfc9fd49ab..91604f5ae29 100644 --- a/packages/grafana-data/src/types/graph.ts +++ b/packages/grafana-data/src/types/graph.ts @@ -11,7 +11,7 @@ export type GraphSeriesValue = number | null; /** View model projection of a series */ export interface GraphSeriesXY { - color: string; + color?: string; data: GraphSeriesValue[][]; // [x,y][] isVisible: boolean; label: string; diff --git a/packages/grafana-data/src/utils/series.ts b/packages/grafana-data/src/utils/series.ts index cbc0a1308fa..38937b9a95c 100644 --- a/packages/grafana-data/src/utils/series.ts +++ b/packages/grafana-data/src/utils/series.ts @@ -4,9 +4,10 @@ import { Field } from '../types/dataFrame'; * Returns minimal time step from series time field * @param timeField */ -export const getSeriesTimeStep = (timeField: Field) => { - let previousTime; - let minTimeStep; +export const getSeriesTimeStep = (timeField: Field): number => { + let previousTime: number | undefined; + let minTimeStep: number | undefined; + let returnTimeStep = Number.MAX_VALUE; for (let i = 0; i < timeField.values.length; i++) { const currentTime = timeField.values.get(i); @@ -15,16 +16,16 @@ export const getSeriesTimeStep = (timeField: Field) => { const timeStep = currentTime - previousTime; if (minTimeStep === undefined) { - minTimeStep = timeStep; + returnTimeStep = timeStep; } - if (timeStep < minTimeStep) { - minTimeStep = timeStep; + if (timeStep < returnTimeStep) { + returnTimeStep = timeStep; } } previousTime = currentTime; } - return minTimeStep; + return returnTimeStep; }; /** diff --git a/packages/grafana-ui/src/components/Graph/GraphWithLegend.tsx b/packages/grafana-ui/src/components/Graph/GraphWithLegend.tsx index c4a6aad7d48..4dfc924eb44 100644 --- a/packages/grafana-ui/src/components/Graph/GraphWithLegend.tsx +++ b/packages/grafana-ui/src/components/Graph/GraphWithLegend.tsx @@ -82,7 +82,7 @@ export const GraphWithLegend: React.FunctionComponent = (p : acc.concat([ { label: s.label, - color: s.color, + color: s.color || '', isVisible: s.isVisible, yAxis: s.yAxis.index, displayValues: s.info || [], diff --git a/public/app/core/time_series2.ts b/public/app/core/time_series2.ts index 1452863b84f..aa06d534db0 100644 --- a/public/app/core/time_series2.ts +++ b/public/app/core/time_series2.ts @@ -91,7 +91,7 @@ export default class TimeSeries { label: string; alias: string; aliasEscaped: string; - color: string; + color?: string; valueFormater: any; stats: any; legend: boolean; diff --git a/public/app/core/utils/richHistory.test.ts b/public/app/core/utils/richHistory.test.ts index a8da4776ce4..a76cb2d3b26 100644 --- a/public/app/core/utils/richHistory.test.ts +++ b/public/app/core/utils/richHistory.test.ts @@ -11,6 +11,7 @@ import { } from './richHistory'; import store from 'app/core/store'; import { SortOrder } from './explore'; +import { dateTime } from '@grafana/data'; const mock: any = { history: [ @@ -164,6 +165,8 @@ describe('createDateStringFromTs', () => { describe('createQueryHeading', () => { it('should correctly create heading for queries when sort order is ascending ', () => { + // Have to offset the timezone of a 1 microsecond epoch, and then reverse the changes + mock.history[0].ts = 1 + -1 * dateTime().utcOffset() * 60 * 1000; const heading = createQueryHeading(mock.history[0], SortOrder.Ascending); expect(heading).toEqual('January 1'); }); diff --git a/public/app/features/explore/utils/ResultProcessor.test.ts b/public/app/features/explore/utils/ResultProcessor.test.ts index 36339400f8e..7b8ce0d64aa 100644 --- a/public/app/features/explore/utils/ResultProcessor.test.ts +++ b/public/app/features/explore/utils/ResultProcessor.test.ts @@ -113,7 +113,7 @@ describe('ResultProcessor', () => { [200, 5], [300, 6], ], - info: undefined, + info: [], isVisible: true, yAxis: { index: 1, @@ -234,7 +234,7 @@ describe('ResultProcessor', () => { [200, 5], [300, 6], ], - info: undefined, + info: [], isVisible: true, yAxis: { index: 1, diff --git a/public/app/plugins/panel/graph/align_yaxes.ts b/public/app/plugins/panel/graph/align_yaxes.ts index 8e619d14807..5a36bb9c4cf 100644 --- a/public/app/plugins/panel/graph/align_yaxes.ts +++ b/public/app/plugins/panel/graph/align_yaxes.ts @@ -105,25 +105,25 @@ interface AxisSide { min: number; } -function checkCorrectAxis(axis: any[]) { +function checkCorrectAxis(axis: any[]): boolean { return axis.length === 2 && checkCorrectAxes(axis[0]) && checkCorrectAxes(axis[1]); } -function checkCorrectAxes(axes: any) { +function checkCorrectAxes(axes: any): boolean { return 'min' in axes && 'max' in axes; } -function checkOneSide(yLeft: AxisSide, yRight: AxisSide) { +function checkOneSide(yLeft: AxisSide, yRight: AxisSide): boolean { // on the one hand with respect to zero return (yLeft.min >= 0 && yRight.min >= 0) || (yLeft.max <= 0 && yRight.max <= 0); } -function checkTwoCross(yLeft: AxisSide, yRight: AxisSide) { +function checkTwoCross(yLeft: AxisSide, yRight: AxisSide): boolean { // both across zero return yLeft.min <= 0 && yLeft.max >= 0 && yRight.min <= 0 && yRight.max >= 0; } -function checkOppositeSides(yLeft: AxisSide, yRight: AxisSide) { +function checkOppositeSides(yLeft: AxisSide, yRight: AxisSide): boolean { // on the opposite sides with respect to zero return (yLeft.min >= 0 && yRight.max <= 0) || (yLeft.max <= 0 && yRight.min >= 0); } @@ -141,13 +141,13 @@ function getRate(yLeft: AxisSide, yRight: AxisSide): number { const absLeftMax = Math.abs(yLeft.max); const absRightMin = Math.abs(yRight.min); const absRightMax = Math.abs(yRight.max); - const upLeft = _.max([absLeftMin, absLeftMax]); - const downLeft = _.min([absLeftMin, absLeftMax]); - const upRight = _.max([absRightMin, absRightMax]); - const downRight = _.min([absRightMin, absRightMax]); + const upLeft = Math.max(absLeftMin, absLeftMax); + const downLeft = Math.min(absLeftMin, absLeftMax); + const upRight = Math.max(absRightMin, absRightMax); + const downRight = Math.min(absRightMin, absRightMax); - const rateLeft = downLeft ? upLeft / downLeft : upLeft; - const rateRight = downRight ? upRight / downRight : upRight; + const rateLeft = downLeft !== 0 ? upLeft / downLeft : upLeft; + const rateRight = downRight !== 0 ? upRight / downRight : upRight; return rateLeft > rateRight ? rateLeft : rateRight; } diff --git a/public/app/plugins/panel/graph/graph.ts b/public/app/plugins/panel/graph/graph.ts index be1e36d9484..f6c6cd98a7e 100644 --- a/public/app/plugins/panel/graph/graph.ts +++ b/public/app/plugins/panel/graph/graph.ts @@ -249,7 +249,7 @@ class GraphElement { return; } else { this.tooltip.clear(this.plot); - let linksSupplier: LinkModelSupplier; + let linksSupplier: LinkModelSupplier | undefined; if (item) { // pickup y-axis index to know which field's config to apply @@ -258,7 +258,7 @@ class GraphElement { const field = dataFrame.fields[item.series.fieldIndex]; const dataIndex = this.getDataIndexWithNullValuesCorrection(item, dataFrame); - let links = this.panel.options.dataLinks || []; + let links: any[] = this.panel.options.dataLinks || []; if (field.config.links && field.config.links.length) { // Append the configured links to the panel datalinks links = [...links, ...field.config.links]; diff --git a/public/app/plugins/panel/graph/time_region_manager.ts b/public/app/plugins/panel/graph/time_region_manager.ts index 8181fbf006e..31eb4ce7814 100644 --- a/public/app/plugins/panel/graph/time_region_manager.ts +++ b/public/app/plugins/panel/graph/time_region_manager.ts @@ -170,17 +170,19 @@ export class TimeRegionManager { fromEnd = dateTime(fromStart); - if (hRange.from.h <= hRange.to.h) { - fromEnd.add(hRange.to.h - hRange.from.h, 'hours'); - } else if (hRange.from.h > hRange.to.h) { - while (fromEnd.hour() !== hRange.to.h) { - fromEnd.add(1, 'hours'); - } - } else { - fromEnd.add(24 - hRange.from.h, 'hours'); + if (fromEnd.hour) { + if (hRange.from.h <= hRange.to.h) { + fromEnd.add(hRange.to.h - hRange.from.h, 'hours'); + } else if (hRange.from.h > hRange.to.h) { + while (fromEnd.hour() !== hRange.to.h) { + fromEnd.add(1, 'hours'); + } + } else { + fromEnd.add(24 - hRange.from.h, 'hours'); - while (fromEnd.hour() !== hRange.to.h) { - fromEnd.add(1, 'hours'); + while (fromEnd.hour() !== hRange.to.h) { + fromEnd.add(1, 'hours'); + } } } diff --git a/public/app/plugins/panel/graph2/getGraphSeriesModel.ts b/public/app/plugins/panel/graph2/getGraphSeriesModel.ts index 8bc63245a72..c690139b7c5 100644 --- a/public/app/plugins/panel/graph2/getGraphSeriesModel.ts +++ b/public/app/plugins/panel/graph2/getGraphSeriesModel.ts @@ -62,8 +62,8 @@ export const getGraphSeriesModel = ( }); if (points.length > 0) { - const seriesStats = reduceField({ field, reducers: legendOptions.stats }); - let statsDisplayValues: DisplayValue[]; + const seriesStats = reduceField({ field, reducers: legendOptions.stats || [] }); + let statsDisplayValues: DisplayValue[] = []; if (legendOptions.stats) { statsDisplayValues = legendOptions.stats.map(stat => { diff --git a/public/app/plugins/panel/heatmap/color_scale.ts b/public/app/plugins/panel/heatmap/color_scale.ts index 368e724883c..76296e6fef9 100644 --- a/public/app/plugins/panel/heatmap/color_scale.ts +++ b/public/app/plugins/panel/heatmap/color_scale.ts @@ -16,7 +16,7 @@ export function getOpacityScale( options: { cardColor?: null; colorScale?: any; exponent?: any }, maxValue: number, minValue = 0 -) { +): any { let legendOpacityScale; if (options.colorScale === 'linear') { legendOpacityScale = d3 diff --git a/public/app/plugins/panel/heatmap/heatmap_ctrl.ts b/public/app/plugins/panel/heatmap/heatmap_ctrl.ts index d67a4fd9207..f5c25092ec4 100644 --- a/public/app/plugins/panel/heatmap/heatmap_ctrl.ts +++ b/public/app/plugins/panel/heatmap/heatmap_ctrl.ts @@ -296,7 +296,7 @@ export class HeatmapCtrl extends MetricsPanelCtrl { // Directly support DataFrame onDataFramesReceived(data: DataFrame[]) { this.series = this.processor.getSeriesList({ dataList: data, range: this.range }).map(ts => { - ts.color = null; // remove whatever the processor set + ts.color = undefined; // remove whatever the processor set ts.flotpairs = ts.getFlotPairs(this.panel.nullPointMode); return ts; }); diff --git a/public/app/plugins/panel/heatmap/heatmap_data_converter.ts b/public/app/plugins/panel/heatmap/heatmap_data_converter.ts index 1db4a5ab98a..c4d4c60fa0a 100644 --- a/public/app/plugins/panel/heatmap/heatmap_data_converter.ts +++ b/public/app/plugins/panel/heatmap/heatmap_data_converter.ts @@ -275,7 +275,7 @@ function pushToYBuckets( } if (buckets[bucketNum]) { buckets[bucketNum].values.push(value); - buckets[bucketNum].points.push(point); + buckets[bucketNum].points?.push(point); buckets[bucketNum].count += count; } else { buckets[bucketNum] = { diff --git a/public/app/plugins/panel/table/module.ts b/public/app/plugins/panel/table/module.ts index 067ee57ad6b..4354fd17129 100644 --- a/public/app/plugins/panel/table/module.ts +++ b/public/app/plugins/panel/table/module.ts @@ -9,6 +9,8 @@ import { TableRenderer } from './renderer'; import { isTableData, PanelEvents, PanelPlugin } from '@grafana/data'; import { TemplateSrv } from 'app/features/templating/template_srv'; import { dispatch } from 'app/store/store'; +import { ComponentType } from 'react'; +import { PanelProps } from '@grafana/data'; import { applyFilterFromTable } from 'app/features/variables/adhoc/actions'; export class TablePanelCtrl extends MetricsPanelCtrl { @@ -268,6 +270,6 @@ export class TablePanelCtrl extends MetricsPanelCtrl { } } -export const plugin = new PanelPlugin(null); +export const plugin = new PanelPlugin((null as unknown) as ComponentType>); plugin.angularPanelCtrl = TablePanelCtrl; plugin.setNoPadding();