mirror of
https://github.com/lukaszraczylo/gohoarder.git
synced 2026-11-25 06:35:21 +00:00
fix(ui): show stored downloads when analytics hold no record
The Downloads tab hid the stored count when analytics had no record since the last restart, and it requested a Go file key per version. It now always shows the stored count, labels the in-memory totals as counted since the restart, and asks for a Go module by its path. A failed request is reported instead of a 404 being swallowed.
This commit is contained in:
@@ -83,7 +83,7 @@
|
||||
</TabsContent>
|
||||
|
||||
<TabsContent value="downloads" class="pt-4">
|
||||
<DownloadsPanel :registry="registry" :name="packageName" :version="version" :download-count="row?.download_count ?? 0" />
|
||||
<DownloadsPanel :registry="registry" :name="packageName" :download-count="row?.download_count ?? 0" />
|
||||
</TabsContent>
|
||||
|
||||
<TabsContent value="bypass" class="pt-4">
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import axios from 'axios'
|
||||
import { flushPromises } from '@vue/test-utils'
|
||||
import DownloadsPanel from './DownloadsPanel.vue'
|
||||
import { PACKAGE_ANALYTICS } from '@/test/fixtures'
|
||||
import { mountStrict } from '@/test/mountStrict'
|
||||
|
||||
vi.mock('axios')
|
||||
|
||||
let restore: (() => void) | undefined
|
||||
|
||||
async function open(props: { registry: string; name: string; downloadCount: number }) {
|
||||
const mounted = await mountStrict(DownloadsPanel, { props })
|
||||
restore = mounted.restore
|
||||
await flushPromises()
|
||||
return mounted.wrapper
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
vi.mocked(axios.get).mockReset()
|
||||
})
|
||||
afterEach(() => {
|
||||
restore?.()
|
||||
restore = undefined
|
||||
})
|
||||
|
||||
describe('DownloadsPanel', () => {
|
||||
it('shows the stored count even when analytics have no record since the restart', async () => {
|
||||
vi.mocked(axios.get).mockResolvedValue({ data: { package: null } })
|
||||
const wrapper = await open({ registry: 'npm', name: 'left-pad', downloadCount: 12 })
|
||||
|
||||
expect(wrapper.text()).toContain('12')
|
||||
expect(wrapper.text()).toContain('No downloads since the last restart')
|
||||
expect(wrapper.text()).not.toContain('Could not load')
|
||||
})
|
||||
|
||||
it('adds the in-memory totals and labels them as since the restart', async () => {
|
||||
vi.mocked(axios.get).mockResolvedValue({ data: PACKAGE_ANALYTICS })
|
||||
const wrapper = await open({ registry: 'npm', name: 'left-pad', downloadCount: 12 })
|
||||
|
||||
expect(wrapper.text()).toContain('kept across restarts')
|
||||
expect(wrapper.text()).toContain('2 versions since the last restart')
|
||||
expect(wrapper.text()).toContain('80 KB')
|
||||
})
|
||||
|
||||
it('asks for a Go module by its path, with no cache-file suffix', async () => {
|
||||
vi.mocked(axios.get).mockResolvedValue({ data: { package: null } })
|
||||
await open({ registry: 'go', name: 'github.com/google/uuid/@v/v1.6.0.zip', downloadCount: 2 })
|
||||
|
||||
expect(vi.mocked(axios.get)).toHaveBeenCalledWith('/api/analytics/package/go/github.com/google/uuid')
|
||||
})
|
||||
|
||||
it('reports a failed request', async () => {
|
||||
vi.mocked(axios.get).mockRejectedValue(new Error('boom'))
|
||||
const wrapper = await open({ registry: 'npm', name: 'left-pad', downloadCount: 1 })
|
||||
expect(wrapper.text()).toContain('Could not load download history')
|
||||
})
|
||||
})
|
||||
@@ -1,27 +1,29 @@
|
||||
<template>
|
||||
<LoadingState v-if="loading" layout="inline" label="Loading download history..." />
|
||||
<ErrorState v-else-if="error" variant="inline" title="Could not load download history" :message="error" @retry="load" />
|
||||
<EmptyState
|
||||
v-else-if="!stats"
|
||||
size="sm"
|
||||
:icon="faDownload"
|
||||
title="No download history"
|
||||
description="The server records downloads in memory, so history starts at its last restart."
|
||||
/>
|
||||
<div v-else class="flex flex-col gap-4">
|
||||
<StatGroup :columns="3" aria-label="Downloads of this package">
|
||||
<KpiTile label="This version" :value="downloadCount" hint="Times served from the cache" />
|
||||
<KpiTile
|
||||
:label="isGo ? 'Module zip' : 'All versions'"
|
||||
:value="stats.TotalDownloads"
|
||||
:hint="isGo ? 'Downloads of this version' : `${stats.UniqueVersions} version${stats.UniqueVersions === 1 ? '' : 's'} seen`"
|
||||
/>
|
||||
<KpiTile label="Data served" :value="formatBytes(stats.BytesServed)" :hint="isGo ? 'This version' : 'Across all versions'" />
|
||||
<KpiTile label="This version" :value="downloadCount" hint="Times served from the cache, kept across restarts" />
|
||||
<template v-if="stats">
|
||||
<KpiTile
|
||||
label="All versions"
|
||||
:value="stats.TotalDownloads"
|
||||
:hint="`${stats.UniqueVersions} version${stats.UniqueVersions === 1 ? '' : 's'} since the last restart`"
|
||||
/>
|
||||
<KpiTile label="Data served" :value="formatBytes(stats.BytesServed)" hint="Across all versions, since the last restart" />
|
||||
</template>
|
||||
</StatGroup>
|
||||
<DescriptionList :columns="2" divided>
|
||||
<DescriptionItem term="First seen"><RelativeTime :datetime="stats.FirstSeen" /></DescriptionItem>
|
||||
<DescriptionList v-if="stats" :columns="2" divided>
|
||||
<DescriptionItem term="First seen since restart"><RelativeTime :datetime="stats.FirstSeen" /></DescriptionItem>
|
||||
<DescriptionItem term="Last download"><RelativeTime :datetime="stats.LastDownload" /></DescriptionItem>
|
||||
</DescriptionList>
|
||||
<EmptyState
|
||||
v-else
|
||||
size="sm"
|
||||
:icon="faDownload"
|
||||
title="No downloads since the last restart"
|
||||
description="The totals across versions are counted in memory, so they start again when the server restarts."
|
||||
/>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
@@ -50,29 +52,26 @@ interface PackageAnalytics {
|
||||
LastDownload: string
|
||||
}
|
||||
|
||||
const props = defineProps<{ registry: string; name: string; version: string; downloadCount: number }>()
|
||||
|
||||
const isGo = computed(() => props.registry === 'go')
|
||||
const props = defineProps<{ registry: string; name: string; downloadCount: number }>()
|
||||
|
||||
const stats = ref<PackageAnalytics | null>(null)
|
||||
const loading = ref(false)
|
||||
const error = ref<string | null>(null)
|
||||
|
||||
// Go analytics are keyed by cache file, one per version.
|
||||
const analyticsName = computed(() => (isGo.value ? `${modulePath(props.name)}/@v/${props.version}.zip` : props.name))
|
||||
// Analytics are keyed by package name; a Go module is named by its path.
|
||||
const analyticsName = computed(() => modulePath(props.name))
|
||||
|
||||
async function load() {
|
||||
loading.value = true
|
||||
error.value = null
|
||||
try {
|
||||
const response = await axios.get<{ package: PackageAnalytics }>(
|
||||
const response = await axios.get<{ package: PackageAnalytics | null }>(
|
||||
`/api/analytics/package/${props.registry}/${analyticsName.value}`,
|
||||
)
|
||||
stats.value = response.data.package
|
||||
} catch (err) {
|
||||
stats.value = null
|
||||
const status = (err as { response?: { status?: number } }).response?.status
|
||||
if (status !== 404) error.value = err instanceof Error ? err.message : 'Failed to load download history'
|
||||
error.value = err instanceof Error ? err.message : 'Failed to load download history'
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user