Commit 67afc023 authored by Marek Vavrusa's avatar Marek Vavrusa

modules/http: stream stats and outbound name freq

parent 6050461d
......@@ -13,17 +13,26 @@ local M = {
-- 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()
......
......@@ -2,7 +2,7 @@
<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; }
......
......@@ -38,6 +38,18 @@ window.onload = function() {
}
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';
......@@ -45,6 +57,7 @@ window.onload = function() {
var ws = new Socket(wsStats);
ws.onmessage = function(evt) {
var data = $.parseJSON(evt.data);
pushMetrics(data);
pushMetrics(data.stats);
updateFeed(data.freq)
};
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment