refactor:
- extend NdmDeviceAlarmLogVO - only query alarm counts - separate request and store update in useQuery - refactor station card and alarm modal, data fetching is now inside modal - optimize device tree - optimize query station list - make export size follow page size - fix query sequence and make them follow stations -> devices -> alarms
This commit is contained in:
104
src/composables/query/alarm/use-line-alarm-counts-query.ts
Normal file
104
src/composables/query/alarm/use-line-alarm-counts-query.ts
Normal file
@@ -0,0 +1,104 @@
|
||||
import { LINE_ALARM_COUNTS_QUERY_KEY } from '@/constants';
|
||||
import { useLineAlarmCountsStore } from '@/stores/line-alarm-counts';
|
||||
import { useQueryControlStore } from '@/stores/query-control';
|
||||
import { useStationStore } from '@/stores/station';
|
||||
import { useMutation, useQuery } from '@tanstack/vue-query';
|
||||
import { storeToRefs } from 'pinia';
|
||||
import { computed } from 'vue';
|
||||
import dayjs from 'dayjs';
|
||||
import { postNdmDeviceAlarmLogPage } from '@/apis/requests';
|
||||
import { sleep } from '@/utils/sleep';
|
||||
import type { Station } from '@/apis/domains';
|
||||
import { DeviceType, getDeviceTypeVal } from '@/enums/device-type';
|
||||
import type { StationAlarmCounts } from './domains';
|
||||
|
||||
const createEmptyStationAlarmCounts = () => {
|
||||
return {
|
||||
[DeviceType.Camera]: 0,
|
||||
[DeviceType.Decoder]: 0,
|
||||
[DeviceType.Keyboard]: 0,
|
||||
[DeviceType.MediaServer]: 0,
|
||||
[DeviceType.Nvr]: 0,
|
||||
[DeviceType.SecurityBox]: 0,
|
||||
[DeviceType.Switch]: 0,
|
||||
[DeviceType.VideoServer]: 0,
|
||||
unclassified: 0,
|
||||
};
|
||||
};
|
||||
|
||||
export function useLineAlarmCountsQuery() {
|
||||
const stationStore = useStationStore();
|
||||
const { stationList } = storeToRefs(stationStore);
|
||||
const queryControlStore = useQueryControlStore();
|
||||
const { alarmQueryStamp } = storeToRefs(queryControlStore);
|
||||
const { mutateAsync: getStationAlarmCounts } = useStationAlarmCountsMutation();
|
||||
|
||||
return useQuery({
|
||||
queryKey: [LINE_ALARM_COUNTS_QUERY_KEY, alarmQueryStamp],
|
||||
enabled: computed(() => alarmQueryStamp.value > 0),
|
||||
staleTime: Infinity,
|
||||
refetchOnMount: false,
|
||||
refetchOnReconnect: false,
|
||||
refetchOnWindowFocus: false,
|
||||
queryFn: async ({ signal }) => {
|
||||
console.time('useLineALarmCountsQuery');
|
||||
for (const station of stationList.value) {
|
||||
await getStationAlarmCounts({ station, signal });
|
||||
}
|
||||
console.timeEnd('useLineALarmCountsQuery');
|
||||
return null;
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
interface StationAlarmCountsMutationParams {
|
||||
station: Station;
|
||||
signal?: AbortSignal;
|
||||
}
|
||||
|
||||
function useStationAlarmCountsMutation() {
|
||||
const lineAlarmCountsStore = useLineAlarmCountsStore();
|
||||
const { lineAlarmCounts } = storeToRefs(lineAlarmCountsStore);
|
||||
|
||||
return useMutation<StationAlarmCounts, Error, StationAlarmCountsMutationParams>({
|
||||
mutationFn: async ({ station, signal }) => {
|
||||
const stationAlarmCounts = createEmptyStationAlarmCounts();
|
||||
if (!station.online) {
|
||||
return stationAlarmCounts;
|
||||
}
|
||||
const now = dayjs();
|
||||
const todayStart = now.startOf('date').valueOf();
|
||||
const todayEnd = now.endOf('date').valueOf();
|
||||
const { records: alarmList, total } = await postNdmDeviceAlarmLogPage(
|
||||
station.code,
|
||||
{
|
||||
model: {
|
||||
stationCode: station.code,
|
||||
},
|
||||
extra: {
|
||||
alarmDate_ge: todayStart,
|
||||
alarmDate_le: todayEnd,
|
||||
},
|
||||
size: 50000,
|
||||
current: 1,
|
||||
sort: 'id',
|
||||
order: 'descending',
|
||||
},
|
||||
signal,
|
||||
);
|
||||
for (const alarm of alarmList) {
|
||||
stationAlarmCounts[getDeviceTypeVal(alarm.deviceType)]++;
|
||||
}
|
||||
stationAlarmCounts.unclassified = parseInt(total);
|
||||
return stationAlarmCounts;
|
||||
},
|
||||
onSuccess: async (stationAlarmCounts, { station }) => {
|
||||
lineAlarmCounts.value[station.code] = stationAlarmCounts;
|
||||
await sleep();
|
||||
},
|
||||
onError: (error, { station }) => {
|
||||
console.error(`获取车站 ${station.name} 设备告警数据失败:`, error);
|
||||
lineAlarmCounts.value[station.code] = createEmptyStationAlarmCounts();
|
||||
},
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user