运维服务中台
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.

224 lines
5.8 KiB

2 years ago
'use strict';
const moment = require('moment')
async function inspection (ctx) {
// 巡查
2 years ago
try {
const models = ctx.fs.dc.models;
2 years ago
const { projectAppId, screenshot = [], } = ctx.request.body
2 years ago
const now = moment().format()
const storageData = screenshot.map(s => {
return {
projectAppId,
2 years ago
screenshot: s.url,
2 years ago
createTime: now,
2 years ago
description: s.description,
2 years ago
}
})
await models.AppInspection.bulkCreate(storageData)
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
}
}
}
2 years ago
async function inspectionList (ctx) {
try {
const models = ctx.fs.dc.models;
const { clickHouse } = ctx.app.fs
const { timeStart, timeEnd, projectId, appId, noted } = ctx.query
let findOption = {
where: {
},
order: [['id', 'DESC']],
include: [{
model: models.ProjectApp,
required: true,
2 years ago
where: appId ? {
id: appId
} : undefined,
include: {
model: models.ProjectCorrelation,
required: true,
where: Object.assign({},
{
del: false
},
projectId ?
{
id: projectId
} :
{}
),
2 years ago
attributes: {
exclude: ['anxinProjectId', 'createTime', 'createUser']
}
}
}]
}
if (timeStart && timeEnd) {
findOption.where.createTime = { $between: [moment(timeStart).format(), moment(timeEnd).format()] }
}
if (noted) {
if (noted == 'noted') {
findOption.where.notedTime = { $ne: null }
} else if (noted == 'unnote') {
findOption.where.notedTime = null
}
}
const inspectionRes = await models.AppInspection.findAll(findOption)
let notedUserIds = new Set()
for (let ins of inspectionRes) {
if (ins.notedPepUserId) {
notedUserIds.add(ins.notedPepUserId)
}
}
let userPepRes = notedUserIds.size ?
await clickHouse.pepEmis.query(`SELECT DISTINCT user.id AS id, "user"."name" AS name FROM user WHERE user.id IN (${[...notedUserIds].join(',')})`).toPromise() :
[]
for (let ins of inspectionRes) {
if (ins.notedPepUserId) {
const corUser = userPepRes.find(up => up.id == ins.notedPepUserId)
ins.dataValues.notedPepUser = corUser ? corUser.name : ''
}
}
ctx.status = 200;
ctx.body = inspectionRes
} catch (error) {
2 years ago
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
2 years ago
ctx.status = 400;
ctx.body = {
message: typeof error == 'string' ? error : undefined
}
}
}
2 years ago
async function notedInspection (ctx) {
2 years ago
try {
const models = ctx.fs.dc.models;
const { inspectionId } = ctx.request.body
const { userId } = ctx.fs.api
await models.AppInspection.update({
notedPepUserId: userId,
notedTime: moment().format()
}, {
where: {
id: inspectionId
}
})
ctx.status = 204;
} catch (error) {
2 years ago
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
2 years ago
ctx.status = 400;
ctx.body = {
message: typeof error == 'string' ? error : undefined
}
}
}
2 years ago
async function apiError (ctx) {
try {
const models = ctx.fs.dc.models;
const { projectAppId, alarmContent, router, statusCode, screenshot = '' } = ctx.request.body
2 years ago
const now = moment().format()
let storageData = {
projectAppId, alarmContent, router, statusCode
}
const existRes = await models.AppAlarm.findOne({
where: {
projectAppId, alarmContent, router, statusCode,
confirm: null
}
})
if (existRes) {
await models.AppAlarm.update({
updateTime: now
}, {
where: {
id: existRes.id
}
})
} else {
const existCount = await models.AppAlarm.count({
where: {
}
})
storageData.serialNumber = 'WEB' + (existCount < 9 ? '0' + (existCount + 1) : existCount)
storageData.createTime = now
storageData.screenshot = screenshot
2 years ago
await models.AppAlarm.create(storageData)
}
ctx.status = 200;
} catch (error) {
2 years ago
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
2 years ago
ctx.status = 400;
ctx.body = {
message: typeof error == 'string' ? error : undefined
}
}
}
2 years ago
async function apiErrorList (ctx) {
try {
const models = ctx.fs.dc.models;
const { clickHouse } = ctx.app.fs
const { } = ctx.query
ctx.status = 200;
ctx.body = []
} catch (error) {
2 years ago
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
2 years ago
ctx.status = 400;
ctx.body = {
message: typeof error == 'string' ? error : undefined
}
}
}
2 years ago
async function confirmApiError (ctx) {
try {
const models = ctx.fs.dc.models;
const { confirm, appAlarmId } = ctx.request.body
await models.AppAlarm.update({
confirm,
}, {
where: {
id: appAlarmId
}
})
ctx.status = 204;
} catch (error) {
2 years ago
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
2 years ago
ctx.status = 400;
ctx.body = {
message: typeof error == 'string' ? error : undefined
}
}
}
2 years ago
module.exports = {
inspection,
2 years ago
inspectionList,
2 years ago
notedInspection,
apiError,
2 years ago
apiErrorList,
2 years ago
confirmApiError,
2 years ago
};