-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
305 lines (277 loc) · 8.56 KB
/
app.js
File metadata and controls
305 lines (277 loc) · 8.56 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/**
* Main application controller
* @author ishafer
*/
//require.paths.unshift("./node_modules");
//require.paths.unshift("./public/js");
//require.paths.unshift(".");
var sys = require("sys"),
express = require("express"),
engine = require("ejs-locals"),
db = require("./mydb"),
formidable = require("formidable"),
log = require("./public/js/log").getLogger(0),
utils = require("./public/js/utils");
String.prototype.endsWith = function(suffix) {
return this.indexOf(suffix, this.length - suffix.length) !== -1;
};
var app = express();
app.use(express.cookieParser());
app.use(express.session({secret: "proud in the cloud foundry"}));
app.use(app.router);
app.use(express.static(__dirname + "/public"));
app.engine("ejs", engine);
app.configure(function() {
app.set("views", __dirname + "/tmpl");
app.set("view engine", "ejs");
});
/**
* Bind a closure for pagename context
* @param pname the name of the page
*/
var context = function(pname) {
return {
pagename: pname,
selif: function(name) {
return (pname==name) ? "sel" : "";
}
}
}
app.get("/", function(req, res) {
res.render("index.ejs", {c: context("home")});
});
app.get("/about", function(req, res) {
res.render("about.ejs", {c: context("about")});
});
app.get("/demo", function(req, res) {
res.render("demo.ejs", {c: context("demo")});
});
function die(res, obj) {
res.contentType("application/json");
res.send(obj);
}
/**
* Only do next if user is logged in.
*/
function requireLogin(req, res, next) {
if (req.session && (req.session.loggedin === true)) {
next();
} else {
die(res, {"error": "login required"})
}
}
var LOGIN_KEY = "frabjousfling";
app.get("/login/:key", function(req, res) {
if (req.session) {
if (req.params.key == LOGIN_KEY) {
req.session.loggedin = true;
die(res, {"message":"logged in!"});
} else {
die(res, {"error":"incorrect key."});
}
} else {
die(res, {"error":"couldn't log you in"})
}
});
app.get("/logout", function(req, res) {
var next = function(err) {
if (err) {
die(res, {"error":"Couldn't log you out"});
} else {
die(res, {"message":"logged out"});
}
};
if (!req.session) {
next("No session");
} else {
req.session.destroy(next);
}
});
/**
* Debugging information about database
*/
app.get("/db/info", requireLogin, function(req, res) {
res.write(db.mongourl+"\n");
res.write(JSON.stringify(db.mongo)+"\n\n");
res.write(JSON.stringify(process.env.VCAP_SERVICES)+"\n");
res.end("\n");
});
//TODO: add cache eviction
var tscache = {};
var INFLIGHT = "inflight";
//should the app worker cache results from the database?
var workercaching = true;
function hashnameTS(name, level) {
return name + "." + level;
}
function getTSBuf(name, level, next) {
var n = hashnameTS(name, level);
if (tscache[n] && workercaching) {
//use strict comparison!
if (tscache[n] === INFLIGHT) {
log(0, "Inflight hit for ts " + n);
var waitreq = function(next) {
if (tscache[n] === INFLIGHT) {
setTimeout(function() {return waitreq(next);}, 50);
} else {
next(tscache[n]);
}
}
} else {
log(0, "Cache hit for ts " + n);
next(tscache[n]);
}
} else {
log(0, "Cache miss for ts " + n);
if (workercaching) { tscache[n] = INFLIGHT; }
var ts = new db.TStore(name, level, {mode: "r"}, function() {
log(1, "Requesting TStore name=" + name + " level=" + level);
ts.readBuf(function(buf) {
if (workercaching) { tscache[n] = buf; }
next(buf);
});
});
}
}
/**
* Obtain time range.
* Query params:
* name: timeseries name
* level: zoom level
* lbnd: lower x (time) bound
* hbnd: upper x (time) bound
*/
app.get("/db/get", function(req, res) {
var pname = req.query.name,
plevel = req.query.level,
plbnd = req.query.lbnd,
phbnd = req.query.hbnd;
var t1 = utils.curms();
getTSBuf(pname, plevel, function(buf) {
var i = 0;
var lo = Math.abs(db.binSearch(buf, plbnd));
var hi = Math.abs(db.binSearch(buf, phbnd));
log(0, "Got bounds lo=" + lo + " hi=" + hi + " of " + buf.length/8);
log(0, ((new Date()).getTime()-t1) + "ms -> pull");
//using the inbuilt stringify is much faster.
var arr = db.toArray(buf, lo, hi);
log(0, ((new Date()).getTime()-t1) + "ms -> retrieve");
res.end(JSON.stringify(arr));
log(0, ((new Date()).getTime()-t1) + "ms -> finish");
//writing out a response manually is very expensive
/*
res.write("[");
while (i < buf.length) {
res.write("{"x":");
res.write(""+(buf[i++] | buf[i++]<<8 | buf[i++]<<16 | buf[i++]<<24));
res.write(","y":");
res.write(""+(buf[i++] | buf[i++]<<8 | buf[i++]<<16 | buf[i++]<<24));
res.write("},\n");
}
res.end("]");
*/
});
});
var MINLENGTH = 1000;
/**
* Create aggregates for the given sfile
* Produces all aggregates from the given level down until length is below MINLENGTH
* @param sfile file to compute aggs for. Must have been opened for read.
* @param next continuation
*/
function createAggregates(sfile, next) {
sfile.computeAggregate(function(buf) {
//make the new tstore with an incremented level
var lvl = sfile.getLevel();
var newf = new db.TStore(
sfile.getName(),
lvl+1,
{mode: "w"},
function() {
log(0, "Writing aggregate @ lvl " + lvl);
newf.writeBuf(buf, function() {
newf.reopen("r", function(reopenedf) {
//continue aggregating if we're above the min length
if (buf.length > MINLENGTH) {
createAggregates(reopenedf, next);
} else {
next();
}
});
});
});
});
}
/**
* Compute aggregates for the given name.
*/
app.get("/db/agg/:name", function(req, res) {
log(0, "Starting to aggregate");
var sfile = new db.TStore(req.params.name, 0, {mode: "r"}, function() {
createAggregates(sfile, function() {
log(0, "Done creating aggregates");
res.end(JSON.stringify({"msg": "Done creating aggregates"}));
});
});
});
/**
* List all level 0 timeseries files and metadata
*/
app.get("/db/list", function(req, res) {
var jsres = {};
db.getFileInfo({}, function(lst) {
lst.forEach(function(el) {
if (el.filename.endsWith(".0")) {
var name = el.filename.substring(0, el.filename.length-2);
jsres[name] = {
name: name,
points: el.length/8,
metadata: el.metadata
};
}
});
res.end(JSON.stringify(jsres));
});
})
/**
* Render timeseries upload page
*/
app.get("/db/add/:name/:scale", function(req, res) {
res.render("upload.ejs", {c: context("upload")});
});
/**
* Postback for timeseries upload page
*/
app.post("/db/add/:name/:scale", function(req, res) {
var form = new formidable.IncomingForm();
var pname = req.params.name;
//save file
var sfile = new db.TStore(pname, 0, {
mode: "w",
metadata: {
"scale": req.params.scale
}
}, function() {});
var obtained = 0;
form.onPart = function(part) {
part.addListener("data", function(chunk) {
log(0, "Rcv chunk len=" + chunk.length);
obtained += chunk.length;
sfile.writeBuf(chunk, function() {
log(0, "RcvWrote chunk len=" + chunk.length);
});
});
part.addListener("end", function() {
log(0, "Finished stream; closing files; total now " + obtained);
sfile.close(function() {
res.end(JSON.stringify({"msg": "Received file " + pname}));
});
});
};
//we must begin parsing immediately.
//the defensive locking on TStores keeps this safe.
form.parse(req);
});
var myport = process.env.VCAP_APP_PORT || 3000;
app.listen(myport);
sys.puts("Server running on port " + myport);