refactor&perf
- proxy config - query export - optimize station card request
This commit is contained in:
87
src/composables/query/alarm/use-station-alarms-query.ts
Normal file
87
src/composables/query/alarm/use-station-alarms-query.ts
Normal file
@@ -0,0 +1,87 @@
|
||||
import type { Station } from '@/apis/domains';
|
||||
import { postNdmDeviceAlarmLogPage } from '@/apis/requests';
|
||||
import { DeviceType } from '@/enums/device-type';
|
||||
import { useQueryControlStore } from '@/stores/query-control';
|
||||
import { useStationStore } from '@/stores/station';
|
||||
import { useQuery } from '@tanstack/vue-query';
|
||||
import dayjs from 'dayjs';
|
||||
import { storeToRefs } from 'pinia';
|
||||
import { computed } from 'vue';
|
||||
import type { StationAlarms } from './use-line-alarms-query';
|
||||
|
||||
const createEmptyStationAlarms = (): StationAlarms => ({
|
||||
[DeviceType.Camera]: [],
|
||||
[DeviceType.Decoder]: [],
|
||||
[DeviceType.Keyboard]: [],
|
||||
[DeviceType.MediaServer]: [],
|
||||
[DeviceType.Nvr]: [],
|
||||
[DeviceType.SecurityBox]: [],
|
||||
[DeviceType.Switch]: [],
|
||||
[DeviceType.VideoServer]: [],
|
||||
unclassified: [],
|
||||
});
|
||||
|
||||
export function useStationAlarmsQuery(stationCode: Station['code']) {
|
||||
const stationStore = useStationStore();
|
||||
const { updatedTime, onlineStationList } = storeToRefs(stationStore);
|
||||
|
||||
const queryControlStore = useQueryControlStore();
|
||||
const { pollingEnabled } = storeToRefs(queryControlStore);
|
||||
|
||||
const isOnline = computed(() => onlineStationList.value.some((stn) => stn.code === stationCode));
|
||||
|
||||
return useQuery({
|
||||
queryKey: ['station-alarms', stationCode, updatedTime],
|
||||
enabled: computed(() => isOnline.value && pollingEnabled.value),
|
||||
placeholderData: (prev) => prev ?? createEmptyStationAlarms(),
|
||||
queryFn: async (): Promise<StationAlarms> => {
|
||||
// 如果车站离线,返回空数据
|
||||
if (!isOnline.value) {
|
||||
return createEmptyStationAlarms();
|
||||
}
|
||||
|
||||
try {
|
||||
const now = dayjs();
|
||||
const todayStart = now.startOf('date').valueOf();
|
||||
const todayEnd = now.endOf('date').valueOf();
|
||||
|
||||
const { records: alarmList } = await postNdmDeviceAlarmLogPage(stationCode, {
|
||||
model: {},
|
||||
extra: {
|
||||
alarmDate_ge: todayStart,
|
||||
alarmDate_le: todayEnd,
|
||||
},
|
||||
size: 50000,
|
||||
current: 1,
|
||||
sort: 'id',
|
||||
order: 'descending',
|
||||
});
|
||||
|
||||
const cameraAlarms = alarmList.filter((alarm) => alarm.deviceType === DeviceType.Camera);
|
||||
const decoderAlarms = alarmList.filter((alarm) => alarm.deviceType === DeviceType.Decoder);
|
||||
const keyboardAlarms = alarmList.filter((alarm) => alarm.deviceType === DeviceType.Keyboard);
|
||||
const mediaServerAlarms = alarmList.filter((alarm) => alarm.deviceType === DeviceType.MediaServer);
|
||||
const nvrAlarms = alarmList.filter((alarm) => alarm.deviceType === DeviceType.Nvr);
|
||||
const securityBoxAlarms = alarmList.filter((alarm) => alarm.deviceType === DeviceType.SecurityBox);
|
||||
const switchAlarms = alarmList.filter((alarm) => alarm.deviceType === DeviceType.Switch);
|
||||
const videoServerAlarms = alarmList.filter((alarm) => alarm.deviceType === DeviceType.VideoServer);
|
||||
|
||||
return {
|
||||
[DeviceType.Camera]: cameraAlarms,
|
||||
[DeviceType.Decoder]: decoderAlarms,
|
||||
[DeviceType.Keyboard]: keyboardAlarms,
|
||||
[DeviceType.MediaServer]: mediaServerAlarms,
|
||||
[DeviceType.Nvr]: nvrAlarms,
|
||||
[DeviceType.SecurityBox]: securityBoxAlarms,
|
||||
[DeviceType.Switch]: switchAlarms,
|
||||
[DeviceType.VideoServer]: videoServerAlarms,
|
||||
unclassified: alarmList ?? [],
|
||||
};
|
||||
} catch (error) {
|
||||
console.error(`获取车站 ${stationCode} 告警数据失败:`, error);
|
||||
// 如果获取失败,返回空数据
|
||||
return createEmptyStationAlarms();
|
||||
}
|
||||
},
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user