Skip to content

Commit

Permalink
StateTimeline: add spanNulls editor (grafana#44811)
Browse files Browse the repository at this point in the history
  • Loading branch information
leeoniya authored Feb 4, 2022
1 parent 935059a commit b07345e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
10 changes: 4 additions & 6 deletions packages/grafana-ui/src/components/GraphNG/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@ function applySpanNullsThresholds(frame: DataFrame) {
continue;
}

if (field.type === FieldType.number) {
let spanNulls = field.config.custom?.spanNulls;
let spanNulls = field.config.custom?.spanNulls;

if (typeof spanNulls === 'number') {
if (spanNulls !== -1) {
field.values = new ArrayVector(nullToUndefThreshold(refValues, field.values.toArray(), spanNulls));
}
if (typeof spanNulls === 'number') {
if (spanNulls !== -1) {
field.values = new ArrayVector(nullToUndefThreshold(refValues, field.values.toArray(), spanNulls));
}
}
}
Expand Down
19 changes: 18 additions & 1 deletion public/app/plugins/panel/state-timeline/module.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import { FieldColorModeId, FieldConfigProperty, PanelPlugin } from '@grafana/data';
import {
FieldColorModeId,
FieldConfigProperty,
FieldType,
identityOverrideProcessor,
PanelPlugin,
} from '@grafana/data';
import { StateTimelinePanel } from './StateTimelinePanel';
import { TimelineOptions, TimelineFieldConfig, defaultPanelOptions, defaultTimelineFieldConfig } from './types';
import { VisibilityMode } from '@grafana/schema';
import { commonOptionsBuilder } from '@grafana/ui';
import { timelinePanelChangedHandler } from './migrations';
import { StatTimelineSuggestionsSupplier } from './suggestions';
import { SpanNullsEditor } from '../timeseries/SpanNullsEditor';

export const plugin = new PanelPlugin<TimelineOptions, TimelineFieldConfig>(StateTimelinePanel)
.setPanelChangeHandler(timelinePanelChangedHandler)
Expand Down Expand Up @@ -40,6 +47,16 @@ export const plugin = new PanelPlugin<TimelineOptions, TimelineFieldConfig>(Stat
max: 100,
step: 1,
},
})
.addCustomEditor<void, boolean>({
id: 'spanNulls',
path: 'spanNulls',
name: 'Connect null values',
defaultValue: false,
editor: SpanNullsEditor,
override: SpanNullsEditor,
shouldApply: (f) => f.type !== FieldType.time,
process: identityOverrideProcessor,
});
},
})
Expand Down
15 changes: 11 additions & 4 deletions public/app/plugins/panel/state-timeline/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,15 @@ export function unsetSameFutureValues(values: any[]): any[] | undefined {
return clone;
}

function getSpanNulls(field: Field) {
let spanNulls = field.config.custom?.spanNulls;

// magic value for join() to leave nulls alone instead of expanding null ranges
// should be set to -1 when spanNulls = null|undefined|false|0, which is "retain nulls, without expanding"
// Infinity is not optimal here since it causes spanNulls to be more expensive than simply removing all nulls unconditionally
return !spanNulls ? -1 : spanNulls === true ? Infinity : spanNulls;
}

/**
* Merge values by the threshold
*/
Expand Down Expand Up @@ -359,8 +368,7 @@ export function mergeThresholdValues(field: Field, theme: GrafanaTheme2): Field
...field.config,
custom: {
...field.config.custom,
// magic value for join() to leave nulls alone
spanNulls: -1,
spanNulls: getSpanNulls(field),
},
},
type: FieldType.string,
Expand Down Expand Up @@ -413,8 +421,7 @@ export function prepareTimelineFields(
...field.config,
custom: {
...field.config.custom,
// magic value for join() to leave nulls alone
spanNulls: -1,
spanNulls: getSpanNulls(field),
},
},
};
Expand Down

0 comments on commit b07345e

Please sign in to comment.