2022-04-22 14:33:13 +01:00
|
|
|
import { isNumber } from 'lodash';
|
2019-03-28 06:57:49 -07:00
|
|
|
import React, { PureComponent } from 'react';
|
2022-04-22 14:33:13 +01:00
|
|
|
|
2019-12-01 17:02:44 +01:00
|
|
|
import {
|
2020-05-05 15:26:42 +02:00
|
|
|
DisplayValueAlignmentFactors,
|
2019-12-01 17:02:44 +01:00
|
|
|
FieldDisplay,
|
2021-10-07 03:48:49 -05:00
|
|
|
FieldType,
|
2019-12-01 17:02:44 +01:00
|
|
|
getDisplayValueAlignmentFactors,
|
2020-05-05 15:26:42 +02:00
|
|
|
getFieldDisplayValues,
|
2021-10-07 03:48:49 -05:00
|
|
|
NumericRange,
|
2020-05-05 15:26:42 +02:00
|
|
|
PanelProps,
|
2019-12-01 17:02:44 +01:00
|
|
|
} from '@grafana/data';
|
2022-04-22 14:33:13 +01:00
|
|
|
import { findNumericFieldMinMax } from '@grafana/data/src/field/fieldOverrides';
|
2022-05-25 12:07:32 +01:00
|
|
|
import { BigValueTextMode, BigValueGraphMode } from '@grafana/schema';
|
|
|
|
|
import { BigValue, DataLinksContextMenu, VizRepeater, VizRepeaterRenderValueProps } from '@grafana/ui';
|
2022-04-22 14:33:13 +01:00
|
|
|
import { DataLinksContextMenuApi } from '@grafana/ui/src/components/DataLinks/DataLinksContextMenu';
|
2020-05-05 15:26:42 +02:00
|
|
|
import { config } from 'app/core/config';
|
2022-04-22 14:33:13 +01:00
|
|
|
|
2023-05-15 23:07:54 -04:00
|
|
|
import { Options } from './panelcfg.gen';
|
2020-05-05 15:26:42 +02:00
|
|
|
|
2023-05-15 23:07:54 -04:00
|
|
|
export class StatPanel extends PureComponent<PanelProps<Options>> {
|
2020-05-05 15:26:42 +02:00
|
|
|
renderComponent = (
|
|
|
|
|
valueProps: VizRepeaterRenderValueProps<FieldDisplay, DisplayValueAlignmentFactors>,
|
|
|
|
|
menuProps: DataLinksContextMenuApi
|
|
|
|
|
): JSX.Element => {
|
2019-10-04 12:01:42 +02:00
|
|
|
const { timeRange, options } = this.props;
|
2022-09-16 17:53:09 +01:00
|
|
|
const { value, alignmentFactors, width, height, count, orientation } = valueProps;
|
2020-05-05 15:26:42 +02:00
|
|
|
const { openMenu, targetClassName } = menuProps;
|
2020-12-08 08:13:12 -08:00
|
|
|
let sparkline = value.sparkline;
|
|
|
|
|
if (sparkline) {
|
|
|
|
|
sparkline.timeRange = timeRange;
|
2019-05-04 01:08:48 -07:00
|
|
|
}
|
2019-03-20 09:07:11 -07:00
|
|
|
|
2019-08-27 23:50:43 -07:00
|
|
|
return (
|
2020-05-05 15:26:42 +02:00
|
|
|
<BigValue
|
|
|
|
|
value={value.display}
|
2020-07-07 10:16:06 +02:00
|
|
|
count={count}
|
2020-05-05 15:26:42 +02:00
|
|
|
sparkline={sparkline}
|
|
|
|
|
colorMode={options.colorMode}
|
|
|
|
|
graphMode={options.graphMode}
|
|
|
|
|
justifyMode={options.justifyMode}
|
2020-07-28 07:28:50 +02:00
|
|
|
textMode={this.getTextMode()}
|
2020-05-05 15:26:42 +02:00
|
|
|
alignmentFactors={alignmentFactors}
|
2022-09-16 17:53:09 +01:00
|
|
|
parentOrientation={orientation}
|
2020-12-04 10:03:59 -08:00
|
|
|
text={options.text}
|
2020-05-05 15:26:42 +02:00
|
|
|
width={width}
|
|
|
|
|
height={height}
|
2021-04-29 12:44:06 +02:00
|
|
|
theme={config.theme2}
|
2020-05-05 15:26:42 +02:00
|
|
|
onClick={openMenu}
|
|
|
|
|
className={targetClassName}
|
2023-10-03 20:10:51 +02:00
|
|
|
disableWideLayout={true}
|
2020-05-05 15:26:42 +02:00
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
};
|
2020-07-28 07:28:50 +02:00
|
|
|
|
|
|
|
|
getTextMode() {
|
|
|
|
|
const { options, fieldConfig, title } = this.props;
|
|
|
|
|
|
|
|
|
|
// If we have manually set displayName or panel title switch text mode to value and name
|
|
|
|
|
if (options.textMode === BigValueTextMode.Auto && (fieldConfig.defaults.displayName || !title)) {
|
|
|
|
|
return BigValueTextMode.ValueAndName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return options.textMode;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-05 15:26:42 +02:00
|
|
|
renderValue = (valueProps: VizRepeaterRenderValueProps<FieldDisplay, DisplayValueAlignmentFactors>): JSX.Element => {
|
|
|
|
|
const { value } = valueProps;
|
|
|
|
|
const { getLinks, hasLinks } = value;
|
|
|
|
|
|
2020-06-17 04:26:01 -06:00
|
|
|
if (hasLinks && getLinks) {
|
|
|
|
|
return (
|
2022-06-27 14:23:29 +02:00
|
|
|
<DataLinksContextMenu links={getLinks}>
|
2021-01-20 07:59:48 +01:00
|
|
|
{(api) => {
|
2020-06-17 04:26:01 -06:00
|
|
|
return this.renderComponent(valueProps, api);
|
|
|
|
|
}}
|
|
|
|
|
</DataLinksContextMenu>
|
|
|
|
|
);
|
2020-05-05 15:26:42 +02:00
|
|
|
}
|
|
|
|
|
|
2020-06-17 04:26:01 -06:00
|
|
|
return this.renderComponent(valueProps, {});
|
2019-05-04 01:08:48 -07:00
|
|
|
};
|
2019-03-28 06:57:49 -07:00
|
|
|
|
2019-05-04 01:08:48 -07:00
|
|
|
getValues = (): FieldDisplay[] => {
|
2020-04-27 15:28:06 +02:00
|
|
|
const { data, options, replaceVariables, fieldConfig, timeZone } = this.props;
|
2019-10-04 12:01:42 +02:00
|
|
|
|
2021-10-07 03:48:49 -05:00
|
|
|
let globalRange: NumericRange | undefined = undefined;
|
|
|
|
|
|
|
|
|
|
for (let frame of data.series) {
|
|
|
|
|
for (let field of frame.fields) {
|
|
|
|
|
let { config } = field;
|
|
|
|
|
// mostly copied from fieldOverrides, since they are skipped during streaming
|
|
|
|
|
// Set the Min/Max value automatically
|
|
|
|
|
if (field.type === FieldType.number) {
|
|
|
|
|
if (field.state?.range) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (!globalRange && (!isNumber(config.min) || !isNumber(config.max))) {
|
|
|
|
|
globalRange = findNumericFieldMinMax(data.series);
|
|
|
|
|
}
|
|
|
|
|
const min = config.min ?? globalRange!.min;
|
|
|
|
|
const max = config.max ?? globalRange!.max;
|
|
|
|
|
field.state = field.state ?? {};
|
|
|
|
|
field.state.range = { min, max, delta: max! - min! };
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-04 01:08:48 -07:00
|
|
|
return getFieldDisplayValues({
|
2020-03-19 11:50:31 +01:00
|
|
|
fieldConfig,
|
2020-03-28 23:11:50 +01:00
|
|
|
reduceOptions: options.reduceOptions,
|
2019-05-04 01:08:48 -07:00
|
|
|
replaceVariables,
|
2021-04-29 12:44:06 +02:00
|
|
|
theme: config.theme2,
|
2019-05-04 01:08:48 -07:00
|
|
|
data: data.series,
|
2019-12-01 17:02:44 +01:00
|
|
|
sparkline: options.graphMode !== BigValueGraphMode.None,
|
2020-04-27 15:28:06 +02:00
|
|
|
timeZone,
|
2019-03-22 15:25:33 +01:00
|
|
|
});
|
2019-03-14 14:47:01 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
render() {
|
2019-12-01 17:02:44 +01:00
|
|
|
const { height, options, width, data, renderCounter } = this.props;
|
|
|
|
|
|
2019-03-14 14:47:01 -07:00
|
|
|
return (
|
2019-04-05 12:59:29 +02:00
|
|
|
<VizRepeater
|
|
|
|
|
getValues={this.getValues}
|
2019-12-01 17:02:44 +01:00
|
|
|
getAlignmentFactors={getDisplayValueAlignmentFactors}
|
2019-03-14 14:47:01 -07:00
|
|
|
renderValue={this.renderValue}
|
|
|
|
|
width={width}
|
|
|
|
|
height={height}
|
2019-03-15 15:37:56 -07:00
|
|
|
source={data}
|
2020-04-06 08:22:49 +02:00
|
|
|
itemSpacing={3}
|
2019-03-19 14:07:48 +01:00
|
|
|
renderCounter={renderCounter}
|
2020-04-04 16:57:06 +02:00
|
|
|
autoGrid={true}
|
|
|
|
|
orientation={options.orientation}
|
2019-03-14 14:47:01 -07:00
|
|
|
/>
|
|
|
|
|
);
|
2019-03-14 13:20:24 -07:00
|
|
|
}
|
|
|
|
|
}
|