112 lines
3.5 KiB
TypeScript
112 lines
3.5 KiB
TypeScript
import { pageDeviceAlarmLogApi, type Station, type StationAlarmCounts } from '@/apis';
|
|
import { LINE_ALARMS_QUERY_KEY } from '@/constants';
|
|
import { DeviceType, tryGetDeviceTypeVal } from '@/enums';
|
|
import { useAlarmStore, usePollingStore, useStationStore } from '@/stores';
|
|
import { runTask } from '@/utils';
|
|
import { isCancelledError, useMutation, useQuery } from '@tanstack/vue-query';
|
|
import dayjs from 'dayjs';
|
|
import { storeToRefs } from 'pinia';
|
|
import { computed } from 'vue';
|
|
|
|
const createEmptyStationAlarmCounts = () => {
|
|
return {
|
|
[DeviceType.AlarmHost]: 0,
|
|
[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 useLineAlarmsQuery() {
|
|
const stationStore = useStationStore();
|
|
const { stationList } = storeToRefs(stationStore);
|
|
const pollingStore = usePollingStore();
|
|
const { alarmQueryStamp } = storeToRefs(pollingStore);
|
|
const { mutateAsync: getStationAlarmCounts } = useStationAlarmCountsMutation();
|
|
|
|
return useQuery({
|
|
queryKey: [LINE_ALARMS_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 }).catch(() => {});
|
|
}
|
|
console.timeEnd('useLineALarmCountsQuery');
|
|
// pollingStore.updateAlarmQueryUpdatedAt();
|
|
return null;
|
|
},
|
|
});
|
|
}
|
|
|
|
interface StationAlarmCountsMutationParams {
|
|
station: Station;
|
|
signal?: AbortSignal;
|
|
}
|
|
|
|
function useStationAlarmCountsMutation() {
|
|
const lineAlarmsStore = useAlarmStore();
|
|
const { lineAlarmCounts } = storeToRefs(lineAlarmsStore);
|
|
|
|
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 pageDeviceAlarmLogApi(
|
|
{
|
|
model: {
|
|
stationCode: station.code,
|
|
},
|
|
extra: {
|
|
alarmDate_ge: todayStart,
|
|
alarmDate_le: todayEnd,
|
|
},
|
|
size: 50000,
|
|
current: 1,
|
|
sort: 'id',
|
|
order: 'descending',
|
|
},
|
|
{
|
|
stationCode: station.code,
|
|
signal,
|
|
},
|
|
);
|
|
for (const alarm of alarmList) {
|
|
const deviceTypeVal = tryGetDeviceTypeVal(alarm.deviceType);
|
|
if (deviceTypeVal) {
|
|
stationAlarmCounts[deviceTypeVal]++;
|
|
}
|
|
}
|
|
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;
|
|
}
|
|
},
|
|
});
|
|
}
|