|
1 | 1 | <script setup lang="ts"> |
| 2 | +import { ref, onMounted, onUnmounted } from 'vue' |
| 3 | +import { useI18n } from 'vue-i18n' |
2 | 4 | import Card from '@/components/ui/card/Card.vue' |
3 | 5 | import { Item } from '@/components/ui/item' |
| 6 | +import { McpClientActivityService } from '@/services/mcpClientActivityService' |
| 7 | +import type { McpClientActivity } from '@/services/mcpClientActivityService' |
4 | 8 |
|
5 | | -interface ClientConnection { |
6 | | - id: string |
7 | | - name: string |
8 | | - type: string |
9 | | - connectedAt: string |
10 | | - status: 'active' | 'inactive' |
| 9 | +const { t } = useI18n() |
| 10 | +
|
| 11 | +const activities = ref<McpClientActivity[]>([]) |
| 12 | +const isLoading = ref(false) |
| 13 | +const error = ref<string | null>(null) |
| 14 | +let pollingInterval: number | null = null |
| 15 | +
|
| 16 | +function getRelativeTime(isoString: string): string { |
| 17 | + const now = new Date() |
| 18 | + const activityDate = new Date(isoString) |
| 19 | + const diffMs = now.getTime() - activityDate.getTime() |
| 20 | + const diffMinutes = Math.floor(diffMs / 60000) |
| 21 | + |
| 22 | + if (diffMinutes < 1) return t('mcpServer.clientConnections.activity.justNow') |
| 23 | + if (diffMinutes === 1) return t('mcpServer.clientConnections.activity.minuteAgo') |
| 24 | + if (diffMinutes < 60) return t('mcpServer.clientConnections.activity.minutesAgo', { count: diffMinutes }) |
| 25 | + |
| 26 | + const diffHours = Math.floor(diffMinutes / 60) |
| 27 | + if (diffHours === 1) return t('mcpServer.clientConnections.activity.hourAgo') |
| 28 | + if (diffHours < 24) return t('mcpServer.clientConnections.activity.hoursAgo', { count: diffHours }) |
| 29 | + |
| 30 | + const diffDays = Math.floor(diffHours / 24) |
| 31 | + if (diffDays === 1) return t('mcpServer.clientConnections.activity.dayAgo') |
| 32 | + return t('mcpServer.clientConnections.activity.daysAgo', { count: diffDays }) |
| 33 | +} |
| 34 | +
|
| 35 | +async function fetchClientActivity() { |
| 36 | + try { |
| 37 | + isLoading.value = true |
| 38 | + error.value = null |
| 39 | + |
| 40 | + const response = await McpClientActivityService.getMyClientActivity({ |
| 41 | + limit: 20, |
| 42 | + active_within_minutes: 30 |
| 43 | + }) |
| 44 | + |
| 45 | + activities.value = response.data.activities |
| 46 | + } catch (err) { |
| 47 | + error.value = err instanceof Error ? err.message : t('mcpServer.clientConnections.error.failed') |
| 48 | + console.error('Failed to fetch MCP client activity:', err) |
| 49 | + } finally { |
| 50 | + isLoading.value = false |
| 51 | + } |
| 52 | +} |
| 53 | +
|
| 54 | +function startPolling() { |
| 55 | + pollingInterval = window.setInterval(() => { |
| 56 | + fetchClientActivity() |
| 57 | + }, 30000) |
11 | 58 | } |
12 | 59 |
|
13 | | -const dummyClients: ClientConnection[] = [ |
14 | | - { |
15 | | - id: '1', |
16 | | - name: 'VS Code', |
17 | | - type: 'Desktop Client', |
18 | | - connectedAt: '2 hours ago', |
19 | | - status: 'active' |
20 | | - }, |
21 | | - { |
22 | | - id: '2', |
23 | | - name: 'Cursor', |
24 | | - type: 'Desktop Client', |
25 | | - connectedAt: '5 minutes ago', |
26 | | - status: 'active' |
| 60 | +function stopPolling() { |
| 61 | + if (pollingInterval !== null) { |
| 62 | + clearInterval(pollingInterval) |
| 63 | + pollingInterval = null |
27 | 64 | } |
28 | | -] |
| 65 | +} |
| 66 | +
|
| 67 | +onMounted(async () => { |
| 68 | + await fetchClientActivity() |
| 69 | + startPolling() |
| 70 | +}) |
| 71 | +
|
| 72 | +onUnmounted(() => { |
| 73 | + stopPolling() |
| 74 | +}) |
29 | 75 | </script> |
30 | 76 |
|
31 | 77 | <template> |
32 | 78 | <Card variant="gray"> |
33 | 79 | <div class="px-6"> |
34 | | - <h3 class="text-lg font-semibold mb-4">Client Connections</h3> |
35 | | - <div class="space-y-3"> |
| 80 | + <h3 class="text-lg font-semibold mb-4"> |
| 81 | + {{ t('mcpServer.clientConnections.title') }} |
| 82 | + </h3> |
| 83 | + |
| 84 | + <div v-if="isLoading && activities.length === 0" class="text-center py-8 text-muted-foreground"> |
| 85 | + {{ t('mcpServer.clientConnections.loading') }} |
| 86 | + </div> |
| 87 | + |
| 88 | + <div v-else-if="error" class="text-center py-8"> |
| 89 | + <p class="text-sm text-destructive mb-2">{{ error }}</p> |
| 90 | + <button |
| 91 | + @click="fetchClientActivity" |
| 92 | + class="text-sm text-primary hover:underline" |
| 93 | + > |
| 94 | + {{ t('mcpServer.clientConnections.error.retry') }} |
| 95 | + </button> |
| 96 | + </div> |
| 97 | + |
| 98 | + <div v-else-if="activities.length === 0" class="text-center py-8 text-muted-foreground"> |
| 99 | + <p class="mb-2">{{ t('mcpServer.clientConnections.empty.title') }}</p> |
| 100 | + <p class="text-xs">{{ t('mcpServer.clientConnections.empty.description') }}</p> |
| 101 | + </div> |
| 102 | + |
| 103 | + <div v-else class="space-y-3"> |
36 | 104 | <Item |
37 | | - v-for="client in dummyClients" |
38 | | - :key="client.id" |
| 105 | + v-for="activity in activities" |
| 106 | + :key="activity.id" |
39 | 107 | variant="filled" |
40 | 108 | class="cursor-pointer" |
41 | 109 | > |
42 | 110 | <div class="flex items-center justify-between w-full"> |
43 | 111 | <div class="flex flex-col"> |
44 | | - <span class="font-medium">{{ client.name }}</span> |
45 | | - <span class="text-sm text-muted-foreground">{{ client.type }}</span> |
| 112 | + <span class="font-medium"> |
| 113 | + {{ activity.client_name || t('mcpServer.clientConnections.client.unknown') }} |
| 114 | + </span> |
| 115 | + <span class="text-sm text-muted-foreground">{{ activity.satellite.name }}</span> |
46 | 116 | </div> |
47 | 117 | <div class="flex flex-col items-end"> |
48 | | - <span class="text-xs text-muted-foreground">{{ client.connectedAt }}</span> |
49 | | - <span |
50 | | - class="text-xs font-medium" |
51 | | - :class="{ |
52 | | - 'text-green-600': client.status === 'active', |
53 | | - 'text-gray-400': client.status === 'inactive' |
54 | | - }" |
55 | | - > |
56 | | - {{ client.status }} |
| 118 | + <span class="text-xs text-muted-foreground"> |
| 119 | + {{ getRelativeTime(activity.last_activity_at) }} |
| 120 | + </span> |
| 121 | + <span class="text-xs text-green-600 font-medium"> |
| 122 | + {{ t('mcpServer.clientConnections.activity.stats', { |
| 123 | + requests: activity.total_requests, |
| 124 | + tools: activity.total_tool_calls |
| 125 | + }) }} |
57 | 126 | </span> |
58 | 127 | </div> |
59 | 128 | </div> |
|
0 commit comments