File size: 6,155 Bytes
eeb9404 | 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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 | 'use client';
import React, { useState, useEffect } from 'react';
import { FunctionLog } from '@/lib/vfs/types';
import {
Loader2, AlertCircle, RefreshCw, Trash2, CheckCircle2, XCircle,
Clock, ArrowRight
} from 'lucide-react';
import { Button } from '@/components/ui/button';
import { cn } from '@/lib/utils';
interface LogsViewerProps {
deploymentId: string;
workspaceId?: string;
}
interface EnrichedLog extends FunctionLog {
functionName?: string;
}
export function LogsViewer({ deploymentId, workspaceId }: LogsViewerProps) {
const apiBase = workspaceId ? `/api/w/${workspaceId}` : '/api';
const [logs, setLogs] = useState<EnrichedLog[]>([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
useEffect(() => {
loadLogs();
}, [deploymentId]);
const loadLogs = async () => {
try {
setLoading(true);
setError(null);
const res = await fetch(`${apiBase}/admin/deployments/${deploymentId}/database/logs?limit=200`);
if (!res.ok) {
const data = await res.json();
throw new Error(data.error || 'Failed to load logs');
}
const data = await res.json();
setLogs(data.logs);
} catch (err) {
setError(err instanceof Error ? err.message : 'Failed to load logs');
} finally {
setLoading(false);
}
};
const clearLogs = async () => {
if (!confirm('Clear all function execution logs? This cannot be undone.')) return;
try {
const res = await fetch(`${apiBase}/admin/deployments/${deploymentId}/database/logs`, {
method: 'DELETE',
});
if (!res.ok) throw new Error('Failed to clear logs');
await loadLogs();
} catch (err) {
console.error('Failed to clear logs:', err);
}
};
const formatDate = (date: Date | string) => {
const d = typeof date === 'string' ? new Date(date) : date;
return d.toLocaleString();
};
if (loading) {
return (
<div className="flex items-center justify-center h-full">
<Loader2 className="h-6 w-6 animate-spin text-muted-foreground" />
</div>
);
}
if (error) {
return (
<div className="flex flex-col items-center justify-center h-full gap-4">
<AlertCircle className="h-8 w-8 text-destructive" />
<p className="text-sm text-muted-foreground">{error}</p>
<Button variant="outline" onClick={loadLogs}>
Retry
</Button>
</div>
);
}
return (
<div className="h-full flex flex-col">
<div className="flex items-center justify-between mb-4">
<h3 className="text-sm font-medium">Execution Logs</h3>
<div className="flex items-center gap-2">
<Button variant="ghost" size="sm" onClick={loadLogs}>
<RefreshCw className="h-4 w-4 mr-1" />
Refresh
</Button>
<Button
variant="ghost"
size="sm"
onClick={clearLogs}
disabled={logs.length === 0}
className="text-destructive hover:text-destructive"
>
<Trash2 className="h-4 w-4 mr-1" />
Clear
</Button>
</div>
</div>
<div className="flex-1 overflow-auto border rounded-lg">
{logs.length === 0 ? (
<div className="flex flex-col items-center justify-center h-full p-8 text-center">
<Clock className="h-8 w-8 text-muted-foreground mb-2" />
<p className="text-sm text-muted-foreground">No execution logs yet</p>
<p className="text-xs text-muted-foreground mt-1">
Logs will appear here when functions are invoked
</p>
</div>
) : (
<table className="w-full text-sm">
<thead className="sticky top-0 bg-muted">
<tr>
<th className="text-left p-3 font-medium">Status</th>
<th className="text-left p-3 font-medium">Function</th>
<th className="text-left p-3 font-medium">Method</th>
<th className="text-left p-3 font-medium">Path</th>
<th className="text-left p-3 font-medium">Duration</th>
<th className="text-left p-3 font-medium">Time</th>
</tr>
</thead>
<tbody>
{logs.map(log => (
<tr key={log.id} className="border-t hover:bg-muted/30">
<td className="p-3">
{log.statusCode >= 200 && log.statusCode < 300 ? (
<CheckCircle2 className="h-4 w-4 text-green-500" />
) : log.statusCode >= 400 ? (
<XCircle className="h-4 w-4 text-destructive" />
) : (
<ArrowRight className="h-4 w-4 text-yellow-500" />
)}
</td>
<td className="p-3 font-mono">
{log.functionName || log.functionId.slice(0, 8)}
</td>
<td className="p-3">
<span className={cn(
"text-xs px-1.5 py-0.5 rounded",
log.method === 'GET' ? "bg-green-500/20 text-green-600" :
log.method === 'POST' ? "bg-blue-500/20 text-blue-600" :
log.method === 'PUT' ? "bg-yellow-500/20 text-yellow-600" :
log.method === 'DELETE' ? "bg-red-500/20 text-red-600" :
"bg-muted text-muted-foreground"
)}>
{log.method}
</span>
</td>
<td className="p-3 font-mono text-xs text-muted-foreground">
{log.path}
</td>
<td className="p-3 text-muted-foreground">
{log.durationMs}ms
</td>
<td className="p-3 text-xs text-muted-foreground">
{formatDate(log.timestamp)}
</td>
</tr>
))}
</tbody>
</table>
)}
</div>
</div>
);
}
|