<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[14102] CalendarServer/trunk/calendarserver/tools/dashboard.py</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.calendarserver.org//changeset/14102">14102</a></dd>
<dt>Author</dt> <dd>dre@apple.com</dd>
<dt>Date</dt> <dd>2014-10-24 10:34:33 -0700 (Fri, 24 Oct 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>Add ability to toggle panels individually. Float the help panel on the top right. Show the help panel at start time if it would fit. Add a 'no windows' hotkey (n) to clear everything but the help panel.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#CalendarServertrunkcalendarservertoolsdashboardpy">CalendarServer/trunk/calendarserver/tools/dashboard.py</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="CalendarServertrunkcalendarservertoolsdashboardpy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/calendarserver/tools/dashboard.py (14101 => 14102)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/calendarserver/tools/dashboard.py        2014-10-23 22:41:05 UTC (rev 14101)
+++ CalendarServer/trunk/calendarserver/tools/dashboard.py        2014-10-24 17:34:33 UTC (rev 14102)
</span><span class="lines">@@ -22,6 +22,7 @@
</span><span class="cx"> from getopt import getopt, GetoptError
</span><span class="cx"> 
</span><span class="cx"> import curses
</span><ins>+import curses.panel
</ins><span class="cx"> import json
</span><span class="cx"> import os
</span><span class="cx"> import sched
</span><span class="lines">@@ -29,6 +30,9 @@
</span><span class="cx"> import time
</span><span class="cx"> import socket
</span><span class="cx"> import errno
</span><ins>+import logging
+LOG_FILENAME = 'db.log'
+#logging.basicConfig(filename=LOG_FILENAME,level=logging.DEBUG)
</ins><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="lines">@@ -152,27 +156,55 @@
</span><span class="cx">         self.sched.enter(self.seconds, 0, self.updateDisplay, ())
</span><span class="cx">         self.sched.run()
</span><span class="cx"> 
</span><ins>+    def terminal_size(self):
+        import fcntl, termios, struct
+        h, w, hp, wp = struct.unpack('HHHH',
+            fcntl.ioctl(0, termios.TIOCGWINSZ,
+            struct.pack('HHHH', 0, 0, 0, 0)))
+        return w, h
</ins><span class="cx"> 
</span><span class="cx">     def displayWindow(self, wtype):
</span><span class="cx">         &quot;&quot;&quot;
</span><del>-        Display a new window type, clearing out the old one first.
</del><ins>+        Toggle the specified window, or reset to launch state if None.
</ins><span class="cx">         &quot;&quot;&quot;
</span><del>-        if self.windows:
-            for window in self.windows:
-                window.deactivate()
-            self.windows = []
</del><span class="cx"> 
</span><ins>+        # Toggle a specific window on or off
</ins><span class="cx">         if wtype is not None:
</span><del>-            self.windows.append(wtype(self.usesCurses, self.client).makeWindow())
-            self.windows[-1].activate()
</del><ins>+            if wtype not in [type(w) for w in self.windows]:
+                self.windows.append(wtype(self.usesCurses, self.client).makeWindow())
+                self.windows[-1].activate()
+            else:
+                for window in self.windows:
+                    if type(window) == wtype:
+                        window.deactivate()
+                        self.windows.remove(window)
+                    if len(self.windows) == 0:
+                        self.displayWindow(self.registered_windows[&quot;h&quot;])
+
+            self.resetWindows()
+        # Reset the screen to the default config
</ins><span class="cx">         else:
</span><ins>+            if self.windows:
+                for window in self.windows:
+                    window.deactivate()
+                self.windows = []
</ins><span class="cx">             top = 0
</span><span class="cx">             ordered_windows = [self.registered_windows[i] for i in self.registered_order]
</span><span class="cx">             for wtype in filter(lambda x: x.all, ordered_windows):
</span><ins>+                new_win = wtype(self.usesCurses, self.client).makeWindow(top=top)
+                logging.debug('created %r at panel level %r' % (new_win,new_win.z_order))
</ins><span class="cx">                 self.windows.append(wtype(self.usesCurses, self.client).makeWindow(top=top))
</span><span class="cx">                 self.windows[-1].activate()
</span><span class="cx">                 top += self.windows[-1].nlines + 1
</span><ins>+            # Don't display help panel if the window is too narrow
+            term_w, term_h = self.terminal_size()
+            logging.debug(&quot;logger displayWindow: rows: %s  cols: %s&quot; % (term_h, term_w))
+            if int(term_w) &gt; 100:
+                logging.debug('term_w &gt; 100, making window with top at %d' % (top))
+                self.windows.append(HelpWindow(self.usesCurses, self.client).makeWindow(top=top))
+                self.windows[-1].activate()
</ins><span class="cx"> 
</span><ins>+        curses.panel.update_panels()
</ins><span class="cx">         self.updateDisplay(True)
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="lines">@@ -181,15 +213,19 @@
</span><span class="cx">         Reset the current set of windows.
</span><span class="cx">         &quot;&quot;&quot;
</span><span class="cx">         if self.windows:
</span><ins>+            logging.debug('resetting windows: %r' % (self.windows))
</ins><span class="cx">             for window in self.windows:
</span><span class="cx">                 window.deactivate()
</span><span class="cx">             old_windows = self.windows
</span><span class="cx">             self.windows = []
</span><span class="cx">             top = 0
</span><span class="cx">             for old in old_windows:
</span><ins>+                logging.debug('processing window of type %r' % (type(old)))
</ins><span class="cx">                 self.windows.append(old.__class__(self.usesCurses, self.client).makeWindow(top=top))
</span><span class="cx">                 self.windows[-1].activate()
</span><del>-                top += self.windows[-1].nlines + 1
</del><ins>+                # Allow the help window to float on the right edge
+                if old.__class__.__name__ != &quot;HelpWindow&quot;:
+                    top += self.windows[-1].nlines + 1
</ins><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx">     def updateDisplay(self, initialUpdate=False):
</span><span class="lines">@@ -231,6 +267,12 @@
</span><span class="cx">                 self.seconds = 1.0 if self.seconds == 0.1 else 0.1
</span><span class="cx">             elif c == &quot;a&quot;:
</span><span class="cx">                 self.displayWindow(None)
</span><ins>+            elif c == &quot;n&quot;:
+                if self.windows:
+                    for window in self.windows:
+                        window.deactivate()
+                    self.windows = []
+                    self.displayWindow(self.registered_windows[&quot;h&quot;])
</ins><span class="cx">             elif c in self.registered_windows:
</span><span class="cx">                 self.displayWindow(self.registered_windows[c])
</span><span class="cx"> 
</span><span class="lines">@@ -331,8 +373,17 @@
</span><span class="cx">         self.client = client
</span><span class="cx">         self.rowCount = 0
</span><span class="cx">         self.needsReset = False
</span><ins>+        self.z_order = 'bottom'
</ins><span class="cx"> 
</span><span class="cx"> 
</span><ins>+    def terminal_size(self):
+        import fcntl, termios, struct
+        h, w, hp, wp = struct.unpack('HHHH',
+            fcntl.ioctl(0, termios.TIOCGWINSZ,
+            struct.pack('HHHH', 0, 0, 0, 0)))
+        return w, h
+
+
</ins><span class="cx">     def makeWindow(self, top=0, left=0):
</span><span class="cx">         raise NotImplementedError()
</span><span class="cx"> 
</span><span class="lines">@@ -346,6 +397,8 @@
</span><span class="cx">         if self.usesCurses:
</span><span class="cx">             self.window = curses.newwin(nlines, ncols, begin_y, begin_x)
</span><span class="cx">             self.window.nodelay(1)
</span><ins>+            self.panel = curses.panel.new_panel(self.window)
+            eval(&quot;self.panel.%s()&quot; % (self.z_order,))
</ins><span class="cx">         else:
</span><span class="cx">             self.window = None
</span><span class="cx">         self.title = title
</span><span class="lines">@@ -412,11 +465,12 @@
</span><span class="cx">     Display help for the dashboard.
</span><span class="cx">     &quot;&quot;&quot;
</span><span class="cx"> 
</span><del>-    help = &quot;display dashboard help&quot;
</del><ins>+    help = &quot;dashboard help&quot;
</ins><span class="cx">     all = False
</span><span class="cx">     helpItems = (
</span><span class="cx">         &quot;&quot;,
</span><span class="cx">         &quot;a - all windows&quot;,
</span><ins>+        &quot;n - no windows&quot;,
</ins><span class="cx">         &quot;  - (space) pause dashboard polling&quot;,
</span><span class="cx">         &quot;t - toggle update between 0.1 and 1.0 seconds&quot;,
</span><span class="cx">         &quot;&quot;,
</span><span class="lines">@@ -424,10 +478,14 @@
</span><span class="cx">     )
</span><span class="cx"> 
</span><span class="cx">     def makeWindow(self, top=0, left=0):
</span><ins>+        term_w, term_h = self.terminal_size()
+        help_x_offset = term_w - BOX_WIDTH + 4
</ins><span class="cx">         self._createWindow(
</span><span class="cx">             &quot;Help&quot;,
</span><span class="cx">             len(self.helpItems) + len(Dashboard.registered_windows) + 2,
</span><del>-            begin_y=top, begin_x=left
</del><ins>+            ncols=BOX_WIDTH-4,
+            begin_y=0,
+            begin_x=help_x_offset,
</ins><span class="cx">         )
</span><span class="cx">         return self
</span><span class="cx"> 
</span><span class="lines">@@ -441,7 +499,7 @@
</span><span class="cx">         if self.usesCurses:
</span><span class="cx">             self.window.erase()
</span><span class="cx">             self.window.border()
</span><del>-            self.window.addstr(0, 2, &quot;Help for Dashboard&quot;)
</del><ins>+            self.window.addstr(0, 2, &quot;Hotkeys&quot;)
</ins><span class="cx"> 
</span><span class="cx">         x = 1
</span><span class="cx">         y = 1
</span><span class="lines">@@ -469,7 +527,7 @@
</span><span class="cx">     Display the status of the server's job queue.
</span><span class="cx">     &quot;&quot;&quot;
</span><span class="cx"> 
</span><del>-    help = &quot;display server jobs&quot;
</del><ins>+    help = &quot;server jobs&quot;
</ins><span class="cx">     clientItem = &quot;jobs&quot;
</span><span class="cx">     FORMAT_WIDTH = 98
</span><span class="cx"> 
</span><span class="lines">@@ -580,7 +638,7 @@
</span><span class="cx">     Displays the status of the server's master process worker slave slots.
</span><span class="cx">     &quot;&quot;&quot;
</span><span class="cx"> 
</span><del>-    help = &quot;display server child job assignments&quot;
</del><ins>+    help = &quot;server child job assignments&quot;
</ins><span class="cx">     clientItem = &quot;job_assignments&quot;
</span><span class="cx">     FORMAT_WIDTH = 40
</span><span class="cx"> 
</span><span class="lines">@@ -674,7 +732,7 @@
</span><span class="cx">     Displays the status of the server's master process worker slave slots.
</span><span class="cx">     &quot;&quot;&quot;
</span><span class="cx"> 
</span><del>-    help = &quot;display server child slots&quot;
</del><ins>+    help = &quot;server child slots&quot;
</ins><span class="cx">     clientItem = &quot;slots&quot;
</span><span class="cx">     FORMAT_WIDTH = 72
</span><span class="cx"> 
</span><span class="lines">@@ -784,7 +842,7 @@
</span><span class="cx">     Displays the system information provided by the server.
</span><span class="cx">     &quot;&quot;&quot;
</span><span class="cx"> 
</span><del>-    help = &quot;display system details&quot;
</del><ins>+    help = &quot;system details&quot;
</ins><span class="cx">     clientItem = &quot;stats_system&quot;
</span><span class="cx"> 
</span><span class="cx">     def makeWindow(self, top=0, left=0):
</span><span class="lines">@@ -862,7 +920,7 @@
</span><span class="cx">     Displays the status of the server's master process worker slave slots.
</span><span class="cx">     &quot;&quot;&quot;
</span><span class="cx"> 
</span><del>-    help = &quot;display server request stats&quot;
</del><ins>+    help = &quot;server request stats&quot;
</ins><span class="cx">     clientItem = &quot;stats&quot;
</span><span class="cx">     FORMAT_WIDTH = 84
</span><span class="cx"> 
</span><span class="lines">@@ -937,7 +995,7 @@
</span><span class="cx">     Displays the status of the server's directory service calls
</span><span class="cx">     &quot;&quot;&quot;
</span><span class="cx"> 
</span><del>-    help = &quot;display directory service stats&quot;
</del><ins>+    help = &quot;directory service stats&quot;
</ins><span class="cx">     clientItem = &quot;directory&quot;
</span><span class="cx">     FORMAT_WIDTH = 89
</span><span class="cx"> 
</span><span class="lines">@@ -1051,13 +1109,13 @@
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx"> 
</span><ins>+Dashboard.registerWindow(HelpWindow, &quot;h&quot;)
</ins><span class="cx"> Dashboard.registerWindow(SystemWindow, &quot;s&quot;)
</span><ins>+Dashboard.registerWindow(AssignmentsWindow, &quot;w&quot;)
</ins><span class="cx"> Dashboard.registerWindow(RequestStatsWindow, &quot;r&quot;)
</span><span class="cx"> Dashboard.registerWindow(JobsWindow, &quot;j&quot;)
</span><del>-Dashboard.registerWindow(AssignmentsWindow, &quot;w&quot;)
</del><span class="cx"> Dashboard.registerWindow(HTTPSlotsWindow, &quot;c&quot;)
</span><span class="cx"> Dashboard.registerWindow(DirectoryStatsWindow, &quot;d&quot;)
</span><del>-Dashboard.registerWindow(HelpWindow, &quot;h&quot;)
</del><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx"> if __name__ == &quot;__main__&quot;:
</span></span></pre>
</div>
</div>

</body>
</html>