forked from osmlab/to-fix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
169 lines (149 loc) · 5.59 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
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 http = require('http'),
Route = require('routes-router'),
levelup = require('levelup'),
leveldown = require('leveldown'),
fs = require('fs'),
key = require('./lib/key.js');
var level = {};
function debug(x){
process.stderr.write(x + '\n');
}
var router = Route();
var headers = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'POST, GET',
'Access-Control-Allow-Credentials': false,
'Access-Control-Max-Age': '86400'
};
var port = 3001;
var lockperiod = 10*60;
var dbs = fs.readdirSync('./ldb/').filter(function(item) {
return item.indexOf('.ldb') > -1;
});
dbs.forEach(function(ldb, idx) {
debug('-- Loading db ' + ldb);
levelup('./ldb/' + ldb, {
createIfMissing: false,
max_open_files: 500
}, function(err, db) {
if (err) debug('ERROR: ' + err);
level[ldb] = db;
if (idx >= dbs.length-1) runPostInit();
});
});
function runPostInit(){
http.createServer(router).listen(port, function() {
debug('running on port ' + port);
});
}
router.addRoute('/error/:error', {
POST: function(req, res, opts) {
var body = '';
req.on('data', function(data) {
body += data;
});
req.on('end', function() {
body = JSON.parse(body);
getNextItem(opts.params.error, res, function(err, kv) {
if (err) {
debug('/error route', err);
return error(res, 500, err);
}
track(opts.params.error, body.user, 'got', {_id: kv.key});
res.writeHead(200, headers);
return res.end(JSON.stringify(kv));
});
});
}
});
router.addRoute('/fixed/:error', {
POST: function(req, res, opts) {
var body = '';
req.on('data', function(data) {
body += data;
});
req.on('end', function() {
body = JSON.parse(body);
if (body.user && body.state._id) {
track(opts.params.error, body.user, 'fixed', body.state);
// check that submitted error type exists
var db = level[opts.params.error + '.ldb'];
if (!db) return error(res, 500, opts.params.error + ' is not a valid error type');
// retrieve the record's current state -- we can't trust the submitted state
// not to have been modified by the clientside JS
db.get(body.state._id, function(err, value) {
if (err) {
debug('error fetching key value', body.state._id);
return error(res, 500, 'error fetching key value ' + body.state._id);
}
// delete the record from positive skipval keyspace
db.del(body.state._id, function(err) {
if (err){
debug('error deleting', err);
return error(res, 500, 'error deleting key value ' + body.state._id);
}
// move record into skipval=0 keyspace, meaning it's fixed
var oldID = key.decompose(body.state._id);
var newID = key.compose(0, oldID.hash);
db.put(newID, value);
});
res.writeHead(200, headers);
return res.end('');
});
}
});
}
});
function getNextItem(error, res, callback) {
if (error === (undefined || 'undefined')) {
return callback('db type cannot be undefined');
} else {
var db = level[error + '.ldb'];
if (!db) {
return callback('Database \'' + error + '\' not loaded');
} else {
db.createReadStream({limit: 1, gt: '0001'})
.on('data', function(data) {
var task_data = JSON.parse(data.value);
if (task_data.ignore) {
debug('Empty task database for error ' + error);
return callback('No valid tasks available for this error type.');
} else {
db.del(data.key, function() {
var oldID = key.decompose(data.key);
var newID = key.compose((oldID.skipval + 1), oldID.hash);
db.put(newID, JSON.stringify(task_data), function(err) {
if (err) debug('put', err);
data.key = newID;
data.value = JSON.parse(data.value);
return callback(null, data);
});
});
}
})
.on('error', function(err) {
debug('CreateReadStream error', err);
return callback('Something wrong with the database for error type \'' + err + '\'');
});
}
}
}
function track(error, user, action, value) {
// value must be an object
var trackKey = +new Date() + ':' + user;
value._action = action;
value._hash = key.decompose(value._id).hash;
value = JSON.stringify(value);
var db = level[error + '-tracking.ldb'];
if (db) {
db.put(trackKey, value, function(err) {
if (err) debug('put', err);
});
} else {
debug('could not track: database does not exist for', error);
}
}
function error(res, code, errString) {
res.writeHead(code, headers);
return res.end(errString);
}