mirror of
https://github.com/lukaszraczylo/gohoarder.git
synced 2026-11-26 06:37:07 +00:00
feat(frontend): rebuild the dashboard and stats on library components
KpiTile and StatGroup carry the totals, BarChart draws download activity, ActivityFeed lists live events and DataTable lists recent packages and registries. Loading, empty and error states use the library states. Severity badges are StatusBadge with the tones critical danger, high warning, moderate info and low neutral, and always print the word.
This commit is contained in:
@@ -17,7 +17,7 @@ describe('Dashboard.vue', () => {
|
||||
|
||||
it('renders dashboard component', () => {
|
||||
const wrapper = mount(Dashboard)
|
||||
expect(wrapper.find('h2').text()).toBe('Dashboard')
|
||||
expect(wrapper.find('h1').text()).toBe('Dashboard')
|
||||
})
|
||||
|
||||
it('displays error message when error occurs', async () => {
|
||||
@@ -57,9 +57,9 @@ describe('Dashboard.vue', () => {
|
||||
}
|
||||
await wrapper.vm.$nextTick()
|
||||
|
||||
expect(wrapper.text()).toContain('Total Packages')
|
||||
expect(wrapper.text()).toContain('Total Size')
|
||||
expect(wrapper.text()).toContain('Total Downloads')
|
||||
expect(wrapper.text()).toContain('Total packages')
|
||||
expect(wrapper.text()).toContain('Total size')
|
||||
expect(wrapper.text()).toContain('Total downloads')
|
||||
})
|
||||
|
||||
it('displays total packages from stats', async () => {
|
||||
@@ -141,7 +141,7 @@ describe('Dashboard.vue', () => {
|
||||
]
|
||||
await wrapper.vm.$nextTick()
|
||||
|
||||
expect(wrapper.text()).toContain('Recent Packages')
|
||||
expect(wrapper.text()).toContain('Recent packages')
|
||||
})
|
||||
|
||||
it('shows recent packages sorted by cached_at', async () => {
|
||||
@@ -173,7 +173,7 @@ describe('Dashboard.vue', () => {
|
||||
]
|
||||
await wrapper.vm.$nextTick()
|
||||
|
||||
const recentPackagesSection = wrapper.text().split('Recent Packages')[1]
|
||||
const recentPackagesSection = wrapper.text().split('Recent packages')[1]
|
||||
|
||||
// new-package should appear before old-package since it's more recent
|
||||
expect(recentPackagesSection.indexOf('new-package')).toBeLessThan(
|
||||
@@ -198,7 +198,7 @@ describe('Dashboard.vue', () => {
|
||||
}))
|
||||
await wrapper.vm.$nextTick()
|
||||
|
||||
const recentSection = wrapper.text().split('Recent Packages')[1]
|
||||
const recentSection = wrapper.text().split('Recent packages')[1]
|
||||
// Count how many "package" strings appear (each package has the word "package" in its name)
|
||||
const packageCount = (recentSection.match(/package\d+/g) || []).length
|
||||
expect(packageCount).toBeLessThanOrEqual(10)
|
||||
|
||||
@@ -1,243 +1,134 @@
|
||||
<template>
|
||||
<div>
|
||||
<div class="flex items-center justify-between mb-8">
|
||||
<h2 class="text-3xl font-bold text-gray-900">Dashboard</h2>
|
||||
<div
|
||||
class="flex items-center gap-2 px-3 py-1 rounded-full border text-xs font-medium"
|
||||
:class="
|
||||
rt.connected
|
||||
? 'bg-emerald-50 border-emerald-200 text-emerald-700'
|
||||
: 'bg-gray-100 border-gray-200 text-gray-500'
|
||||
"
|
||||
data-testid="ws-live-indicator"
|
||||
>
|
||||
<span
|
||||
class="w-2 h-2 rounded-full"
|
||||
:class="rt.connected ? 'bg-emerald-500 animate-pulse' : 'bg-gray-400'"
|
||||
></span>
|
||||
{{ rt.connected ? 'Live' : 'Offline' }}
|
||||
</div>
|
||||
</div>
|
||||
<div class="flex flex-col gap-6">
|
||||
<PageHeader title="Dashboard" description="What the cache holds and what is moving through it.">
|
||||
<template #actions>
|
||||
<StatusBadge
|
||||
:status="rt.connected ? 'live' : 'offline'"
|
||||
:label="rt.connected ? 'Live' : 'Offline'"
|
||||
:tones="{ live: 'success', offline: 'neutral' }"
|
||||
:pulse="rt.connected"
|
||||
data-testid="ws-live-indicator"
|
||||
/>
|
||||
</template>
|
||||
</PageHeader>
|
||||
|
||||
<!-- Error Alert -->
|
||||
<Alert v-if="error" variant="destructive" class="mb-4">
|
||||
<i class="fas fa-exclamation-circle mr-2"></i>
|
||||
<AlertDescription>{{ error }}</AlertDescription>
|
||||
</Alert>
|
||||
<ErrorState
|
||||
v-if="error"
|
||||
variant="card"
|
||||
title="Could not load statistics"
|
||||
:message="error"
|
||||
@retry="refresh"
|
||||
/>
|
||||
|
||||
<!-- Loading State -->
|
||||
<div v-if="loading" class="text-center py-12">
|
||||
<i class="fas fa-spinner fa-spin text-4xl text-primary-600"></i>
|
||||
<p class="mt-4 text-gray-600">Loading statistics...</p>
|
||||
</div>
|
||||
<LoadingState v-if="loading" variant="kpi-grid" :columns="4" label="Loading statistics..." />
|
||||
|
||||
<!-- Stats Grid -->
|
||||
<div v-else class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6 mb-10">
|
||||
<Card class="border-0 shadow-lg hover:shadow-xl transition-shadow">
|
||||
<CardContent class="p-6">
|
||||
<div class="flex items-start justify-between">
|
||||
<div class="space-y-2">
|
||||
<p class="text-sm font-medium text-muted-foreground">Total Packages</p>
|
||||
<p class="text-3xl font-bold text-foreground tracking-tight">
|
||||
{{ formatNumber(displayStats?.total_packages || 0) }}
|
||||
</p>
|
||||
</div>
|
||||
<div class="w-12 h-12 bg-slate-100 rounded-xl flex items-center justify-center">
|
||||
<i class="fas fa-boxes text-slate-700 text-lg"></i>
|
||||
</div>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
<StatGroup v-else :columns="4" aria-label="Cache totals">
|
||||
<KpiTile label="Total packages" :value="displayStats?.total_packages || 0" :icon="faBoxesStacked" />
|
||||
<KpiTile label="Total size" :value="formatBytes(displayStats?.total_size || 0)" :icon="faHardDrive" />
|
||||
<KpiTile label="Total downloads" :value="displayStats?.total_downloads || 0" :icon="faDownload" />
|
||||
<KpiTile label="Scanned packages" :value="displayStats?.scanned_packages || 0" :icon="faShieldHalved" />
|
||||
</StatGroup>
|
||||
|
||||
<Card class="border-0 shadow-lg hover:shadow-xl transition-shadow">
|
||||
<CardContent class="p-6">
|
||||
<div class="flex items-start justify-between">
|
||||
<div class="space-y-2">
|
||||
<p class="text-sm font-medium text-muted-foreground">Total Size</p>
|
||||
<p class="text-3xl font-bold text-foreground tracking-tight">
|
||||
{{ formatBytes(displayStats?.total_size || 0) }}
|
||||
</p>
|
||||
</div>
|
||||
<div class="w-12 h-12 bg-sky-50 rounded-xl flex items-center justify-center">
|
||||
<i class="fas fa-hard-drive text-sky-600 text-lg"></i>
|
||||
</div>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<Card class="border-0 shadow-lg hover:shadow-xl transition-shadow">
|
||||
<CardContent class="p-6">
|
||||
<div class="flex items-start justify-between">
|
||||
<div class="space-y-2">
|
||||
<p class="text-sm font-medium text-muted-foreground">Total Downloads</p>
|
||||
<p class="text-3xl font-bold text-foreground tracking-tight">
|
||||
{{ formatNumber(displayStats?.total_downloads || 0) }}
|
||||
</p>
|
||||
</div>
|
||||
<div class="w-12 h-12 bg-emerald-50 rounded-xl flex items-center justify-center">
|
||||
<i class="fas fa-download text-emerald-600 text-lg"></i>
|
||||
</div>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<Card class="border-0 shadow-lg hover:shadow-xl transition-shadow">
|
||||
<CardContent class="p-6">
|
||||
<div class="flex items-start justify-between">
|
||||
<div class="space-y-2">
|
||||
<p class="text-sm font-medium text-muted-foreground">Scanned Packages</p>
|
||||
<p class="text-3xl font-bold text-foreground tracking-tight">
|
||||
{{ formatNumber(displayStats?.scanned_packages || 0) }}
|
||||
</p>
|
||||
</div>
|
||||
<div class="w-12 h-12 bg-violet-50 rounded-xl flex items-center justify-center">
|
||||
<i class="fas fa-shield-alt text-violet-600 text-lg"></i>
|
||||
</div>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
|
||||
<!-- Realtime Feed -->
|
||||
<Card v-if="recentRealtimeEvents.length > 0" class="border-0 shadow-lg mb-10" data-testid="ws-event-feed">
|
||||
<CardContent class="p-6">
|
||||
<h3 class="text-xl font-semibold text-foreground mb-4">
|
||||
<i class="fas fa-bolt mr-2 text-amber-500"></i>Live Activity
|
||||
</h3>
|
||||
<ul class="space-y-2">
|
||||
<li
|
||||
v-for="(evt, idx) in recentRealtimeEvents"
|
||||
:key="`${evt.timestamp}-${idx}`"
|
||||
class="flex items-center gap-3 text-sm"
|
||||
>
|
||||
<component :is="iconForEvent(evt.type)" class="w-4 h-4 text-muted-foreground" />
|
||||
<span class="font-medium">{{ labelForEvent(evt.type) }}</span>
|
||||
<span class="text-muted-foreground truncate">
|
||||
{{ describeEvent(evt) }}
|
||||
</span>
|
||||
<span class="ml-auto text-xs text-muted-foreground">
|
||||
{{ formatTimestamp(evt.timestamp) }}
|
||||
</span>
|
||||
</li>
|
||||
</ul>
|
||||
<Card v-if="recentRealtimeEvents.length > 0" data-testid="ws-event-feed">
|
||||
<CardHeader>
|
||||
<CardTitle as="h2">Live activity</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<ActivityFeed :events="feedEvents" variant="compact" label="Live activity" />
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<!-- Downloads Chart -->
|
||||
<Card class="border-0 shadow-lg mb-10">
|
||||
<CardContent class="p-6">
|
||||
<div class="flex items-center justify-between mb-6">
|
||||
<h3 class="text-xl font-semibold text-foreground">Download Activity</h3>
|
||||
<div class="flex gap-2">
|
||||
<Button
|
||||
v-for="period in chartPeriods"
|
||||
:key="period.value"
|
||||
@click="selectedPeriod = period.value"
|
||||
:variant="selectedPeriod === period.value ? 'default' : 'outline'"
|
||||
size="sm"
|
||||
>
|
||||
{{ period.label }}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="h-64 flex items-end justify-between gap-2">
|
||||
<div
|
||||
v-for="(value, index) in chartData"
|
||||
:key="index"
|
||||
class="flex-1 flex flex-col items-center gap-2"
|
||||
>
|
||||
<div class="w-full bg-slate-100 rounded-t-lg relative" :style="{ height: `${(value / maxChartValue) * 100}%` }">
|
||||
<div class="absolute inset-0 bg-gradient-to-t from-slate-700 to-slate-500 rounded-t-lg"></div>
|
||||
</div>
|
||||
<span class="text-xs text-muted-foreground">{{ getChartLabel(index) }}</span>
|
||||
</div>
|
||||
</div>
|
||||
<div v-if="chartLoading || chartData.length === 0" class="mt-4 text-center">
|
||||
<p class="text-sm text-muted-foreground">
|
||||
<i class="fas fa-info-circle mr-1"></i>
|
||||
{{ chartLoading ? 'Loading chart data...' : 'No download activity in this period' }}
|
||||
</p>
|
||||
</div>
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle as="h2">Download activity</CardTitle>
|
||||
<CardAction>
|
||||
<SegmentedControl v-model="selectedPeriod" :options="chartPeriods" aria-label="Period" />
|
||||
</CardAction>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<LoadingState
|
||||
v-if="chartLoading && chartData.length === 0"
|
||||
layout="inline"
|
||||
label="Loading chart data..."
|
||||
/>
|
||||
<BarChart
|
||||
v-else
|
||||
:labels="chartLabels"
|
||||
:series="[{ name: 'Downloads', data: chartData }]"
|
||||
label="Downloads over the selected period"
|
||||
empty-text="No download activity in this period"
|
||||
/>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<!-- Recent Packages -->
|
||||
<Card><CardContent class="p-6">
|
||||
<h3 class="text-xl font-semibold text-gray-900 mb-4">
|
||||
<i class="fas fa-clock mr-2"></i>Recent Packages
|
||||
</h3>
|
||||
<div v-if="packages.length === 0" class="text-center py-8 text-gray-500">
|
||||
<i class="fas fa-inbox text-4xl mb-4"></i>
|
||||
<p>No packages cached yet</p>
|
||||
</div>
|
||||
<div v-else class="overflow-x-auto">
|
||||
<table class="min-w-full divide-y divide-gray-200">
|
||||
<thead>
|
||||
<tr>
|
||||
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
|
||||
Package
|
||||
</th>
|
||||
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
|
||||
Version
|
||||
</th>
|
||||
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
|
||||
Registry
|
||||
</th>
|
||||
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
|
||||
Size
|
||||
</th>
|
||||
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
|
||||
Downloads
|
||||
</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody class="bg-white divide-y divide-gray-200">
|
||||
<tr v-for="pkg in recentPackages" :key="pkg.id">
|
||||
<td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">
|
||||
{{ pkg.name }}
|
||||
</td>
|
||||
<td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
|
||||
{{ pkg.version }}
|
||||
</td>
|
||||
<td class="px-6 py-4 whitespace-nowrap">
|
||||
<Badge variant="outline" :class="getRegistryBadgeClass(pkg.registry)">
|
||||
{{ pkg.registry }}
|
||||
</Badge>
|
||||
</td>
|
||||
<td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
|
||||
{{ formatBytes(pkg.size) }}
|
||||
</td>
|
||||
<td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
|
||||
{{ formatNumber(pkg.download_count) }}
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle as="h2">Recent packages</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<EmptyState
|
||||
v-if="packages.length === 0"
|
||||
size="sm"
|
||||
:icon="faBoxOpen"
|
||||
title="No packages cached yet"
|
||||
description="Packages appear here once they are downloaded through the proxy."
|
||||
/>
|
||||
<DataTable
|
||||
v-else
|
||||
label="Recent packages"
|
||||
layout="cards"
|
||||
:columns="packageColumns"
|
||||
:rows="recentPackages"
|
||||
>
|
||||
<template #cell-registry="{ row }">
|
||||
<Badge variant="outline">{{ row.registry }}</Badge>
|
||||
</template>
|
||||
</DataTable>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { computed, onMounted, ref, watch, type Component } from 'vue'
|
||||
import { computed, onMounted, ref, watch } from 'vue'
|
||||
import { storeToRefs } from 'pinia'
|
||||
import axios from 'axios'
|
||||
import {
|
||||
Package as PackageIcon,
|
||||
Download as DownloadIcon,
|
||||
Trash2 as TrashIcon,
|
||||
ShieldCheck as ShieldIcon,
|
||||
Activity as ActivityIcon,
|
||||
} from 'lucide-vue-next'
|
||||
import { usePackageStore } from '../stores/packages'
|
||||
ActivityFeed,
|
||||
BarChart,
|
||||
Badge,
|
||||
Card,
|
||||
CardAction,
|
||||
CardContent,
|
||||
CardHeader,
|
||||
CardTitle,
|
||||
DataTable,
|
||||
EmptyState,
|
||||
ErrorState,
|
||||
KpiTile,
|
||||
LoadingState,
|
||||
PageHeader,
|
||||
SegmentedControl,
|
||||
StatGroup,
|
||||
StatusBadge,
|
||||
type ActivityEvent,
|
||||
type DataTableColumn,
|
||||
} from '@perfect-design/ui'
|
||||
import type { IconDefinition } from '@perfect-design/ui/lib/icons-tools'
|
||||
import { usePackageStore, type Package } from '../stores/packages'
|
||||
import { useRealtimeStore } from '../stores/realtime'
|
||||
import type { EventType, RealtimeEvent } from '@/lib/ws'
|
||||
import { Alert, AlertDescription } from '@/components/ui/alert'
|
||||
import { Card, CardContent } from '@/components/ui/card'
|
||||
import { Badge } from '@/components/ui/badge'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { getRegistryBadgeClass } from '@/composables/useBadgeStyles'
|
||||
import { formatBytes, formatNumber } from '@/lib/format'
|
||||
import {
|
||||
faBox,
|
||||
faBoxOpen,
|
||||
faBoxesStacked,
|
||||
faChartLine,
|
||||
faDownload,
|
||||
faHardDrive,
|
||||
faShieldHalved,
|
||||
faTrash,
|
||||
} from '@/lib/icons'
|
||||
|
||||
const store = usePackageStore()
|
||||
const { packages, stats, loading, error } = storeToRefs(store)
|
||||
@@ -261,18 +152,28 @@ const EVENT_LABELS: Record<EventType, string> = {
|
||||
stats_update: 'Stats update',
|
||||
}
|
||||
|
||||
const EVENT_ICONS: Record<EventType, Component> = {
|
||||
package_cached: PackageIcon,
|
||||
package_deleted: TrashIcon,
|
||||
package_downloaded: DownloadIcon,
|
||||
scan_complete: ShieldIcon,
|
||||
stats_update: ActivityIcon,
|
||||
const EVENT_ICONS: Record<EventType, IconDefinition> = {
|
||||
package_cached: faBox,
|
||||
package_deleted: faTrash,
|
||||
package_downloaded: faDownload,
|
||||
scan_complete: faShieldHalved,
|
||||
stats_update: faChartLine,
|
||||
}
|
||||
|
||||
function iconForEvent(type: EventType): Component {
|
||||
return EVENT_ICONS[type] ?? ActivityIcon
|
||||
function iconForEvent(type: EventType): IconDefinition {
|
||||
return EVENT_ICONS[type] ?? faChartLine
|
||||
}
|
||||
|
||||
const feedEvents = computed<ActivityEvent[]>(() =>
|
||||
recentRealtimeEvents.value.map((evt, idx) => ({
|
||||
id: `${evt.timestamp}-${idx}`,
|
||||
action: labelForEvent(evt.type),
|
||||
target: describeEvent(evt) || undefined,
|
||||
time: evt.timestamp,
|
||||
icon: iconForEvent(evt.type),
|
||||
})),
|
||||
)
|
||||
|
||||
function labelForEvent(type: EventType): string {
|
||||
return EVENT_LABELS[type] ?? type
|
||||
}
|
||||
@@ -287,18 +188,13 @@ function describeEvent(evt: RealtimeEvent): string {
|
||||
return ''
|
||||
}
|
||||
|
||||
function formatTimestamp(ts: number): string {
|
||||
if (!ts) return ''
|
||||
return new Date(ts).toLocaleTimeString()
|
||||
}
|
||||
|
||||
// Chart periods and data
|
||||
const selectedPeriod = ref<string>('1day')
|
||||
const chartPeriods = [
|
||||
{ value: '1h', label: '1 Hour' },
|
||||
{ value: '1day', label: '24 Hours' },
|
||||
{ value: '7day', label: '7 Days' },
|
||||
{ value: '30day', label: '30 Days' },
|
||||
{ value: '1h', label: '1 hour' },
|
||||
{ value: '1day', label: '24 hours' },
|
||||
{ value: '7day', label: '7 days' },
|
||||
{ value: '30day', label: '30 days' },
|
||||
]
|
||||
|
||||
// Time-series data from API
|
||||
@@ -338,11 +234,20 @@ const chartData = computed(() => {
|
||||
return timeSeriesData.value.data_points.map(point => point.value)
|
||||
})
|
||||
|
||||
const maxChartValue = computed(() => {
|
||||
if (chartData.value.length === 0) return 100
|
||||
const max = Math.max(...chartData.value)
|
||||
return max === 0 ? 100 : max
|
||||
})
|
||||
const chartLabels = computed(() => chartData.value.map((_, index) => getChartLabel(index)))
|
||||
|
||||
const packageColumns: DataTableColumn<Package>[] = [
|
||||
{ key: 'name', header: 'Package', rowHeader: true },
|
||||
{ key: 'version', header: 'Version' },
|
||||
{ key: 'registry', header: 'Registry' },
|
||||
{ key: 'size', header: 'Size', numeric: true, accessor: (pkg) => formatBytes(pkg.size) },
|
||||
{ key: 'download_count', header: 'Downloads', numeric: true, accessor: (pkg) => formatNumber(pkg.download_count) },
|
||||
]
|
||||
|
||||
function refresh() {
|
||||
void store.fetchStats()
|
||||
void store.fetchPackages()
|
||||
}
|
||||
|
||||
function getChartLabel(index: number): string {
|
||||
// Use timestamp from data if available
|
||||
@@ -396,15 +301,4 @@ onMounted(async () => {
|
||||
await fetchTimeSeriesData()
|
||||
})
|
||||
|
||||
function formatNumber(num: number): string {
|
||||
return new Intl.NumberFormat().format(num)
|
||||
}
|
||||
|
||||
function formatBytes(bytes: number): string {
|
||||
if (bytes === 0) return '0 B'
|
||||
const k = 1024
|
||||
const sizes = ['B', 'KB', 'MB', 'GB', 'TB']
|
||||
const i = Math.floor(Math.log(bytes) / Math.log(k))
|
||||
return Math.round(bytes / Math.pow(k, i) * 100) / 100 + ' ' + sizes[i]
|
||||
}
|
||||
</script>
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
<template>
|
||||
<StatGroup :columns="4" :aria-label="label">
|
||||
<KpiTile v-for="level in SEVERITY_LEVELS" :key="level" :label="formatSeverityName(level)" :value="counts[level]">
|
||||
<template #icon><StatusDot :tone="SEVERITY_TONES[level]" /></template>
|
||||
</KpiTile>
|
||||
</StatGroup>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { KpiTile, StatGroup, StatusDot } from '@perfect-design/ui'
|
||||
import type { VulnerabilityCounts } from '../stores/packages'
|
||||
import { SEVERITY_LEVELS, SEVERITY_TONES, formatSeverityName } from '@/lib/severity'
|
||||
|
||||
defineProps<{
|
||||
counts: VulnerabilityCounts
|
||||
label: string
|
||||
}>()
|
||||
</script>
|
||||
@@ -1,17 +1,22 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest'
|
||||
import { mount } from '@vue/test-utils'
|
||||
import { mount as mountWith } from '@vue/test-utils'
|
||||
import { createPinia, setActivePinia } from 'pinia'
|
||||
import Stats from './Stats.vue'
|
||||
import { createTestRouter } from '@/test/router'
|
||||
import { usePackageStore } from '../stores/packages'
|
||||
|
||||
describe('Stats.vue', () => {
|
||||
beforeEach(() => {
|
||||
let router: Awaited<ReturnType<typeof createTestRouter>>
|
||||
const mount = (component: typeof Stats) => mountWith(component, { global: { plugins: [router] } })
|
||||
|
||||
beforeEach(async () => {
|
||||
setActivePinia(createPinia())
|
||||
router = await createTestRouter('/stats')
|
||||
})
|
||||
|
||||
it('renders stats component', () => {
|
||||
const wrapper = mount(Stats)
|
||||
expect(wrapper.find('h2').text()).toBe('Statistics')
|
||||
expect(wrapper.find('h1').text()).toBe('Statistics')
|
||||
})
|
||||
|
||||
it('displays loading state when loading', async () => {
|
||||
@@ -51,7 +56,7 @@ describe('Stats.vue', () => {
|
||||
}
|
||||
await wrapper.vm.$nextTick()
|
||||
|
||||
expect(wrapper.text()).toContain('Overall Statistics')
|
||||
expect(wrapper.text()).toContain('Overall statistics')
|
||||
expect(wrapper.text()).toContain('100')
|
||||
expect(wrapper.text()).toContain('500')
|
||||
})
|
||||
@@ -73,9 +78,9 @@ describe('Stats.vue', () => {
|
||||
}
|
||||
await wrapper.vm.$nextTick()
|
||||
|
||||
expect(wrapper.text()).toContain('Security Scanning')
|
||||
expect(wrapper.text()).toContain('Scanned Packages')
|
||||
expect(wrapper.text()).toContain('Vulnerable Packages')
|
||||
expect(wrapper.text()).toContain('Security scanning')
|
||||
expect(wrapper.text()).toContain('Scanned packages')
|
||||
expect(wrapper.text()).toContain('Vulnerable packages')
|
||||
})
|
||||
|
||||
it('displays registry breakdown', async () => {
|
||||
@@ -112,13 +117,15 @@ describe('Stats.vue', () => {
|
||||
}
|
||||
await wrapper.vm.$nextTick()
|
||||
|
||||
expect(wrapper.text()).toContain('Registry Breakdown')
|
||||
expect(wrapper.text()).toContain('NPM Registry')
|
||||
expect(wrapper.text()).toContain('PyPI Registry')
|
||||
expect(wrapper.text()).toContain('Go Modules')
|
||||
expect(wrapper.text()).toContain('50 packages')
|
||||
expect(wrapper.text()).toContain('30 packages')
|
||||
expect(wrapper.text()).toContain('20 packages')
|
||||
expect(wrapper.text()).toContain('Registry breakdown')
|
||||
expect(wrapper.text()).toContain('npm')
|
||||
expect(wrapper.text()).toContain('PyPI')
|
||||
expect(wrapper.text()).toContain('Go modules')
|
||||
// The cards layout prefixes each cell with its column label.
|
||||
const packageCounts = wrapper
|
||||
.findAll('tbody tr')
|
||||
.map((row) => row.findAll('td')[0].text().replace(/^Packages/, ''))
|
||||
expect(packageCounts).toEqual(['50', '30', '20'])
|
||||
})
|
||||
|
||||
it('formats bytes correctly in overall stats', async () => {
|
||||
@@ -150,7 +157,7 @@ describe('Stats.vue', () => {
|
||||
expect(fetchSpy).toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('displays correct icon colors for different registries', async () => {
|
||||
it('lists the registries in the order the API reports them', async () => {
|
||||
const wrapper = mount(Stats)
|
||||
const store = usePackageStore()
|
||||
|
||||
@@ -172,13 +179,8 @@ describe('Stats.vue', () => {
|
||||
}
|
||||
await wrapper.vm.$nextTick()
|
||||
|
||||
// Scope to the registry icon containers (w-12 h-12 rounded-full),
|
||||
// excluding the storage progress bar which also uses rounded-full.
|
||||
const containers = wrapper.findAll('.w-12.h-12.rounded-full')
|
||||
expect(containers).toHaveLength(3)
|
||||
expect(containers[0].classes()).toContain('bg-red-100') // npm
|
||||
expect(containers[1].classes()).toContain('bg-blue-100') // pypi
|
||||
expect(containers[2].classes()).toContain('bg-cyan-100') // go
|
||||
const labels = wrapper.findAll('tbody th').map((cell) => cell.text().replace(/^Registry/, ''))
|
||||
expect(labels).toEqual(['npm', 'PyPI', 'Go modules'])
|
||||
})
|
||||
|
||||
it('handles empty registries data', async () => {
|
||||
@@ -199,9 +201,8 @@ describe('Stats.vue', () => {
|
||||
store.registries = {}
|
||||
await wrapper.vm.$nextTick()
|
||||
|
||||
expect(wrapper.text()).toContain('Registry Breakdown')
|
||||
// Should have no registry items
|
||||
const registryItems = wrapper.findAll('.bg-gray-50')
|
||||
expect(registryItems.length).toBe(3) // Only the overall stats cards
|
||||
expect(wrapper.text()).toContain('Registry breakdown')
|
||||
expect(wrapper.findAll('tbody th')).toHaveLength(0)
|
||||
expect(wrapper.text()).toContain('No registries reported')
|
||||
})
|
||||
})
|
||||
|
||||
+125
-184
@@ -1,150 +1,87 @@
|
||||
<template>
|
||||
<div>
|
||||
<h2 class="text-3xl font-bold text-gray-900 mb-8">Statistics</h2>
|
||||
<div class="flex flex-col gap-6">
|
||||
<PageHeader title="Statistics" description="Storage, traffic and scan results across every registry." />
|
||||
|
||||
<!-- Error Alert -->
|
||||
<Alert v-if="error" variant="destructive" class="mb-4">
|
||||
<i class="fas fa-exclamation-circle mr-2"></i>
|
||||
<AlertDescription>{{ error }}</AlertDescription>
|
||||
</Alert>
|
||||
<ErrorState
|
||||
v-if="error"
|
||||
variant="card"
|
||||
title="Could not load statistics"
|
||||
:message="error"
|
||||
@retry="refresh"
|
||||
/>
|
||||
|
||||
<!-- Loading State -->
|
||||
<div v-if="loading" class="text-center py-12">
|
||||
<i class="fas fa-spinner fa-spin text-4xl text-primary-600"></i>
|
||||
<p class="mt-4 text-gray-600">Loading statistics...</p>
|
||||
</div>
|
||||
<LoadingState v-if="loading" variant="kpi-grid" :columns="3" label="Loading statistics..." />
|
||||
|
||||
<!-- Stats Cards -->
|
||||
<div v-else>
|
||||
<!-- Overall Stats -->
|
||||
<Card class="mb-8">
|
||||
<CardContent class="p-6">
|
||||
<h3 class="text-xl font-semibold text-gray-900 mb-6">
|
||||
<i class="fas fa-chart-bar mr-2"></i>Overall Statistics
|
||||
</h3>
|
||||
<div class="grid grid-cols-1 md:grid-cols-3 gap-6">
|
||||
<div class="text-center p-6 bg-gray-50 rounded-lg">
|
||||
<p class="text-4xl font-bold text-primary-600 mb-2">
|
||||
{{ formatNumber(stats?.total_packages || 0) }}
|
||||
</p>
|
||||
<p class="text-sm text-gray-600">Total Packages</p>
|
||||
</div>
|
||||
<div class="p-6 bg-gray-50 rounded-lg">
|
||||
<div class="text-center mb-3">
|
||||
<p class="text-2xl font-bold text-blue-600">
|
||||
{{ formatBytes(stats?.total_size || 0) }} / {{ formatBytes(stats?.max_cache_size || 0) }}
|
||||
</p>
|
||||
<p class="text-sm text-gray-600 mt-1">Storage Used</p>
|
||||
</div>
|
||||
<div class="w-full bg-gray-200 rounded-full h-3 overflow-hidden">
|
||||
<div
|
||||
class="h-full bg-blue-600 rounded-full transition-all duration-300"
|
||||
:style="{ width: storagePercentage + '%' }"
|
||||
:class="{
|
||||
'bg-green-600': storagePercentage < 50,
|
||||
'bg-yellow-600': storagePercentage >= 50 && storagePercentage < 80,
|
||||
'bg-orange-600': storagePercentage >= 80 && storagePercentage < 90,
|
||||
'bg-red-600': storagePercentage >= 90
|
||||
}"
|
||||
></div>
|
||||
</div>
|
||||
<p class="text-xs text-gray-500 text-center mt-1">{{ storagePercentage.toFixed(1) }}% used</p>
|
||||
</div>
|
||||
<div class="text-center p-6 bg-gray-50 rounded-lg">
|
||||
<p class="text-4xl font-bold text-green-600 mb-2">
|
||||
{{ formatNumber(stats?.total_downloads || 0) }}
|
||||
</p>
|
||||
<p class="text-sm text-gray-600">Total Downloads</p>
|
||||
</div>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<!-- Security Stats -->
|
||||
<Card class="mb-8">
|
||||
<CardContent class="p-6">
|
||||
<h3 class="text-xl font-semibold text-gray-900 mb-6">
|
||||
<i class="fas fa-shield-alt mr-2"></i>Security Scanning
|
||||
</h3>
|
||||
<div class="grid grid-cols-1 md:grid-cols-3 gap-6">
|
||||
<div class="flex items-center justify-between p-6 bg-green-50 rounded-lg border border-green-200">
|
||||
<div>
|
||||
<p class="text-3xl font-bold text-green-600">
|
||||
{{ formatNumber(stats?.scanned_packages || 0) }}
|
||||
</p>
|
||||
<p class="text-sm text-gray-600 mt-1">Scanned Packages</p>
|
||||
</div>
|
||||
<i class="fas fa-check-circle text-5xl text-green-400"></i>
|
||||
</div>
|
||||
<div
|
||||
@click="showVulnerablePackages"
|
||||
class="flex items-center justify-between p-6 bg-orange-50 rounded-lg border border-orange-200 cursor-pointer hover:bg-orange-100 transition-colors"
|
||||
:class="{ 'opacity-50': (stats?.vulnerable_packages || 0) === 0 }"
|
||||
>
|
||||
<div>
|
||||
<p class="text-3xl font-bold text-orange-600">
|
||||
{{ formatNumber(stats?.vulnerable_packages || 0) }}
|
||||
</p>
|
||||
<p class="text-sm text-gray-600 mt-1">
|
||||
Vulnerable Packages
|
||||
<span v-if="(stats?.vulnerable_packages || 0) > 0" class="text-xs ml-1">(click to view)</span>
|
||||
</p>
|
||||
</div>
|
||||
<i class="fas fa-exclamation-triangle text-5xl text-orange-400"></i>
|
||||
</div>
|
||||
<div
|
||||
@click="showBlockedPackages"
|
||||
class="flex items-center justify-between p-6 bg-red-50 rounded-lg border border-red-200 cursor-pointer hover:bg-red-100 transition-colors"
|
||||
:class="{ 'opacity-50': (stats?.blocked_packages || 0) === 0 }"
|
||||
>
|
||||
<div>
|
||||
<p class="text-3xl font-bold text-red-600">
|
||||
{{ formatNumber(stats?.blocked_packages || 0) }}
|
||||
</p>
|
||||
<p class="text-sm text-gray-600 mt-1">
|
||||
Blocked Packages
|
||||
<span v-if="(stats?.blocked_packages || 0) > 0" class="text-xs ml-1">(click to view)</span>
|
||||
</p>
|
||||
</div>
|
||||
<i class="fas fa-hand text-5xl text-red-400"></i>
|
||||
</div>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<!-- Registry Breakdown -->
|
||||
<template v-else>
|
||||
<Card>
|
||||
<CardContent class="p-6">
|
||||
<h3 class="text-xl font-semibold text-gray-900 mb-6">
|
||||
<i class="fas fa-server mr-2"></i>Registry Breakdown
|
||||
</h3>
|
||||
<div class="space-y-4">
|
||||
<div
|
||||
v-for="registry in registries"
|
||||
:key="registry.name"
|
||||
class="flex items-center justify-between p-4 bg-gray-50 rounded-lg"
|
||||
>
|
||||
<div class="flex items-center space-x-4">
|
||||
<div
|
||||
class="w-12 h-12 rounded-full flex items-center justify-center"
|
||||
:class="registry.color"
|
||||
>
|
||||
<i :class="registry.icon + ' text-2xl'"></i>
|
||||
</div>
|
||||
<div>
|
||||
<p class="font-bold text-gray-900">{{ registry.label }}</p>
|
||||
<p class="text-sm text-gray-600">{{ registry.packages }} packages</p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="text-right">
|
||||
<p class="text-lg font-bold text-gray-900">{{ registry.size }}</p>
|
||||
<p class="text-sm text-gray-600">{{ registry.downloads }} downloads</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<CardHeader>
|
||||
<CardTitle as="h2">Overall statistics</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<StatGroup :columns="3" aria-label="Overall statistics">
|
||||
<KpiTile label="Total packages" :value="stats?.total_packages || 0" :icon="faBoxesStacked" />
|
||||
<KpiTile
|
||||
label="Storage used"
|
||||
:value="`${formatBytes(stats?.total_size || 0)} / ${formatBytes(stats?.max_cache_size || 0)}`"
|
||||
:hint="`${storagePercentage.toFixed(1)}% used`"
|
||||
:progress="storagePercentage"
|
||||
:tone="storageTone"
|
||||
:icon="faHardDrive"
|
||||
/>
|
||||
<KpiTile label="Total downloads" :value="stats?.total_downloads || 0" :icon="faDownload" />
|
||||
</StatGroup>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle as="h2">Security scanning</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<StatGroup :columns="3" aria-label="Security scanning">
|
||||
<KpiTile label="Scanned packages" :value="stats?.scanned_packages || 0" :icon="faCircleCheck" />
|
||||
<KpiTile
|
||||
as="button"
|
||||
type="button"
|
||||
variant="interactive"
|
||||
label="Vulnerable packages"
|
||||
:value="stats?.vulnerable_packages || 0"
|
||||
:hint="(stats?.vulnerable_packages || 0) > 0 ? 'View the list' : undefined"
|
||||
:icon="faTriangleExclamation"
|
||||
:disabled="(stats?.vulnerable_packages || 0) === 0"
|
||||
@click="showVulnerablePackages"
|
||||
/>
|
||||
<KpiTile
|
||||
as="button"
|
||||
type="button"
|
||||
variant="interactive"
|
||||
label="Blocked packages"
|
||||
:value="stats?.blocked_packages || 0"
|
||||
:hint="(stats?.blocked_packages || 0) > 0 ? 'View the list' : undefined"
|
||||
:icon="faBan"
|
||||
:disabled="(stats?.blocked_packages || 0) === 0"
|
||||
@click="showBlockedPackages"
|
||||
/>
|
||||
</StatGroup>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle as="h2">Registry breakdown</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<DataTable
|
||||
label="Registry breakdown"
|
||||
layout="cards"
|
||||
row-key="name"
|
||||
empty-text="No registries reported"
|
||||
:columns="registryColumns"
|
||||
:rows="registries"
|
||||
/>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</template>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
@@ -153,8 +90,21 @@ import { computed, onMounted } from 'vue'
|
||||
import { storeToRefs } from 'pinia'
|
||||
import { useRouter } from 'vue-router'
|
||||
import { usePackageStore } from '../stores/packages'
|
||||
import { Alert, AlertDescription } from '@/components/ui/alert'
|
||||
import { Card, CardContent } from '@/components/ui/card'
|
||||
import {
|
||||
Card,
|
||||
CardContent,
|
||||
CardHeader,
|
||||
CardTitle,
|
||||
DataTable,
|
||||
ErrorState,
|
||||
KpiTile,
|
||||
LoadingState,
|
||||
PageHeader,
|
||||
StatGroup,
|
||||
type DataTableColumn,
|
||||
} from '@perfect-design/ui'
|
||||
import { formatBytes, formatNumber } from '@/lib/format'
|
||||
import { faBan, faDownload, faHardDrive, faBoxesStacked, faCircleCheck, faTriangleExclamation } from '@/lib/icons'
|
||||
|
||||
const store = usePackageStore()
|
||||
const { stats, loading, error } = storeToRefs(store)
|
||||
@@ -180,43 +130,36 @@ function showBlockedPackages() {
|
||||
router.push('/blocked-packages')
|
||||
}
|
||||
|
||||
// Registry configuration for icons and colors
|
||||
const registryConfig: Record<string, {label: string, icon: string, color: string}> = {
|
||||
npm: {
|
||||
label: 'NPM Registry',
|
||||
icon: 'fab fa-npm text-red-500',
|
||||
color: 'bg-red-100'
|
||||
},
|
||||
pypi: {
|
||||
label: 'PyPI Registry',
|
||||
icon: 'fab fa-python text-blue-500',
|
||||
color: 'bg-blue-100'
|
||||
},
|
||||
go: {
|
||||
label: 'Go Modules',
|
||||
icon: 'fas fa-code text-cyan-500',
|
||||
color: 'bg-cyan-100'
|
||||
}
|
||||
const REGISTRY_LABELS: Record<string, string> = {
|
||||
npm: 'npm',
|
||||
pypi: 'PyPI',
|
||||
go: 'Go modules',
|
||||
}
|
||||
|
||||
interface RegistryRow {
|
||||
name: string
|
||||
label: string
|
||||
packages: number
|
||||
size: string
|
||||
downloads: number
|
||||
}
|
||||
|
||||
const registryColumns: DataTableColumn<RegistryRow>[] = [
|
||||
{ key: 'label', header: 'Registry', rowHeader: true },
|
||||
{ key: 'packages', header: 'Packages', numeric: true, accessor: (r) => formatNumber(r.packages) },
|
||||
{ key: 'size', header: 'Size', numeric: true },
|
||||
{ key: 'downloads', header: 'Downloads', numeric: true, accessor: (r) => formatNumber(r.downloads) },
|
||||
]
|
||||
|
||||
const registries = computed(() => {
|
||||
const apiRegistries = store.registries || {}
|
||||
return Object.entries(apiRegistries).map(([name, data]: [string, any]) => {
|
||||
const config = registryConfig[name] || {
|
||||
label: name.toUpperCase(),
|
||||
icon: 'fas fa-box',
|
||||
color: 'bg-gray-100'
|
||||
}
|
||||
return {
|
||||
name,
|
||||
label: config.label,
|
||||
icon: config.icon,
|
||||
color: config.color,
|
||||
packages: data.count || 0,
|
||||
size: formatBytes(data.size || 0),
|
||||
downloads: data.downloads || 0
|
||||
}
|
||||
})
|
||||
return Object.entries(apiRegistries).map(([name, data]: [string, any]): RegistryRow => ({
|
||||
name,
|
||||
label: REGISTRY_LABELS[name] ?? name.toUpperCase(),
|
||||
packages: data.count || 0,
|
||||
size: formatBytes(data.size || 0),
|
||||
downloads: data.downloads || 0,
|
||||
}))
|
||||
})
|
||||
|
||||
const storagePercentage = computed(() => {
|
||||
@@ -225,15 +168,13 @@ const storagePercentage = computed(() => {
|
||||
return (totalSize / maxSize) * 100
|
||||
})
|
||||
|
||||
function formatNumber(num: number): string {
|
||||
return new Intl.NumberFormat().format(num)
|
||||
}
|
||||
const storageTone = computed(() => {
|
||||
if (storagePercentage.value >= 90) return 'danger'
|
||||
if (storagePercentage.value >= 50) return 'warning'
|
||||
return 'success'
|
||||
})
|
||||
|
||||
function formatBytes(bytes: number): string {
|
||||
if (bytes === 0) return '0 B'
|
||||
const k = 1024
|
||||
const sizes = ['B', 'KB', 'MB', 'GB', 'TB']
|
||||
const i = Math.floor(Math.log(bytes) / Math.log(k))
|
||||
return Math.round((bytes / Math.pow(k, i)) * 100) / 100 + ' ' + sizes[i]
|
||||
function refresh() {
|
||||
void store.fetchStats()
|
||||
}
|
||||
</script>
|
||||
|
||||
@@ -1,97 +1,58 @@
|
||||
<template>
|
||||
<div class="flex items-center gap-2">
|
||||
<!-- Blocked Icon (if package exceeds thresholds) -->
|
||||
<span
|
||||
<div class="flex flex-wrap items-center gap-2">
|
||||
<StatusBadge
|
||||
v-if="isBlocked"
|
||||
class="inline-flex items-center px-2.5 py-1 rounded-md text-xs font-medium bg-red-600 text-white border border-red-700"
|
||||
title="Download blocked - exceeds vulnerability thresholds"
|
||||
>
|
||||
<i class="fas fa-hand mr-1"></i>
|
||||
BLOCKED
|
||||
</span>
|
||||
status="blocked"
|
||||
label="Blocked"
|
||||
variant="solid"
|
||||
title="Download blocked: exceeds vulnerability thresholds"
|
||||
/>
|
||||
|
||||
<!-- Critical Vulnerabilities -->
|
||||
<button
|
||||
v-if="counts.critical > 0"
|
||||
@click="handleClick('critical')"
|
||||
class="inline-flex items-center px-2.5 py-1 rounded-md text-xs font-medium bg-red-100 text-red-800 hover:bg-red-200 border border-red-300 transition-colors cursor-pointer"
|
||||
:title="`${counts.critical} critical vulnerabilities - click for details`"
|
||||
v-for="level in presentLevels"
|
||||
:key="level"
|
||||
type="button"
|
||||
:class="buttonVariants({ variant: 'ghost', size: 'xs' })"
|
||||
:aria-label="`${counts[level]} ${level} ${counts[level] === 1 ? 'vulnerability' : 'vulnerabilities'}, view details`"
|
||||
@click="handleClick(level)"
|
||||
>
|
||||
<i class="fas fa-shield-virus mr-1"></i>
|
||||
CRITICAL: {{ counts.critical }}
|
||||
<StatusBadge :status="level" :tones="SEVERITY_TONES" :label="`${formatSeverityName(level)}: ${counts[level]}`" />
|
||||
</button>
|
||||
|
||||
<!-- High Vulnerabilities -->
|
||||
<button
|
||||
v-if="counts.high > 0"
|
||||
@click="handleClick('high')"
|
||||
class="inline-flex items-center px-2.5 py-1 rounded-md text-xs font-medium bg-orange-100 text-orange-800 hover:bg-orange-200 border border-orange-300 transition-colors cursor-pointer"
|
||||
:title="`${counts.high} high severity vulnerabilities - click for details`"
|
||||
>
|
||||
<i class="fas fa-exclamation-triangle mr-1"></i>
|
||||
HIGH: {{ counts.high }}
|
||||
</button>
|
||||
<template v-if="status === 'clean' && total === 0">
|
||||
<StatusBadge
|
||||
status="clean"
|
||||
label="Clean"
|
||||
:tones="{ clean: 'success' }"
|
||||
:title="scannedAt ? `No vulnerabilities found. Scanned ${formatDate(scannedAt)}` : 'No vulnerabilities found'"
|
||||
/>
|
||||
<RelativeTime v-if="scannedAt" :datetime="scannedAt" variant="muted" size="xs" />
|
||||
</template>
|
||||
|
||||
<!-- Moderate Vulnerabilities -->
|
||||
<button
|
||||
v-if="counts.moderate > 0"
|
||||
@click="handleClick('moderate')"
|
||||
class="inline-flex items-center px-2.5 py-1 rounded-md text-xs font-medium bg-yellow-100 text-yellow-800 hover:bg-yellow-200 border border-yellow-300 transition-colors cursor-pointer"
|
||||
:title="`${counts.moderate} moderate severity vulnerabilities - click for details`"
|
||||
>
|
||||
<i class="fas fa-exclamation-circle mr-1"></i>
|
||||
MODERATE: {{ counts.moderate }}
|
||||
</button>
|
||||
|
||||
<!-- Low Vulnerabilities -->
|
||||
<button
|
||||
v-if="counts.low > 0"
|
||||
@click="handleClick('low')"
|
||||
class="inline-flex items-center px-2.5 py-1 rounded-md text-xs font-medium bg-blue-100 text-blue-800 hover:bg-blue-200 border border-blue-300 transition-colors cursor-pointer"
|
||||
:title="`${counts.low} low severity vulnerabilities - click for details`"
|
||||
>
|
||||
<i class="fas fa-info-circle mr-1"></i>
|
||||
LOW: {{ counts.low }}
|
||||
</button>
|
||||
|
||||
<!-- Clean Badge (no vulnerabilities) -->
|
||||
<span
|
||||
v-if="status === 'clean' && total === 0"
|
||||
class="inline-flex items-center px-2.5 py-1 rounded-md text-xs font-medium bg-green-100 text-green-800 border border-green-300"
|
||||
:title="scannedAt ? `No vulnerabilities found - Scanned ${formatTimestamp(scannedAt)}` : 'No vulnerabilities found'"
|
||||
>
|
||||
<i class="fas fa-check-circle mr-1"></i>
|
||||
CLEAN
|
||||
<span v-if="scannedAt" class="ml-1 text-[10px] opacity-70">
|
||||
({{ formatRelativeTime(scannedAt) }})
|
||||
</span>
|
||||
</span>
|
||||
|
||||
<!-- Pending Badge -->
|
||||
<span
|
||||
<StatusBadge
|
||||
v-if="status === 'pending'"
|
||||
class="inline-flex items-center px-2.5 py-1 rounded-md text-xs font-medium bg-gray-100 text-gray-700 border border-gray-300"
|
||||
status="pending"
|
||||
label="Scanning"
|
||||
pulse
|
||||
title="Security scan in progress"
|
||||
>
|
||||
<i class="fas fa-spinner fa-spin mr-1"></i>
|
||||
SCANNING...
|
||||
</span>
|
||||
/>
|
||||
|
||||
<!-- Not Scanned Badge -->
|
||||
<span
|
||||
<StatusBadge
|
||||
v-if="status === 'not_scanned' || !scanned"
|
||||
class="inline-flex items-center px-2.5 py-1 rounded-md text-xs font-medium bg-gray-100 text-gray-600 border border-gray-300"
|
||||
status="not_scanned"
|
||||
label="Not scanned"
|
||||
:tones="{ not_scanned: 'neutral' }"
|
||||
title="Not yet scanned for vulnerabilities"
|
||||
>
|
||||
<i class="fas fa-question-circle mr-1"></i>
|
||||
NOT SCANNED
|
||||
</span>
|
||||
/>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { computed } from 'vue'
|
||||
import { RelativeTime, StatusBadge, buttonVariants } from '@perfect-design/ui'
|
||||
import type { VulnerabilityCounts } from '../stores/packages'
|
||||
import { formatDate } from '@/lib/format'
|
||||
import { NO_VULNERABILITIES, SEVERITY_LEVELS, SEVERITY_TONES, formatSeverityName } from '@/lib/severity'
|
||||
|
||||
interface Props {
|
||||
scanned?: boolean
|
||||
@@ -113,29 +74,10 @@ const emit = defineEmits<{
|
||||
click: [severity: string]
|
||||
}>()
|
||||
|
||||
const counts = computed(() => props.counts || { critical: 0, high: 0, moderate: 0, low: 0 })
|
||||
const counts = computed(() => props.counts || NO_VULNERABILITIES)
|
||||
const presentLevels = computed(() => SEVERITY_LEVELS.filter((level) => counts.value[level] > 0))
|
||||
|
||||
function handleClick(severity: string) {
|
||||
emit('click', severity)
|
||||
}
|
||||
|
||||
function formatTimestamp(timestamp: string): string {
|
||||
const date = new Date(timestamp)
|
||||
return date.toLocaleString()
|
||||
}
|
||||
|
||||
function formatRelativeTime(timestamp: string): string {
|
||||
const date = new Date(timestamp)
|
||||
const now = new Date()
|
||||
const diffMs = now.getTime() - date.getTime()
|
||||
const diffMins = Math.floor(diffMs / 60000)
|
||||
const diffHours = Math.floor(diffMs / 3600000)
|
||||
const diffDays = Math.floor(diffMs / 86400000)
|
||||
|
||||
if (diffMins < 1) return 'just now'
|
||||
if (diffMins < 60) return `${diffMins}m ago`
|
||||
if (diffHours < 24) return `${diffHours}h ago`
|
||||
if (diffDays < 7) return `${diffDays}d ago`
|
||||
return formatTimestamp(timestamp).split(',')[0] // Just the date part
|
||||
}
|
||||
</script>
|
||||
|
||||
Reference in New Issue
Block a user