-- Load dependent modules
if not stats then modules.load('stats') end
+-- Function to sort frequency list
+local function freqsort(a, b) return a.count < b.count end
local function stream_stats(h, ws)
local ok, prev = true, stats.list()
while ok do
-- Get current snapshot
- local cur, update = stats.list(), {}
+ local cur, stats_dt = stats.list(), {}
for k,v in pairs(cur) do
- update[k] = v - (prev[k] or 0)
+ stats_dt[k] = v - (prev[k] or 0)
end
prev = cur
+ -- Update frequent query list
+ local cur, freq = stats.frequent(), {}
+ table.sort(cur, freqsort)
+ for i = 1,math.min(20, #cur) do
+ table.insert(freq, cur[i])
+ end
-- Publish stats updates periodically
- ok = ws:send(tojson(update))
+ local push = tojson({stats=stats_dt,freq=freq})
+ ok = ws:send(push)
cqueues.sleep(0.5)
end
ws:close()
<title>{{.Title}}</title>
<style>
body { font-family: 'Gill Sans', 'Gill Sans MT', Verdana, sans-serif; color: #555; }
- h1, h2, h3 { line-height: 2em; color: #000; text-align: center; border-bottom: 1px solid #ccc; }
+ h1, h2, h3 { line-height: 1.5em; color: #000; text-align: center; border-bottom: 1px solid #ccc; }
h1, h2, h3 { font-weight: 300; }
th { text-align: left; font-weight: normal; margin-bottom: 0.5em; }
#page { font-weight: 300; }
}
statsChart.push(next);
}
+ function updateFeed(resp) {
+ var feed = $('#feed');
+ feed.children().remove();
+ feed.append('<tr><th>Query name</th><th>Type</th><th>Frequency</th></tr>')
+ for (i = 0; i < resp.length; ++i) {
+ var row = $('<tr />');
+ row.append('<td>' + resp[i].name + '</td>');
+ row.append('<td>' + resp[i].type + '</td>');
+ row.append('<td>' + resp[i].count + '</td>');
+ feed.append(row);
+ }
+ }
/* WebSocket endpoints */
var wsStats = 'ws://' + location.host + '/stats';
var ws = new Socket(wsStats);
ws.onmessage = function(evt) {
var data = $.parseJSON(evt.data);
- pushMetrics(data);
+ pushMetrics(data.stats);
+ updateFeed(data.freq)
};
}