-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
82 lines (66 loc) · 2.33 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
'use strict';
var fs = require('fs');
var crc = require('buffer-crc32');
var chokidar = require('chokidar');
var ROOT_KEY = 'dcdr';
function Dcdr() {
this.features = {};
}
Dcdr.prototype.init = function(config) {
this.config = config;
this.config.logger = this.config.logger || console;
if (fs.existsSync(this.config.dcdr.path)) {
this.loadFeatures(this.config.dcdr.path, true);
this.watchConfig();
} else {
this.config.logger.error(this.config.dcdr.path + ' not found.');
}
};
Dcdr.prototype.watchConfig = function() {
function watchHandler() {
this.config.logger.info('Reloading features from ' + this.config.dcdr.path);
this.loadFeatures(this.config.dcdr.path, false);
}
chokidar.watch(this.config.dcdr.path)
.on('change', watchHandler.bind(this))
// for temp debug logging
.on('raw', function(event, path, details) {
this.config.logger.info('Raw event info: ' + event + ', ' + path + ', ' + JSON.stringify(details));
}.bind(this));
};
Dcdr.prototype.loadFeatures = function(path, isInitialLoad) {
var featuresJson = fs.readFileSync(path, 'utf8');
if (featuresJson !== '' || isInitialLoad) {
var features = JSON.parse(featuresJson);
this.setFeatures(features);
}
};
Dcdr.prototype.setFeatures = function(features) {
if (features[ROOT_KEY] && features[ROOT_KEY].features && features[ROOT_KEY].features.default) {
this.features = features[ROOT_KEY].features.default;
} else {
this.config.logger.error(ROOT_KEY + '.features.default key not found.');
}
};
Dcdr.prototype.withinPercentile = function(feature, id, val) {
var uid = this.crc(feature + id.toString());
var percent = val * 100.00;
return uid % 100 < percent;
};
Dcdr.prototype.crc = function(feature) {
var buf = Buffer.from(feature);
return crc.unsigned(buf);
};
Dcdr.prototype.isAvailable = function(feature) {
return this.features && this.features[feature] !== null
&& this.features[feature] === true;
};
Dcdr.prototype.isAvailableForId = function(feature, id) {
return (this.features && this.features[feature] !== null
&& this.withinPercentile(feature, id, this.features[feature]));
};
Dcdr.prototype.scaleValue = function(feature, min, max) {
if (!this.features || this.features[feature] === null) return min;
return min + ((max - min) * this.features[feature]);
};
module.exports = new Dcdr();