-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
130 lines (99 loc) · 2.82 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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
var moment = require('moment');
var through2 = require('through2');
var Joi = require('joi');
var util = require('util');
var stream = require('stream');
module.exports = function build(db) {
var now;
var count;
var incCounter = function () {
var current = Date.now();
if (current === now) {
count++;
} else {
count = 0;
}
now = current;
return now;
};
var addValueSchema = Joi.object().keys({
assetId: Joi.string().required(),
date: Joi.date(),
value: Joi.any().required()
});
function generateWriteStream(){
var myStream = new stream.Writable({objectMode: true});
myStream._write=function (chunk, encoding, done) {
add(chunk,done);
};
return myStream;
};
function add(value, cb) {
var validationResult = Joi.validate(value, addValueSchema);
if (validationResult.error !== null) {
console.error("add validation error is", validationResult.error);
cb(validationResult.error);
return;
}
value.date = value.date || moment().toDate();
incCounter();
var formattedDate = Math.floor(value.date / 1000)
var keyTs = "ts::" + formattedDate + ":" + count;
var keyAs = "as::" + value.assetId + "--" + formattedDate + ":" + count;
db.batch()
.put(keyTs, value, { valueEncoding: 'json' })
.put(keyAs, value, { valueEncoding: 'json' })
.write(function () {
cb(null, value);
});
}
var getQueryValuesSchema = Joi.object().keys({
assetId: Joi.string().required(),
from: Joi.date(),
to: Joi.date()
});
function getDataStream(queryValues) {
var validationResult = Joi.validate(queryValues, getQueryValuesSchema);
if (validationResult.error !== null) {
console.error("getDataStream error is", validationResult.error);
var stream = through2.obj()
process.nextTick(function () {
stream.emit('error', validationResult.error)
})
return stream;
}
var gt;
var lt;
var prefix;
var options;
var assetId;
queryValues.from = queryValues.from || "";
queryValues.to = queryValues.to || '\xff';
if (!queryValues.assetId) {
prefix = "ts::";
assetId = "";
}
else {
prefix = "as::";
assetId = queryValues.assetId + "--";
}
gt = prefix + assetId + queryValues.from;
lt = prefix + assetId + queryValues.to;
options = {
valueEncoding: 'json',
lte: lt,
gte: gt
};
return db.createReadStream(options).pipe(through2.obj(function (chunk, enc, callback) {
this.push(chunk.value);
callback();
}));
}
return {
addSchema: function () { return addValueSchema; },
add: add,
getQuerySchema: function () { return getQueryValuesSchema; },
getDataStream: getDataStream,
generateWriteStream:generateWriteStream
};
};