refactor&perf
- proxy config - query export - optimize station card request
This commit is contained in:
109
src/composables/query/alarm/use-line-alarms-query.ts
Normal file
109
src/composables/query/alarm/use-line-alarms-query.ts
Normal file
@@ -0,0 +1,109 @@
|
||||
import type { Station } from '@/apis/domains';
|
||||
import type { NdmDeviceAlarmLogResultVO } from '@/apis/models';
|
||||
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';
|
||||
|
||||
export interface CategorizedAlarms {
|
||||
occurred: NdmDeviceAlarmLogResultVO[];
|
||||
recovered: NdmDeviceAlarmLogResultVO[];
|
||||
}
|
||||
|
||||
export interface StationAlarms {
|
||||
[DeviceType.Camera]: NdmDeviceAlarmLogResultVO[];
|
||||
[DeviceType.Decoder]: NdmDeviceAlarmLogResultVO[];
|
||||
[DeviceType.Keyboard]: NdmDeviceAlarmLogResultVO[];
|
||||
[DeviceType.MediaServer]: NdmDeviceAlarmLogResultVO[];
|
||||
[DeviceType.Nvr]: NdmDeviceAlarmLogResultVO[];
|
||||
[DeviceType.SecurityBox]: NdmDeviceAlarmLogResultVO[];
|
||||
[DeviceType.Switch]: NdmDeviceAlarmLogResultVO[];
|
||||
[DeviceType.VideoServer]: NdmDeviceAlarmLogResultVO[];
|
||||
unclassified: NdmDeviceAlarmLogResultVO[];
|
||||
}
|
||||
|
||||
export interface LineAlarms {
|
||||
[stationCode: Station['code']]: StationAlarms;
|
||||
}
|
||||
|
||||
export function useLineAlarmsQuery() {
|
||||
const stationStore = useStationStore();
|
||||
const { updatedTime, stationList, onlineStationList } = storeToRefs(stationStore);
|
||||
const queryControlStore = useQueryControlStore();
|
||||
const { pollingEnabled } = storeToRefs(queryControlStore);
|
||||
|
||||
return useQuery({
|
||||
queryKey: ['line-alarms', updatedTime],
|
||||
enabled: computed(() => onlineStationList.value.length > 0 && pollingEnabled.value),
|
||||
placeholderData: (prev) => prev,
|
||||
queryFn: async (): Promise<LineAlarms> => {
|
||||
// console.time('useLineAlarmsQuery');
|
||||
|
||||
const lineAlarms: LineAlarms = {};
|
||||
|
||||
if (!stationList?.value) {
|
||||
return lineAlarms;
|
||||
}
|
||||
|
||||
for (const station of stationList.value) {
|
||||
lineAlarms[station.code] = {
|
||||
[DeviceType.Camera]: [],
|
||||
[DeviceType.Decoder]: [],
|
||||
[DeviceType.Keyboard]: [],
|
||||
[DeviceType.MediaServer]: [],
|
||||
[DeviceType.Nvr]: [],
|
||||
[DeviceType.SecurityBox]: [],
|
||||
[DeviceType.Switch]: [],
|
||||
[DeviceType.VideoServer]: [],
|
||||
unclassified: [],
|
||||
};
|
||||
|
||||
try {
|
||||
const now = dayjs();
|
||||
const todayStart = now.startOf('date').valueOf();
|
||||
const todayEnd = now.endOf('date').valueOf();
|
||||
const { records: alarmList } = await postNdmDeviceAlarmLogPage(station.code, {
|
||||
model: {},
|
||||
extra: {
|
||||
alarmDate_ge: todayStart,
|
||||
alarmDate_le: todayEnd,
|
||||
},
|
||||
size: 50000,
|
||||
current: 1,
|
||||
sort: 'id',
|
||||
order: 'descending',
|
||||
});
|
||||
const cameraAlarms = alarmList.filter((device) => device.deviceType === DeviceType.Camera);
|
||||
const decoderAlarms = alarmList.filter((device) => device.deviceType === DeviceType.Decoder);
|
||||
const keyboardAlarms = alarmList.filter((device) => device.deviceType === DeviceType.Keyboard);
|
||||
const mediaServerAlarms = alarmList.filter((device) => device.deviceType === DeviceType.MediaServer);
|
||||
const nvrAlarms = alarmList.filter((device) => device.deviceType === DeviceType.Nvr);
|
||||
const securityBoxAlarms = alarmList.filter((device) => device.deviceType === DeviceType.SecurityBox);
|
||||
const switchAlarms = alarmList.filter((device) => device.deviceType === DeviceType.Switch);
|
||||
const videoServerAlarms = alarmList.filter((device) => device.deviceType === DeviceType.VideoServer);
|
||||
lineAlarms[station.code] = {
|
||||
[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(`获取车站 ${station.name} 设备告警数据失败:`, error);
|
||||
}
|
||||
}
|
||||
|
||||
// console.timeEnd('useLineAlarmsQuery');
|
||||
|
||||
return lineAlarms;
|
||||
},
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user