|
@ -122,20 +122,21 @@ async function getSensors (ctx) { |
|
|
async function getMonitorData(ctx) { |
|
|
async function getMonitorData(ctx) { |
|
|
try { |
|
|
try { |
|
|
const { clickHouse } = ctx.app.fs |
|
|
const { clickHouse } = ctx.app.fs |
|
|
const { factorId,sensorId,startTime, endTime,limit,page } = ctx.query |
|
|
const { factorId, sensorId, startTime, endTime } = ctx.query |
|
|
const offset=page*limit |
|
|
|
|
|
const factorsList = await clickHouse.alarmLocal.query(`SELECT FactorID,Items,ItemNames,ItemUnits FROM factors
|
|
|
const factorsList = await clickHouse.alarmLocal.query(`SELECT FactorID,Items,ItemNames,ItemUnits FROM factors
|
|
|
WHERE FactorID=${factorId} |
|
|
WHERE FactorID=${factorId} |
|
|
`).toPromise() || []
|
|
|
`).toPromise() || []
|
|
|
const id=sensorId&&sensorId.replace('-',':') |
|
|
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 SensorId,CollectTime,Values FROM themes
|
|
|
WHERE SensorId in ('${id}') |
|
|
WHERE SensorId in ${id} |
|
|
AND CollectTime >= toDate('${startTime}') |
|
|
AND CollectTime >= toDate('${startTime}') |
|
|
AND CollectTime <= toDate('${endTime}') |
|
|
AND CollectTime <= toDate('${endTime}') |
|
|
LIMIT ${limit} OFFSET ${offset} |
|
|
|
|
|
`).toPromise() || []
|
|
|
`).toPromise() || []
|
|
|
console.log('vmonitorDatamonitorDatamonitorData',monitorData) |
|
|
const sensor = await clickHouse.alarmLocal.query(`SELECT distinct ID, SensorId,SensorLocationDescription FROM sensors WHERE ID in ${id}`).toPromise() |
|
|
const sensor=await clickHouse.alarmLocal.query(`SELECT distinct SensorId,SensorLocationDescription FROM sensors WHERE ID='${id}'`).toPromise() |
|
|
|
|
|
//监测项
|
|
|
//监测项
|
|
|
let items = {} |
|
|
let items = {} |
|
|
if (factorsList && factorsList.length > 0) { |
|
|
if (factorsList && factorsList.length > 0) { |
|
@ -155,24 +156,24 @@ async function getMonitorData (ctx) { |
|
|
} |
|
|
} |
|
|
//设备数据+数据
|
|
|
//设备数据+数据
|
|
|
let sensors = [] |
|
|
let sensors = [] |
|
|
if(monitorData&&monitorData.length>0){ |
|
|
let data=[] |
|
|
monitorData.map(data => { |
|
|
if(sensor&&monitorData&&sensor.length && monitorData.length){ |
|
|
const values = {}; |
|
|
sensor.map(item=>{ |
|
|
|
|
|
const filterData=monitorData.filter(p=>p.SensorId==item.ID) |
|
|
|
|
|
filterData.map(data=>{ |
|
|
|
|
|
const values = {} |
|
|
Object.keys(items).forEach(key => { |
|
|
Object.keys(items).forEach(key => { |
|
|
const index = Object.keys(items).indexOf(key); |
|
|
const index = Object.keys(items).indexOf(key) |
|
|
values[key] = data.Values[index]; |
|
|
values[key] = data.Values[index] |
|
|
}); |
|
|
}) |
|
|
sensors.push({ values, time: moment(data.CollectTime).format('YYYY-MM-DD HH:mm:ss') }); |
|
|
sensors.push({ values, time: moment(data.CollectTime).format('YYYY-MM-DD HH:mm:ss') }) |
|
|
}); |
|
|
}) |
|
|
|
|
|
data.push({id:item.ID,name:item.SensorLocationDescription,data:sensors}) |
|
|
|
|
|
}) |
|
|
} |
|
|
} |
|
|
if(sensor&&sensor.length){ |
|
|
ctx.body = { items, sensors: data } |
|
|
ctx.body={items,sensors:[{data:sensors,id:sensor[0].SensorId,name:sensor[0].SensorLocationDescription}]} |
|
|
|
|
|
ctx.status = 200 |
|
|
ctx.status = 200 |
|
|
}else{ |
|
|
|
|
|
ctx.body={items,sensors:[]} |
|
|
|
|
|
ctx.status=200 |
|
|
|
|
|
} |
|
|
|
|
|
} catch (error) { |
|
|
} catch (error) { |
|
|
ctx.fs.logger.error(`path: ${ctx.path}, error: error`); |
|
|
ctx.fs.logger.error(`path: ${ctx.path}, error: error`); |
|
|
ctx.status = 400; |
|
|
ctx.status = 400; |
|
|