annotate mod_statistics/top.lib.lua @ 3738:9cd6d602ad1b

mod_bookmarks2: Simplify for loop (thanks MattJ!).
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Wed, 13 Nov 2019 15:51:59 +0100
parents 66bda434d476
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1072
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 local array = require "util.array";
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 local it = require "util.iterators";
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 local curses = require "curses";
3414
66bda434d476 mod_statistics: Update 'top' to (hopefully) work with Prosody 0.11/Lua 5.2
Matthew Wild <mwild1@gmail.com>
parents: 1343
diff changeset
4 local stats = module:require "stats".stats;
1072
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 local time = require "socket".gettime;
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6
1084
9612cea92503 mod_statistics: top: Clean up sessions after 60s of inactivity
Matthew Wild <mwild1@gmail.com>
parents: 1083
diff changeset
7 local sessions_idle_after = 60;
1072
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 local stanza_names = {"message", "presence", "iq"};
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 local top = {};
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 top.__index = top;
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 local status_lines = {
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 "Prosody $version - $time up $up_since, $total_users users, $cpu busy";
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 "Connections: $total_c2s c2s, $total_s2sout s2sout, $total_s2sin s2sin, $total_component component";
1082
6c555e7dc942 mod_statistics/top.lua: Adjust displayed memory stats for previous commit (3e2c4f424797)
Matthew Wild <mwild1@gmail.com>
parents: 1079
diff changeset
16 "Memory: $memory_lua lua, $memory_allocated process ($memory_used in use)";
1072
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 "Stanzas in: $message_in_per_second message/s, $presence_in_per_second presence/s, $iq_in_per_second iq/s";
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 "Stanzas out: $message_out_per_second message/s, $presence_out_per_second presence/s, $iq_out_per_second iq/s";
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 };
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 function top:draw()
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 self:draw_status();
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 self:draw_column_titles();
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 self:draw_conn_list();
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 self.statuswin:refresh();
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 self.listwin:refresh();
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 --self.infowin:refresh()
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 self.stdscr:move(#status_lines,0)
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 end
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 -- Width specified as cols or % of unused space, defaults to
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 -- title width if not specified
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 local conn_list_columns = {
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 { title = "ID", key = "id", width = "8" };
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 { title = "JID", key = "jid", width = "100%" };
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 { title = "STANZAS IN>", key = "total_stanzas_in", align = "right" };
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 { title = "MSG", key = "message_in", align = "right", width = "4" };
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 { title = "PRES", key = "presence_in", align = "right", width = "4" };
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 { title = "IQ", key = "iq_in", align = "right", width = "4" };
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 { title = "STANZAS OUT>", key = "total_stanzas_out", align = "right" };
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 { title = "MSG", key = "message_out", align = "right", width = "4" };
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 { title = "PRES", key = "presence_out", align = "right", width = "4" };
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 { title = "IQ", key = "iq_out", align = "right", width = "4" };
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 { title = "BYTES IN", key = "bytes_in", align = "right" };
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 { title = "BYTES OUT", key = "bytes_out", align = "right" };
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 };
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 function top:draw_status()
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 for row, line in ipairs(status_lines) do
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 self.statuswin:mvaddstr(row-1, 0, (line:gsub("%$([%w_]+)", self.data)));
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 self.statuswin:clrtoeol();
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 end
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 -- Clear stanza counts
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 for _, stanza_type in ipairs(stanza_names) do
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 self.prosody[stanza_type.."_in_per_second"] = 0;
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 self.prosody[stanza_type.."_out_per_second"] = 0;
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 end
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 end
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 local function padright(s, width)
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 return s..string.rep(" ", width-#s);
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 end
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 local function padleft(s, width)
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 return string.rep(" ", width-#s)..s;
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 end
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 function top:resized()
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 self:recalc_column_widths();
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 --self.stdscr:clear();
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 self:draw();
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 end
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 function top:recalc_column_widths()
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 local widths = {};
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 self.column_widths = widths;
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 local total_width = curses.cols()-4;
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 local free_width = total_width;
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 for i = 1, #conn_list_columns do
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 local width = conn_list_columns[i].width or "0";
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 if not(type(width) == "string" and width:sub(-1) == "%") then
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 width = math.max(tonumber(width), #conn_list_columns[i].title+1);
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 widths[i] = width;
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 free_width = free_width - width;
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 end
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 end
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 for i = 1, #conn_list_columns do
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 if not widths[i] then
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 local pc_width = tonumber((conn_list_columns[i].width:gsub("%%$", "")));
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 widths[i] = math.floor(free_width*(pc_width/100));
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 end
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 end
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 return widths;
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95 end
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97 function top:draw_column_titles()
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98 local widths = self.column_widths;
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 self.listwin:attron(curses.A_REVERSE);
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 self.listwin:mvaddstr(0, 0, " ");
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 for i, column in ipairs(conn_list_columns) do
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102 self.listwin:addstr(padright(column.title, widths[i]));
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103 end
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104 self.listwin:addstr(" ");
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105 self.listwin:attroff(curses.A_REVERSE);
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
106 end
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
107
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108 local function session_compare(session1, session2)
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109 local stats1, stats2 = session1.stats, session2.stats;
1073
fe57e9332e52 mod_statistics/top.lua: Fix session sorting function (seems it must always return false when a==b)
Matthew Wild <mwild1@gmail.com>
parents: 1072
diff changeset
110 return (stats1.total_stanzas_in + stats1.total_stanzas_out) >
1072
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111 (stats2.total_stanzas_in + stats2.total_stanzas_out);
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112 end
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114 function top:draw_conn_list()
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115 local rows = curses.lines()-(#status_lines+2)-5;
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116 local cutoff_time = time() - sessions_idle_after;
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
117 local widths = self.column_widths;
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
118 local top_sessions = array.collect(it.values(self.active_sessions)):sort(session_compare);
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119 for index = 1, rows do
3414
66bda434d476 mod_statistics: Update 'top' to (hopefully) work with Prosody 0.11/Lua 5.2
Matthew Wild <mwild1@gmail.com>
parents: 1343
diff changeset
120 local session = top_sessions[index];
1072
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121 if session then
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 if session.last_update < cutoff_time then
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123 self.active_sessions[session.id] = nil;
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124 else
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125 local row = {};
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126 for i, column in ipairs(conn_list_columns) do
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
127 local width = widths[i];
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
128 local v = tostring(session[column.key] or ""):sub(1, width);
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
129 if #v < width then
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
130 if column.align == "right" then
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
131 v = padleft(v, width-1).." ";
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
132 else
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
133 v = padright(v, width);
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
134 end
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
135 end
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
136 table.insert(row, v);
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
137 end
1083
6685ebe6f8cf mod_statistics: top: Make sessions temporarily bold when they are updated
Matthew Wild <mwild1@gmail.com>
parents: 1082
diff changeset
138 if session.updated then
6685ebe6f8cf mod_statistics: top: Make sessions temporarily bold when they are updated
Matthew Wild <mwild1@gmail.com>
parents: 1082
diff changeset
139 self.listwin:attron(curses.A_BOLD);
6685ebe6f8cf mod_statistics: top: Make sessions temporarily bold when they are updated
Matthew Wild <mwild1@gmail.com>
parents: 1082
diff changeset
140 end
1072
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
141 self.listwin:mvaddstr(index, 0, " "..table.concat(row));
1083
6685ebe6f8cf mod_statistics: top: Make sessions temporarily bold when they are updated
Matthew Wild <mwild1@gmail.com>
parents: 1082
diff changeset
142 if session.updated then
6685ebe6f8cf mod_statistics: top: Make sessions temporarily bold when they are updated
Matthew Wild <mwild1@gmail.com>
parents: 1082
diff changeset
143 session.updated = false;
6685ebe6f8cf mod_statistics: top: Make sessions temporarily bold when they are updated
Matthew Wild <mwild1@gmail.com>
parents: 1082
diff changeset
144 self.listwin:attroff(curses.A_BOLD);
6685ebe6f8cf mod_statistics: top: Make sessions temporarily bold when they are updated
Matthew Wild <mwild1@gmail.com>
parents: 1082
diff changeset
145 end
1072
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
146 end
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
147 else
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
148 -- FIXME: How to clear a line? It's 5am and I don't feel like reading docs.
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
149 self.listwin:move(index, 0);
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
150 self.listwin:clrtoeol();
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
151 end
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
152 end
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
153 end
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
154
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
155 function top:update_stat(name, value)
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
156 self.prosody[name] = value;
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
157 end
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
158
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
159 function top:update_session(id, jid, stats)
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
160 self.active_sessions[id] = stats;
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
161 stats.id, stats.jid, stats.stats = id, jid, stats;
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
162 stats.total_bytes = stats.bytes_in + stats.bytes_out;
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
163 for _, stanza_type in ipairs(stanza_names) do
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
164 self.prosody[stanza_type.."_in_per_second"] = (self.prosody[stanza_type.."_in_per_second"] or 0) + stats[stanza_type.."_in"];
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
165 self.prosody[stanza_type.."_out_per_second"] = (self.prosody[stanza_type.."_out_per_second"] or 0) + stats[stanza_type.."_out"];
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
166 end
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
167 stats.total_stanzas_in = stats.message_in + stats.presence_in + stats.iq_in;
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
168 stats.total_stanzas_out = stats.message_out + stats.presence_out + stats.iq_out;
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
169 stats.last_update = time();
1083
6685ebe6f8cf mod_statistics: top: Make sessions temporarily bold when they are updated
Matthew Wild <mwild1@gmail.com>
parents: 1082
diff changeset
170 stats.updated = true;
1072
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
171 end
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
172
3414
66bda434d476 mod_statistics: Update 'top' to (hopefully) work with Prosody 0.11/Lua 5.2
Matthew Wild <mwild1@gmail.com>
parents: 1343
diff changeset
173 local function new(base)
1072
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
174 setmetatable(base, top);
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
175 base.data = setmetatable({}, {
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
176 __index = function (t, k)
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
177 local stat = stats[k];
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
178 if stat and stat.tostring then
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
179 if type(stat.tostring) == "function" then
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
180 return stat.tostring(base.prosody[k]);
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
181 elseif type(stat.tostring) == "string" then
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
182 local v = base.prosody[k];
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
183 if v == nil then
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
184 return "?";
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
185 end
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
186 return (stat.tostring):format(v);
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
187 end
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
188 end
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
189 return base.prosody[k];
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
190 end;
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
191 });
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
192
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
193 base.active_sessions = {};
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
194
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
195 base.statuswin = curses.newwin(#status_lines, 0, 0, 0);
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
196
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
197 base.promptwin = curses.newwin(1, 0, #status_lines, 0);
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
198 base.promptwin:addstr("");
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
199 base.promptwin:refresh();
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
200
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
201 base.listwin = curses.newwin(curses.lines()-(#status_lines+2)-5, 0, #status_lines+1, 0);
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
202 base.listwin:syncok();
1343
7dbde05b48a9 all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1084
diff changeset
203
1072
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
204 base.infowin = curses.newwin(5, 0, curses.lines()-5, 0);
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
205 base.infowin:mvaddstr(1, 1, "Hello world");
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
206 base.infowin:border(0,0,0,0);
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
207 base.infowin:syncok();
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
208 base.infowin:refresh();
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
209
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
210 base:resized();
1343
7dbde05b48a9 all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1084
diff changeset
211
1072
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
212 return base;
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
213 end
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
214
3414
66bda434d476 mod_statistics: Update 'top' to (hopefully) work with Prosody 0.11/Lua 5.2
Matthew Wild <mwild1@gmail.com>
parents: 1343
diff changeset
215 return { new = new };