forked from hopglass/hopglass-server
-
Notifications
You must be signed in to change notification settings - Fork 1
/
purgeRaw.js
executable file
·112 lines (95 loc) · 2.92 KB
/
purgeRaw.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/node
'use strict'
const _ = require('lodash')
const fs = require('fs')
const async = require('async')
const argv = require('minimist')(process.argv.slice(2))
argv.file = _.get(argv, 'file', './raw.json')
argv.type = _.get(argv, 'type', false)
argv.value = _.get(argv, 'value', false)
argv.quiet = _.get(argv, 'quiet', false)
argv.quiet = _.get(argv, 'q', false)
argv.inverse = _.get(argv, 'inverse', false)
if (argv['?'] || argv.help || !(_.isString(argv.type) && argv.value))
outputHelp()
argv.value = _.toString(argv.value).split(',')
const now = (new Date()).getTime()
const count = {total: 0, removed: 0}
fs.readFile(argv.file, 'utf8', function(err, res) {
if (err)
throw(err)
const raw = JSON.parse(res)
async.forEachOfSeries(raw, function(node, id, callback) {
count.total++
filter(node, function(res, err) {
if (res ^ argv.inverse) {
count.removed++
if (!argv.quiet)
console.log(' remove ' + _.get(node, 'nodeinfo.node_id') + '(' + _.get(node, 'nodeinfo.hostname') + ')')
raw[id] = undefined
}
if (err)
return callback(err)
else
callback()
})
}, function(error) {
if (error)
return console.error(error)
fs.writeFile(argv.file + '.purged', JSON.stringify(raw), function(err) {
if (err)
throw(err)
if (!argv.quiet)
console.log('removed ' + count.removed + '/' + count.total)
})
})
})
function filter(node, callback) {
let out = false
async.forEachOfSeries(argv.value, function(element, index, finished) {
switch (argv.type) {
case 'site':
if (_.get(node, 'nodeinfo.system.site_code', element) == element)
out = true
break
case 'node':
if (_.get(node, 'nodeinfo.node_id', element) == element)
out = true
break
case 'offline': {
const lastseen = (new Date(_.get(node, 'lastseen', 0))).getTime()
const v = _.toNumber(element)*86400*1000
if (now - lastseen >= v)
out = true
break
}
default:
return callback(false, 'unknown type \'' + argv.type + '\'\n')
}
finished()
}, function() {
callback(out)
})
}
function outputHelp() {
let out = ''
const ln = function(content) { out += (content ? ' ' + content : '') + '\n' }
ln()
ln('Usage: ./purgeRaw.js <options>')
ln()
ln('Options:')
ln(' --file <filepath> [opt.] default: ./raw.json')
ln(' --quiet, -q [opt.] default: false')
ln(' --inverse [opt.] default: false')
ln(' --help, -? [opt.] displays this help')
ln(' --type <type> [req.]')
ln(' --value <value> [req.] single value or multiple comma separated values')
ln()
ln('<type>:')
ln(' site remove nodes with defined sitecode')
ln(' node remove nodes with defined nodeid')
ln(' offline remove nodes, older than defined days')
ln()
console.info(out)
process.exit()
}