use persistant worker to query DSLAM
[APKPM.git] / public / user.html
index 67a6eb5..c5062eb 100644 (file)
                if ( ! username ) username = self.search_username;
                $log.info( 'CRM_search', username );
                self.clear();
+               self.message.CRM = 'search for '+username;
                self.r.CRM_search.query({username: username}, function(CRM) {
                        self.CRM = CRM;
                        $log.info( 'CRM', CRM )
+                       if ( ! CRM.length ) {
+                               self.message.CRM = 'no results';
+                               return;
+                       }
+                       self.message.CRM = null;
                        if ( CRM.length == 1 ) {
                                self.selected_username = CRM[0].USERNAME;
-                               $log.info( 'auto-selected', self.selected_username );
+                               $log.info( 'auto-selected ', self.selected_username );
                        }
                });
        };
                if ( ! username ) username = self.selected_username;
 
                if ( ! username ) {
-                       $log.error( 'no username for LDAP_search' );
+                       self.message.LDAP = 'no username';
                        return;
                }
                $log.info( 'LDAP_search', username );
                self.clear_LDAP();
                self.selected_username = username;
+               self.message.LDAP = 'search '+username;
                self.r.LDAP_search.query({ username: username }, function(LDAP) {
                        self.LDAP = LDAP;
                        $log.info( 'LDAP', LDAP );
                        if ( self.LDAP.length == 0 ) {
-                               $log.error( 'no LDAP results for', username );
+                               self.message.LDAP ='no results for '+username;
                                return;
                        }
                        self.cpe.parser = LDAP[0]._cpe_parser;
                        self.cpe.table_name  = 'cpe_' + LDAP[0]._cpe_parser;
                        self.username = LDAP[0].cn;
+                       self.message.LDAP = null;
                        $log.info( 'cpe', self.cpe );
                });
        };
        self.$watch('selected_username', self.LDAP_search );
 
-       self.gnuplot_draw = function(h_cols) {
-               $log.info( 'gnuplot_draw', h_cols );
-               if (h_cols.length) {
-                       self.gnuplot.cols = h_cols;
-               } else {
+       self.gnuplot_changed = function() {
+               var h_cols = self.gnuplot.cols;
+               $log.info( 'gnuplot_changed', h_cols );
+               if ( ! h_cols || h_cols.length < 1) {
                        $log.warn("no columns for gnuplot");
                        return;
                }
+
                var cols = [ 'timestamp' ];
                for ( var i = 0; i < h_cols.length; i++ ) {
                        cols.push( 'h->\'' + h_cols[i] + '\' as "' + h_cols[i] + '"' );
                }
-console.debug( cols );
                self.gnuplot.sql = 'select ' + cols.join(',') + ' from cpe_' + self.cpe.parser + ' where username = \'' + self.username + '\' order by timestamp desc limit 100';
+               $log.info( 'gnuplot', cols, self.gnuplot );
+       };
+       self.gnuplot_refresh = function() {
+               if ( ! self.gnuplot || ! self.gnuplot.cols || ! self.gnuplot.sql) return;
+               self.message.gnuplot = self.gnuplot.cols.join(' ');
+               self.gnuplot.img = '/gnuplot?hide=1;with='+self.gnuplot.with+';sql=' + self.gnuplot.sql;
+               $log.info('gnuplot_refresh', self.gnuplot.img);
+       };
+       self.$watch('gnuplot.sql', self.gnuplot_refresh );
+       self.$watch('gnuplot.with', self.gnuplot_refresh );
 
-               $log.info( 'gnuplot', self.gnuplot );
-               self.gnuplot.img = '/gnuplot?hide=1;with=points;sql=' + self.gnuplot.sql;
-
+       self.gnuplot_onload = function() {
+               $log.info( 'gnuplot_onload' );
+               self.message.gnuplot = '<a href="#gnuplot">ready</a>';
        };
 
        self.r.table = $resource('/table/:table');
        self.table_update = function () {
                if ( ! self.username || ! self.cpe.table_name ) return;
-               self.r.table.get({ username: self.username, table: self.cpe.table_name, limit: self.cpe.limit }, function(table) {
+
+               self.message.ping = self.username+' loading';
+               self.r.table.get({ username: self.username, table: 'ping', limit: 1 }, function(ping) {
+                       $log.info('ping', ping);
+                       self.message.ping = null;
+                       var hash = {};
+                       for(var i = 0; i <= ping.columns.length; i++) {
+                               hash[ping.columns[i]] = ping.rows[0][i];
+                       }
+                       self.cpe.ping = hash;
+                       $log.info('ping hash', hash);
+               });
+
+               self.message.table = self.username + ' loading from ' + self.cpe.table_name;
+               self.r.table.get({ username: self.username, table: self.cpe.table_name, limit: self.cpe_limit }, function(table) {
                        $log.info( 'table', table );
                        self.cpe.table = table;
+                       self.message.table = null;
+                       if ( table.rows.length == 0 ) {
+                               self.message.table = 'no results for '+self.username;
+                               return;
+                       }
 
                        if ( h_cols = self.cpe_hash[self.cpe.parser] ) {
-                               self.gnuplot_draw( h_cols );
+                               self.gnuplot.cols = h_cols;
+                               self.gnuplot_changed();
                        } else {
-                               self.message = 'no graph defined for ' + self.cpe.parser;
+                               self.message.gnuplot = 'no graph for ' + self.cpe.parser;
                        }
                });
        };
@@ -100,19 +137,20 @@ console.debug( cols );
                $log.info('clear_LDAP');
                self.LDAP = null;
                self.selected_username = null;
-               self.gnuplot = {};
-               self.cpe = { limit: 1 };
-               self.message = null;
+               self.gnuplot = { 'with': 'points', cols: [] };
+               self.cpe = {};
+               self.cpe_limit = 1;
        };
 
+       self.message = {};
+
        self.$watch('selected_username', function() {
                if ( self.selected_username == null ) self.clear_LDAP();
        });
 
        self.clear();
-       self.cpe_limit = self.cpe.limit;
        self.$watch('cpe_limit', function() {
-               $log.info( 'limit', self.cpe.limit = self.cpe_limit );
+               $log.info( 'cpe_limit', self.cpe_limit );
                self.table_update();
        });
 
@@ -159,25 +197,6 @@ angular.formatter('include', {
   </script>
 
 <style type="text/css">
-#columns {
-       position: fixed;
-       top: 0;
-       right: 0;
-       z-index: 10;
-       background: #eee;
-}
-
-#columns > span {
-       float: right;
-}
-
-#columns ul {
-       display: none;
-}
-
-#columns:hover ul {
-       display: block;
-}
 
 table {
        border-collapse:collapse;
@@ -204,16 +223,29 @@ th, td {
        position: fixed;
 }
 
-#gnuplot_cols {
-       float: right;
-       background: #eee;
-       padding: 0.5em;
+.panel_right {
+       z-index: 10;
+       position: absolute;
+       right: 0;
+       background: #ffc;
+       padding: 3px;
+}
+
+.panel_right ul {
+       padding: 0;
 }
 
-#gnuplot_cols li {
+.panel_right ul > li {
        list-style-type: none;
 }
-       
+
+.panel_right > label {
+       float: right;
+}
+
+li > tt {
+       font-weight: bold;
+}
 
 </style>
 
@@ -221,20 +253,26 @@ th, td {
 
    <form ng:submit="CRM_search()">
     <label for="args">username:
-    <input type="text" name="search_username" placeholder="test" size="10" ng:required />
+    <input type="text" name="search_username" placeholder="test" size="10" autofocus ng:required />
     </label>
     <input type="submit" value="search in CRM">
     <input type="reset"  ng:click="clear()" value="clear">
 
-    <span id="message" ng:show="message">{{message | html}}</span>
+    <span id="message" ng:show="message.$size()" ng:click="message={}" title="click to close">
+      <div ng:repeat="(category,status) in message" ng:show="status"><b>{{category}}</b> {{status}}</div>
+    </span>
 
    </form>
 
-<div id="columns" ng:show="CRM">
-<span>Add columns</span>
-<ul>
-<li ng:repeat="c in keys(CRM)" ng:show="columns.indexOf(c) < 0" ng:click="columns.push(c)">{{c}}
-</ul>
+
+<div class="panel_right" ng:show="CRM">
+ <label>
+  Add columns
+  <input type=checkbox name=show_columns>
+ </label>
+ <ul ng:show="show_columns">
+  <li ng:repeat="c in keys(CRM)" ng:show="columns.indexOf(c) < 0" ng:click="columns.push(c)">{{c}}
+ </ul>
 </div>
 
     <table ng:show="CRM">
@@ -256,18 +294,9 @@ th, td {
      <li ng:repeat="c in keys(LDAP)"><tt>{{c}}</tt> {{LDAP[0][c]}}</li>
     </ul>
 
-    <ul ng:show="cpe.table" id="gnuplot_cols">
-     <input type=button ng:click="gnuplot_draw(gnuplot.cols)" value="Redraw graph">
-     <li ng:repeat="(k,v) in cpe.table.rows[0][cpe.table.hash_col]">
-        <label>
-        <input type="checkbox" name="gnuplot_cols" ng:format="include:k:gnuplot.cols" ng:change="$log.debug(gnuplot_cols)">
-       {{k}} <tt>{{v}}</tt>
-        </label>
-     </li>
-     <input type=button ng:click="gnuplot_draw(gnuplot.cols)" value="Redraw graph">
-    </ul>
-
-    <ng:include src="gnuplot.img" ng:show="gnuplot.img" onload="message = 'done'" ></ng:include>
+    <div ng:show="cpe.ping">
+     ping <tt>{{cpe.ping.ip}}</tt> rtt <tt>{{cpe.ping.rtt}}</tt> at <tt>{{cpe.ping.timestamp}}</tt>
+    </div>
 
     <h2 ng:show="cpe.table_name">{{cpe.table_name}} {{username}}</h2>
 
@@ -290,6 +319,29 @@ th, td {
     </tr>
     </table>
 
+    <div class="panel_right" ng:show="cpe.table">
+     <label>
+      Graph columns
+      <input type=checkbox name=show_gnuplot_cols>
+     </label>
+
+     <ul ng:show="show_gnuplot_cols">
+      with <select name="gnuplot.with">
+       <option ng:repeat="with in ['points','dots','steps','lines']">{{with}}</option>
+      </select>
+
+      <li ng:repeat="(k,v) in cpe.table.rows[0][cpe.table.hash_col]">
+         <label>
+         <input type="checkbox" name="checked_cols" ng:format="include:k:gnuplot.cols" ng:click="gnuplot_changed()">
+       {{k}} <tt>{{v}}</tt>
+         </label>
+      </li>
+     </ul>
+    </div>
+
+    <a name="gnuplot"> 
+    <ng:include src="gnuplot.img" ng:show="gnuplot.img" onload="message.gnuplot = gnuplot_onload" ></ng:include>
+
   </div>
 
     <input type=checkbox name=debug value=1>