Comparison

mod_statistics/prosodytop.lib.lua @ 3414:66bda434d476

mod_statistics: Update 'top' to (hopefully) work with Prosody 0.11/Lua 5.2
author Matthew Wild <mwild1@gmail.com>
date Sun, 23 Dec 2018 11:00:38 +0000
parent 1699:mod_statistics/prosodytop.lua@54b93cf0f631
child 3427:8ea7d8f90d25
comparison
equal deleted inserted replaced
3413:6421c9f05e02 3414:66bda434d476
1 local curses = require "curses";
2 local server = require "net.server_select";
3 local timer = require "util.timer";
4
5 assert(curses.timeout, "Incorrect version of curses library. Try 'sudo luarocks install luaposix'");
6
7 local top = module:require "top";
8
9 function main()
10 local stdscr = curses.stdscr() -- it's a userdatum
11 --stdscr:clear();
12 local view = top.new({
13 stdscr = stdscr;
14 prosody = { up_since = os.time() };
15 conn_list = {};
16 });
17
18 timer.add_task(0.01, function ()
19 local ch = stdscr:getch();
20 if ch then
21 if stdscr:getch() == 410 then
22 view:resized();
23 else
24 curses.ungetch(ch);
25 end
26 end
27 return 0.2;
28 end);
29
30 timer.add_task(0, function ()
31 view:draw();
32 return 1;
33 end);
34
35 --[[
36 posix.signal(28, function ()
37 table.insert(view.conn_list, { jid = "WINCH" });
38 --view:draw();
39 end);
40 ]]
41
42 -- Fake socket object around stdin
43 local stdin = {
44 getfd = function () return 0; end;
45 dirty = function (self) return false; end;
46 settimeout = function () end;
47 send = function (_, d) return #d, 0; end;
48 close = function () end;
49 receive = function (_, patt)
50 local ch = stdscr:getch();
51 if ch >= 0 and ch <=255 then
52 return string.char(ch);
53 elseif ch == 410 then
54 view:resized();
55 else
56 table.insert(view.conn_list, { jid = tostring(ch) }); --FIXME
57 end
58 return "";
59 end
60 };
61 local function on_incoming(stdin, text)
62 -- TODO: Handle keypresses
63 if text:lower() == "q" then os.exit(); end
64 end
65 stdin = server.wrapclient(stdin, "stdin", 0, {
66 onincoming = on_incoming, ondisconnect = function () end
67 }, "*a");
68
69 local function handle_line(line)
70 local e = {
71 STAT = function (name) return function (value)
72 view:update_stat(name, value);
73 end end;
74 SESS = function (id) return function (jid) return function (stats)
75 view:update_session(id, jid, stats);
76 end end end;
77 };
78 local chunk = assert(loadstring(line));
79 setfenv(chunk, e);
80 chunk();
81 end
82
83 local stats_listener = {};
84
85 function stats_listener.onconnect(conn)
86 --stdscr:mvaddstr(6, 0, "CONNECTED");
87 end
88
89 local partial = "";
90 function stats_listener.onincoming(conn, data)
91 --print("DATA", data)
92 data = partial..data;
93 local lastpos = 1;
94 for line, pos in data:gmatch("([^\n]+)\n()") do
95 lastpos = pos;
96 handle_line(line);
97 end
98 partial = data:sub(lastpos);
99 end
100
101 function stats_listener.ondisconnect(conn, err)
102 stdscr:mvaddstr(6, 0, "DISCONNECTED: "..(err or "unknown"));
103 end
104
105 local conn = require "socket".tcp();
106 assert(conn:connect("localhost", 5782));
107 handler = server.wrapclient(conn, "localhost", 5782, stats_listener, "*a");
108 end
109
110 return {
111 run = function ()
112 --os.setlocale("UTF-8", "all")
113
114 curses.initscr()
115 curses.cbreak()
116 curses.echo(false) -- not noecho !
117 curses.nl(false) -- not nonl !
118 curses.timeout(0);
119
120 local ok, err = pcall(main);
121
122 --while true do stdscr:getch() end
123 --stdscr:endwin()
124
125 if ok then
126 ok, err = xpcall(server.loop, debug.traceback);
127 end
128
129 curses.endwin();
130
131 --stdscr:refresh();
132 if not ok then
133 print(err);
134 end
135
136 print"DONE"
137 end;
138 };