@tduniec/backstage-plugin-time-saver
Version:
This plugin provides an implementation of charts and statistics related to your time savings that are coming from usage of your templates. Plugins is built from frontend and backend part. This part of plugin `frontend` is responsible of providing views wi
91 lines (88 loc) • 2.59 kB
JavaScript
import { jsx } from 'react/jsx-runtime';
import { useState, useEffect } from 'react';
import { Chart, CategoryScale, LinearScale, BarElement, Title, Tooltip } from 'chart.js';
import { Bar } from 'react-chartjs-2';
import { useApi, configApiRef, fetchApiRef } from '@backstage/core-plugin-api';
import { createUrlWithDates, getRandomColor } from '../utils.esm.js';
import CircularProgress from '@material-ui/core/CircularProgress';
import { useTheme } from '@material-ui/core';
Chart.register(CategoryScale, LinearScale, BarElement, Title, Tooltip);
function AllStatsBarChart({
dates
}) {
const configApi = useApi(configApiRef);
const fetchApi = useApi(fetchApiRef);
const [data, setData] = useState(null);
const theme = useTheme();
useEffect(() => {
fetchApi.fetch(
createUrlWithDates(
`${configApi.getString("backend.baseUrl")}/api/time-saver/getStats`,
dates
)
).then((response) => response.json()).then((dt) => setData(dt)).catch();
}, [configApi, fetchApi, dates]);
if (!data) {
return /* @__PURE__ */ jsx(CircularProgress, {});
}
const options = {
plugins: {
title: {
display: true,
text: "All Statistics",
color: theme.palette.text.primary
},
legend: {
display: true,
labels: {
color: theme.palette.text.primary
}
}
},
responsive: true,
interaction: {
mode: "index",
intersect: false
},
scales: {
x: {
stacked: true,
grid: {
display: false
},
ticks: {
color: theme.palette.text.primary
}
},
y: {
stacked: true,
grid: {
display: true
},
ticks: {
color: theme.palette.text.primary
}
}
}
};
const labels = Array.from(new Set(data.stats.map((stat) => stat.team)));
const datasets = Array.from(
new Set(data.stats.map((stat) => stat.templateName))
);
const backgroundColors = datasets.map(() => getRandomColor());
const dataAll = {
labels,
datasets: datasets.map((templateName, index) => ({
label: `${templateName}`,
data: labels.map(
(team) => data.stats.filter(
(stat) => stat.team === team && stat.templateName === templateName
).reduce((sum, stat) => sum + stat.timeSaved, 0)
),
backgroundColor: backgroundColors[index]
}))
};
return /* @__PURE__ */ jsx(Bar, { options, data: dataAll });
}
export { AllStatsBarChart };
//# sourceMappingURL=AllStatsBarChartComponent.esm.js.map