display last ping for user
[APKPM.git] / public / user.html
1 <!doctype html>
2 <html xmlns:ng="http://angularjs.org">
3  <script src="http://code.angularjs.org/angular-0.9.17.min.js" ng:autobind></script>
4  <body>
5   <script>
6     function user($xhr,$resource,$log) {
7         var self = this;
8    
9         self.r = {};
10
11         self.r.CRM_search = $resource('/g/CRM_search/:username', {username:'@search_username'});
12         self.CRM_search = function(username) {
13                 if ( ! username ) username = self.search_username;
14                 $log.info( 'CRM_search', username );
15                 self.clear();
16                 self.message.CRM = 'search for '+username;
17                 self.r.CRM_search.query({username: username}, function(CRM) {
18                         self.CRM = CRM;
19                         $log.info( 'CRM', CRM )
20                         if ( ! CRM.length ) {
21                                 self.message.CRM = 'no results';
22                                 return;
23                         }
24                         self.message.CRM = null;
25                         if ( CRM.length == 1 ) {
26                                 self.selected_username = CRM[0].USERNAME;
27                                 $log.info( 'auto-selected ', self.selected_username );
28                         }
29                 });
30         };
31
32         self.r.LDAP_search = $resource('/g/LDAP_search/:username', {username:'@selected_username'});
33         self.LDAP_search = function(username) {
34                 if ( self.selected_username == username ) {
35                         $log.error("LDAP_search - not changed", username);
36                         return;
37                 }
38
39                 if ( ! username ) username = self.selected_username;
40
41                 if ( ! username ) {
42                         self.message.LDAP = 'no username';
43                         return;
44                 }
45                 $log.info( 'LDAP_search', username );
46                 self.clear_LDAP();
47                 self.selected_username = username;
48                 self.message.LDAP = 'search '+username;
49                 self.r.LDAP_search.query({ username: username }, function(LDAP) {
50                         self.LDAP = LDAP;
51                         $log.info( 'LDAP', LDAP );
52                         if ( self.LDAP.length == 0 ) {
53                                 self.message.LDAP ='no results for '+username;
54                                 return;
55                         }
56                         self.cpe.parser = LDAP[0]._cpe_parser;
57                         self.cpe.table_name  = 'cpe_' + LDAP[0]._cpe_parser;
58                         self.username = LDAP[0].cn;
59                         self.message.LDAP = null;
60                         $log.info( 'cpe', self.cpe );
61                 });
62         };
63         self.$watch('selected_username', self.LDAP_search );
64
65         self.gnuplot_draw = function(h_cols) {
66                 $log.info( 'gnuplot_draw', h_cols );
67                 if (h_cols.length) {
68                         self.gnuplot.cols = h_cols;
69                 } else {
70                         $log.warn("no columns for gnuplot");
71                         return;
72                 }
73
74                 self.message.gnuplot = 'redrawing '+h_cols.join(' ');
75
76                 var cols = [ 'timestamp' ];
77                 for ( var i = 0; i < h_cols.length; i++ ) {
78                         cols.push( 'h->\'' + h_cols[i] + '\' as "' + h_cols[i] + '"' );
79                 }
80 console.debug( cols );
81                 self.gnuplot.sql = 'select ' + cols.join(',') + ' from cpe_' + self.cpe.parser + ' where username = \'' + self.username + '\' order by timestamp desc limit 100';
82
83                 $log.info( 'gnuplot', self.gnuplot );
84                 self.gnuplot.img = '/gnuplot?hide=1;with=points;sql=' + self.gnuplot.sql;
85
86         };
87
88         self.r.table = $resource('/table/:table');
89         self.table_update = function () {
90                 if ( ! self.username || ! self.cpe.table_name ) return;
91
92                 self.message.ping = self.username+' loading';
93                 self.r.table.get({ username: self.username, table: 'ping', limit: 1 }, function(ping) {
94                         $log.info('ping', ping);
95                         self.message.ping = null;
96                         var hash = {};
97                         for(var i = 0; i <= ping.columns.length; i++) {
98                                 hash[ping.columns[i]] = ping.rows[0][i];
99                         }
100                         self.cpe.ping = hash;
101                         $log.info('ping hash', hash);
102                 });
103
104                 self.message.table = self.username + ' loading from ' + self.cpe.table_name;
105                 self.r.table.get({ username: self.username, table: self.cpe.table_name, limit: self.cpe_limit }, function(table) {
106                         $log.info( 'table', table );
107                         self.cpe.table = table;
108                         self.message.table = null;
109                         if ( table.rows.length == 0 ) {
110                                 self.message.table = 'no results for '+self.username;
111                                 return;
112                         }
113
114                         if ( h_cols = self.cpe_hash[self.cpe.parser] ) {
115                                 self.gnuplot_draw( h_cols );
116                         } else {
117                                 self.message.gnuplot = 'no graph for ' + self.cpe.parser;
118                         }
119                 });
120         };
121         self.$watch('username', self.table_update );
122  
123         this.clear = function() {
124                 $log.info('clear');
125                 self.CRM = null;
126                 this.clear_LDAP();
127         }
128
129         this.clear_LDAP = function() {
130                 $log.info('clear_LDAP');
131                 self.LDAP = null;
132                 self.selected_username = null;
133                 self.gnuplot = {};
134                 self.cpe = {};
135                 self.cpe_limit = 1;
136         };
137
138         self.message = {};
139
140         self.$watch('selected_username', function() {
141                 if ( self.selected_username == null ) self.clear_LDAP();
142         });
143
144         self.clear();
145         self.$watch('cpe_limit', function() {
146                 $log.info( 'cpe_limit', self.cpe_limit );
147                 self.table_update();
148         });
149
150         this.columns = [
151                 'USERNAME',
152                 'BROJ',
153                 'USLUGA',
154                 'IP_MANAGEMENT',
155                 'IP_VOICE',
156                 'KOLOKACIJA',
157                 'OPT82'
158         ];
159
160         this.cpe_hash = {
161                 'Davolink': [ 'Max_down', 'Max_up' ],
162                 'EasyGateway': [ 'upstreamCurrRate', 'upstreamMaxRate', 'upstreamNoiseMargin' ],
163         };
164
165       this.keys = function(h) {
166              if ( angular.isArray(h) ) h = h[0];
167              var keys = [];
168              for(i in h) if (h.hasOwnProperty(i))
169              {
170                keys.push(i);
171              }
172              return keys;
173       };
174
175     }
176
177     user.$inject = ['$xhr','$resource','$log'];
178
179 // http://jsfiddle.net/gronky/cLEck/
180 angular.formatter('include', {
181     parse: function(apply, value, list) {
182         angular.Array[apply ? 'add' : 'remove'](list, value);
183         return apply;
184     },
185     format: function(apply, value, list) {
186         return angular.Array.indexOf(list, value) != -1;
187     },
188 });
189
190   </script>
191
192 <style type="text/css">
193
194 table {
195         border-collapse:collapse;
196 }
197
198 th {
199         border-bottom: 2px solid gray;
200 }
201
202 th, td {
203         border-left: 1em solid white;
204         border-right: 1em solid white;
205 }
206
207 .zebra {
208         background: #eee;
209 }
210
211 #message {
212         padding: 3px;
213         background: #ff8;
214
215         margin-left: 1em;
216         position: fixed;
217 }
218
219 .panel_right {
220         z-index: 10;
221         position: absolute;
222         right: 0;
223         background: #ffc;
224         padding: 3px;
225 }
226
227 .panel_right ul {
228         padding: 0;
229 }
230
231 .panel_right ul > li {
232         list-style-type: none;
233 }
234
235 .panel_right > label {
236         float: right;
237 }
238
239 li > tt {
240         font-weight: bold;
241 }
242
243 </style>
244
245   <div ng:controller="user">
246
247    <form ng:submit="CRM_search()">
248     <label for="args">username:
249     <input type="text" name="search_username" placeholder="test" size="10" autofocus ng:required />
250     </label>
251     <input type="submit" value="search in CRM">
252     <input type="reset"  ng:click="clear()" value="clear">
253
254     <span id="message" ng:show="message.$size()" ng:click="message={}" title="click to close">
255       <div ng:repeat="(category,status) in message" ng:show="status"><b>{{category}}</b> {{status}}</div>
256     </span>
257
258    </form>
259
260
261 <div class="panel_right" ng:show="CRM">
262  <label>
263   Add columns
264   <input type=checkbox name=show_columns>
265  </label>
266  <ul ng:show="show_columns">
267   <li ng:repeat="c in keys(CRM)" ng:show="columns.indexOf(c) < 0" ng:click="columns.push(c)">{{c}}
268  </ul>
269 </div>
270
271     <table ng:show="CRM">
272     <tr>
273         <th ng:repeat="c in columns" ng:click="columns.$remove(c)">{{c}}</th>
274     </tr>
275     <tr ng:repeat="u in CRM" ng:click="LDAP_search(u.USERNAME)" ng:class-even="'zebra'">
276         <td ng:repeat="c in columns" ng:show="! selected_username || selected_username == u.USERNAME">{{u[c]}}</td>
277     </tr>
278     </table>
279
280     <input type=button value="Show ALL results, not just {{selected_username}}" ng:click="selected_username=''" ng:show="selected_username">
281
282   <div ng:show="selected_username">
283
284     <h2><tt>{{selected_username}}</tt> LDAP entry</h2>
285
286     <ul ng:show="LDAP">
287      <li ng:repeat="c in keys(LDAP)"><tt>{{c}}</tt> {{LDAP[0][c]}}</li>
288     </ul>
289
290     <div ng:show="cpe.ping">
291      ping <tt>{{cpe.ping.ip}}</tt> rtt <tt>{{cpe.ping.rtt}}</tt> at <tt>{{cpe.ping.timestamp}}</tt>
292     </div>
293
294    <div class="panel_right" ng:show="cpe.table">
295     <label>
296      Graph columns
297      <input type=checkbox name=show_gnuplot_cols>
298     </label>
299     <ul ng:show="show_gnuplot_cols">
300      <input type=button ng:click="gnuplot_draw(gnuplot.cols)" value="Redraw graph">
301      <li ng:repeat="(k,v) in cpe.table.rows[0][cpe.table.hash_col]">
302         <label>
303         <input type="checkbox" name="gnuplot_cols" ng:format="include:k:gnuplot.cols" ng:change="$log.debug(gnuplot_cols)">
304         {{k}} <tt>{{v}}</tt>
305         </label>
306      </li>
307      <input type=button ng:click="gnuplot_draw(gnuplot.cols)" value="Redraw graph">
308     </ul>
309    </div>
310
311     <ng:include src="gnuplot.img" ng:show="gnuplot.img" onload="message.gnuplot = null" ></ng:include>
312
313     <h2 ng:show="cpe.table_name">{{cpe.table_name}} {{username}}</h2>
314
315     <table ng:show="cpe.table">
316      <tr>
317       <th ng:repeat="c in cpe.table.columns">{{c}}</th>
318      </tr>
319      <tr ng:repeat="r in cpe.table.rows" ng:class-even="'zebra'">
320       <td ng:repeat="v in r">{{v}}</td>
321      </tr>
322      <tr>
323       <td colspan="{{cpe.table.columns.length}}">
324       limit:
325         <label>1<input name="cpe_limit" type="radio" value=1></label>
326         &middot; &middot; &middot;
327         <label>5<input name="cpe_limit" type="radio" value=5></label>
328         &middot; &middot; &middot;
329         <label>10<input name="cpe_limit" type="radio" value=10></label>
330       </td>
331     </tr>
332     </table>
333
334   </div>
335
336     <input type=checkbox name=debug value=1>
337     <pre ng:show=debug>
338 CRM={{CRM}}
339 LDAP={{LDAP}}
340 cpe={{cpe}}
341 gnuplot={{gnuplot}}
342     </pre>
343   </div>
344  </body>
345 </html>