|
|
|
@ -4,85 +4,140 @@ import { Series } from '../models/series';
|
|
|
|
|
import { BarSeries } from '../models/barSeries'; |
|
|
|
|
|
|
|
|
|
import { PanelOptions, Pod } from '../types'; |
|
|
|
|
|
|
|
|
|
import { DataProcessor } from '../grafana/data_processor'; |
|
|
|
|
import { formatValue, getGrafanaSeriesList, getLastMetricValue } from '../utils'; |
|
|
|
|
|
|
|
|
|
import { ChartwerkGaugePod } from '@chartwerk/gauge-pod'; |
|
|
|
|
import { ChartwerkBarPod } from '@chartwerk/bar-pod'; |
|
|
|
|
|
|
|
|
|
import { PanelData, TimeRange, PanelProps } from '@grafana/data'; |
|
|
|
|
import { PanelProps } from '@grafana/data'; |
|
|
|
|
import { VizLegend } from '@grafana/ui'; |
|
|
|
|
import { LegendDisplayMode } from '@grafana/schema'; |
|
|
|
|
|
|
|
|
|
import React, { useCallback, useRef } from 'react'; |
|
|
|
|
import React, { useRef, useEffect, useMemo } from 'react'; |
|
|
|
|
import { css } from 'emotion'; |
|
|
|
|
import * as _ from 'lodash'; |
|
|
|
|
|
|
|
|
|
interface Props extends PanelProps<PanelOptions> {} |
|
|
|
|
|
|
|
|
|
export function Panel({ options, data, width, height, timeRange, onChangeTimeRange, replaceVariables }: Props) { |
|
|
|
|
const grafanaSeriesList = getGrafanaSeriesList(data, timeRange); |
|
|
|
|
let chartContainer = useRef(null); |
|
|
|
|
const grafanaSeriesList = useMemo(() => getGrafanaSeriesList(data, timeRange), [data, timeRange]); |
|
|
|
|
const podContainerRef = useRef<HTMLDivElement>(null); |
|
|
|
|
|
|
|
|
|
const podContainer = useMemo(() => { |
|
|
|
|
const chartClickHandler = (event: React.MouseEvent<HTMLDivElement>) => { |
|
|
|
|
event.preventDefault(); |
|
|
|
|
if (options.gauge.link === undefined || options.gauge.link === '') { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
const link = replaceVariables(options.gauge.link); |
|
|
|
|
window.open(link, '_self'); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
const isLinkActive = options.gauge.link !== undefined && options.gauge.link !== ''; |
|
|
|
|
let containerHeight = height; |
|
|
|
|
if (options.visualizationType === Pod.BAR) { |
|
|
|
|
containerHeight = height - 20; |
|
|
|
|
} |
|
|
|
|
if (options.visualizationType === Pod.GAUGE && options.gauge.additionalInfo.display) { |
|
|
|
|
containerHeight = height - options.gauge.additionalInfo.size - 8; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// we request animation frame here because we need an existing DOM-element at the moment we render the pod
|
|
|
|
|
window.requestAnimationFrame(() => { |
|
|
|
|
return ( |
|
|
|
|
<div |
|
|
|
|
ref={podContainerRef} |
|
|
|
|
className={css` |
|
|
|
|
width: ${width}px; |
|
|
|
|
height: ${containerHeight}px; |
|
|
|
|
cursor: ${isLinkActive ? 'pointer' : 'default'}; |
|
|
|
|
`}
|
|
|
|
|
onClick={chartClickHandler} |
|
|
|
|
></div> |
|
|
|
|
); |
|
|
|
|
}, [width, height, options, replaceVariables]); |
|
|
|
|
|
|
|
|
|
useEffect(() => { |
|
|
|
|
let pod; |
|
|
|
|
if (podContainerRef.current === null) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
switch (options.visualizationType) { |
|
|
|
|
case Pod.GAUGE: |
|
|
|
|
const series = new Series(grafanaSeriesList, options.gauge.value).getChartwerkSeries(); |
|
|
|
|
const chartwerkGaugeOptions = new GaugeOptions(grafanaSeriesList, options).getChartwerkOptions(); |
|
|
|
|
pod = new ChartwerkGaugePod((chartContainer as any).current, series, chartwerkGaugeOptions); |
|
|
|
|
pod = new ChartwerkGaugePod(podContainerRef.current, series, chartwerkGaugeOptions); |
|
|
|
|
break; |
|
|
|
|
case Pod.BAR: |
|
|
|
|
const barSeries = new BarSeries(grafanaSeriesList).getChartwerkSeries(); |
|
|
|
|
const chartwerkBarOptions = new BarOptions(grafanaSeriesList, onChangeTimeRange).getChartwerkOptions(); |
|
|
|
|
pod = new ChartwerkBarPod((chartContainer as any).current, barSeries, chartwerkBarOptions); |
|
|
|
|
pod = new ChartwerkBarPod(podContainerRef.current, barSeries, chartwerkBarOptions); |
|
|
|
|
break; |
|
|
|
|
default: |
|
|
|
|
throw new Error(`Unknown visualization type: ${options.visualizationType}`); |
|
|
|
|
console.warn(`Unknown visualization type: ${options.visualizationType}`); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
pod.render(); |
|
|
|
|
}); |
|
|
|
|
}, [podContainer, grafanaSeriesList, onChangeTimeRange, options]); |
|
|
|
|
|
|
|
|
|
const isLinkActive = !_.isEmpty(options.gauge.link); |
|
|
|
|
const chartClickHandler = useCallback((event: React.MouseEvent<HTMLDivElement>) => { |
|
|
|
|
event.preventDefault(); |
|
|
|
|
if (!isLinkActive) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
const link = replaceVariables(options.gauge.link as string); |
|
|
|
|
window.open(link, '_self'); |
|
|
|
|
}, [options.gauge.link]); |
|
|
|
|
const legendItems = _.map(grafanaSeriesList, (serie) => { |
|
|
|
|
return { |
|
|
|
|
label: serie.alias, |
|
|
|
|
color: serie.color, |
|
|
|
|
yAxis: 1, |
|
|
|
|
}; |
|
|
|
|
}); |
|
|
|
|
const chartHeight = options.visualizationType !== Pod.GAUGE ? height - 20 : height; |
|
|
|
|
return ( |
|
|
|
|
<div> |
|
|
|
|
<div |
|
|
|
|
ref={chartContainer} |
|
|
|
|
className={css` |
|
|
|
|
width: ${width}px; |
|
|
|
|
height: ${chartHeight}px; |
|
|
|
|
cursor: ${isLinkActive ? 'pointer' : 'default'}; |
|
|
|
|
`}
|
|
|
|
|
onClick={chartClickHandler} |
|
|
|
|
></div> |
|
|
|
|
{options.visualizationType !== Pod.GAUGE && ( |
|
|
|
|
<VizLegend placement={'bottom'} items={legendItems} displayMode={LegendDisplayMode.List} /> |
|
|
|
|
)} |
|
|
|
|
</div> |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
switch (options.visualizationType) { |
|
|
|
|
case Pod.GAUGE: |
|
|
|
|
let additionalInfo; |
|
|
|
|
const additionalInfoConfig = options.gauge.additionalInfo; |
|
|
|
|
if (additionalInfoConfig.display) { |
|
|
|
|
// TODO: move `useMetric` handling to utils, as it's duplicated in gaugeOptions.ts
|
|
|
|
|
let value: number | undefined = undefined; |
|
|
|
|
if (!additionalInfoConfig.value?.useMetric) { |
|
|
|
|
value = additionalInfoConfig.value.value; |
|
|
|
|
} else { |
|
|
|
|
if (!_.isEmpty(additionalInfoConfig.value.metricName)) { |
|
|
|
|
const aggregatedValue = getLastMetricValue( |
|
|
|
|
grafanaSeriesList, |
|
|
|
|
additionalInfoConfig.value.metricName, |
|
|
|
|
'Additional Info' |
|
|
|
|
); |
|
|
|
|
value = aggregatedValue !== null ? aggregatedValue : undefined; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
additionalInfo = ( |
|
|
|
|
<div |
|
|
|
|
className={css` |
|
|
|
|
width: ${width}px; |
|
|
|
|
height: ${additionalInfoConfig.size + 8}px; |
|
|
|
|
font-size: ${additionalInfoConfig.size}px; |
|
|
|
|
color: ${additionalInfoConfig.color}; |
|
|
|
|
display: flex; |
|
|
|
|
justify-content: center; |
|
|
|
|
align-items: center; |
|
|
|
|
`}
|
|
|
|
|
> |
|
|
|
|
{additionalInfoConfig.prefix} {value !== undefined ? formatValue(value, additionalInfoConfig) : '-'} |
|
|
|
|
</div> |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
return ( |
|
|
|
|
<div> |
|
|
|
|
{podContainer} |
|
|
|
|
{additionalInfo && additionalInfo} |
|
|
|
|
</div> |
|
|
|
|
); |
|
|
|
|
case Pod.BAR: |
|
|
|
|
const legendItems = _.map(grafanaSeriesList, (serie) => { |
|
|
|
|
return { |
|
|
|
|
label: serie.alias, |
|
|
|
|
color: serie.color, |
|
|
|
|
yAxis: 1, |
|
|
|
|
}; |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
return ( |
|
|
|
|
<div> |
|
|
|
|
{podContainer} |
|
|
|
|
|
|
|
|
|
function getGrafanaSeriesList(grafanaData: PanelData, timeRange: TimeRange): any[] { |
|
|
|
|
const processor = new DataProcessor({}); |
|
|
|
|
return processor.getSeriesList({ |
|
|
|
|
dataList: grafanaData.series, |
|
|
|
|
range: timeRange, |
|
|
|
|
}); |
|
|
|
|
<VizLegend placement={'bottom'} items={legendItems} displayMode={LegendDisplayMode.List} /> |
|
|
|
|
</div> |
|
|
|
|
); |
|
|
|
|
default: |
|
|
|
|
console.warn(`Unknown visualization type: ${options.visualizationType}`); |
|
|
|
|
return <div>This visualization is not supported</div>; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|