forked from Jinksi/sp-bandwidth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
62 lines (51 loc) · 1.76 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env node
const path = require('path')
const fs = require('fs')
const chalk = require('chalk')
const _ = require('lodash')
const logDir = 'logs'
const currentDir = fs.readdirSync('.')
if (currentDir.indexOf(logDir) === -1) {
console.log(chalk.bgRed(`\nNo ./${logDir} directory found\n`))
console.log(chalk.red(`cd ~`))
process.exit()
}
const files = fs.readdirSync('./' + logDir)
const output = files.map(readFile)
function readFile (file) {
const title = file.split('.')[0]
const content = fs.readFileSync(`./${logDir}/${file}`, 'utf8')
if(!content) return `
${chalk.yellow.bold(_.padEnd(title, 20))} No info
`
const obj = JSON.parse(content)
let {
start_date,
end_date,
bandwidth
} = obj.general
let estimate = _.round(bandwidth / 1024 / 1024 / 1024 * 0.09 * 4, 4)
if (estimate > 10) {
estimate = chalk.red(estimate)
} else if (estimate > 1) {
estimate = chalk.yellow(estimate)
} else {
estimate = chalk.green(estimate)
}
bandwidth = bytesToSize(bandwidth * 4)
if (bandwidth.includes('T')) bandwidth = chalk.magenta(bandwidth)
if (bandwidth.includes('GB')) bandwidth = chalk.red(bandwidth)
if (bandwidth.includes('MB')) bandwidth = chalk.yellow(bandwidth)
if (bandwidth.includes('KB')) bandwidth = chalk.green(bandwidth)
const output = `
${chalk.green.bold(_.padEnd(title, 20))} ${_.padEnd(bandwidth, 20)} $${_.padEnd(estimate + '/mo', 20)} From: ${chalk.blue(start_date)} To: ${chalk.blue(end_date)}
`
return output
}
console.log(output.join(''))
function bytesToSize(bytes) {
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB']
if (bytes == 0) return '0 Byte'
const i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)))
return Math.round(bytes / Math.pow(1024, i), 2) + ' ' + sizes[i];
}