-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.coffee
76 lines (58 loc) · 1.82 KB
/
index.coffee
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
mongo = require "mongoskin"
express = require "express"
# Set up db connection
# db = mongo.db("emongo2.heyzap.com/mobile")
db = mongo.db("localhost/mobile")
# Set up express
app = express.createServer();
app.configure ->
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.static(__dirname + '/public'));
app.configure "development", ->
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
app.configure "production", ->
app.use(express.errorHandler());
app.listen 8080
# App rendering helpers
app.helpers
queryFormatter: (query) ->
""
# Endpoints
app.get "/", (req, res) ->
range = 1000 * 60 * 60 * 24 * 1
now = new Date()
query =
ts:
$gte: new Date(now.getTime() - range)
$lt: now
db.collection("system.profile").find(query, {sort: [["millis", -1]]}).toArray (err, records) ->
ops = (getNormalizedOperation(r) for r in records)
res.render "index",
ops: ops
console.log "Starting server on port 8080"
getNormalizedQuery = (query) ->
getNormalizedOperation = (profile) ->
res =
ts: profile.ts
millis: profile.millis
nscanned: profile.nscanned
nreturned: profile.nreturned
if profile.op == "command"
if profile.command.count
res.operation = "count"
res.collection = profile.command.count
res.query = profile.command.query
else
res.operation = "unknown"
else
res.operation = profile.op
res.collection = profile.ns
res.payload = "TODO"
res.query = profile.query
res.query = profile.query.$query if profile.query && profile.query.$query
res.query = profile.query.query if profile.query && profile.query.query
res.normalized_query = getNormalizedQuery(res.query) if res.query
return res