-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreader.js
More file actions
169 lines (137 loc) · 3.6 KB
/
reader.js
File metadata and controls
169 lines (137 loc) · 3.6 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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
var _ = require('lodash');
var util = require('../../core/util.js');
var config = util.getConfig();
var log = require(util.dirs().core + 'log');
var sqlite = require('./handle');
var sqliteUtil = require('./util');
var Reader = function() {
_.bindAll(this);
this.db = sqlite.initDB(true);
}
// returns the most recent window complete candle
// windows within `from` and `to`
Reader.prototype.mostRecentWindow = function(from, to, next) {
to = to.unix();
from = from.unix();
var maxAmount = to - from + 1;
this.db.all(`
SELECT start from ${sqliteUtil.table('candles')}
WHERE start <= ${to} AND start >= ${from}
ORDER BY start DESC
`, function(err, rows) {
if(err) {
// bail out if the table does not exist
if(err.message.split(':')[1] === ' no such table')
return next(false);
log.error(err);
return util.die('DB error while reading mostRecentWindow');
}
// no candles are available
if(rows.length === 0) {
return next(false);
}
if(rows.length === maxAmount) {
// full history is available!
return next({
from: from,
to: to
});
}
// we have at least one gap, figure out where
var mostRecent = _.first(rows).start;
var gapIndex = _.findIndex(rows, function(r, i) {
return r.start !== mostRecent - i * 60;
});
// if there was no gap in the records, but
// there were not enough records.
if(gapIndex === -1) {
var leastRecent = _.last(rows).start;
return next({
from: leastRecent,
to: mostRecent
});
}
// else return mostRecent and the
// the minute before the gap
return next({
from: rows[ gapIndex - 1 ].start,
to: mostRecent
});
})
}
Reader.prototype.tableExists = function(name, next) {
this.db.all(`
SELECT name FROM sqlite_master WHERE type='table' AND name='${sqliteUtil.table(name)}';
`, function(err, rows) {
if(err) {
console.error(err);
return util.die('DB error at `get`');
}
next(null, rows.length === 1);
});
}
Reader.prototype.get = function(from, to, what, next) {
if(what === 'full')
what = '*';
this.db.all(`
SELECT ${what} from ${sqliteUtil.table('candles')}
WHERE start <= ${to} AND start >= ${from}
ORDER BY start ASC
`, function(err, rows) {
if(err) {
console.error(err);
return util.die('DB error at `get`');
}
next(null, rows);
});
}
Reader.prototype.count = function(from, to, next) {
this.db.all(`
SELECT COUNT(*) as count from ${sqliteUtil.table('candles')}
WHERE start <= ${to} AND start >= ${from}
`, function(err, res) {
if(err) {
console.error(err);
return util.die('DB error at `get`');
}
next(null, _.first(res).count);
});
}
Reader.prototype.countTotal = function(next) {
this.db.all(`
SELECT COUNT(*) as count from ${sqliteUtil.table('candles')}
`, function(err, res) {
if(err) {
console.error(err);
return util.die('DB error at `get`');
}
next(null, _.first(res).count);
});
}
Reader.prototype.getBoundry = function(next) {
this.db.all(`
SELECT
(
SELECT start
FROM ${sqliteUtil.table('candles')}
ORDER BY start LIMIT 1
) as 'first',
(
SELECT start
FROM ${sqliteUtil.table('candles')}
ORDER BY start DESC
LIMIT 1
) as 'last'
`, function(err, rows) {
if(err) {
console.error(err);
return util.die('DB error at `get`');
}
next(null, _.first(rows));
});
}
Reader.prototype.close = function() {
this.db.close();
this.db = null;
}
module.exports = Reader;