Commit c0762b6d by Ryan McKinley Committed by GitHub

Chore: reduce null check errors/warnigns (#25223)

* fix a few null errors

* more

* flip logic

* merge master

* more

* fewer changes

* processor

Co-authored-by: Torkel Ödegaard <torkel@grafana.com>
parent 26852ca7
...@@ -53,15 +53,15 @@ export class DataFrameView<T = any> extends FunctionalVector<T> { ...@@ -53,15 +53,15 @@ export class DataFrameView<T = any> extends FunctionalVector<T> {
* Helper function to return the {@link DisplayProcessor} for a given field column. * Helper function to return the {@link DisplayProcessor} for a given field column.
* @param colIndex - the field column index for the data frame. * @param colIndex - the field column index for the data frame.
*/ */
getFieldDisplayProcessor(colIndex: number): DisplayProcessor | null { getFieldDisplayProcessor(colIndex: number): DisplayProcessor | undefined {
if (!this.dataFrame || !this.dataFrame.fields) { if (!this.dataFrame || !this.dataFrame.fields) {
return null; return undefined;
} }
const field = this.dataFrame.fields[colIndex]; const field = this.dataFrame.fields[colIndex];
if (!field || !field.display) { if (!field || !field.display) {
return null; return undefined;
} }
return field.display; return field.display;
......
...@@ -40,9 +40,9 @@ export class MixedDatasource extends DataSourceApi<DataQuery> { ...@@ -40,9 +40,9 @@ export class MixedDatasource extends DataSourceApi<DataQuery> {
const mixed: BatchedQueries[] = []; const mixed: BatchedQueries[] = [];
for (const key in sets) { for (const key in sets) {
const targets = sets[key]; const targets = sets[key];
const dsName = targets[0].datasource; const dsName: string | undefined = targets[0].datasource;
mixed.push({ mixed.push({
datasource: getDataSourceSrv().get(dsName), datasource: getDataSourceSrv().get(dsName, request.scopedVars),
targets, targets,
}); });
} }
......
...@@ -90,10 +90,10 @@ class AlertListPanel extends PanelCtrl { ...@@ -90,10 +90,10 @@ class AlertListPanel extends PanelCtrl {
if (this.panel.show === 'current') { if (this.panel.show === 'current') {
getAlertsPromise = this.getCurrentAlertState(); getAlertsPromise = this.getCurrentAlertState();
} } else if (this.panel.show === 'changes') {
if (this.panel.show === 'changes') {
getAlertsPromise = this.getStateChanges(); getAlertsPromise = this.getStateChanges();
} else {
getAlertsPromise = Promise.resolve();
} }
getAlertsPromise.then(() => { getAlertsPromise.then(() => {
......
...@@ -4,20 +4,17 @@ import React, { PureComponent } from 'react'; ...@@ -4,20 +4,17 @@ import React, { PureComponent } from 'react';
import { AnnoOptions } from './types'; import { AnnoOptions } from './types';
import { AnnotationEvent, AppEvents, dateTime, DurationUnit, PanelProps } from '@grafana/data'; import { AnnotationEvent, AppEvents, dateTime, DurationUnit, PanelProps } from '@grafana/data';
import { Tooltip } from '@grafana/ui'; import { Tooltip } from '@grafana/ui';
import { getBackendSrv } from '@grafana/runtime'; import { getBackendSrv, getLocationSrv } from '@grafana/runtime';
import { AbstractList } from '@grafana/ui/src/components/List/AbstractList'; import { AbstractList } from '@grafana/ui/src/components/List/AbstractList';
import { TagBadge } from 'app/core/components/TagFilter/TagBadge'; import { TagBadge } from 'app/core/components/TagFilter/TagBadge';
import { getDashboardSrv } from 'app/features/dashboard/services/DashboardSrv'; import { getDashboardSrv } from 'app/features/dashboard/services/DashboardSrv';
import appEvents from 'app/core/app_events'; import appEvents from 'app/core/app_events';
import { updateLocation } from 'app/core/actions';
import { store } from 'app/store/store';
import { css, cx } from 'emotion'; import { css, cx } from 'emotion';
interface UserInfo { interface UserInfo {
id: number; id?: number;
login: string; login?: string;
email: string; email?: string;
} }
interface Props extends PanelProps<AnnoOptions> {} interface Props extends PanelProps<AnnoOptions> {}
...@@ -108,6 +105,10 @@ export class AnnoListPanel extends PureComponent<Props, State> { ...@@ -108,6 +105,10 @@ export class AnnoListPanel extends PureComponent<Props, State> {
onAnnoClick = (e: React.SyntheticEvent, anno: AnnotationEvent) => { onAnnoClick = (e: React.SyntheticEvent, anno: AnnotationEvent) => {
e.stopPropagation(); e.stopPropagation();
if (!anno.time) {
return;
}
const { options } = this.props; const { options } = this.props;
const dashboardSrv = getDashboardSrv(); const dashboardSrv = getDashboardSrv();
const current = dashboardSrv.getCurrent(); const current = dashboardSrv.getCurrent();
...@@ -122,12 +123,10 @@ export class AnnoListPanel extends PureComponent<Props, State> { ...@@ -122,12 +123,10 @@ export class AnnoListPanel extends PureComponent<Props, State> {
} }
if (current.id === anno.dashboardId) { if (current.id === anno.dashboardId) {
store.dispatch( getLocationSrv().update({
updateLocation({
query: params, query: params,
partial: true, partial: true,
}) });
);
return; return;
} }
...@@ -136,12 +135,10 @@ export class AnnoListPanel extends PureComponent<Props, State> { ...@@ -136,12 +135,10 @@ export class AnnoListPanel extends PureComponent<Props, State> {
.then((res: any[]) => { .then((res: any[]) => {
if (res && res.length && res[0].id === anno.dashboardId) { if (res && res.length && res[0].id === anno.dashboardId) {
const dash = res[0]; const dash = res[0];
store.dispatch( getLocationSrv().update({
updateLocation({
query: params, query: params,
path: dash.url, path: dash.url,
}) });
);
return; return;
} }
appEvents.emit(AppEvents.alertWarning, ['Unknown Dashboard: ' + anno.dashboardId]); appEvents.emit(AppEvents.alertWarning, ['Unknown Dashboard: ' + anno.dashboardId]);
...@@ -164,7 +161,7 @@ export class AnnoListPanel extends PureComponent<Props, State> { ...@@ -164,7 +161,7 @@ export class AnnoListPanel extends PureComponent<Props, State> {
return t.add(incr, unit as DurationUnit).valueOf(); return t.add(incr, unit as DurationUnit).valueOf();
} }
onTagClick = (e: React.SyntheticEvent, tag: string, remove: boolean) => { onTagClick = (e: React.SyntheticEvent, tag: string, remove?: boolean) => {
e.stopPropagation(); e.stopPropagation();
const queryTags = remove ? this.state.queryTags.filter(item => item !== tag) : [...this.state.queryTags, tag]; const queryTags = remove ? this.state.queryTags.filter(item => item !== tag) : [...this.state.queryTags, tag];
...@@ -188,7 +185,7 @@ export class AnnoListPanel extends PureComponent<Props, State> { ...@@ -188,7 +185,7 @@ export class AnnoListPanel extends PureComponent<Props, State> {
}); });
}; };
renderTags = (tags: string[], remove: boolean): JSX.Element | null => { renderTags = (tags?: string[], remove?: boolean): JSX.Element | null => {
if (!tags || !tags.length) { if (!tags || !tags.length) {
return null; return null;
} }
...@@ -197,7 +194,7 @@ export class AnnoListPanel extends PureComponent<Props, State> { ...@@ -197,7 +194,7 @@ export class AnnoListPanel extends PureComponent<Props, State> {
{tags.map(tag => { {tags.map(tag => {
return ( return (
<span key={tag} onClick={e => this.onTagClick(e, tag, remove)} className="pointer"> <span key={tag} onClick={e => this.onTagClick(e, tag, remove)} className="pointer">
<TagBadge label={tag} removeIcon={remove} count={0} /> <TagBadge label={tag} removeIcon={!!remove} count={0} />
</span> </span>
); );
})} })}
...@@ -251,7 +248,9 @@ export class AnnoListPanel extends PureComponent<Props, State> { ...@@ -251,7 +248,9 @@ export class AnnoListPanel extends PureComponent<Props, State> {
{showTags && this.renderTags(anno.tags, false)} {showTags && this.renderTags(anno.tags, false)}
</span> </span>
<span className="pluginlist-version">{showTime && <span>{dashboard.formatDate(anno.time)}</span>}</span> <span className="pluginlist-version">
{showTime && anno.time && <span>{dashboard.formatDate(anno.time)}</span>}
</span>
</span> </span>
</div> </div>
); );
......
...@@ -6,6 +6,7 @@ import { ...@@ -6,6 +6,7 @@ import {
getFieldDisplayValues, getFieldDisplayValues,
PanelProps, PanelProps,
FieldConfig, FieldConfig,
DisplayProcessor,
DisplayValue, DisplayValue,
} from '@grafana/data'; } from '@grafana/data';
import { BarGauge, DataLinksContextMenu, VizRepeater, VizRepeaterRenderValueProps } from '@grafana/ui'; import { BarGauge, DataLinksContextMenu, VizRepeater, VizRepeaterRenderValueProps } from '@grafana/ui';
...@@ -13,6 +14,7 @@ import { BarGauge, DataLinksContextMenu, VizRepeater, VizRepeaterRenderValueProp ...@@ -13,6 +14,7 @@ import { BarGauge, DataLinksContextMenu, VizRepeater, VizRepeaterRenderValueProp
import { config } from 'app/core/config'; import { config } from 'app/core/config';
import { BarGaugeOptions } from './types'; import { BarGaugeOptions } from './types';
import { DataLinksContextMenuApi } from '@grafana/ui/src/components/DataLinks/DataLinksContextMenu'; import { DataLinksContextMenuApi } from '@grafana/ui/src/components/DataLinks/DataLinksContextMenu';
import { isNumber } from 'lodash';
export class BarGaugePanel extends PureComponent<PanelProps<BarGaugeOptions>> { export class BarGaugePanel extends PureComponent<PanelProps<BarGaugeOptions>> {
renderComponent = ( renderComponent = (
...@@ -24,6 +26,11 @@ export class BarGaugePanel extends PureComponent<PanelProps<BarGaugeOptions>> { ...@@ -24,6 +26,11 @@ export class BarGaugePanel extends PureComponent<PanelProps<BarGaugeOptions>> {
const { field, display, view, colIndex } = value; const { field, display, view, colIndex } = value;
const { openMenu, targetClassName } = menuProps; const { openMenu, targetClassName } = menuProps;
let processor: DisplayProcessor | undefined = undefined;
if (view && isNumber(colIndex)) {
processor = view!.getFieldDisplayProcessor(colIndex as number);
}
return ( return (
<BarGauge <BarGauge
value={clearNameForSingleSeries(count, field, display)} value={clearNameForSingleSeries(count, field, display)}
...@@ -31,7 +38,7 @@ export class BarGaugePanel extends PureComponent<PanelProps<BarGaugeOptions>> { ...@@ -31,7 +38,7 @@ export class BarGaugePanel extends PureComponent<PanelProps<BarGaugeOptions>> {
height={height} height={height}
orientation={orientation} orientation={orientation}
field={field} field={field}
display={view?.getFieldDisplayProcessor(colIndex)} display={processor}
theme={config.theme} theme={config.theme}
itemSpacing={this.getItemSpacing()} itemSpacing={this.getItemSpacing()}
displayMode={options.displayMode} displayMode={options.displayMode}
......
...@@ -286,7 +286,8 @@ class LegendTable extends PureComponent<Partial<LegendComponentProps>> { ...@@ -286,7 +286,8 @@ class LegendTable extends PureComponent<Partial<LegendComponentProps>> {
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
{seriesList.map((series, i) => ( {seriesList &&
seriesList.map((series, i) => (
<LegendItem <LegendItem
key={`${series.id}-${i}`} key={`${series.id}-${i}`}
asTable={true} asTable={true}
......
...@@ -136,6 +136,8 @@ class LegendSeriesLabel extends PureComponent<LegendSeriesLabelProps & LegendSer ...@@ -136,6 +136,8 @@ class LegendSeriesLabel extends PureComponent<LegendSeriesLabelProps & LegendSer
render() { render() {
const { label, color, yaxis } = this.props; const { label, color, yaxis } = this.props;
const { onColorChange, onToggleAxis } = this.props; const { onColorChange, onToggleAxis } = this.props;
const onLabelClick = this.props.onLabelClick ? this.props.onLabelClick : () => {};
return [ return [
<LegendSeriesIcon <LegendSeriesIcon
key="icon" key="icon"
...@@ -148,7 +150,7 @@ class LegendSeriesLabel extends PureComponent<LegendSeriesLabelProps & LegendSer ...@@ -148,7 +150,7 @@ class LegendSeriesLabel extends PureComponent<LegendSeriesLabelProps & LegendSer
className="graph-legend-alias pointer" className="graph-legend-alias pointer"
title={label} title={label}
key="label" key="label"
onClick={e => this.props.onLabelClick(e)} onClick={onLabelClick}
aria-label={selectors.components.Panels.Visualization.Graph.Legend.legendItemAlias(label)} aria-label={selectors.components.Panels.Visualization.Graph.Legend.legendItemAlias(label)}
> >
{label} {label}
......
...@@ -405,8 +405,9 @@ class SingleStatCtrl extends MetricsPanelCtrl { ...@@ -405,8 +405,9 @@ class SingleStatCtrl extends MetricsPanelCtrl {
function addGauge() { function addGauge() {
const data: ShowData = ctrl.data; const data: ShowData = ctrl.data;
const width = elem.width(); const width = elem.width() || 10;
const height = elem.height(); const height = elem.height() || 10;
// Allow to use a bit more space for wide gauges // Allow to use a bit more space for wide gauges
const dimension = Math.min(width, height * 1.3); const dimension = Math.min(width, height * 1.3);
...@@ -501,13 +502,15 @@ class SingleStatCtrl extends MetricsPanelCtrl { ...@@ -501,13 +502,15 @@ class SingleStatCtrl extends MetricsPanelCtrl {
function addSparkline() { function addSparkline() {
const data: ShowData = ctrl.data; const data: ShowData = ctrl.data;
const width = elem.width(); const width = elem.width() || 30;
if (width && width < 30) { if (width && width < 30) {
// element has not gotten it's width yet // element has not gotten it's width yet
// delay sparkline render // delay sparkline render
setTimeout(addSparkline, 30); setTimeout(addSparkline, 30);
return; return;
} }
if (!data.sparkline || !data.sparkline.length) { if (!data.sparkline || !data.sparkline.length) {
// no sparkline data // no sparkline data
return; return;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment