diff --git a/src/components/device-page/device-card/history-diag-card/device-usage-history-diag-card.vue b/src/components/device-page/device-card/history-diag-card/device-usage-history-diag-card.vue index 6dad784..dc27267 100644 --- a/src/components/device-page/device-card/history-diag-card/device-usage-history-diag-card.vue +++ b/src/components/device-page/device-card/history-diag-card/device-usage-history-diag-card.vue @@ -6,13 +6,11 @@ function getValueByFieldPath(record: Record, fieldPath?: string) { diff --git a/src/composables/query/device/index.ts b/src/composables/query/device/index.ts index 13d01bb..1a247b2 100644 --- a/src/composables/query/device/index.ts +++ b/src/composables/query/device/index.ts @@ -1,3 +1,4 @@ export * from './domains'; +export * from './use-device-snmp-logs-query'; export * from './use-line-devices-query'; diff --git a/src/composables/query/device/use-device-snmp-logs-query.ts b/src/composables/query/device/use-device-snmp-logs-query.ts new file mode 100644 index 0000000..ef880f1 --- /dev/null +++ b/src/composables/query/device/use-device-snmp-logs-query.ts @@ -0,0 +1,88 @@ +import type { NdmDeviceResultVO, PageParams } from '@/apis/models'; +import { postSnmpLogPage } from '@/apis/requests'; +import { DEVICE_SNMP_LOGS_QUERY_KEY } from '@/constants'; +import { useQuery, useQueryClient } from '@tanstack/vue-query'; +import dayjs from 'dayjs'; +import type { PaginationProps } from 'naive-ui'; +import { computed, h, watch } from 'vue'; +import { reactive, type Ref } from 'vue'; + +export function useDeviceSnmpLogsQuery(stationCode: Ref, ndmDevice: Ref, dateTimeRange: Ref<[number, number]>) { + const DEFAULT_PAGE_SIZE = 10; + const pagination = reactive({ + size: 'small', + showSizePicker: true, + page: 1, + pageSize: DEFAULT_PAGE_SIZE, + pageSizes: [5, 10, 20, 50, 80, 100], + itemCount: 0, + prefix: ({ itemCount }) => { + return h('div', {}, { default: () => `共${itemCount}条` }); + }, + onUpdatePage: (page) => { + pagination.page = page; + refresh(); + }, + onUpdatePageSize: (pageSize) => { + pagination.pageSize = pageSize; + pagination.page = 1; + refresh(); + }, + }); + const queryKey = reactive([DEVICE_SNMP_LOGS_QUERY_KEY, stationCode, ndmDevice, dateTimeRange, pagination.page, pagination.pageSize]); + + const queryClient = useQueryClient(); + + const { + data: snmpLogRespData, + isPending, + isFetching, + error, + } = useQuery({ + queryKey, + enabled: computed(() => dateTimeRange.value.every((dateTime) => dateTime > 0)), + queryFn: async () => { + const deviceId = ndmDevice.value.id; + const createdTime_precisest = dayjs(dateTimeRange.value[0]).format('YYYY-MM-DD HH:mm:ss'); + const createdTime_preciseed = dayjs(dateTimeRange.value[1]).format('YYYY-MM-DD HH:mm:ss'); + const restParams: Omit, 'model' | 'extra'> = { + current: pagination.page ?? 1, + size: pagination.pageSize ?? DEFAULT_PAGE_SIZE, + sort: 'id', + order: 'descending', + }; + const respData = await postSnmpLogPage(stationCode.value, { + model: { deviceId }, + extra: { createdTime_precisest, createdTime_preciseed }, + ...restParams, + }); + return respData; + }, + }); + + watch(snmpLogRespData, (data) => { + if (data) { + pagination.itemCount = parseInt(data.total); + } + }); + + watch(error, (err) => { + if (err) { + console.error(`查询${ndmDevice.value.name}的SNMP日志失败:`, err); + window.$message.error(err.message); + } + }); + + const refresh = () => { + queryClient.invalidateQueries({ queryKey }); + }; + + return { + snmpLogRespData, + isPending, + isFetching, + error, + pagination, + refresh, + }; +} diff --git a/src/constants/query.ts b/src/constants/query.ts index 69a9435..e9fa5ea 100644 --- a/src/constants/query.ts +++ b/src/constants/query.ts @@ -1,4 +1,4 @@ export const STATION_LIST_QUERY_KEY = 'station-list'; export const LINE_DEVICES_QUERY_KEY = 'line-devices'; export const LINE_ALARM_COUNTS_QUERY_KEY = 'line-alarm-counts'; -export const LINE_ALARMS_QUERY_KEY = 'line-alarms'; +export const DEVICE_SNMP_LOGS_QUERY_KEY = 'device-snmp-logs';