110 lines
3.6 KiB
TypeScript
110 lines
3.6 KiB
TypeScript
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 { isCancelledError, useMutation, useQuery } from '@tanstack/vue-query';
|
|
import { storeToRefs } from 'pinia';
|
|
import { computed } from 'vue';
|
|
import dayjs from 'dayjs';
|
|
import { postNdmDeviceAlarmLogPage } from '@/apis/requests';
|
|
import type { Station } from '@/apis/domains';
|
|
import { DeviceType, getDeviceTypeVal } from '@/enums/device-type';
|
|
import type { StationAlarmCounts } from './domains';
|
|
import { runTask } from '@/utils/run-task';
|
|
|
|
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');
|
|
// queryControlStore.updateAlarmQueryUpdatedAt();
|
|
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: (stationAlarmCounts, { station }) => {
|
|
runTask(() => {
|
|
lineAlarmCounts.value[station.code] = stationAlarmCounts;
|
|
});
|
|
},
|
|
onError: (error, { station }) => {
|
|
if (!isCancelledError(error)) {
|
|
console.error(`获取车站 ${station.name} 设备告警数据失败:`, error);
|
|
lineAlarmCounts.value[station.code] = createEmptyStationAlarmCounts();
|
|
throw error;
|
|
}
|
|
},
|
|
});
|
|
}
|