Spaces:
Runtime error
Runtime error
File size: 2,587 Bytes
56b6519 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
import {
BarElement,
CategoryScale,
Chart as ChartJS,
Legend,
LinearScale,
Title,
Tooltip,
} from 'chart.js';
import { t } from 'i18next';
import React, { useEffect, useState } from 'react';
import { Bar } from 'react-chartjs-2';
import { useParams } from 'react-router-dom';
import { Finding, getAuditById } from '../../services/audits';
ChartJS.register(
CategoryScale,
LinearScale,
BarElement,
Title,
Tooltip,
Legend,
);
type RemediationPriorityProps = {
auditId?: string;
};
const RemediationPriority: React.FC<RemediationPriorityProps> = ({
auditId,
}) => {
const paramId = useParams().auditId;
if (!auditId) {
auditId = paramId;
}
const [priorityData, setPriorityData] = useState<number[]>([0, 0, 0, 0]);
useEffect(() => {
const fetchAuditData = async () => {
if (!auditId) {
return;
}
try {
const dataAudit = await getAuditById(auditId);
const findings = dataAudit.datas.findings;
const counts = [0, 0, 0, 0];
findings.forEach((finding: Finding) => {
if (
finding.priority &&
finding.priority >= 1 &&
finding.priority <= 4
) {
counts[finding.priority - 1] += 1;
}
});
setPriorityData(counts);
} catch (error) {
console.error('Error fetching audit data:', error);
}
};
if (auditId) {
fetchAuditData().catch(console.error);
}
}, [auditId]);
const data = {
labels: [t('low'), t('medium'), t('high'), t('urgent')],
datasets: [
{
data: priorityData,
backgroundColor: ['#2ECC40', '#FFDC00', '#FF851B', '#FF4136'],
},
],
};
const maxCount = Math.max(...priorityData) + 2;
const options = {
responsive: true,
maintainAspectRatio: false,
scales: {
y: {
beginAtZero: true,
max: maxCount,
ticks: {
stepSize: 1,
color: 'white',
},
grid: {
color: 'rgba(255, 255, 255, 0.1)',
},
},
x: {
ticks: {
color: 'white',
},
grid: {
display: false,
},
},
},
plugins: {
legend: {
display: false,
},
datalabels: {
formatter: () => '',
},
},
};
return (
<div className="bg-gray-800 rounded-lg p-6">
<div className="chart-container" style={{ height: '400px' }}>
<Bar data={data} options={options} />
</div>
</div>
);
};
export default RemediationPriority;
|