@ -1,16 +1,16 @@
const moment = require ( 'moment' ) ;
async function getStructures ( ctx ) {
async function getStructures ( ctx ) {
try {
const { models } = ctx . fs . dc ;
const { clickHouse } = ctx . app . fs
const { pomsProjectId } = ctx . query
let bindRes = [ ]
let bindRes = [ ]
//选择全局就是查询所有项目下的结构物,有选择项目就只查询对应项目的结构物
if ( pomsProjectId ) {
bindRes = await models . ProjectCorrelation . findAll ( { where : { id : { $in : pomsProjectId . split ( ',' ) } } } )
} else {
if ( pomsProjectId ) {
bindRes = await models . ProjectCorrelation . findAll ( { where : { id : { $in : pomsProjectId . split ( ',' ) } } } )
} else {
bindRes = await models . ProjectCorrelation . findAll ( )
}
let anxinProjectIds = new Set ( )
@ -21,7 +21,7 @@ async function getStructures (ctx) {
}
}
}
let undelStrucRes = [ ]
let undelStrucRes = [ ]
if ( bindRes ) {
undelStrucRes = anxinProjectIds . size ?
await clickHouse . anxinyun . query (
@ -62,8 +62,8 @@ async function getStructures (ctx) {
) . toPromise ( ) :
[ ]
}
// undelStrucRes.push({strucId:4036,strucName:'象山港大桥'})
// undelStrucRes.push({strucId:1,strucName:'象山港大'})
// undelStrucRes.push({ strucId: 4036, strucName: '象山港大桥' })
// undelStrucRes.push({ strucId: 1, strucName: '象山港大' })
ctx . status = 200 ;
ctx . body = undelStrucRes
} catch ( error ) {
@ -75,22 +75,22 @@ async function getStructures (ctx) {
}
}
async function getFactors ( ctx ) {
async function getFactors ( ctx ) {
try {
const { models } = ctx . fs . dc ;
const { clickHouse } = ctx . app . fs
const { structId , cacl } = ctx . query
let list = [ ]
if ( cacl ) {
const factorList = await clickHouse . alarmLocal . query ( ` select distinct SafetyFactorTypeId, SafetyFactorTypeName from sensors where PlatformStructureId = ${ structId } ` ) . toPromise ( )
let fList = factorList . map ( m => m . SafetyFactorTypeId )
list = await clickHouse . alarmLocal . query ( ` select distinct FactorID, Name,Items,ItemNames from factors where FactorID in ( ${ [ ... fList , - 1 ] . join ( ',' ) } ) ` ) . toPromise ( )
} else {
list = await clickHouse . alarmLocal . query ( ` select distinct SafetyFactorTypeId, SafetyFactorTypeName from sensors where PlatformStructureId = ${ structId } ` ) . toPromise ( )
}
ctx . body = list
ctx . status = 200
} catch ( error ) {
const { structId , cacl } = ctx . query
let list = [ ]
if ( cacl ) {
const factorList = await clickHouse . alarmLocal . query ( ` select distinct SafetyFactorTypeId, SafetyFactorTypeName from sensors where PlatformStructureId = ${ structId } ` ) . toPromise ( )
let fList = factorList . map ( m => m . SafetyFactorTypeId )
list = await clickHouse . alarmLocal . query ( ` select distinct FactorID, Name,Items,ItemNames from factors where FactorID in ( ${ [ ... fList , - 1 ] . join ( ',' ) } ) ` ) . toPromise ( )
} else {
list = await clickHouse . alarmLocal . query ( ` select distinct SafetyFactorTypeId, SafetyFactorTypeName from sensors where PlatformStructureId = ${ structId } ` ) . toPromise ( )
}
ctx . body = list
ctx . status = 200
} catch ( error ) {
ctx . fs . logger . error ( ` path: ${ ctx . path } , error: error ` ) ;
ctx . status = 400 ;
ctx . body = {
@ -99,18 +99,18 @@ async function getFactors (ctx) {
}
}
//查询设备
async function getSensors ( ctx ) {
async function getSensors ( ctx ) {
try {
const { models } = ctx . fs . dc ;
const { clickHouse } = ctx . app . fs
const { structId , SafetyFactorTypeId } = ctx . query
const list = await clickHouse . alarmLocal . query ( `
const { structId , SafetyFactorTypeId } = ctx . query
const list = await clickHouse . alarmLocal . query ( `
select distinct SensorId , SensorLocationDescription , Project
from sensors where PlatformStructureId = $ { structId }
and SafetyFactorTypeId = $ { SafetyFactorTypeId } ` ).toPromise()
ctx . body = list
ctx . status = 200
} catch ( error ) {
ctx . body = list
ctx . status = 200
} catch ( error ) {
ctx . fs . logger . error ( ` path: ${ ctx . path } , error: error ` ) ;
ctx . status = 400 ;
ctx . body = {
@ -119,61 +119,62 @@ async function getSensors (ctx) {
}
}
//根据设备id和监测因素id查询监测数据
async function getMonitorData ( ctx ) {
try {
async function getMonitorData ( ctx ) {
try {
const { clickHouse } = ctx . app . fs
const { factorId , sensorId , startTime , endTime , limit , page } = ctx . query
const offset = page * limit
const factorsList = await clickHouse . alarmLocal . query ( ` SELECT FactorID,Items,ItemNames,ItemUnits FROM factors
const { factorId , sensorId , startTime , endTime } = ctx . query
const factorsList = await clickHouse . alarmLocal . query ( ` SELECT FactorID,Items,ItemNames,ItemUnits FROM factors
WHERE FactorID = $ { factorId }
` ).toPromise()||[]
const id = sensorId && sensorId . replace ( '-' , ':' )
const monitorData = await clickHouse . alarmLocal . query ( ` SELECT SensorId,CollectTime,Values FROM themes
WHERE SensorId in ( '${id}' )
` ).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
WHERE SensorId in $ { id }
AND CollectTime >= toDate ( '${startTime}' )
AND CollectTime <= toDate ( '${endTime}' )
LIMIT $ { limit } OFFSET $ { offset }
` ).toPromise()||[]
console . log ( 'vmonitorDatamonitorDatamonitorData' , monitorData )
const sensor = await clickHouse . alarmLocal . query ( ` SELECT distinct SensorId,SensorLocationDescription FROM sensors WHERE ID=' ${ id } ' ` ) . toPromise ( )
` ).toPromise() || []
const sensor = await clickHouse . alarmLocal . query ( ` SELECT distinct ID, SensorId,SensorLocationDescription FROM sensors WHERE ID in ${ id } ` ) . toPromise ( )
//监测项
let items = { }
if ( factorsList && factorsList . length > 0 ) {
let items = { }
if ( factorsList && factorsList . length > 0 ) {
//因素解释
let factors = [ ]
let factors = [ ]
//因素名词
let factorNames = [ ]
factorsList . map ( item => {
factors = item . ItemNames . split ( ',' )
factorNames = item . Items . split ( ',' )
factors . map ( child => {
factorNames . map ( p => {
items [ p ] = { name : child , unit : item . ItemUnits }
let factorNames = [ ]
factorsList . map ( item => {
factors = item . ItemNames . split ( ',' )
factorNames = item . Items . split ( ',' )
factors . map ( child => {
factorNames . map ( p => {
items [ p ] = { name : child , unit : item . ItemUnits }
} )
} )
} )
}
//设备数据+数据
let sensors = [ ]
if ( monitorData && monitorData . length > 0 ) {
monitorData . map ( data => {
const values = { } ;
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' ) } ) ;
} ) ;
}
if ( sensor && sensor . length ) {
ctx . body = { items , sensors : [ { data : sensors , id : sensor [ 0 ] . SensorId , name : sensor [ 0 ] . SensorLocationDescription } ] }
ctx . status = 200
} else {
ctx . body = { items , sensors : [ ] }
ctx . status = 200
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 } )
} )
}
} catch ( error ) {
ctx . body = { items , sensors : data }
ctx . status = 200
} catch ( error ) {
ctx . fs . logger . error ( ` path: ${ ctx . path } , error: error ` ) ;
ctx . status = 400 ;
ctx . body = {
@ -188,5 +189,5 @@ async function getMonitorData (ctx) {
module . exports = {
getStructures , getFactors , getSensors , getMonitorData
getStructures , getFactors , getSensors , getMonitorData
}