useHistoryData.ts
2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import { EChartsOption } from 'echarts';
import moment from 'moment';
import { computed, ref, unref } from 'vue';
import { eChartOptions } from '../../localtion/config.data';
import { HistoryData } from '/@/api/alarm/position/model';
import { getDeviceAttributes } from '/@/api/dataBoard';
import { DeviceAttributeRecord } from '/@/api/dataBoard/model';
import { dateUtil } from '/@/utils/dateUtil';
import { isArray } from '/@/utils/is';
import { DEFAULT_DATE_FORMAT } from '/@/views/visual/board/detail/config/util';
import { QueryWay, SchemaFiled } from '/@/views/visual/palette/components/HistoryTrendModal/config';
interface DeviceOption {
deviceProfileId: string;
}
export function useHistoryData() {
const deviceAttrs = ref<DeviceAttributeRecord[]>([]);
const getDeviceKeys = computed(() => {
return unref(deviceAttrs).map((item) => item.identifier);
});
const getDeviceAttribute = async (record: DeviceOption) => {
try {
const { deviceProfileId } = record;
const list = (await getDeviceAttributes({ deviceProfileId })) || [];
deviceAttrs.value = isArray(list) ? list : [];
} catch (error) {
throw error;
}
};
function getSearchParams(value: Partial<Record<SchemaFiled, string>>) {
const { startTs, endTs, interval, agg, limit, keys, way, deviceId, orderBy } = value;
const basicRecord = {
entityId: deviceId,
keys: keys ? keys : unref(getDeviceKeys).join(),
interval,
agg,
limit,
orderBy,
};
if (way === QueryWay.LATEST) {
return Object.assign(basicRecord, {
startTs: moment().subtract(startTs, 'ms').valueOf(),
endTs: Date.now(),
});
} else {
return Object.assign(basicRecord, {
startTs: moment(startTs).valueOf(),
endTs: moment(endTs).valueOf(),
});
}
}
function setChartOptions(
data: HistoryData,
keys?: DeviceAttributeRecord | DeviceAttributeRecord[]
) {
const dataArray: [string, string, string][] = [];
for (const key in data) {
for (const item of data[key]) {
let { value } = item;
const { ts } = item;
const time = dateUtil(ts).format(DEFAULT_DATE_FORMAT);
value = Number(value).toFixed(2);
dataArray.push([time, value, key as string]);
}
}
keys = keys ? [keys as DeviceAttributeRecord] : unref(deviceAttrs);
const legend = keys.map((item) => item.name);
const series: EChartsOption['series'] = (keys as DeviceAttributeRecord[]).map((item) => {
return {
name: item.name,
type: 'line',
data: dataArray.filter((temp) => temp[2] === item.identifier),
};
});
return eChartOptions(series, legend);
}
return {
deviceAttrs,
getDeviceKeys,
getDeviceAttribute,
getSearchParams,
setChartOptions,
};
}