specify just hash names and generate SQL from it
authorDobrica Pavlinusic <dpavlin@rot13.org>
Sat, 9 Jul 2011 19:50:18 +0000 (21:50 +0200)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Sat, 9 Jul 2011 19:50:18 +0000 (21:50 +0200)
templates/user.html.ep

index caea7d3..1aa71cf 100644 (file)
                'OPT82'
        ];
 
+       this.cpe_hash = {
+               'Davolink': [ 'Max_down', 'Max_up' ],
+//             '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' ],
+//             'EasyGateway':
+       };
+
       this.keys = function(h) {
              if ( angular.isArray(h) ) h = h[0];
             var keys = [];
                        , '/g/LDAP_search/' + self.selected_username + '?callback=JSON_CALLBACK'
                        , function(code, response) {
                                console.debug( code, response );
+                               if ( ! response[0] ) {
+                                       self.message = 'no user ' + self.selected_username + ' in LDAP';
+                                       return;
+                               }
                                self.ldap = response;
-                               self.gnuplot.sql = 'select timestamp,h->\'Max_down\' as Max_down,h->\'Max_up\' as Max_up from cpe_Davolink where username = \'' + response[0].cn + '\' order by timestamp desc limit 100';
-                               self.gnuplot.img = '/gnuplot?hide=1;with=points;sql=' + self.gnuplot.sql;
-                               console.info( 'gnuplot', self.gnuplot );
-                               self.message = 'generating ADSL graph for '+response[0].cn + ' ';
+                               var parser = response[0]._cpe_parser;
+                               console.info( 'parser', parser );
+                               if ( h_cols = self.cpe_hash[parser] ) {
+                                       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_' + parser + ' where username = \'' + response[0].cn + '\' order by timestamp desc limit 100';
+
+                                       self.gnuplot.img = '/gnuplot?hide=1;with=points;sql=' + self.gnuplot.sql;
+                                       console.info( 'gnuplot', self.gnuplot );
+                                       self.message = 'generating ADSL graph for '+response[0].cn + ' ';
+                               } else {
+                                       self.message = 'no graph defined for ' + parser;
+                               }
                        }
                );
                self.message = 'LDAP search for '+self.selected_username;