fix: 轮询按运营商账户限流
All checks were successful
构建并部署前端到测试环境 / deploy (push) Successful in 55s

This commit is contained in:
luo
2026-10-26 14:31:15 +08:00
parent 049f8257fb
commit 06684e1fc0
88 changed files with 7919 additions and 486 deletions

View File

@@ -0,0 +1,348 @@
<template>
<div class="polling-stats-page" id="table-full-screen">
<ElTabs v-if="canPreReset || canTiers" v-model="activeTab">
<ElTabPane v-if="canPreReset" label="重置日前加密查流量效果" name="pre-reset">
<ArtSearchBar
:filter="filters"
:items="searchItems"
:show-expand="searchItems.length >= 4"
label-width="110px"
@update:filter="(value) => Object.assign(filters, value)"
@search="searchPreReset"
@reset="resetFilters"
/>
<ElCard shadow="never" class="art-table-card">
<ElAlert
type="info"
:closable="false"
title="窗口检查卡数为近似去重值,未检查卡数为近似值;分钟分布按接口原始桶展示。筛选只过滤本页数据。"
/>
<ElAlert v-if="carrierError" :title="carrierError" type="error" :closable="false"
><ElButton link type="primary" @click="searchCarriers()"
>重试运营商账户</ElButton
></ElAlert
>
<ArtTableHeader v-model:columns="preColumnChecks" @refresh="loadPreReset" />
<ElAlert v-if="preError" :title="preError" type="error" :closable="false"
><ElButton link type="primary" @click="loadPreReset">重试</ElButton></ElAlert
>
<ArtTable
:loading="preLoading"
:data="preRows"
row-key="key"
:pagination="false"
empty-text="暂无符合条件的周期统计"
>
<ElTableColumn
v-for="col in preColumns"
:key="col.prop"
:label="col.label"
:min-width="col.width || 140"
show-overflow-tooltip
>
<template #default="{ row }">{{ preValue(row, col.prop) }}</template>
</ElTableColumn>
</ArtTable>
</ElCard>
</ElTabPane>
<ElTabPane v-if="canTiers" label="分层轮询统计" name="tiers">
<ElCard shadow="never">
<div class="stats-heading"
><h2>分层轮询统计</h2
><ElButton type="primary" :loading="tierLoading" @click="loadTiers"
>刷新统计</ElButton
></div
>
<ElAlert
type="info"
:closable="false"
title="统计结果缓存 5 分钟,刷新仅重新请求接口,不保证强制清除缓存。"
/>
<ElAlert v-if="tierError" :title="tierError" type="error" :closable="false" />
<div v-loading="tierLoading">
<template v-if="tiers">
<ElDescriptions :column="width < 640 ? 1 : 2" border class="stats-overview">
<ElDescriptionsItem label="统计时间">{{
formatDateTime(tiers.computed_at)
}}</ElDescriptionsItem>
<ElDescriptionsItem label="已开启轮询卡数">{{
tiers.total_cards
}}</ElDescriptionsItem>
<ElDescriptionsItem label="未匹配的在库卡数"
><ElTag :type="tiers.unmatched_in_stock > 0 ? 'danger' : 'success'">{{
tiers.unmatched_in_stock
}}</ElTag></ElDescriptionsItem
>
<ElDescriptionsItem label="未匹配的已销售无套餐卡数"
><ElTag :type="tiers.unmatched_sold_no_package > 0 ? 'danger' : 'success'">{{
tiers.unmatched_sold_no_package
}}</ElTag></ElDescriptionsItem
>
</ElDescriptions>
<ElAlert
v-if="tiers.unmatched_in_stock > 0 || tiers.unmatched_sold_no_package > 0"
type="warning"
show-icon
:closable="false"
title="存在未匹配轮询配置的在库卡或已销售无套餐卡,请检查配置。"
/>
<h3>按首个命中配置统计</h3>
<ArtTableHeader v-model:columns="tierColumnChecks" @refresh="loadTiers" />
<ArtTable
:data="tiers.by_config"
row-key="config_id"
:pagination="false"
empty-text="暂无配置分层统计"
>
<ElTableColumn
v-for="col in tierColumns"
:key="col.prop"
:label="col.label"
:min-width="180"
><template #default="{ row }">{{
col.prop === 'config_name' && row.config_id === 0
? '未匹配任何配置'
: (row[col.prop] ?? '-')
}}</template></ElTableColumn
>
</ArtTable>
<h3>按套餐状态统计</h3>
<ArtTable
:data="packageRows"
row-key="state"
:pagination="false"
empty-text="暂无套餐状态统计"
><ElTableColumn prop="label" label="套餐状态" min-width="200" /><ElTableColumn
prop="cards"
label="卡数"
min-width="150"
/></ArtTable>
</template>
<ElEmpty v-else-if="!tierLoading && !tierError" description="暂无分层统计" />
</div>
</ElCard>
</ElTabPane>
</ElTabs>
<ElEmpty v-else description="暂无统计查看权限" />
</div>
</template>
<script setup lang="ts">
import { computed, reactive, ref, onMounted, onBeforeUnmount, watch } from 'vue'
import { useWindowSize } from '@vueuse/core'
import { PollingConfigService } from '@/api/modules/pollingConfig'
import { useAuth } from '@/composables/useAuth'
import { useGatewayCarriers, useGatewayColumns } from '@/composables/useGateway'
import type { SearchFormItem } from '@/types'
import type { PollingPreResetStats, PollingTierStats } from '@/types/api/pollingConfigExtensions'
import {
POLLING_CONFIG_PERMISSIONS,
POLLING_PACKAGE_STATES,
pollingOptionLabel
} from '@/config/constants/pollingConfig'
import { pollingPreResetRows } from '@/utils/business/pollingConfig'
import { gatewayData, gatewayError, requireGatewayAccess } from '@/utils/business/gateway'
import { formatDateTime } from '@/utils/business/format'
defineOptions({ name: 'PollingConfigStats' })
const { width } = useWindowSize()
const { hasAuth } = useAuth()
const canPreReset = computed(
() =>
hasAuth(POLLING_CONFIG_PERMISSIONS.statsPage) && hasAuth(POLLING_CONFIG_PERMISSIONS.preReset)
)
const canTiers = computed(
() => hasAuth(POLLING_CONFIG_PERMISSIONS.statsPage) && hasAuth(POLLING_CONFIG_PERMISSIONS.tiers)
)
const activeTab = ref(canPreReset.value ? 'pre-reset' : 'tiers')
const {
options: carrierOptions,
loading: carrierLoading,
error: carrierError,
search: searchCarriers,
select: selectCarrier
} = useGatewayCarriers(canPreReset.value)
const filters = reactive({ carrier_id: undefined as number | undefined, period: 'all' })
const appliedFilters = ref({ ...filters })
const searchItems = computed<SearchFormItem[]>(() => [
{
label: '运营商账户',
prop: 'carrier_id',
type: 'select',
options: carrierOptions.value,
onChange: ({ val }) => selectCarrier(val),
config: {
remote: true,
filterable: true,
clearable: true,
remoteMethod: searchCarriers,
loading: carrierLoading.value,
placeholder: '输入运营商名称搜索'
}
},
{
label: '周期',
prop: 'period',
type: 'select',
options: [
{ label: '全部周期', value: 'all' },
{ label: '前一个周期', value: 'previous' },
{ label: '即将到来周期', value: 'upcoming' }
],
config: { clearable: false }
}
])
const preReset = ref<PollingPreResetStats | null>(null)
const preLoading = ref(false)
const preError = ref('')
const tiers = ref<PollingTierStats | null>(null)
const tierLoading = ref(false)
const tierError = ref('')
let preSequence = 0
let tierSequence = 0
const preRows = computed(() => {
if (!preReset.value) return []
return pollingPreResetRows(preReset.value).filter(
(row) =>
(appliedFilters.value.carrier_id == null ||
row.carrier_id === appliedFilters.value.carrier_id) &&
(appliedFilters.value.period === 'all' ||
row.key.endsWith(`:${appliedFilters.value.period}`))
)
})
const packageRows = computed(() =>
Object.entries(tiers.value?.by_package_state || {}).map(([state, cards]) => ({
state,
cards,
label: state === 'unknown' ? '未知' : pollingOptionLabel(POLLING_PACKAGE_STATES, state)
}))
)
const { columns: preColumns, columnChecks: preColumnChecks } = useGatewayColumns([
{ prop: 'carrier_name', label: '运营商账户', width: 180 },
{ prop: 'period_name', label: '周期' },
{ prop: 'reset_at', label: '重置时刻', width: 180 },
{ prop: 'active_package_cards', label: '有生效套餐卡数' },
{ prop: 'checked_in_window', label: '窗口内已检查卡数(近似)', width: 200 },
{ prop: 'not_checked_in_window', label: '窗口内未检查卡数(近似)', width: 210 },
{ prop: 'le_5', label: '距重置 ≤5 分钟' },
{ prop: 'le_15', label: '距重置 ≤15 分钟' },
{ prop: 'le_30', label: '距重置 ≤30 分钟' },
{ prop: 'le_60', label: '距重置 ≤60 分钟' },
{ prop: 'gt_60', label: '距重置 >60 分钟' }
])
const { columns: tierColumns, columnChecks: tierColumnChecks } = useGatewayColumns([
{ prop: 'config_id', label: '配置 ID' },
{ prop: 'config_name', label: '配置名称' },
{ prop: 'cards', label: '卡数' }
])
function preValue(row: ReturnType<typeof pollingPreResetRows>[number], prop: string) {
if (prop === 'carrier_name') return row.carrier_name || `运营商账户(${row.carrier_id})`
if (prop === 'period_name') return row.period_name
if (!row.period) return prop === 'reset_at' ? '无周期数据' : '-'
if (prop === 'reset_at') return formatDateTime(row.period.reset_at)
if (prop in row.period.last_offset_buckets)
return (
row.period.last_offset_buckets[prop as keyof typeof row.period.last_offset_buckets] ?? '-'
)
return row.period[prop as keyof typeof row.period] ?? '-'
}
async function loadPreReset() {
if (preLoading.value) return
const current = ++preSequence
preLoading.value = true
preError.value = ''
try {
requireGatewayAccess(canPreReset.value)
const data = gatewayData(await PollingConfigService.getPreResetStats())
if (current === preSequence && canPreReset.value) preReset.value = data
} catch (cause) {
if (current === preSequence) {
preError.value = gatewayError(cause)
preReset.value = null
}
} finally {
if (current === preSequence) preLoading.value = false
}
}
async function loadTiers() {
if (tierLoading.value) return
const current = ++tierSequence
tierLoading.value = true
tierError.value = ''
try {
requireGatewayAccess(canTiers.value)
const data = gatewayData(await PollingConfigService.getTierStats())
if (current === tierSequence && canTiers.value) tiers.value = data
} catch (cause) {
if (current === tierSequence) {
tierError.value = gatewayError(cause)
tiers.value = null
}
} finally {
if (current === tierSequence) tierLoading.value = false
}
}
function searchPreReset() {
appliedFilters.value = { ...filters }
void loadPreReset()
}
function resetFilters() {
Object.assign(filters, { carrier_id: undefined, period: 'all' })
searchPreReset()
}
watch(canPreReset, (allowed) => {
if (!allowed) {
++preSequence
preReset.value = null
preLoading.value = false
activeTab.value = 'tiers'
}
})
watch(canTiers, (allowed) => {
if (!allowed) {
++tierSequence
tiers.value = null
tierLoading.value = false
activeTab.value = 'pre-reset'
}
})
onMounted(() => {
if (canPreReset.value) void loadPreReset()
if (canTiers.value) void loadTiers()
})
onBeforeUnmount(() => {
++preSequence
++tierSequence
})
</script>
<style scoped lang="scss">
.polling-stats-page {
display: flex;
flex-direction: column;
min-width: 0;
}
.stats-heading {
display: flex;
gap: 16px;
align-items: center;
justify-content: space-between;
margin-bottom: 16px;
}
.stats-heading h2 {
margin: 0;
font-size: 20px;
font-weight: 600;
}
.stats-overview {
margin: 16px 0;
}
h3 {
margin: 20px 0 12px;
font-size: 16px;
}
</style>