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