@kinvolk/headlamp-plugin
Version:
The needed infrastructure for building Headlamp plugins.
59 lines (58 loc) • 2.34 kB
JavaScript
import { jsx as _jsx } from "react/jsx-runtime";
/*
* Copyright 2025 The Kubernetes Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import '../../../i18n/config';
import _ from 'lodash';
import { useTranslation } from 'react-i18next';
import TileChart from '../TileChart';
export function CircularChart(props) {
const { items, itemsMetrics, noMetrics = false, resourceUsedGetter, resourceAvailableGetter, title, getLegend, ...others } = props;
const { t } = useTranslation();
const [used, available] = getResourceUsage();
function filterMetrics(items, metrics) {
if (!items || !metrics)
return [];
const names = items.map(({ metadata }) => metadata.name);
return metrics.filter(item => names.includes(item.metadata.name));
}
function getLabel() {
if (available === 0 || used === -1) {
return '…';
}
return `${((used / available) * 100).toFixed(1)} %`;
}
function getResourceUsage() {
if (!items)
return [-1, -1];
const nodeMetrics = filterMetrics(items, itemsMetrics);
const usedValue = _.sumBy(nodeMetrics, resourceUsedGetter);
const availableValue = _.sumBy(items, resourceAvailableGetter);
return [usedValue, availableValue];
}
function makeData() {
if (used === -1) {
return [];
}
return [
{
name: 'used',
value: used,
},
];
}
return (_jsx(TileChart, { title: title, data: noMetrics ? null : makeData(), legend: !!getLegend ? getLegend(used, available) : '', label: getLabel(), total: available, infoTooltip: noMetrics ? t('translation|Install the metrics-server to get usage data.') : null, ...others }));
}
export default CircularChart;