mirror of
https://github.com/lukaszraczylo/gohoarder.git
synced 2026-11-27 06:46:01 +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>
|
||||||
|
|
||||||
<TabsContent value="downloads" class="pt-4">
|
<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>
|
||||||
|
|
||||||
<TabsContent value="bypass" class="pt-4">
|
<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>
|
<template>
|
||||||
<LoadingState v-if="loading" layout="inline" label="Loading download history..." />
|
<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" />
|
<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">
|
<div v-else class="flex flex-col gap-4">
|
||||||
<StatGroup :columns="3" aria-label="Downloads of this package">
|
<StatGroup :columns="3" aria-label="Downloads of this package">
|
||||||
<KpiTile label="This version" :value="downloadCount" hint="Times served from the cache" />
|
<KpiTile label="This version" :value="downloadCount" hint="Times served from the cache, kept across restarts" />
|
||||||
<KpiTile
|
<template v-if="stats">
|
||||||
:label="isGo ? 'Module zip' : 'All versions'"
|
<KpiTile
|
||||||
:value="stats.TotalDownloads"
|
label="All versions"
|
||||||
:hint="isGo ? 'Downloads of this version' : `${stats.UniqueVersions} version${stats.UniqueVersions === 1 ? '' : 's'} seen`"
|
:value="stats.TotalDownloads"
|
||||||
/>
|
:hint="`${stats.UniqueVersions} version${stats.UniqueVersions === 1 ? '' : 's'} since the last restart`"
|
||||||
<KpiTile label="Data served" :value="formatBytes(stats.BytesServed)" :hint="isGo ? 'This version' : 'Across all versions'" />
|
/>
|
||||||
|
<KpiTile label="Data served" :value="formatBytes(stats.BytesServed)" hint="Across all versions, since the last restart" />
|
||||||
|
</template>
|
||||||
</StatGroup>
|
</StatGroup>
|
||||||
<DescriptionList :columns="2" divided>
|
<DescriptionList v-if="stats" :columns="2" divided>
|
||||||
<DescriptionItem term="First seen"><RelativeTime :datetime="stats.FirstSeen" /></DescriptionItem>
|
<DescriptionItem term="First seen since restart"><RelativeTime :datetime="stats.FirstSeen" /></DescriptionItem>
|
||||||
<DescriptionItem term="Last download"><RelativeTime :datetime="stats.LastDownload" /></DescriptionItem>
|
<DescriptionItem term="Last download"><RelativeTime :datetime="stats.LastDownload" /></DescriptionItem>
|
||||||
</DescriptionList>
|
</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>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
@@ -50,29 +52,26 @@ interface PackageAnalytics {
|
|||||||
LastDownload: string
|
LastDownload: string
|
||||||
}
|
}
|
||||||
|
|
||||||
const props = defineProps<{ registry: string; name: string; version: string; downloadCount: number }>()
|
const props = defineProps<{ registry: string; name: string; downloadCount: number }>()
|
||||||
|
|
||||||
const isGo = computed(() => props.registry === 'go')
|
|
||||||
|
|
||||||
const stats = ref<PackageAnalytics | null>(null)
|
const stats = ref<PackageAnalytics | null>(null)
|
||||||
const loading = ref(false)
|
const loading = ref(false)
|
||||||
const error = ref<string | null>(null)
|
const error = ref<string | null>(null)
|
||||||
|
|
||||||
// Go analytics are keyed by cache file, one per version.
|
// Analytics are keyed by package name; a Go module is named by its path.
|
||||||
const analyticsName = computed(() => (isGo.value ? `${modulePath(props.name)}/@v/${props.version}.zip` : props.name))
|
const analyticsName = computed(() => modulePath(props.name))
|
||||||
|
|
||||||
async function load() {
|
async function load() {
|
||||||
loading.value = true
|
loading.value = true
|
||||||
error.value = null
|
error.value = null
|
||||||
try {
|
try {
|
||||||
const response = await axios.get<{ package: PackageAnalytics }>(
|
const response = await axios.get<{ package: PackageAnalytics | null }>(
|
||||||
`/api/analytics/package/${props.registry}/${analyticsName.value}`,
|
`/api/analytics/package/${props.registry}/${analyticsName.value}`,
|
||||||
)
|
)
|
||||||
stats.value = response.data.package
|
stats.value = response.data.package
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
stats.value = null
|
stats.value = null
|
||||||
const status = (err as { response?: { status?: number } }).response?.status
|
error.value = err instanceof Error ? err.message : 'Failed to load download history'
|
||||||
if (status !== 404) error.value = err instanceof Error ? err.message : 'Failed to load download history'
|
|
||||||
} finally {
|
} finally {
|
||||||
loading.value = false
|
loading.value = false
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user