feat(k8s): 事件页补全时间/次数/来源等字段
- 后端解析 source/reportingComponent 与 object_kind/name - 新增 EventTable 统一展示:最后/首次时间、次数、重复跨度、类型、原因、对象、来源、消息 - Deployment 事件 Tab 与事件中心共用表格组件
This commit is contained in:
1 parent
ee2588fb52
commit
b2939470f4
8 files changed
+323
-176
No files matched your search
@@ -7,6 +7,7 @@ import (
|
||||
"time"
|
||||
|
||||
"ezops/model"
|
||||
k8sclient "ezops/pkg/kubernetesclient"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
@@ -23,31 +24,42 @@ func (h *KubernetesHandler) ListEvents(c *gin.Context) {
|
||||
if err != nil { c.JSON(500, model.Response{Code: 500, Message: "K8s API error: " + err.Error()}); return }
|
||||
|
||||
type EventSummary struct {
|
||||
Namespace string `json:"namespace"`
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"`
|
||||
Reason string `json:"reason"`
|
||||
Message string `json:"message"`
|
||||
Object string `json:"object"`
|
||||
Count int `json:"count"`
|
||||
FirstTime string `json:"first_time"`
|
||||
LastTime string `json:"last_time"`
|
||||
Namespace string `json:"namespace"`
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"`
|
||||
Reason string `json:"reason"`
|
||||
Message string `json:"message"`
|
||||
Object string `json:"object"`
|
||||
ObjectKind string `json:"object_kind"`
|
||||
ObjectName string `json:"object_name"`
|
||||
Source string `json:"source"`
|
||||
Count int `json:"count"`
|
||||
FirstTime string `json:"first_time"`
|
||||
LastTime string `json:"last_time"`
|
||||
EventTime string `json:"event_time"`
|
||||
}
|
||||
result := []EventSummary{}
|
||||
for _, e := range events.Items {
|
||||
if eventType != "" && e.Type != eventType { continue }
|
||||
object := fmt.Sprintf("%s/%s", e.InvolvedObject.Kind, e.InvolvedObject.Name)
|
||||
objectKind := e.InvolvedObject.Kind
|
||||
objectName := e.InvolvedObject.Name
|
||||
object := fmt.Sprintf("%s/%s", objectKind, objectName)
|
||||
if e.InvolvedObject.Namespace != "" {
|
||||
object = fmt.Sprintf("%s/%s/%s", e.InvolvedObject.Namespace, e.InvolvedObject.Kind, e.InvolvedObject.Name)
|
||||
object = fmt.Sprintf("%s/%s/%s", e.InvolvedObject.Namespace, objectKind, objectName)
|
||||
}
|
||||
firstTime, lastTime, eventTime := formatEventTimes(e)
|
||||
count := int(e.Count)
|
||||
if count <= 0 {
|
||||
count = 1
|
||||
}
|
||||
firstTime := ""; if !e.FirstTimestamp.IsZero() { firstTime = e.FirstTimestamp.Format(time.RFC3339) }
|
||||
lastTime := ""; if !e.LastTimestamp.IsZero() { lastTime = e.LastTimestamp.Format(time.RFC3339) }
|
||||
result = append(result, EventSummary{
|
||||
Namespace: e.Metadata.Namespace, Name: e.Metadata.Name, Type: e.Type, Reason: e.Reason,
|
||||
Message: e.Message, Object: object, Count: int(e.Count),
|
||||
FirstTime: firstTime, LastTime: lastTime,
|
||||
Message: e.Message, Object: object, ObjectKind: objectKind, ObjectName: objectName,
|
||||
Source: formatEventSource(e), Count: count,
|
||||
FirstTime: firstTime, LastTime: lastTime, EventTime: eventTime,
|
||||
})
|
||||
}
|
||||
sort.Slice(result, func(i, j int) bool { return result[i].LastTime > result[j].LastTime })
|
||||
c.JSON(200, model.Response{Code: 200, Data: result})
|
||||
}
|
||||
|
||||
@@ -122,15 +134,20 @@ func (h *KubernetesHandler) GetEventsSummary(c *gin.Context) {
|
||||
|
||||
// Collect recent warning events (top 10 by lastTimestamp)
|
||||
if len(recentWarnings) < 50 {
|
||||
firstTime, lastTime, eventTime := formatEventTimes(e)
|
||||
recentWarnings = append(recentWarnings, map[string]interface{}{
|
||||
"namespace": nsName,
|
||||
"type": e.Type,
|
||||
"reason": e.Reason,
|
||||
"message": e.Message,
|
||||
"object": fmt.Sprintf("%s/%s", e.InvolvedObject.Kind, e.InvolvedObject.Name),
|
||||
"count": count,
|
||||
"last_time": e.LastTimestamp.Format(time.RFC3339),
|
||||
"first_time": e.FirstTimestamp.Format(time.RFC3339),
|
||||
"namespace": nsName,
|
||||
"type": e.Type,
|
||||
"reason": e.Reason,
|
||||
"message": e.Message,
|
||||
"object": fmt.Sprintf("%s/%s", e.InvolvedObject.Kind, e.InvolvedObject.Name),
|
||||
"object_kind": e.InvolvedObject.Kind,
|
||||
"object_name": e.InvolvedObject.Name,
|
||||
"source": formatEventSource(e),
|
||||
"count": count,
|
||||
"last_time": lastTime,
|
||||
"first_time": firstTime,
|
||||
"event_time": eventTime,
|
||||
})
|
||||
}
|
||||
} else {
|
||||
@@ -200,6 +217,44 @@ func (h *KubernetesHandler) GetEventsSummary(c *gin.Context) {
|
||||
c.JSON(200, model.Response{Code: 200, Data: result})
|
||||
}
|
||||
|
||||
// formatEventSource 格式化事件来源(component / reportingComponent)// Logan.Li
|
||||
func formatEventSource(e k8sclient.EventItem) string {
|
||||
if e.ReportingComponent != "" {
|
||||
if e.ReportingInstance != "" {
|
||||
return e.ReportingComponent + "/" + e.ReportingInstance
|
||||
}
|
||||
return e.ReportingComponent
|
||||
}
|
||||
src := e.Source.Component
|
||||
if e.Source.Host != "" {
|
||||
if src != "" {
|
||||
return src + "@" + e.Source.Host
|
||||
}
|
||||
return e.Source.Host
|
||||
}
|
||||
return src
|
||||
}
|
||||
|
||||
// formatEventTimes 提取首次/最后/事件时间(RFC3339)// Logan.Li
|
||||
func formatEventTimes(e k8sclient.EventItem) (first, last, eventTime string) {
|
||||
if !e.FirstTimestamp.IsZero() {
|
||||
first = e.FirstTimestamp.Format(time.RFC3339)
|
||||
}
|
||||
if !e.LastTimestamp.IsZero() {
|
||||
last = e.LastTimestamp.Format(time.RFC3339)
|
||||
}
|
||||
if !e.EventTime.IsZero() {
|
||||
eventTime = e.EventTime.Format(time.RFC3339)
|
||||
}
|
||||
if last == "" && eventTime != "" {
|
||||
last = eventTime
|
||||
}
|
||||
if first == "" && eventTime != "" {
|
||||
first = eventTime
|
||||
}
|
||||
return first, last, eventTime
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Capacity Analysis — CPU/Memory request/limit per Deployment
|
||||
// ============================================================================
|
||||
|
||||
@@ -1463,16 +1463,24 @@ type EventList struct {
|
||||
Items []EventItem `json:"items"`
|
||||
}
|
||||
|
||||
type EventSource struct {
|
||||
Component string `json:"component"`
|
||||
Host string `json:"host"`
|
||||
}
|
||||
|
||||
type EventItem struct {
|
||||
Metadata ObjectMeta `json:"metadata"`
|
||||
InvolvedObject InvolvedObject `json:"involvedObject"`
|
||||
Type string `json:"type"`
|
||||
Reason string `json:"reason"`
|
||||
Message string `json:"message"`
|
||||
Count int32 `json:"count"`
|
||||
FirstTimestamp time.Time `json:"firstTimestamp"`
|
||||
LastTimestamp time.Time `json:"lastTimestamp"`
|
||||
EventTime time.Time `json:"eventTime"`
|
||||
Metadata ObjectMeta `json:"metadata"`
|
||||
InvolvedObject InvolvedObject `json:"involvedObject"`
|
||||
Type string `json:"type"`
|
||||
Reason string `json:"reason"`
|
||||
Message string `json:"message"`
|
||||
Source EventSource `json:"source"`
|
||||
ReportingComponent string `json:"reportingComponent"`
|
||||
ReportingInstance string `json:"reportingInstance"`
|
||||
Count int32 `json:"count"`
|
||||
FirstTimestamp time.Time `json:"firstTimestamp"`
|
||||
LastTimestamp time.Time `json:"lastTimestamp"`
|
||||
EventTime time.Time `json:"eventTime"`
|
||||
}
|
||||
|
||||
type InvolvedObject struct {
|
||||
|
||||
@@ -13,6 +13,7 @@ import { kubernetesApi } from '@/api';
|
||||
import { useToast } from '@/hooks/use-toast';
|
||||
import { useConfirm } from '@/components/ui/confirm-dialog';
|
||||
import { formatK8sTime, STATUS_COLORS } from './k8s-yaml-utils';
|
||||
import { normalizeK8sEvent } from './k8s-event-utils';
|
||||
|
||||
import { InfoItem, PodConditionsRow } from './deployment-view/helpers';
|
||||
import { PodsTab } from './deployment-view/PodsTab';
|
||||
@@ -111,15 +112,7 @@ export function DeploymentView(props: DeploymentViewProps) {
|
||||
if (obj.endsWith(`/${depName}`)) return true;
|
||||
const name = obj.split('/').pop() || '';
|
||||
return name === depName || name.startsWith(`${depName}-`);
|
||||
}).map((e: any) => ({
|
||||
type: e.type || 'Normal',
|
||||
reason: e.reason || '',
|
||||
message: e.message || '',
|
||||
source: e.object || e.source?.component || e.sourceComponent || '-',
|
||||
firstTimestamp: e.first_time || e.firstTimestamp || e.first_timestamp || '',
|
||||
lastTimestamp: e.last_time || e.lastTimestamp || e.last_timestamp || '',
|
||||
count: e.count || 1,
|
||||
}));
|
||||
}).map((e: any) => normalizeK8sEvent(e));
|
||||
setEvents(depEvents);
|
||||
}).catch(() => {}).finally(() => setTabLoading(''));
|
||||
}, [tab, clusterCode, namespace, deployment?.name]);
|
||||
|
||||
@@ -0,0 +1,124 @@
|
||||
// EventTable — K8s 事件列表(时间/次数/类型/原因/对象/来源/消息)// Logan.Li
|
||||
import { useState } from 'react';
|
||||
import { Badge } from '@/components/ui/badge';
|
||||
import {
|
||||
Table, TableBody, TableCell, TableHead, TableHeader, TableRow,
|
||||
} from '@/components/ui/table';
|
||||
import { Loader2 } from 'lucide-react';
|
||||
import {
|
||||
type NormalizedEvent,
|
||||
formatEventRepeatSpan,
|
||||
formatEventTimeCell,
|
||||
} from './k8s-event-utils';
|
||||
|
||||
interface EventTableProps {
|
||||
events: NormalizedEvent[];
|
||||
loading?: boolean | string;
|
||||
emptyText?: string;
|
||||
maxRows?: number;
|
||||
showNamespace?: boolean;
|
||||
}
|
||||
|
||||
export function EventTable({
|
||||
events,
|
||||
loading = false,
|
||||
emptyText = '暂无事件',
|
||||
maxRows,
|
||||
showNamespace = false,
|
||||
}: EventTableProps) {
|
||||
const [expandedRow, setExpandedRow] = useState<string | null>(null);
|
||||
const isLoading = loading === true || loading === 'events';
|
||||
const rows = maxRows ? events.slice(0, maxRows) : events;
|
||||
const colSpan = showNamespace ? 10 : 9;
|
||||
|
||||
return (
|
||||
<div className="overflow-x-auto">
|
||||
<Table>
|
||||
<TableHeader>
|
||||
<TableRow className="border-[var(--dg-border)] hover:bg-transparent">
|
||||
<TableHead className="text-[var(--dg-text-muted)] w-[148px]">最后时间</TableHead>
|
||||
<TableHead className="text-[var(--dg-text-muted)] w-[148px]">首次时间</TableHead>
|
||||
<TableHead className="text-[var(--dg-text-muted)] w-16 text-center">次数</TableHead>
|
||||
<TableHead className="text-[var(--dg-text-muted)] w-20">重复</TableHead>
|
||||
<TableHead className="text-[var(--dg-text-muted)] w-20">类型</TableHead>
|
||||
<TableHead className="text-[var(--dg-text-muted)] w-28">原因</TableHead>
|
||||
<TableHead className="text-[var(--dg-text-muted)] min-w-[120px]">对象</TableHead>
|
||||
{showNamespace && <TableHead className="text-[var(--dg-text-muted)] w-24">命名空间</TableHead>}
|
||||
<TableHead className="text-[var(--dg-text-muted)] w-28">来源</TableHead>
|
||||
<TableHead className="text-[var(--dg-text-muted)] min-w-[200px]">消息</TableHead>
|
||||
</TableRow>
|
||||
</TableHeader>
|
||||
<TableBody>
|
||||
{isLoading ? (
|
||||
<TableRow>
|
||||
<TableCell colSpan={colSpan} className="text-center py-10">
|
||||
<Loader2 className="h-5 w-5 animate-spin mx-auto text-[var(--dg-text-muted)]" />
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
) : rows.length === 0 ? (
|
||||
<TableRow>
|
||||
<TableCell colSpan={colSpan} className="text-center text-[var(--dg-text-muted)] py-10">{emptyText}</TableCell>
|
||||
</TableRow>
|
||||
) : rows.map((e, i) => {
|
||||
const rowKey = `${e.name || e.object}-${i}`;
|
||||
const isExpanded = expandedRow === rowKey;
|
||||
const isWarning = e.type === 'Warning';
|
||||
const last = formatEventTimeCell(e.lastTime);
|
||||
const first = formatEventTimeCell(e.firstTime);
|
||||
return (
|
||||
<TableRow
|
||||
key={rowKey}
|
||||
className={`border-[var(--dg-border)] cursor-pointer hover:bg-[var(--dg-bg-tertiary)]/40 ${isExpanded ? 'bg-[var(--dg-bg-tertiary)]/30' : ''}`}
|
||||
onClick={() => setExpandedRow(isExpanded ? null : rowKey)}
|
||||
>
|
||||
<TableCell className="text-xs text-[var(--dg-text-muted)]">
|
||||
<div>{last.absolute}</div>
|
||||
<div className="text-[10px]">{last.relative}</div>
|
||||
</TableCell>
|
||||
<TableCell className="text-xs text-[var(--dg-text-muted)]">
|
||||
<div>{first.absolute}</div>
|
||||
<div className="text-[10px]">{first.relative}</div>
|
||||
</TableCell>
|
||||
<TableCell className="text-sm text-center tabular-nums text-[var(--dg-text-secondary)]">{e.count}</TableCell>
|
||||
<TableCell className="text-xs text-[var(--dg-text-muted)]">{formatEventRepeatSpan(e.firstTime, e.lastTime, e.count)}</TableCell>
|
||||
<TableCell>
|
||||
<Badge
|
||||
variant="outline"
|
||||
className={isWarning
|
||||
? 'text-[var(--dg-warning)] border-[var(--dg-warning)]/30 text-[10px]'
|
||||
: 'text-[var(--dg-text-muted)] border-[var(--dg-border)] text-[10px]'}
|
||||
>
|
||||
{e.type}
|
||||
</Badge>
|
||||
</TableCell>
|
||||
<TableCell className="text-xs font-medium text-[var(--dg-text-secondary)]">{e.reason}</TableCell>
|
||||
<TableCell className="text-xs font-mono text-[var(--dg-text-muted)]">
|
||||
{e.objectKind ? (
|
||||
<span><span className="text-[var(--dg-text-secondary)]">{e.objectKind}</span>/{e.objectName || e.object.split('/').pop()}</span>
|
||||
) : e.object}
|
||||
</TableCell>
|
||||
{showNamespace && <TableCell className="text-xs text-[var(--dg-text-muted)]">{e.namespace || '-'}</TableCell>}
|
||||
<TableCell className="text-xs text-[var(--dg-text-muted)] font-mono">{e.source}</TableCell>
|
||||
<TableCell className="text-xs text-[var(--dg-text-secondary)] max-w-[360px]">
|
||||
<div className={`${isExpanded ? 'whitespace-normal break-words' : 'truncate'}`} title={!isExpanded ? e.message : undefined}>
|
||||
{e.message}
|
||||
</div>
|
||||
{isExpanded && e.name && (
|
||||
<div className="mt-1 pt-1 border-t border-[var(--dg-border)] text-[10px] text-[var(--dg-text-muted)]">
|
||||
事件名: {e.name}
|
||||
</div>
|
||||
)}
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
);
|
||||
})}
|
||||
</TableBody>
|
||||
</Table>
|
||||
{maxRows && events.length > maxRows && (
|
||||
<div className="text-center py-2 text-xs text-[var(--dg-text-muted)] border-t border-[var(--dg-border)]">
|
||||
仅显示前 {maxRows} 条,共 {events.length} 条
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -5,11 +5,10 @@ import { Button } from '@/components/ui/button';
|
||||
import {
|
||||
Select, SelectContent, SelectItem, SelectTrigger, SelectValue,
|
||||
} from '@/components/ui/select';
|
||||
import {
|
||||
Table, TableBody, TableCell, TableHead, TableHeader, TableRow,
|
||||
} from '@/components/ui/table';
|
||||
import { RefreshCw, ChevronRight, Bell, AlertTriangle, X } from 'lucide-react';
|
||||
import { kubernetesApi } from '@/api';
|
||||
import { EventTable } from './EventTable';
|
||||
import { normalizeK8sEvent, formatEventRelative } from './k8s-event-utils';
|
||||
|
||||
interface RankItem { name: string; count: number }
|
||||
|
||||
@@ -87,7 +86,7 @@ export default function EventsPage() {
|
||||
const [summary, setSummary] = useState<any>(null);
|
||||
const [selectedReason, setSelectedReason] = useState('');
|
||||
const [selectedKind, setSelectedKind] = useState('');
|
||||
const [expandedRow, setExpandedRow] = useState<string | null>(null);
|
||||
|
||||
|
||||
useEffect(() => {
|
||||
kubernetesApi.getClusters().then((res: any) => {
|
||||
@@ -133,17 +132,18 @@ export default function EventsPage() {
|
||||
useEffect(() => { loadSummary(); }, [loadSummary]);
|
||||
|
||||
const displayEvents = useMemo(() => {
|
||||
let filtered = events;
|
||||
let filtered = events.map((e) => normalizeK8sEvent(e));
|
||||
const hours = parseInt(timeWindow, 10);
|
||||
if (hours > 0) {
|
||||
const since = Date.now() - hours * 3600 * 1000;
|
||||
filtered = filtered.filter((e) => {
|
||||
const ts = e.last_time || e.lastTime || e.last_timestamp;
|
||||
return !ts || new Date(ts).getTime() >= since;
|
||||
});
|
||||
filtered = filtered.filter((e) => !e.lastTime || new Date(e.lastTime).getTime() >= since);
|
||||
}
|
||||
if (selectedReason) filtered = filtered.filter((e) => e.reason === selectedReason);
|
||||
if (selectedKind) filtered = filtered.filter((e) => (e.object || '').startsWith(`${selectedKind}/`));
|
||||
if (selectedKind) {
|
||||
filtered = filtered.filter((e) =>
|
||||
e.objectKind === selectedKind || e.object.startsWith(`${selectedKind}/`),
|
||||
);
|
||||
}
|
||||
return filtered;
|
||||
}, [events, selectedReason, selectedKind, timeWindow]);
|
||||
|
||||
@@ -162,20 +162,6 @@ export default function EventsPage() {
|
||||
(summary?.by_kind || []).map((k: any) => ({ name: k.kind || 'Unknown', count: k.count })),
|
||||
[summary]);
|
||||
|
||||
const formatTime = (ts: string) => (ts ? ts.replace('T', ' ').substring(0, 19) : '-');
|
||||
|
||||
const formatRelative = (ts: string) => {
|
||||
if (!ts) return '';
|
||||
const diff = Date.now() - new Date(ts).getTime();
|
||||
if (diff < 0) return '';
|
||||
const mins = Math.floor(diff / 60000);
|
||||
if (mins < 1) return '刚刚';
|
||||
if (mins < 60) return `${mins}分钟前`;
|
||||
const hrs = Math.floor(mins / 60);
|
||||
if (hrs < 24) return `${hrs}小时前`;
|
||||
return `${Math.floor(hrs / 24)}天前`;
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="space-y-4">
|
||||
<div className="flex items-center gap-2 text-sm text-[var(--dg-text-muted)]">
|
||||
@@ -280,8 +266,9 @@ export default function EventsPage() {
|
||||
<span className="shrink-0 font-mono text-[var(--dg-warning)]">{w.reason}</span>
|
||||
<span className="text-[var(--dg-text-muted)] font-mono truncate">{w.object}</span>
|
||||
<span className="text-[var(--dg-text-muted)]">{w.namespace}</span>
|
||||
{w.source && <span className="text-[var(--dg-text-muted)] font-mono">{w.source}</span>}
|
||||
<span className="text-[var(--dg-text-muted)]">×{w.count}</span>
|
||||
<span className="ml-auto text-[var(--dg-text-muted)]">{formatRelative(w.last_time)}</span>
|
||||
<span className="ml-auto text-[var(--dg-text-muted)]">{formatEventRelative(w.last_time)}</span>
|
||||
</div>
|
||||
<p className="text-[var(--dg-text-secondary)] truncate mt-0.5" title={w.message}>{w.message}</p>
|
||||
</div>
|
||||
@@ -304,70 +291,12 @@ export default function EventsPage() {
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
<Table>
|
||||
<TableHeader>
|
||||
<TableRow className="border-[var(--dg-border)] hover:bg-transparent">
|
||||
<TableHead className="text-[var(--dg-text-muted)] w-16">类型</TableHead>
|
||||
<TableHead className="text-[var(--dg-text-muted)] w-24">原因</TableHead>
|
||||
<TableHead className="text-[var(--dg-text-muted)]">对象</TableHead>
|
||||
<TableHead className="text-[var(--dg-text-muted)]">消息</TableHead>
|
||||
<TableHead className="text-[var(--dg-text-muted)] w-12">次数</TableHead>
|
||||
<TableHead className="text-[var(--dg-text-muted)] w-20">命名空间</TableHead>
|
||||
<TableHead className="text-[var(--dg-text-muted)] w-40">最后时间</TableHead>
|
||||
</TableRow>
|
||||
</TableHeader>
|
||||
<TableBody>
|
||||
{displayEvents.length === 0 ? (
|
||||
<TableRow>
|
||||
<TableCell colSpan={7} className="text-center text-[var(--dg-text-muted)] py-8">
|
||||
{loading ? <RefreshCw className="h-4 w-4 animate-spin mx-auto" /> : '暂无事件'}
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
) : displayEvents.slice(0, 200).map((e, i) => {
|
||||
const rowKey = `${e.name}-${i}`;
|
||||
const isExpanded = expandedRow === rowKey;
|
||||
const isWarning = e.type === 'Warning';
|
||||
return (
|
||||
<TableRow
|
||||
key={rowKey}
|
||||
className={`border-[var(--dg-border)] cursor-pointer hover:bg-[var(--dg-bg-tertiary)]/40 ${isExpanded ? 'bg-[var(--dg-bg-tertiary)]/30' : ''}`}
|
||||
onClick={() => setExpandedRow(isExpanded ? null : rowKey)}
|
||||
>
|
||||
<TableCell>
|
||||
<span className={`text-xs font-medium ${isWarning ? 'text-[var(--dg-warning)]' : 'text-[var(--dg-text-muted)]'}`}>
|
||||
{isWarning ? 'Warning' : 'Normal'}
|
||||
</span>
|
||||
</TableCell>
|
||||
<TableCell className="text-xs text-[var(--dg-text-secondary)] font-medium">{e.reason || '-'}</TableCell>
|
||||
<TableCell className="text-xs text-[var(--dg-text-muted)] font-mono">{e.object || '-'}</TableCell>
|
||||
<TableCell className="text-xs text-[var(--dg-text-secondary)] max-w-[400px]">
|
||||
<div className={`truncate ${isExpanded ? 'whitespace-normal' : ''}`} title={!isExpanded ? e.message : undefined}>
|
||||
{e.message || '-'}
|
||||
</div>
|
||||
{isExpanded && (
|
||||
<div className="mt-1 pt-1 border-t border-[var(--dg-border)] text-[10px] text-[var(--dg-text-muted)] space-y-0.5">
|
||||
<div>首次: {formatTime(e.first_time)} ({formatRelative(e.first_time)})</div>
|
||||
<div>最后: {formatTime(e.last_time)} ({formatRelative(e.last_time)})</div>
|
||||
<div>名称: {e.name}</div>
|
||||
</div>
|
||||
)}
|
||||
</TableCell>
|
||||
<TableCell className="text-sm text-[var(--dg-text-muted)] text-center tabular-nums">{e.count > 1 ? e.count : 1}</TableCell>
|
||||
<TableCell className="text-xs text-[var(--dg-text-muted)]">{e.namespace || '-'}</TableCell>
|
||||
<TableCell className="text-xs text-[var(--dg-text-muted)]">
|
||||
<div>{formatTime(e.last_time)}</div>
|
||||
<div className="text-[10px]">{formatRelative(e.last_time)}</div>
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
);
|
||||
})}
|
||||
</TableBody>
|
||||
</Table>
|
||||
{displayEvents.length > 200 && (
|
||||
<div className="text-center py-2 text-xs text-[var(--dg-text-muted)] border-t border-[var(--dg-border)]">
|
||||
仅显示前 200 条,共 {displayEvents.length} 条
|
||||
</div>
|
||||
)}
|
||||
<EventTable
|
||||
events={displayEvents}
|
||||
loading={loading}
|
||||
showNamespace
|
||||
maxRows={200}
|
||||
/>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
|
||||
@@ -1,49 +1,13 @@
|
||||
// deployment-view/EventsTab.tsx — 事件 Tab // Logan.Li
|
||||
import { Badge } from '@/components/ui/badge';
|
||||
import {
|
||||
Table, TableBody, TableCell, TableHead, TableHeader, TableRow,
|
||||
} from '@/components/ui/table';
|
||||
import { Loader2 } from 'lucide-react';
|
||||
import { formatK8sTime } from '../k8s-yaml-utils';
|
||||
import { EventTable } from '../EventTable';
|
||||
import type { EventItem } from './types';
|
||||
|
||||
export function EventsTab({ events, loading }: { events: EventItem[]; loading: string }) {
|
||||
if (loading === 'events') {
|
||||
return <div className="flex items-center justify-center py-12"><Loader2 className="h-6 w-6 animate-spin text-[var(--dg-text-muted)]" /></div>;
|
||||
}
|
||||
if (events.length === 0) {
|
||||
return <div className="text-center text-[var(--dg-text-muted)] py-12">暂无事件</div>;
|
||||
}
|
||||
return (
|
||||
<div className="overflow-x-auto">
|
||||
<Table>
|
||||
<TableHeader>
|
||||
<TableRow className="border-[var(--dg-border)]">
|
||||
<TableHead className="text-[var(--dg-text-muted)] w-16">类型</TableHead>
|
||||
<TableHead className="text-[var(--dg-text-muted)] w-24">原因</TableHead>
|
||||
<TableHead className="text-[var(--dg-text-muted)]">消息</TableHead>
|
||||
<TableHead className="text-[var(--dg-text-muted)] w-20">来源</TableHead>
|
||||
<TableHead className="text-[var(--dg-text-muted)] w-10">次数</TableHead>
|
||||
<TableHead className="text-[var(--dg-text-muted)] w-40">最后时间</TableHead>
|
||||
</TableRow>
|
||||
</TableHeader>
|
||||
<TableBody>
|
||||
{events.map((e, i) => (
|
||||
<TableRow key={i} className="border-[var(--dg-border)]">
|
||||
<TableCell>
|
||||
<Badge variant="outline" className={e.type === 'Warning' ? 'text-[var(--dg-yellow)] border-[var(--dg-yellow)]/30' : 'text-[var(--dg-text-muted)] border-[var(--dg-border)]'}>
|
||||
{e.type}
|
||||
</Badge>
|
||||
</TableCell>
|
||||
<TableCell className="text-sm text-[var(--dg-text-secondary)]">{e.reason}</TableCell>
|
||||
<TableCell className="text-sm text-[var(--dg-text-primary)] max-w-md whitespace-normal break-words">{e.message}</TableCell>
|
||||
<TableCell className="text-xs text-[var(--dg-text-muted)]">{e.source}</TableCell>
|
||||
<TableCell className="text-sm text-[var(--dg-text-muted)]">{e.count}</TableCell>
|
||||
<TableCell className="text-xs text-[var(--dg-text-muted)]">{formatK8sTime(e.lastTimestamp)}</TableCell>
|
||||
</TableRow>
|
||||
))}
|
||||
</TableBody>
|
||||
</Table>
|
||||
</div>
|
||||
<EventTable
|
||||
events={events}
|
||||
loading={loading}
|
||||
emptyText="暂无事件"
|
||||
/>
|
||||
);
|
||||
}
|
||||
@@ -19,10 +19,7 @@ export interface IngItem {
|
||||
name: string; className: string; hosts: string[]; rules: any[]; age: string;
|
||||
}
|
||||
|
||||
export interface EventItem {
|
||||
type: string; reason: string; message: string; source: string;
|
||||
firstTimestamp: string; lastTimestamp: string; count: number;
|
||||
}
|
||||
export type EventItem = import('../k8s-event-utils').NormalizedEvent;
|
||||
|
||||
export interface DeploymentViewProps {
|
||||
clusterCode: string; namespace: string; deployment: any;
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
// k8s-event-utils — K8s Event 字段归一化与展示 // Logan.Li
|
||||
import { elapsedSeconds, formatDuration, formatK8sTime } from './k8s-yaml-utils';
|
||||
|
||||
export interface NormalizedEvent {
|
||||
name: string;
|
||||
namespace: string;
|
||||
type: string;
|
||||
reason: string;
|
||||
message: string;
|
||||
object: string;
|
||||
objectKind: string;
|
||||
objectName: string;
|
||||
source: string;
|
||||
count: number;
|
||||
firstTime: string;
|
||||
lastTime: string;
|
||||
eventTime: string;
|
||||
}
|
||||
|
||||
/** 后端/API 原始事件 → 统一结构 // Logan.Li */
|
||||
export function normalizeK8sEvent(raw: Record<string, any>): NormalizedEvent {
|
||||
const objectKind = raw.object_kind || raw.objectKind || '';
|
||||
const objectName = raw.object_name || raw.objectName || '';
|
||||
const object = raw.object
|
||||
|| (objectKind && objectName ? `${objectKind}/${objectName}` : '')
|
||||
|| '';
|
||||
const firstTime = raw.first_time || raw.firstTime || raw.first_timestamp || raw.firstTimestamp || '';
|
||||
const lastTime = raw.last_time || raw.lastTime || raw.last_timestamp || raw.lastTimestamp || '';
|
||||
const eventTime = raw.event_time || raw.eventTime || '';
|
||||
const count = Number(raw.count) > 0 ? Number(raw.count) : 1;
|
||||
const source = raw.source
|
||||
|| raw.source_component
|
||||
|| raw.sourceComponent
|
||||
|| (raw.source?.component
|
||||
? `${raw.source.component}${raw.source.host ? `@${raw.source.host}` : ''}`
|
||||
: '')
|
||||
|| '-';
|
||||
|
||||
return {
|
||||
name: raw.name || '',
|
||||
namespace: raw.namespace || '',
|
||||
type: raw.type || 'Normal',
|
||||
reason: raw.reason || '-',
|
||||
message: raw.message || '-',
|
||||
object: object || '-',
|
||||
objectKind,
|
||||
objectName,
|
||||
source: source || '-',
|
||||
count,
|
||||
firstTime: firstTime || eventTime,
|
||||
lastTime: lastTime || eventTime,
|
||||
eventTime,
|
||||
};
|
||||
}
|
||||
|
||||
export function formatEventRelative(ts: string): string {
|
||||
if (!ts) return '';
|
||||
const diff = Date.now() - new Date(ts).getTime();
|
||||
if (diff < 0) return '';
|
||||
const mins = Math.floor(diff / 60000);
|
||||
if (mins < 1) return '刚刚';
|
||||
if (mins < 60) return `${mins}分钟前`;
|
||||
const hrs = Math.floor(mins / 60);
|
||||
if (hrs < 24) return `${hrs}小时前`;
|
||||
return `${Math.floor(hrs / 24)}天前`;
|
||||
}
|
||||
|
||||
/** 重复跨度:首次 → 最后 // Logan.Li */
|
||||
export function formatEventRepeatSpan(first: string, last: string, count: number): string {
|
||||
if (count <= 1) return '-';
|
||||
const span = formatDuration(elapsedSeconds(first, last));
|
||||
return span === '-' ? `×${count}` : `${span} · ×${count}`;
|
||||
}
|
||||
|
||||
export function formatEventTimeCell(ts: string): { absolute: string; relative: string } {
|
||||
return { absolute: formatK8sTime(ts), relative: formatEventRelative(ts) };
|
||||
}
|
||||
Reference in new issue
Block a user