-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathManualAdjust.js
More file actions
87 lines (75 loc) · 2.84 KB
/
ManualAdjust.js
File metadata and controls
87 lines (75 loc) · 2.84 KB
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
const async = require('async');
const BaseTask = require('./BaseTask');
const UtapiClient = require('../client');
const { LoggerContext } = require('../utils');
const logger = new LoggerContext({
module: 'ManualAdjust',
});
function collectArgs(arg, prev) {
return prev.concat([arg]);
}
class ManualAdjust extends BaseTask {
async _setup() {
// Don't include default flags
await super._setup(false);
this._program
.option('-h, --host <host>', 'Utapi server host', '127.0.0.1')
.option('-p, --port <port>', 'Utapi server port', '8100', parseInt)
.option('-b, --bucket <buckets...>', 'target these buckets', collectArgs, [])
.option('-a, --account <accounts...>', 'target these accounts', collectArgs, [])
.option('-u, --user <users...>', 'target these users', collectArgs, [])
.requiredOption('-o, --objects <adjustment>', 'adjust numberOfObjects by this amount', parseInt)
.requiredOption('-s, --storage <adjustment>', 'adjust storageUtilized by this amount', parseInt);
}
async _start() {
const opts = this._program.opts();
this._utapiClient = new UtapiClient({
host: opts.host,
port: opts.port,
disableRetryCache: true,
});
await super._start();
}
async _pushAdjustmentMetric(metric) {
logger.info('pushing adjustment metric', { metric });
await this._utapiClient.pushMetric(metric);
}
async _execute() {
const timestamp = Date.now();
const opts = this._program.opts();
const objectDelta = opts.objects;
const sizeDelta = opts.storage;
if (!opts.bucket.length && !opts.account.length && !opts.user.length) {
throw Error('You must provided at least one of --bucket, --account or --user');
}
logger.info('writing adjustments');
if (opts.bucket.length) {
logger.info('adjusting buckets');
await async.eachSeries(
opts.bucket,
async bucket => this._pushAdjustmentMetric({
bucket, objectDelta, sizeDelta, timestamp,
}),
);
}
if (opts.account.length) {
logger.info('adjusting accounts');
await async.eachSeries(
opts.account,
async account => this._pushAdjustmentMetric({
account, objectDelta, sizeDelta, timestamp,
}),
);
}
if (opts.user.length) {
logger.info('adjusting users');
await async.eachSeries(
opts.user,
async user => this._pushAdjustmentMetric({
user, objectDelta, sizeDelta, timestamp,
}),
);
}
}
}
module.exports = ManualAdjust;