annotate mod_http_stats_stream/mod_http_stats_stream.lua @ 4182:1890115b2773

mod_http_muc_log: Move template into a directory to ease packaging There's no LuaRocks directive like `copy_directories` for individual files, short of writing a whole custom installation method.
author Kim Alvefur <zash@zash.se>
date Thu, 08 Oct 2020 16:39:19 +0200
parents 740870196b97
children bac3dae031ee
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3643
740870196b97 mod_http_stats_stream: Make global to simplify
Kim Alvefur <zash@zash.se>
parents: 3635
diff changeset
1 module:set_global();
740870196b97 mod_http_stats_stream: Make global to simplify
Kim Alvefur <zash@zash.se>
parents: 3635
diff changeset
2
2432
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3 local statsman = require "core.statsmanager";
3635
fd054689a64c mod_http_stats_stream: Use existing header preparation
Kim Alvefur <zash@zash.se>
parents: 2432
diff changeset
4 local http = require "net.http.server";
2432
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5 local json = require "util.json";
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7 local sessions = {};
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9 local function updates_client_closed(response)
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10 module:log("debug", "Streamstats client closed");
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 sessions[response] = nil;
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 end
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14 local function get_updates(event)
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15 local request, response = event.request, event.response;
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17 response.on_destroy = updates_client_closed;
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18
3635
fd054689a64c mod_http_stats_stream: Use existing header preparation
Kim Alvefur <zash@zash.se>
parents: 2432
diff changeset
19 response.headers.content_type = "text/event-stream";
fd054689a64c mod_http_stats_stream: Use existing header preparation
Kim Alvefur <zash@zash.se>
parents: 2432
diff changeset
20 response.headers.x_accel_buffering = "no"; -- for nginx maybe?
fd054689a64c mod_http_stats_stream: Use existing header preparation
Kim Alvefur <zash@zash.se>
parents: 2432
diff changeset
21 local resp = http.prepare_header(response);
fd054689a64c mod_http_stats_stream: Use existing header preparation
Kim Alvefur <zash@zash.se>
parents: 2432
diff changeset
22 table.insert(resp, "event: stats-full\r\n");
fd054689a64c mod_http_stats_stream: Use existing header preparation
Kim Alvefur <zash@zash.se>
parents: 2432
diff changeset
23 table.insert(resp, "data: ");
fd054689a64c mod_http_stats_stream: Use existing header preparation
Kim Alvefur <zash@zash.se>
parents: 2432
diff changeset
24 table.insert(resp, json.encode(statsman.get_stats()));
fd054689a64c mod_http_stats_stream: Use existing header preparation
Kim Alvefur <zash@zash.se>
parents: 2432
diff changeset
25 table.insert(resp, "\r\n\r\n");
fd054689a64c mod_http_stats_stream: Use existing header preparation
Kim Alvefur <zash@zash.se>
parents: 2432
diff changeset
26 response.conn:write(table.concat(resp));
2432
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 sessions[response] = request;
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29 return true;
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30 end
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32
3643
740870196b97 mod_http_stats_stream: Make global to simplify
Kim Alvefur <zash@zash.se>
parents: 3635
diff changeset
33 module:hook("stats-updated", function (event)
2432
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34 local data = table.concat({
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35 "event: stats-updated";
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 "data: "..json.encode(event.changed_stats);
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37 "";
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38 "";
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39 }, "\r\n")
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40 for response in pairs(sessions) do
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41 response.conn:write(data);
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 end
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43 end);
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
44
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
46 module:depends("http");
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47 module:provides("http", {
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48 route = {
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
49 GET = get_updates;
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
50 }
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
51 });