运维服务中台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

237 lines
5.7 KiB

'use strict';
2 years ago
const moment = require('moment');
async function list (ctx) {
try {
const { models } = ctx.fs.dc;
const { userId, pepUserId } = ctx.fs.api
const linkListRes = await models.QuickLink.findAll({
attributes: { exclude: ['userId'] },
where: {
userId,
}
})
ctx.status = 200;
ctx.body = linkListRes
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400;
ctx.body = {
message: typeof error == 'string' ? error : undefined
}
}
}
async function edit (ctx) {
try {
const { models } = ctx.fs.dc;
const { userId, pepUserId } = ctx.fs.api
const { linkId, name, link } = ctx.request.body
if (!name || !link) {
throw '请将参数填写完整'
}
let findOption = {
where: {
userId: userId,
$or: [{
name,
}, {
link,
}]
}
}
if (linkId) {
findOption.where.id = { $ne: linkId }
}
const existRes = await models.QuickLink.findOne({
where: {
userId: userId,
$or: [{
name,
}, {
link,
}]
}
})
if (existRes) {
throw '已有相同名称/地址的工具'
}
if (linkId) {
await models.QuickLink.update({
name,
link,
}, {
where: {
id: linkId
}
})
} else {
await models.QuickLink.create({
userId,
name,
link,
})
}
ctx.status = 204;
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400;
ctx.body = {
message: typeof error == 'string' ? error : undefined
}
}
}
async function del (ctx) {
try {
const { models } = ctx.fs.dc;
const { userId, pepUserId } = ctx.fs.api
const { linkId } = ctx.params
await models.QuickLink.destroy({
where: {
id: linkId,
userId,
}
})
ctx.status = 204;
} catch (error) {
2 years ago
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400;
ctx.body = {
message: typeof error == 'string' ? error : undefined
}
}
}
async function count (ctx) {
try {
const { models } = ctx.fs.dc;
2 years ago
const { userId, pepUserId, userInfo = {}, pepUserInfo } = ctx.fs.api
const { clickHouse } = ctx.app.fs
const { utils: { judgeSuper, anxinStrucIdRange } } = ctx.app.fs
const { database: anxinyun } = clickHouse.anxinyun.opts.config
const { pepProjectId } = ctx.request.body
2 years ago
let anxinStruc = await anxinStrucIdRange({
ctx, pepProjectId
})
if (anxinStruc.length) {
const anxinStrucIds = anxinStruc.map(a => a.strucId) || []
const dataAlarm = await clickHouse.dataAlarm.query(`
SELECT
AlarmId,State,StartTime
FROM
alarms
WHERE
alarms.StructureId IN (${anxinStrucIds.join(",")})
`).toPromise();
//数据告警总数
const alarm = await clickHouse.dataAlarm.query(`
SELECT
count(alarms.AlarmId) AS count
FROM
alarms
WHERE
alarms.StructureId IN (${anxinStrucIds.join(",")})
`).toPromise();
const confirmedAlarm = dataAlarm
// TODO: 开发临时注释
.filter(ar => ar.State && ar.State > 2)
.map(ar => "'" + ar.AlarmId + "'")
//剩余数据告警
const alarmSurplus = dataAlarm.filter(ar => ar.State && ar.State < 3).length || 0
//今日新增数据告警
const alarmNewAdd = await clickHouse.dataAlarm.query(`
SELECT
count(alarms.StartTime) AS count
FROM
alarms
WHERE
alarms.StructureId IN (${anxinStrucIds.join(",")})
AND
alarms.StartTime >= '${moment().startOf('day').format('YYYY-MM-DD HH:mm:ss')}'
AND
alarms.StartTime <= '${moment().endOf('day').format('YYYY-MM-DD HH:mm:ss')}'
`).toPromise();
//今日确认数据告警
const confirmedAlarmDetailMax = confirmedAlarm.length ?
await clickHouse.dataAlarm.query(`
SELECT
max(Time) AS Time, AlarmId , max(Content) AS Content
FROM
alarm_details
WHERE
AlarmId IN (${confirmedAlarm.join(',')})
AND
alarm_details.Time >= '${moment().startOf('day').format('YYYY-MM-DD HH:mm:ss')}'
AND
alarm_details.Time <= '${moment().endOf('day').format('YYYY-MM-DD HH:mm:ss')}'
GROUP BY AlarmId
`).toPromise() :
[];
ctx.status = 200;
ctx.body = {
dataAlarm: {
alarm: alarm[0].count || 0,
alarmSurplus: alarmSurplus,
alarmNewAdd: alarmNewAdd[0].count || 0,
confirmedAlarmDetailMax: confirmedAlarmDetailMax.length || 0,
},
videoAlarm:{
}
2 years ago
}
} else {
ctx.body = {
dataAlarm: 0,
}
}
ctx.status = 200;
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400;
ctx.body = {
message: typeof error == 'string' ? error : undefined
}
}
}
module.exports = {
list, edit, del, count
};