Skip to content

Commit

Permalink
服务端使用prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
HandsomeOne committed Aug 27, 2018
1 parent 7957c96 commit 37a73f6
Show file tree
Hide file tree
Showing 18 changed files with 331 additions and 295 deletions.
17 changes: 0 additions & 17 deletions server/.editorconfig

This file was deleted.

5 changes: 5 additions & 0 deletions server/.prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"semi": false,
"trailingComma": "all",
"singleQuote": true
}
3 changes: 3 additions & 0 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,8 @@
"scripts": {
"build": "tsc",
"start": "tsc && node build/index"
},
"devDependencies": {
"prettier": "^1.14.2"
}
}
12 changes: 7 additions & 5 deletions server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ require('./routes/request')(server)
require('./routes/test')(server)
require('./routes/alertLog')(server)

server.get('/*', restify.plugins.serveStatic({
directory: path.join(__dirname, '../../client/build'),
default: 'index.html',
}))
server.get(
'/*',
restify.plugins.serveStatic({
directory: path.join(__dirname, '../../client/build'),
default: 'index.html',
}),
)
server.listen(3001)


import * as Squad from './models/Squad'

Squad.dispatchAll()
Expand Down
22 changes: 12 additions & 10 deletions server/src/models/Scout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,18 @@ const ScoutSchema = new mongoose.Schema({
readType: { type: String, enum: ['text', 'json'], default: 'text' },
testCase: String,

snapshots: [{
_id: false,
time: { type: Date, default: Date.now },
status: { type: 'String', enum: ['OK', 'Error', 'Idle'], required: true },
statusCode: Number,
responseTime: Number,
errName: String,
errMessage: String,
body: String,
}],
snapshots: [
{
_id: false,
time: { type: Date, default: Date.now },
status: { type: 'String', enum: ['OK', 'Error', 'Idle'], required: true },
statusCode: Number,
responseTime: Number,
errName: String,
errMessage: String,
body: String,
},
],
workTime: Array,
})

Expand Down
207 changes: 109 additions & 98 deletions server/src/models/Squad.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,60 +24,59 @@ function isNowInWork(workTime) {
}

const time = new Date()
const timeArray = [
time.getDay(),
time.getHours(),
time.getMinutes(),
]
const timeArray = [time.getDay(), time.getHours(), time.getMinutes()]

return workTime.some((range) => {
return workTime.some(range => {
if (compare(range[0], range[1]) <= 0) {
return compare(range[0], timeArray) <= 0 &&
compare(timeArray, range[1]) < 0
return (
compare(range[0], timeArray) <= 0 && compare(timeArray, range[1]) < 0
)
}
return compare(range[1], timeArray) <= 0 ||
compare(timeArray, range[0]) < 0
return compare(range[1], timeArray) <= 0 || compare(timeArray, range[0]) < 0
})
}

function alert(scout, snapshot, alertURL) {
const message = Object.assign({
recipients: scout.recipients,
name: scout.name,
URL: scout.URL,
readType: scout.readType,
testCase: scout.testCase,
now: Date.now(),
}, snapshot)
const message = Object.assign(
{
recipients: scout.recipients,
name: scout.name,
URL: scout.URL,
readType: scout.readType,
testCase: scout.testCase,
now: Date.now(),
},
snapshot,
)

let statusCode
fetch(alertURL, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(message),
})
.then((res) => {
statusCode = res.status
return res.text()
})
.then((body) => {
AlertLog.create({
scoutId: scout._id,
message,
status: 'OK',
body,
statusCode,
.then(res => {
statusCode = res.status
return res.text()
})
})
.catch((err) => {
AlertLog.create({
scoutId: scout._id,
message,
status: 'Error',
statusCode,
errMessage: err.message,
.then(body => {
AlertLog.create({
scoutId: scout._id,
message,
status: 'OK',
body,
statusCode,
})
})
.catch(err => {
AlertLog.create({
scoutId: scout._id,
message,
status: 'Error',
statusCode,
errMessage: err.message,
})
})
})
}

function patrol(scout) {
Expand All @@ -99,71 +98,72 @@ function patrol(scout) {
headers: arrayToHeaders(scout.headers),
body: scout.body,
})
.then((res) => {
statusCode = res.status
return res.text()
})
.then((_body) => {
const isJSON = scout.readType === 'json'
responseTime = Date.now() - start
body = _body
scout.script.runInNewContext({
assert,
statusCode,
responseTime,
body: isJSON ? JSON.parse(body) : body,
console: { log() { } },
log() {},
.then(res => {
statusCode = res.status
return res.text()
})

return {
status: 'OK',
statusCode,
responseTime,
}
})
.catch(err => (
{
.then(_body => {
const isJSON = scout.readType === 'json'
responseTime = Date.now() - start
body = _body
scout.script.runInNewContext({
assert,
statusCode,
responseTime,
body: isJSON ? JSON.parse(body) : body,
console: { log() {} },
log() {},
})

return {
status: 'OK',
statusCode,
responseTime,
}
})
.catch(err => ({
status: 'Error',
statusCode,
responseTime,
errName: err.name,
errMessage: err.message,
}
))
.then((snapshot: any) => {
const { status } = snapshot
if (scout.errors === 0 && status === 'Error') {
snapshot.body = body
}
Scout.findByIdAndUpdate(scout._id, {
$push: { snapshots: snapshot },
}).exec()

getSettings().then((settings: any) => {
if (!settings.alertURL) return

if (status === 'OK') {
if (settings.alertOnRecovery && scout.errors > scout.tolerance) {
alert(scout, snapshot, settings.alertURL)
}
scout.errors = 0
} else if (status === 'Error') {
}))
.then((snapshot: any) => {
const { status } = snapshot
if (scout.errors === 0 && status === 'Error') {
snapshot.body = body
if (scout.errors === scout.tolerance) {
alert(scout, snapshot, settings.alertURL)
}
scout.errors += 1
}
Scout.findByIdAndUpdate(scout._id, {
$push: { snapshots: snapshot },
}).exec()

getSettings().then((settings: any) => {
if (!settings.alertURL) return

if (status === 'OK') {
if (settings.alertOnRecovery && scout.errors > scout.tolerance) {
alert(scout, snapshot, settings.alertURL)
}
scout.errors = 0
} else if (status === 'Error') {
snapshot.body = body
if (scout.errors === scout.tolerance) {
alert(scout, snapshot, settings.alertURL)
}
scout.errors += 1
}
})
})
})
}

export function add(scout) {
scout.patrol = patrol.bind(null, scout)
scout.script = new vm.Script(scout.testCase)
scout.errors = 0
timeouts[scout._id] = setTimeout(scout.patrol, Math.random() * scout.interval * 60 * 1000)
timeouts[scout._id] = setTimeout(
scout.patrol,
Math.random() * scout.interval * 60 * 1000,
)
scoutMap[scout._id] = scout
}

Expand All @@ -179,17 +179,28 @@ export function update(id, patch) {
}

export function dispatchAll() {
Scout.find().select({
snapshots: 0,
}).lean()
.then((scouts: any) => { scouts.forEach(add) })
Scout.find()
.select({
snapshots: 0,
})
.lean()
.then((scouts: any) => {
scouts.forEach(add)
})
}

export function removeOldIntels(since = 7) {
Scout.update({}, {
$pull: {
snapshots: {
time: {
$lt: Date.now() - (since * 24 * 60 * 60 * 1000) } } },
}, { multi: true }).exec()
Scout.update(
{},
{
$pull: {
snapshots: {
time: {
$lt: Date.now() - since * 24 * 60 * 60 * 1000,
},
},
},
},
{ multi: true },
).exec()
}
3 changes: 2 additions & 1 deletion server/src/models/getSettings.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Settings from './Settings'

export default () => Settings.findOne().then(doc => doc || new Settings().save())
export default () =>
Settings.findOne().then(doc => doc || new Settings().save())
29 changes: 13 additions & 16 deletions server/src/routes/alertLog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,20 @@ function extract(alertlog) {
message: alertlog.message,
}
}
export = (server) => {
export = server => {
server.get('/alertlogs', (req, res) => {
const {
page = 1,
pageSize = 10,
} = req.params
const { page = 1, pageSize = 10 } = req.params
AlertLog.find()
.sort({ time: -1 })
.skip((page - 1) * pageSize)
.limit(+pageSize)
.lean()
.then((alertlogs: any) => {
res.send(alertlogs.map(extract))
})
.catch((err) => {
res.status(500)
res.end(err.toString())
})
.sort({ time: -1 })
.skip((page - 1) * pageSize)
.limit(+pageSize)
.lean()
.then((alertlogs: any) => {
res.send(alertlogs.map(extract))
})
.catch(err => {
res.status(500)
res.end(err.toString())
})
})
}
Loading

0 comments on commit 37a73f6

Please sign in to comment.