import React from "react";
import { Document, Page, Text, View } from "@react-pdf/renderer";
import { sharedStyles } from "./sharedPdfStyles";
interface AppPdfDocumentProps {
data: any;
appLabel: string;
category: string;
reportDateStr: string;
}
interface TableColumn {
header: string;
width: string;
align?: "center" | "right" | "left";
cell: (row: any) => string | number;
}
function formatBytes(bytes: number): string {
if (!bytes || bytes === 0) return "0 B";
const k = 1000;
const sizes = ["B", "KB", "MB", "GB", "TB"];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + " " + sizes[i];
}
function formatIndonesiaTime(dateStr: string | Date): string {
if (!dateStr) return "Never";
try {
const d = new Date(dateStr);
return d.toLocaleString("id-ID", {
timeZone: "Asia/Jakarta",
day: "2-digit",
month: "2-digit",
year: "numeric",
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
}) + " WIB";
} catch (e) {
return String(dateStr);
}
}
function RenderPdfTable({ title, columns, data }: { title: string; columns: TableColumn[]; data: any[] }) {
return (
{title}
{columns.map((col, idx) => (
{col.header}
))}
{data.length === 0 ? (
No Data
) : (
data.map((row, rowIdx) => (
{columns.map((col, colIdx) => (
{col.cell(row)}
))}
))
)}
);
}
export default function AppPdfDocument({
data,
appLabel,
category,
reportDateStr,
}: AppPdfDocumentProps) {
// Sort by total bandwidth (download + upload) descending for PDF export
const sortByBandwidth = (arr: any[]) =>
[...arr].sort((a, b) =>
((b.download ?? 0) + (b.upload ?? 0)) - ((a.download ?? 0) + (a.upload ?? 0))
);
const topIps = sortByBandwidth(data.top_ips || []).slice(0, 50);
const agents = sortByBandwidth(data.agent_scorecard || []).slice(0, 50);
return (
{/* Header */}
Deep Package Inspection
Security & Traffic Analytics Dashboard
Application Activity Report
Generated at: {reportDateStr}
{/* Application Details */}
Application Summary
Application Name
{appLabel}
Category
{category || "General Web"}
{/* Bandwidth Usage */}
Traffic Metrics
TOTAL DOWNLOAD
{formatBytes(data.total_download)}
TOTAL UPLOAD
{formatBytes(data.total_upload)}
{/* Top Users */}
r.ip_address || "—" },
{ header: "Download", width: "20%", align: "right", cell: (r) => formatBytes(r.download) },
{ header: "Upload", width: "20%", align: "right", cell: (r) => formatBytes(r.upload) },
{ header: "Flows", width: "10%", align: "center", cell: (r) => r.flow_count ?? 0 },
{ header: "Last Seen", width: "20%", cell: (r) => r.last_seen ? formatIndonesiaTime(r.last_seen) : "Never" },
]}
/>
{/* Top Agents */}
r.agent_label || "Unknown Agent" },
{ header: "Download", width: "20%", align: "right", cell: (r) => formatBytes(r.download) },
{ header: "Upload", width: "20%", align: "right", cell: (r) => formatBytes(r.upload) },
{ header: "Flows", width: "10%", align: "center", cell: (r) => r.flow_count ?? 0 },
]}
/>
);
}