2019-11-02 13:45:22 -07:00
|
|
|
import React, { FC } from 'react';
|
2019-10-28 10:09:48 -07:00
|
|
|
import metricToSeriesName from './MetricFormat';
|
2019-10-17 05:38:09 -07:00
|
|
|
|
|
|
|
interface SeriesNameProps {
|
2019-10-28 07:02:42 -07:00
|
|
|
labels: { [key: string]: string } | null;
|
2019-10-17 05:38:09 -07:00
|
|
|
format: boolean;
|
|
|
|
}
|
|
|
|
|
2019-11-02 13:45:22 -07:00
|
|
|
const SeriesName: FC<SeriesNameProps> = ({ labels, format }) => {
|
|
|
|
const renderFormatted = (): React.ReactElement => {
|
|
|
|
const labelNodes: React.ReactElement[] = [];
|
2019-10-17 05:38:09 -07:00
|
|
|
let first = true;
|
2019-10-28 07:02:42 -07:00
|
|
|
for (const label in labels) {
|
2019-10-17 05:38:09 -07:00
|
|
|
if (label === '__name__') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
labelNodes.push(
|
|
|
|
<span key={label}>
|
|
|
|
{!first && ', '}
|
2019-10-28 07:02:42 -07:00
|
|
|
<span className="legend-label-name">{label}</span>=<span className="legend-label-value">"{labels[label]}"</span>
|
2019-10-17 05:38:09 -07:00
|
|
|
</span>
|
|
|
|
);
|
|
|
|
|
|
|
|
if (first) {
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2019-11-02 13:45:22 -07:00
|
|
|
<span className="legend-metric-name">{labels!.__name__ || ''}</span>
|
2019-10-17 05:38:09 -07:00
|
|
|
<span className="legend-label-brace">{'{'}</span>
|
2019-10-28 07:02:42 -07:00
|
|
|
{labelNodes}
|
2019-10-17 05:38:09 -07:00
|
|
|
<span className="legend-label-brace">{'}'}</span>
|
|
|
|
</>
|
|
|
|
);
|
2019-11-02 13:45:22 -07:00
|
|
|
};
|
2019-10-17 05:38:09 -07:00
|
|
|
|
2019-11-02 13:45:22 -07:00
|
|
|
if (labels === null) {
|
|
|
|
return <>scalar</>;
|
2019-10-17 05:38:09 -07:00
|
|
|
}
|
|
|
|
|
2019-11-02 13:45:22 -07:00
|
|
|
if (format) {
|
|
|
|
return renderFormatted();
|
2019-10-17 05:38:09 -07:00
|
|
|
}
|
2019-11-02 13:45:22 -07:00
|
|
|
// Return a simple text node. This is much faster to scroll through
|
|
|
|
// for longer lists (hundreds of items).
|
|
|
|
return <>{metricToSeriesName(labels!)}</>;
|
|
|
|
};
|
2019-10-17 05:38:09 -07:00
|
|
|
|
|
|
|
export default SeriesName;
|