96d44cc7780f9e24a36a3fbc92cfe95bda6c597d
[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.12.min.js" ng:autobind></script>
4  <body>
5   <script>
6     function user($xhr,$resource,$log) {
7         var self = this;
8    
9         self.r = {
10                 CRM_search: $resource('/g/CRM_search/:username', {username:'@search_username'}),
11         };
12         self.CRM_search = function(username) {
13                 if ( ! username ) username = self.search_username;
14                 $log.info( 'CRM_search', username );
15                 self.r.CRM_search.query({username: username}, function(CRM) {
16                         self.CRM = CRM;
17                         $log.info( 'CRM', CRM )
18                         if ( CRM.length == 1 ) {
19                                 self.selected_username = CRM[0].USERNAME;
20                                 $log.info( 'auto-selected', self.selected_username );
21                         }
22                 });
23         };
24  
25       this.clear = function() {
26         self.url = null;
27         self.code = null;
28         self.selected_username = null;
29         self.gnuplot = {};
30         self.ldap = null;
31         self.message = null;
32       };
33
34         this.columns = [
35                 'USERNAME',
36                 'BROJ',
37                 'USLUGA',
38                 'IP_MANAGEMENT',
39                 'IP_VOICE',
40                 'KOLOKACIJA',
41                 'OPT82'
42         ];
43
44         this.cpe_hash = {
45                 'Davolink': [ 'Max_down', 'Max_up' ],
46 //              'Davolink': [ 'select timestamp,h->\'Max_down\' as Max_down,h->\'Max_up\' as Max_up from cpe_Davolink where username = \'', '\' order by timestamp desc limit 100' ],
47 //              'EasyGateway':
48         };
49
50       this.keys = function(h) {
51              if ( angular.isArray(h) ) h = h[0];
52              var keys = [];
53              for(i in h) if (h.hasOwnProperty(i))
54              {
55                keys.push(i);
56              }
57              return keys;
58       };
59
60         this.select = function(crm) {
61                 console.log( 'crm = ',crm );
62                 self.selected_username = crm.USERNAME;
63                 self.gnuplot = {};
64                 $xhr('JSON'
65                         , '/g/LDAP_search/' + self.selected_username + '?callback=JSON_CALLBACK'
66                         , function(code, response) {
67                                 console.debug( code, response );
68                                 if ( ! response[0] ) {
69                                         self.message = 'no user ' + self.selected_username + ' in LDAP';
70                                         return;
71                                 }
72                                 self.ldap = response;
73                                 self._cpe_parser = response[0]._cpe_parser;
74                                 self.username = response[0].cn;
75                                 self.cpe = {
76                                         parser: self._cpe_parser,
77                                         table: 'cpe_' + self._cpe_parser,
78                                 };
79
80                                 $xhr('JSON', '/table/' + self.cpe.table + '?username='+self.username+';callback=JSON_CALLBACK', function( code, table ) {
81                                         console.debug( self.cpe.table, code, table );
82                                         self.cpe.rows = table.rows;
83                                         self.message = 'loaded cpe_'+self.cpe.parser+' rows';
84                                 });
85                                 self.message = 'loading cpe_'+self.cpe.parser+' rows...';
86
87                                 if ( h_cols = self.cpe_hash[self.cpe.parser] ) {
88                                         var cols = [ 'timestamp' ];
89                                         for ( var i = 0; i < h_cols.length; i++ ) {
90                                                 cols.push( 'h->\'' + h_cols[i] + '\' as "' + h_cols[i] + '"' );
91                                         }
92 console.debug( cols );
93                                         self.gnuplot.sql = 'select ' + cols.join(',') + ' from cpe_' + self.cpe.parser + ' where username = \'' + self.username + '\' order by timestamp desc limit 100';
94
95                                         console.info( 'gnuplot', self.gnuplot );
96                                         self.message = 'generating ADSL graph for '+self.username;
97                                         self.gnuplot.img = '/gnuplot?hide=1;with=points;sql=' + self.gnuplot.sql;
98                                 } else {
99                                         self.message = 'no graph defined for ' + self.cpe.parser;
100                                 }
101
102                         }
103                 );
104                 self.message = 'LDAP search for '+self.selected_username;
105         };
106
107     }
108
109     user.$inject = ['$xhr','$resource','$log'];
110   </script>
111
112 <style type="text/css">
113 #columns {
114         position: fixed;
115         top: 0;
116         right: 0;
117         z-index: 10;
118         background: #eee;
119 }
120
121 #columns > span {
122         float: right;
123 }
124
125 #columns ul {
126         display: none;
127 }
128
129 #columns:hover ul {
130         display: block;
131 }
132
133 table {
134         border-collapse:collapse;
135 }
136
137 th {
138         border-bottom: 2px solid gray;
139 }
140
141 th, td {
142         border-left: 1em solid white;
143         border-right: 1em solid white;
144 }
145
146 .zebra {
147         background: #eee;
148 }
149
150 #message {
151         padding: 3px;
152         background: #ff8;
153
154         margin-left: 1em;
155         position: fixed;
156 }
157
158 </style>
159
160   <div ng:controller="user">
161
162    <form ng:submit="CRM_search()">
163     <label for="args">username:
164     <input type="text" name="search_username" placeholder="test" size="10" ng:required />
165     </label>
166     <input type="submit" value="search in CRM">
167     <input type="reset"  ng:click="clear()" value="clear">
168
169     <span id="message" ng:show="message">{{message | html}}</span>
170
171    </form>
172
173 <div id="columns" ng:show="CRM">
174 <span>Add columns</span>
175 <ul>
176 <li ng:repeat="c in keys(CRM)" ng:show="columns.indexOf(c) < 0" ng:click="columns.push(c)">{{c}}
177 </ul>
178 </div>
179
180     <table ng:show="CRM">
181     <tr>
182         <th ng:repeat="c in columns" ng:click="columns.$remove(c)">{{c}}</th>
183     </tr>
184     <tr ng:repeat="u in CRM" ng:click="select(u)" ng:class-even="'zebra'">
185         <td ng:repeat="c in columns" ng:show="! selected_username || selected_username == u.USERNAME">{{u[c]}}</td>
186     </tr>
187     </table>
188
189     <input type=button value="Show ALL results, not just {{selected_username}}" ng:click="selected_username=''" ng:show="selected_username">
190
191   <div ng:show="selected_username">
192
193     <h2><tt>{{selected_username}}</tt> LDAP entry</h2>
194
195     <ul ng:show="ldap">
196     <li ng:repeat="c in keys(ldap)"><tt>{{c}}</tt> {{ldap[0][c]}}</li>
197     </ul>
198
199     <div ng:show="cpe.rows">
200     <h2>{{cpe.table}} {{username}}</h2>
201     <pre>{{cpe.rows[0]}}</pre>
202     </div>
203
204     <ng:include src="gnuplot.img" ng:show="gnuplot.img" onload="message = 'done'">
205
206   </div>
207
208     <input type=checkbox name=debug value=1>
209     <pre ng:show=debug>
210 CRM={{CRM}}
211 ldap={{ldap}}
212 cpe={{cpe}}
213 gnuplot={{gnuplot}}
214     </pre>
215   </div>
216  </body>
217 </html>