mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-10-14 12:02:27 +08:00
feat(opencode): 用量单元格支持主动查询并将周/月徽章改为 7d/1m
This commit is contained in:
@@ -584,6 +584,7 @@
|
||||
<OpenCodeGoUsageCell
|
||||
v-if="account.opencode_go_usage?.eligible"
|
||||
:account="account"
|
||||
@updated="handleOpenCodeGoUsageUpdated"
|
||||
/>
|
||||
<!-- Today stats row (requests, tokens, cost, user_cost) -->
|
||||
<div
|
||||
@@ -1566,6 +1567,10 @@ const handleOllamaCloudUsageUpdated = (state: NonNullable<Account['ollama_cloud_
|
||||
emit('account-updated', { ...props.account, ollama_cloud_usage: state })
|
||||
}
|
||||
|
||||
const handleOpenCodeGoUsageUpdated = (state: NonNullable<Account['opencode_go_usage']>) => {
|
||||
emit('account-updated', { ...props.account, opencode_go_usage: state })
|
||||
}
|
||||
|
||||
// ===== Key account today stats formatters =====
|
||||
|
||||
const formatKeyRequests = computed(() => {
|
||||
|
||||
@@ -38,23 +38,69 @@
|
||||
>
|
||||
{{ statusLabel }}
|
||||
</span>
|
||||
<div class="flex items-center pt-0.5">
|
||||
<button
|
||||
type="button"
|
||||
class="inline-flex items-center gap-0.5 rounded px-1.5 py-0.5 text-[10px] font-medium text-blue-600 transition-colors hover:bg-blue-50 disabled:cursor-not-allowed disabled:opacity-50 dark:text-blue-400 dark:hover:bg-blue-900/30"
|
||||
:disabled="refreshing"
|
||||
data-testid="opencode-go-usage-query"
|
||||
@click="refreshUsage"
|
||||
>
|
||||
<svg
|
||||
class="h-2.5 w-2.5"
|
||||
:class="{ 'animate-spin': refreshing }"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
viewBox="0 0 24 24"
|
||||
>
|
||||
<path
|
||||
stroke-linecap="round"
|
||||
stroke-linejoin="round"
|
||||
stroke-width="2"
|
||||
d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15"
|
||||
/>
|
||||
</svg>
|
||||
{{ t('admin.accounts.usageWindow.activeQuery') }}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<span v-else class="text-sm text-gray-400 dark:text-dark-500">-</span>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { computed } from 'vue'
|
||||
import { computed, ref, watch } from 'vue'
|
||||
import { useI18n } from 'vue-i18n'
|
||||
import type { Account } from '@/types'
|
||||
import { adminAPI } from '@/api/admin'
|
||||
import type { Account, OpenCodeGoUsageState } from '@/types'
|
||||
import UsageProgressBar from './UsageProgressBar.vue'
|
||||
|
||||
const props = defineProps<{ account: Account }>()
|
||||
const emit = defineEmits<{ updated: [state: OpenCodeGoUsageState] }>()
|
||||
const { t } = useI18n()
|
||||
const state = computed(() => props.account.opencode_go_usage)
|
||||
const state = ref(props.account.opencode_go_usage)
|
||||
const refreshing = ref(false)
|
||||
const snapshot = computed(() => state.value?.snapshot)
|
||||
const statusLabel = computed(() => {
|
||||
if (snapshot.value?.status === 'unauthorized') return t('admin.accounts.opencodeGo.unauthorized')
|
||||
if (snapshot.value?.status === 'failed') return t('admin.accounts.opencodeGo.failed')
|
||||
return t('admin.accounts.opencodeGo.ok')
|
||||
})
|
||||
|
||||
watch(() => props.account.opencode_go_usage, (next) => {
|
||||
state.value = next
|
||||
})
|
||||
|
||||
const refreshUsage = async () => {
|
||||
if (refreshing.value) return
|
||||
refreshing.value = true
|
||||
try {
|
||||
const next = await adminAPI.accounts.refreshOpenCodeGoUsage(props.account.id)
|
||||
state.value = next
|
||||
emit('updated', next)
|
||||
} catch (error) {
|
||||
console.error('Failed to refresh OpenCode Go usage:', error)
|
||||
} finally {
|
||||
refreshing.value = false
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
@@ -1,9 +1,21 @@
|
||||
import { mount } from '@vue/test-utils'
|
||||
import { describe, expect, it, vi } from 'vitest'
|
||||
import { flushPromises, mount } from '@vue/test-utils'
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import OpenCodeGoUsageCell from '../OpenCodeGoUsageCell.vue'
|
||||
import UsageProgressBar from '../UsageProgressBar.vue'
|
||||
import type { Account, OpenCodeGoUsageState } from '@/types'
|
||||
|
||||
const { refreshOpenCodeGoUsage } = vi.hoisted(() => ({
|
||||
refreshOpenCodeGoUsage: vi.fn()
|
||||
}))
|
||||
|
||||
vi.mock('@/api/admin', () => ({
|
||||
adminAPI: {
|
||||
accounts: {
|
||||
refreshOpenCodeGoUsage
|
||||
}
|
||||
}
|
||||
}))
|
||||
|
||||
vi.mock('vue-i18n', async () => {
|
||||
const actual = await vi.importActual<typeof import('vue-i18n')>('vue-i18n')
|
||||
return {
|
||||
@@ -12,8 +24,8 @@ vi.mock('vue-i18n', async () => {
|
||||
t: (key: string) => {
|
||||
const short: Record<string, string> = {
|
||||
'admin.accounts.opencodeGo.rollingShort': '5h',
|
||||
'admin.accounts.opencodeGo.weeklyShort': 'W',
|
||||
'admin.accounts.opencodeGo.monthlyShort': 'M',
|
||||
'admin.accounts.opencodeGo.weeklyShort': '7d',
|
||||
'admin.accounts.opencodeGo.monthlyShort': '1m',
|
||||
'admin.accounts.opencodeGo.unauthorized': 'unauthorized',
|
||||
'admin.accounts.opencodeGo.failed': 'failed',
|
||||
'admin.accounts.opencodeGo.ok': 'ok'
|
||||
@@ -70,6 +82,10 @@ const account = (state = usageState()): Account => ({
|
||||
})
|
||||
|
||||
describe('OpenCodeGoUsageCell', () => {
|
||||
beforeEach(() => {
|
||||
refreshOpenCodeGoUsage.mockReset()
|
||||
})
|
||||
|
||||
it('renders rolling, weekly and monthly windows in a shrinkable mobile-safe cell', () => {
|
||||
const wrapper = mount(OpenCodeGoUsageCell, { props: { account: account() } })
|
||||
const cell = wrapper.get('[data-testid="opencode-go-usage-cell"]')
|
||||
@@ -85,20 +101,35 @@ describe('OpenCodeGoUsageCell', () => {
|
||||
color: 'indigo'
|
||||
})
|
||||
expect(bars[1].props()).toMatchObject({
|
||||
label: 'W',
|
||||
label: '7d',
|
||||
utilization: 14.2,
|
||||
resetsAt: '2026-07-29T00:00:00Z',
|
||||
color: 'emerald'
|
||||
})
|
||||
expect(bars[2].props()).toMatchObject({
|
||||
label: 'M',
|
||||
label: '1m',
|
||||
utilization: 33.3,
|
||||
resetsAt: '2026-08-01T00:00:00Z',
|
||||
color: 'amber'
|
||||
})
|
||||
|
||||
expect(wrapper.find('[data-testid="opencode-go-status-badge"]').exists()).toBe(false)
|
||||
expect(wrapper.findAll('button')).toHaveLength(0)
|
||||
const query = wrapper.get('[data-testid="opencode-go-usage-query"]')
|
||||
expect(query.text()).toContain('admin.accounts.usageWindow.activeQuery')
|
||||
})
|
||||
|
||||
it('refreshes usage on demand and emits the new state', async () => {
|
||||
const next = usageState()
|
||||
next.snapshot!.data!.rolling!.percent = 42
|
||||
refreshOpenCodeGoUsage.mockResolvedValueOnce(next)
|
||||
const wrapper = mount(OpenCodeGoUsageCell, { props: { account: account() } })
|
||||
|
||||
await wrapper.get('[data-testid="opencode-go-usage-query"]').trigger('click')
|
||||
await flushPromises()
|
||||
|
||||
expect(refreshOpenCodeGoUsage).toHaveBeenCalledWith(7)
|
||||
expect(wrapper.findAllComponents(UsageProgressBar)[0].props('utilization')).toBe(42)
|
||||
expect(wrapper.emitted<OpenCodeGoUsageState[]>('updated')?.[0]?.[0]).toEqual(next)
|
||||
})
|
||||
|
||||
it('shows a status badge when the snapshot status is not ok', () => {
|
||||
|
||||
@@ -309,9 +309,9 @@ export default {
|
||||
rolling: '5 hour',
|
||||
rollingShort: '5h',
|
||||
weekly: 'Week',
|
||||
weeklyShort: 'W',
|
||||
weeklyShort: '7d',
|
||||
monthly: 'Month',
|
||||
monthlyShort: 'M',
|
||||
monthlyShort: '1m',
|
||||
status: 'Status',
|
||||
updatedAt: 'Updated',
|
||||
ok: 'Current',
|
||||
|
||||
@@ -185,9 +185,9 @@ export default {
|
||||
rolling: '5 小时',
|
||||
rollingShort: '5h',
|
||||
weekly: '周',
|
||||
weeklyShort: '周',
|
||||
weeklyShort: '7d',
|
||||
monthly: '月',
|
||||
monthlyShort: '月',
|
||||
monthlyShort: '1m',
|
||||
status: '状态',
|
||||
updatedAt: '更新时间',
|
||||
ok: '正常',
|
||||
|
||||
Reference in New Issue
Block a user