feat(xo-server-backup-reports): add the transmission data speed (#9)

See vatesfr/xo-web#2096
This commit is contained in:
badrAZ
2017-05-12 11:33:18 +02:00
committed by Julien Fontanet
parent 52a5ce95b6
commit 730ed8bc69
3 changed files with 46 additions and 13 deletions

View File

@@ -35,6 +35,7 @@
"node": ">=4"
},
"dependencies": {
"human-format": "^0.8.0",
"lodash": "^4.13.1",
"moment": "^2.13.0"
},

View File

@@ -1,3 +1,4 @@
import humanFormat from 'human-format'
import moment from 'moment'
import { forEach } from 'lodash'
@@ -58,14 +59,15 @@ class BackupReportsXoPlugin {
}
_listener (status) {
let nSuccess = 0
let globalAverageSpeed = 0
let nCalls = 0
let nSuccess = 0
let reportOnFailure
let reportWhen
const failedBackupsText = []
const successfulBackupText = []
const nagiosText = []
const successfulBackupText = []
forEach(status.calls, call => {
// Ignore call if it's not a Backup a Snapshot or a Disaster Recovery.
@@ -114,14 +116,29 @@ class BackupReportsXoPlugin {
`[ ${vm ? vm.name_label : 'undefined'} : ${call.error.message} ]`
)
} else if (!reportOnFailure) {
let averageSpeed
if (call.method === 'vm.rollingBackup' || call.method === 'vm.rollingDeltaBackup') {
const dataLength = call.returnedValue.size
averageSpeed = dataLength / moment.duration(end - start).asSeconds()
globalAverageSpeed += averageSpeed
}
successfulBackupText.push(
`### VM : ${vm.name_label}`,
` - UUID: ${vm.uuid}`,
` - Start time: ${String(start)}`,
` - End time: ${String(end)}`,
` - Duration: ${duration}`,
''
` - Duration: ${duration}`
)
if (averageSpeed !== undefined) {
successfulBackupText.push(` - Average speed: ${humanFormat(
averageSpeed,
{ scale: 'binary', unit: 'B/S' }
)}`)
}
successfulBackupText.push('')
}
})
@@ -145,23 +162,34 @@ class BackupReportsXoPlugin {
method = method.slice(method.indexOf('.') + 1)
.replace(/([A-Z])/g, ' $1').replace(/^./, letter => letter.toUpperCase()) // humanize
const tag = status.calls[Object.keys(status.calls)[0]].params.tag
const failIcon = '\u274C'
const successIcon = '\u2705'
nCalls - nSuccess > 0 && failedBackupsText.unshift([`## Failed backups:`])
nSuccess > 0 && !reportOnFailure && successfulBackupText.unshift([`## Successful backups:`])
const text = failedBackupsText.concat(successfulBackupText)
if (nCalls - nSuccess > 0) {
failedBackupsText.unshift(`## Failed backups: ${failIcon}`, '')
}
if (nSuccess > 0 && !reportOnFailure) {
successfulBackupText.unshift(`## Successful backups: ${successIcon}`, '')
}
// Global status.
text.unshift(
`## Global status for "${tag}" (${method}): ${globalSuccess ? 'Success' : 'Fail'}`,
const globalText = [
`## Global status for "${tag}" (${method}): ${globalSuccess ? `Success ${successIcon}` : `Fail ${failIcon}`}`,
` - Start time: ${String(start)}`,
` - End time: ${String(end)}`,
` - Duration: ${duration}`,
` - Successful backed up VM number: ${nSuccess}`,
` - Failed backed up VM: ${nCalls - nSuccess}`,
''
)
` - Failed backed up VM: ${nCalls - nSuccess}`
]
if (globalAverageSpeed !== 0) {
globalText.push(` - Average speed: ${humanFormat(
globalAverageSpeed / nSuccess,
{ scale: 'binary', unit: 'B/S' }
)}`)
}
globalText.push('')
const markdown = text.join('\n')
const markdown = globalText.concat(failedBackupsText, successfulBackupText).join('\n')
const markdownNagios = nagiosText.join(' ')
// TODO : Handle errors when `sendEmail` isn't present. (Plugin dependencies)

View File

@@ -1519,6 +1519,10 @@ http-signature@~1.1.0:
jsprim "^1.2.2"
sshpk "^1.7.0"
human-format@^0.8.0:
version "0.8.0"
resolved "https://registry.yarnpkg.com/human-format/-/human-format-0.8.0.tgz#6146370a838fd52718d1e27f16f1916b43a06e92"
ignore@^3.0.9, ignore@^3.2.0:
version "3.2.0"
resolved "https://registry.yarnpkg.com/ignore/-/ignore-3.2.0.tgz#8d88f03c3002a0ac52114db25d2c673b0bf1e435"