|
|
@ -123,15 +123,16 @@ async function getMonitorData(ctx) { |
|
|
|
try { |
|
|
|
const { clickHouse } = ctx.app.fs |
|
|
|
const { factorId, sensorId, startTime, endTime } = ctx.query |
|
|
|
const projectId=sensorId.split('-')[0] |
|
|
|
const factorsList = await clickHouse.alarmLocal.query(`SELECT FactorID,Items,ItemNames,ItemUnits FROM factors
|
|
|
|
WHERE FactorID=${factorId} |
|
|
|
WHERE FactorID=${factorId} AND Project='${projectId}' |
|
|
|
`).toPromise() || []
|
|
|
|
const dataArray = sensorId.split(',') |
|
|
|
const transformedArray = dataArray.map(item => { |
|
|
|
return item.replace('-',':')+'' |
|
|
|
}) |
|
|
|
const id=`(${transformedArray.map(id => `'${id}'`).join(',')})` |
|
|
|
const monitorData = await clickHouse.alarmLocal.query(`SELECT SensorId,CollectTime,Values FROM themes
|
|
|
|
const monitorData = await clickHouse.alarmLocal.query(`SELECT distinct SensorId,CollectTime,Values FROM themes
|
|
|
|
WHERE SensorId in ${id} |
|
|
|
AND CollectTime >= toDate('${startTime}') |
|
|
|
AND CollectTime <= toDate('${endTime}') |
|
|
@ -144,11 +145,14 @@ async function getMonitorData(ctx) { |
|
|
|
let factors = [] |
|
|
|
//因素名词
|
|
|
|
let factorNames = [] |
|
|
|
//单位
|
|
|
|
let c=[] |
|
|
|
factorsList.map(item => { |
|
|
|
factors = item.ItemNames.split(',') |
|
|
|
factorNames = item.Items.split(',') |
|
|
|
factors.map((child,index)=> { |
|
|
|
items[factorNames[index]] = { name: child, unit:factorNames[index] } |
|
|
|
c = item.ItemUnits.split(',') |
|
|
|
factorNames.map((child,index)=> { |
|
|
|
items[factorNames[index]] = { name:factors[index] , unit:c[index] } |
|
|
|
}) |
|
|
|
}) |
|
|
|
} |
|
|
@ -156,18 +160,29 @@ async function getMonitorData(ctx) { |
|
|
|
let sensors = [] |
|
|
|
let data=[] |
|
|
|
if(sensor&&monitorData&&sensor.length && monitorData.length){ |
|
|
|
sensor.map(item=>{ |
|
|
|
const filterData=monitorData.filter(p=>p.SensorId==item.ID) |
|
|
|
filterData.map(data=>{ |
|
|
|
const values = {} |
|
|
|
Object.keys(items).forEach(key => { |
|
|
|
const index = Object.keys(items).indexOf(key) |
|
|
|
values[key] = data.Values[index] |
|
|
|
}) |
|
|
|
sensors.push({ values, time: moment(data.CollectTime).format('YYYY-MM-DD HH:mm:ss') }) |
|
|
|
}) |
|
|
|
data.push({id:item.ID,name:item.SensorLocationDescription,data:sensors}) |
|
|
|
}) |
|
|
|
sensor.forEach((item) => { |
|
|
|
// 过滤与当前传感器相关的数据
|
|
|
|
const sensorDataArray = monitorData.filter((d) => d.SensorId == item.ID) |
|
|
|
// 构建最终的传感器数据结构
|
|
|
|
const sensorDataItem = { |
|
|
|
id: item.ID, |
|
|
|
name: item.SensorLocationDescription, |
|
|
|
data: sensorDataArray.map((d) => { |
|
|
|
// 根据 item 对象进行映射
|
|
|
|
const values = {}; |
|
|
|
itemKeys = Object.keys(items) |
|
|
|
itemKeys.forEach((key, index) => { |
|
|
|
values[key] = d.Values[index]; |
|
|
|
}); |
|
|
|
|
|
|
|
return { |
|
|
|
time: d.CollectTime, |
|
|
|
values: values |
|
|
|
}; |
|
|
|
}) |
|
|
|
}; |
|
|
|
data.push(sensorDataItem) |
|
|
|
}) |
|
|
|
} |
|
|
|
ctx.body = { items, sensors: data } |
|
|
|
ctx.status = 200 |
|
|
|