95 lines
2.8 KiB
TypeScript
95 lines
2.8 KiB
TypeScript
import React, { useMemo } from 'react';
|
|
import { BatchJob, BatchJobStatus } from '../../types';
|
|
|
|
interface BatchStatsProps {
|
|
jobs: BatchJob[];
|
|
}
|
|
|
|
const StatCard: React.FC<{ label: string; value: string; icon: string; color: string }> = ({
|
|
label,
|
|
value,
|
|
icon,
|
|
color,
|
|
}) => (
|
|
<div className="bg-white p-4 rounded-lg shadow-sm flex items-center space-x-4">
|
|
<div className={`text-2xl p-3 rounded-full bg-opacity-10 ${color}`}>
|
|
<i className={`fa-solid ${icon}`}></i>
|
|
</div>
|
|
<div>
|
|
<p className="text-gray-600 text-sm font-medium">{label}</p>
|
|
<p className="text-2xl font-bold text-gray-800">{value}</p>
|
|
</div>
|
|
</div>
|
|
);
|
|
|
|
const formatDuration = (ms: number): string => {
|
|
if (ms < 1000) return `${ms} ms`;
|
|
const seconds = ms / 1000;
|
|
if (seconds < 60) return `${seconds.toFixed(1)} s`;
|
|
const minutes = seconds / 60;
|
|
return `${minutes.toFixed(1)} m`;
|
|
};
|
|
|
|
const BatchStats: React.FC<BatchStatsProps> = ({ jobs }) => {
|
|
const stats = useMemo(() => {
|
|
const today = new Date();
|
|
const startOfToday = new Date(today.getFullYear(), today.getMonth(), today.getDate());
|
|
|
|
const jobsToday = jobs.filter((j) => j.startTime >= startOfToday);
|
|
const completedToday = jobsToday.filter((j) => j.status === BatchJobStatus.COMPLETED);
|
|
const failedToday = jobsToday.filter((j) => j.status === BatchJobStatus.FAILED);
|
|
|
|
const successRate =
|
|
jobsToday.length > 0
|
|
? (completedToday.length / (completedToday.length + failedToday.length)) * 100
|
|
: 100;
|
|
|
|
const totalDuration = completedToday.reduce((sum, job) => {
|
|
if (job.endTime) {
|
|
return sum + (job.endTime.getTime() - job.startTime.getTime());
|
|
}
|
|
return sum;
|
|
}, 0);
|
|
|
|
const avgDuration = completedToday.length > 0 ? totalDuration / completedToday.length : 0;
|
|
|
|
return {
|
|
totalToday: jobsToday.length,
|
|
successRate: successRate.toFixed(1),
|
|
failedToday: failedToday.length,
|
|
avgDuration: formatDuration(avgDuration),
|
|
};
|
|
}, [jobs]);
|
|
|
|
return (
|
|
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6">
|
|
<StatCard
|
|
label="Total Pekerjaan (Hari Ini)"
|
|
value={`${stats.totalToday}`}
|
|
icon="fa-tasks"
|
|
color="text-blue-500 bg-blue-500"
|
|
/>
|
|
<StatCard
|
|
label="Tingkat Sukses"
|
|
value={`${stats.successRate}%`}
|
|
icon="fa-check-circle"
|
|
color="text-green-500 bg-green-500"
|
|
/>
|
|
<StatCard
|
|
label="Pekerjaan Gagal"
|
|
value={`${stats.failedToday}`}
|
|
icon="fa-times-circle"
|
|
color="text-red-500 bg-red-500"
|
|
/>
|
|
<StatCard
|
|
label="Durasi Rata-rata"
|
|
value={`${stats.avgDuration}`}
|
|
icon="fa-stopwatch"
|
|
color="text-purple-500 bg-purple-500"
|
|
/>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default BatchStats;
|