move user.html template to static one
[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 CRM($xhr) {
7       var self = this;
8    
9       this.fetch = function() {
10         self.clear();
11         if ( ! self.args ) {
12                 self.message = 'Enter part of username to search';
13                 return;
14         }
15         self.url = '/g/' + self.function_name + '/' + self.args + '?callback=JSON_CALLBACK';
16         $xhr('JSON', self.url, function(code, response) {
17           self.code = code;
18           self.response = response;
19           self.message = response.length + ' results for <tt>'+self.args+'</tt>';
20         });
21         self.message = 'loading '+self.url;
22       };
23  
24       this.clear = function() {
25         self.url = null;
26         self.code = null;
27         self.response = 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                                         self.cpe.rows = table.rows;
82                                         self.message = 'loaded cpe_'+self.cpe.parser+' rows';
83                                 });
84                                 self.message = 'loading cpe_'+self.cpe.parser+' rows...';
85
86                                 if ( h_cols = self.cpe_hash[self.cpe.parser] ) {
87                                         var cols = [ 'timestamp' ];
88                                         for ( var i = 0; i < h_cols.length; i++ ) {
89                                                 cols.push( 'h->\'' + h_cols[i] + '\' as "' + h_cols[i] + '"' );
90                                         }
91 console.debug( cols );
92                                         self.gnuplot.sql = 'select ' + cols.join(',') + ' from cpe_' + self.cpe.parser + ' where username = \'' + self.username + '\' order by timestamp desc limit 100';
93
94                                         console.info( 'gnuplot', self.gnuplot );
95                                         self.message = 'generating ADSL graph for '+self.username;
96                                         self.gnuplot.img = '/gnuplot?hide=1;with=points;sql=' + self.gnuplot.sql;
97                                 } else {
98                                         self.message = 'no graph defined for ' + self.cpe.parser;
99                                 }
100
101                         }
102                 );
103                 self.message = 'LDAP search for '+self.selected_username;
104         };
105
106     }
107
108     CRM.$inject = ['$xhr'];
109   </script>
110
111 <style type="text/css">
112 #columns {
113         position: fixed;
114         top: 0;
115         right: 0;
116         z-index: 10;
117         background: #eee;
118 }
119
120 #columns > span {
121         float: right;
122 }
123
124 #columns ul {
125         display: none;
126 }
127
128 #columns:hover ul {
129         display: block;
130 }
131
132 table {
133         border-collapse:collapse;
134 }
135
136 th {
137         border-bottom: 2px solid gray;
138 }
139
140 th, td {
141         border-left: 1em solid white;
142         border-right: 1em solid white;
143 }
144
145 .zebra {
146         background: #eee;
147 }
148
149 #message {
150         padding: 3px;
151         background: #ff8;
152
153         margin-left: 1em;
154         position: fixed;
155 }
156
157 </style>
158
159   <div ng:controller="CRM">
160
161    <form ng:submit="fetch()">
162     <input type="hidden" name="function_name" value="CRM_search"      size="20"/>
163     <label for="args">CRM username:
164     <input type="text" name="args"     placeholder="test" size="10"/>
165     </label>
166     <input type="submit" ng:click="fetch()" value="search">
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="response">
174 <span>Add columns</span>
175 <ul>
176 <li ng:repeat="c in keys(response)" ng:show="columns.indexOf(c) < 0" ng:click="columns.push(c)">{{c}}
177 </ul>
178 </div>
179
180     <table ng:show="response">
181     <tr>
182         <th ng:repeat="c in columns" ng:click="columns.$remove(c)">{{c}}</th>
183     </tr>
184     <tr ng:repeat="u in response" 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     <pre ng:show="gnuplot.sql">{{gnuplot.sql}}</pre>
206
207   </div>
208
209     <input type=checkbox name=debug value=1>
210     <pre ng:show=debug>
211 {{self}}
212     </pre>
213   </div>
214  </body>
215 </html>