Software /
code /
prosody-modules
Annotate
mod_statistics/top.lua @ 1078:5f398922f0a3
mod_statistics/top.lua: Keep sessions displayed for 10s (instead of 3s)
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sat, 15 Jun 2013 21:01:28 +0100 |
parent | 1073:fe57e9332e52 |
child | 1079:87a091f88a1d |
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 module("prosodytop", package.seeall); |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
2 |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 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
|
4 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
|
5 local curses = require "curses"; |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
6 local stats = require "stats".stats; |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 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
|
8 |
1078
5f398922f0a3
mod_statistics/top.lua: Keep sessions displayed for 10s (instead of 3s)
Kim Alvefur <zash@zash.se>
parents:
1073
diff
changeset
|
9 local sessions_idle_after = 10; |
1072
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 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
|
11 |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 local top = {}; |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 top.__index = top; |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 local status_lines = { |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 "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
|
17 "Connections: $total_c2s c2s, $total_s2sout s2sout, $total_s2sin s2sin, $total_component component"; |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 "Memory: $memory_used lua, $memory_process process"; |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 "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
|
20 "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
|
21 }; |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 function top:draw() |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 self:draw_status(); |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 self:draw_column_titles(); |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 self:draw_conn_list(); |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 self.statuswin:refresh(); |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 self.listwin:refresh(); |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 --self.infowin:refresh() |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 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
|
31 end |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 -- 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
|
34 -- 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
|
35 local conn_list_columns = { |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 { 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
|
37 { 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
|
38 { 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
|
39 { 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
|
40 { 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
|
41 { 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
|
42 { 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
|
43 { 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
|
44 { 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
|
45 { 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
|
46 { 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
|
47 { 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
|
48 |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 }; |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 function top:draw_status() |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 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
|
53 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
|
54 self.statuswin:clrtoeol(); |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 end |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 -- Clear stanza counts |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
57 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
|
58 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
|
59 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
|
60 end |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
61 end |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
62 |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
63 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
|
64 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
|
65 end |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
66 |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
67 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
|
68 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
|
69 end |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
70 |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
71 function top:resized() |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
72 self:recalc_column_widths(); |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
73 --self.stdscr:clear(); |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
74 self:draw(); |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
75 end |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
76 |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
77 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
|
78 local widths = {}; |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
79 self.column_widths = widths; |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
80 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
|
81 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
|
82 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
|
83 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
|
84 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
|
85 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
|
86 widths[i] = width; |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
87 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
|
88 end |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
89 end |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
90 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
|
91 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
|
92 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
|
93 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
|
94 end |
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 return widths; |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
97 end |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
98 |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
99 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
|
100 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
|
101 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
|
102 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
|
103 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
|
104 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
|
105 end |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
106 self.listwin:addstr(" "); |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
107 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
|
108 end |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
109 |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
110 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
|
111 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
|
112 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
|
113 (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
|
114 end |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
115 |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
116 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
|
117 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
|
118 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
|
119 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
|
120 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
|
121 for index = 1, rows do |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
122 session = top_sessions[index]; |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
123 if session then |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
124 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
|
125 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
|
126 else |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
127 local row = {}; |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
128 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
|
129 local width = widths[i]; |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
130 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
|
131 if #v < width then |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
132 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
|
133 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
|
134 else |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
135 v = padright(v, width); |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
136 end |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
137 end |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
138 table.insert(row, v); |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
139 end |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
140 self.listwin:mvaddstr(index, 0, " "..table.concat(row)); |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
141 end |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
142 else |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
143 -- 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
|
144 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
|
145 self.listwin:clrtoeol(); |
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 end |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
148 end |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
149 |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
150 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
|
151 self.prosody[name] = value; |
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 |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
154 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
|
155 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
|
156 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
|
157 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
|
158 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
|
159 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
|
160 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
|
161 end |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
162 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
|
163 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
|
164 stats.last_update = time(); |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
165 end |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
166 |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
167 function new(base) |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
168 setmetatable(base, top); |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
169 base.data = setmetatable({}, { |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
170 __index = function (t, k) |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
171 local stat = stats[k]; |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
172 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
|
173 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
|
174 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
|
175 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
|
176 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
|
177 if v == nil then |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
178 return "?"; |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
179 end |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
180 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
|
181 end |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
182 end |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
183 return base.prosody[k]; |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
184 end; |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
185 }); |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
186 |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
187 base.active_sessions = {}; |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
188 |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
189 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
|
190 |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
191 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
|
192 base.promptwin:addstr(""); |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
193 base.promptwin:refresh(); |
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.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
|
196 base.listwin:syncok(); |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
197 |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
198 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
|
199 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
|
200 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
|
201 base.infowin:syncok(); |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
202 base.infowin:refresh(); |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
203 |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
204 base:resized(); |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
205 |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
206 return base; |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
207 end |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
208 |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
209 return _M; |