refactor: 重构项目结构
- 优化 `车站-设备-告警` 轮询机制 - 改进设备卡片的布局 - 支持修改设备 - 告警轮询中获取完整告警数据 - 车站告警详情支持导出完整的 `今日告警列表` - 支持将状态持久化到 `IndexedDB` - 新增轮询控制 (调试模式) - 新增离线开发模式 (调试模式) - 新增 `IndexedDB` 数据控制 (调试模式)
This commit is contained in:
88
src/composables/query/use-line-alarms-query.ts
Normal file
88
src/composables/query/use-line-alarms-query.ts
Normal file
@@ -0,0 +1,88 @@
|
||||
import { initStationAlarms, pageDeviceAlarmLogApi, type Station } from '@/apis';
|
||||
import { LINE_ALARMS_QUERY_KEY, STATION_ALARMS_MUTATION_KEY } from '@/constants';
|
||||
import { tryGetDeviceType } from '@/enums';
|
||||
import { useAlarmStore, useStationStore } from '@/stores';
|
||||
import { parseErrorFeedback } from '@/utils';
|
||||
import { CancelledError, useMutation, useQuery } from '@tanstack/vue-query';
|
||||
import { isCancel } from 'axios';
|
||||
import dayjs from 'dayjs';
|
||||
import { storeToRefs } from 'pinia';
|
||||
import { computed } from 'vue';
|
||||
|
||||
export const useStationAlarmsMutation = () => {
|
||||
const alarmStore = useAlarmStore();
|
||||
|
||||
return useMutation({
|
||||
mutationKey: [STATION_ALARMS_MUTATION_KEY],
|
||||
mutationFn: async (params: { station: Station; signal?: AbortSignal }) => {
|
||||
const { station, signal } = params;
|
||||
const stationAlarms = initStationAlarms();
|
||||
if (!station.online) {
|
||||
return stationAlarms;
|
||||
}
|
||||
const now = dayjs();
|
||||
const todayStart = now.startOf('date').valueOf();
|
||||
const todayEnd = now.endOf('date').valueOf();
|
||||
const { records } = await pageDeviceAlarmLogApi(
|
||||
{
|
||||
model: {
|
||||
stationCode: station.code,
|
||||
},
|
||||
extra: {
|
||||
alarmDate_ge: todayStart,
|
||||
alarmDate_le: todayEnd,
|
||||
},
|
||||
size: 50000,
|
||||
current: 1,
|
||||
sort: 'alarmDate',
|
||||
order: 'descending',
|
||||
},
|
||||
{
|
||||
stationCode: station.code,
|
||||
signal,
|
||||
},
|
||||
);
|
||||
for (const alarm of records) {
|
||||
const { deviceType: deviceTypeCode } = alarm;
|
||||
const deviceType = tryGetDeviceType(deviceTypeCode);
|
||||
if (!!deviceType) {
|
||||
stationAlarms[deviceType].unshift(alarm);
|
||||
}
|
||||
}
|
||||
stationAlarms['unclassified'] = records;
|
||||
return stationAlarms;
|
||||
},
|
||||
onSuccess: (stationAlarms, { station }) => {
|
||||
alarmStore.setStationAlarms(station.code, stationAlarms);
|
||||
},
|
||||
onError: (error) => {
|
||||
console.error(error);
|
||||
if (isCancel(error) || error instanceof CancelledError) return;
|
||||
const errorFeedback = parseErrorFeedback(error);
|
||||
window.$message.error(errorFeedback);
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* 由 `useLineStationsQuery` 显式触发
|
||||
* @see [use-line-stations-query.ts](./use-line-stations-query.ts)
|
||||
*/
|
||||
export const useLineAlarmsQuery = () => {
|
||||
const stationStore = useStationStore();
|
||||
const { stations } = storeToRefs(stationStore);
|
||||
const { mutateAsync: getStationAlarms } = useStationAlarmsMutation();
|
||||
|
||||
return useQuery({
|
||||
queryKey: computed(() => [LINE_ALARMS_QUERY_KEY]),
|
||||
enabled: false,
|
||||
queryFn: async ({ signal }) => {
|
||||
console.time(LINE_ALARMS_QUERY_KEY);
|
||||
for (const station of stations.value) {
|
||||
await getStationAlarms({ station, signal }).catch(() => {});
|
||||
}
|
||||
console.timeEnd(LINE_ALARMS_QUERY_KEY);
|
||||
return null;
|
||||
},
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user