Browse Source

[impala] Fetch fields of current dashboard

Romain Rigaux 11 years ago
parent
commit
b8c088ef86

+ 55 - 0
apps/impala/src/impala/models.py

@@ -0,0 +1,55 @@
+#!/usr/bin/env python
+# Licensed to Cloudera, Inc. under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  Cloudera, Inc. licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import json
+
+from beeswax.server.dbms import get_query_server_config
+from beeswax.server import dbms
+
+
+class Dashboard():
+  
+  data = json.dumps(
+      {'layout': [
+              {"size":2,"rows":[{"widgets":[{"size":12,"name":"Top salaries","id":"52f07188-f30f-1296-2450-f77e02e1a5c1","widgetType":"facet-widget",
+                   "properties":{},"offset":0,"isLoading":True,"klass":"card card-widget span12"}]}],"drops":["temp"],"klass":"card card-home card-column span2"},
+              {"size":10,"rows":[{"widgets":[
+                  {"size":12,"name":"Grid Results","id":"52f07188-f30f-1296-2450-f77e02e1a5c0","widgetType":"resultset-widget",
+                   "properties":{},"offset":0,"isLoading":True,"klass":"card card-widget span12"}]}],
+              "drops":["temp"],"klass":"card card-home card-column span10"}
+         ],
+      'facets': [{'id': '52f07188-f30f-1296-2450-f77e02e1a5c1', 'label': 'Top salaries', 'field': 'salary', 'widget_type': 'facet-widget', 
+                    'properties': {'limit': 10}}],
+      'properties': [{'database': 'default', 'table': 'sample_07', 'fields': []}]
+  })
+
+  def get_json(self, user):
+    _data = self.get_data()
+    
+    query_server = get_query_server_config(name='impala')
+    db = dbms.get(user, query_server=query_server)
+    
+    _table = db.get_table(_data['properties'][0]['database'], _data['properties'][0]['table'])
+    _data['properties'][0]['fields'] = [dict([('name', col.name), ('type', col.type), ('comment', col.comment)]) for col in _table.cols]
+    
+    return json.dumps(_data)
+ 
+  def get_data(self):
+    return json.loads(self.data)
+
+    
+        

+ 14 - 19
apps/impala/src/impala/views.py

@@ -28,26 +28,18 @@ from desktop.lib.django_util import render
 from beeswax.design import hql_query
 from beeswax.design import hql_query
 from beeswax.server import dbms
 from beeswax.server import dbms
 from beeswax.server.dbms import get_query_server_config
 from beeswax.server.dbms import get_query_server_config
+from impala.models import Dashboard
 
 
 
 
 LOG = logging.getLogger(__name__)
 LOG = logging.getLogger(__name__)
 
 
 
 
 def dashboard(request):
 def dashboard(request):
+  dashboard = Dashboard()
+
   return render('dashboard.mako', request, {
   return render('dashboard.mako', request, {
     'query_json': json.dumps({}),
     'query_json': json.dumps({}),
-    'dashboard_json': json.dumps({'layout': [
-              {"size":2,"rows":[{"widgets":[{"size":12,"name":"Top salaries","id":"52f07188-f30f-1296-2450-f77e02e1a5c1","widgetType":"facet-widget",
-                   "properties":{},"offset":0,"isLoading":True,"klass":"card card-widget span12"}]}],"drops":["temp"],"klass":"card card-home card-column span2"},
-              {"size":10,"rows":[{"widgets":[
-                  {"size":12,"name":"Grid Results","id":"52f07188-f30f-1296-2450-f77e02e1a5c0","widgetType":"resultset-widget",
-                   "properties":{},"offset":0,"isLoading":True,"klass":"card card-widget span12"}]}],
-              "drops":["temp"],"klass":"card card-home card-column span10"}
-         ],
-        'facets': [{'id': '52f07188-f30f-1296-2450-f77e02e1a5c1', 'label': 'Top salaries', 'field': 'salary', 'widget_type': 'facet-widget', 
-                    'properties': {'limit': 10}}],
-        'properties': [{'database': 'default', 'table': 'sample_07'}]
-        }), 
+    'dashboard_json': dashboard.get_json(request.user), 
   })
   })
 
 
 
 
@@ -56,9 +48,13 @@ def query(request):
     'status': -1,
     'status': -1,
     'data': {}
     'data': {}
   }
   }
-    
+  
+  dashboard = json.loads(request.POST['dashboard'])  
   fqs = json.loads(request.POST['query'])['fqs']
   fqs = json.loads(request.POST['query'])['fqs']
 
 
+  database = dashboard['properties'][0]['database']
+  table = dashboard['properties'][0]['table']
+  
   if fqs:
   if fqs:
     filters = ' AND '.join(['%s = %s' % (fq['field'], value) for fq in fqs for value in fq['filter']])
     filters = ' AND '.join(['%s = %s' % (fq['field'], value) for fq in fqs for value in fq['filter']])
   else:
   else:
@@ -66,10 +62,10 @@ def query(request):
 
 
   if 'facet' in request.POST: 
   if 'facet' in request.POST: 
     facet = json.loads(request.POST['facet'])
     facet = json.loads(request.POST['facet'])
-    database = 'default'
-    table = 'sample_07'  
     hql = "SELECT %(field)s FROM %(database)s.%(table)s WHERE %(field)s IS NOT NULL %(filters)s ORDER BY %(field)s DESC LIMIT %(limit)s" % {
     hql = "SELECT %(field)s FROM %(database)s.%(table)s WHERE %(field)s IS NOT NULL %(filters)s ORDER BY %(field)s DESC LIMIT %(limit)s" % {
-        'database': database, 'table': table, 'limit': facet['properties']['limit'],
+        'database': database,
+        'table': table,
+        'limit': facet['properties']['limit'],
         'field': facet['field'],
         'field': facet['field'],
         'filters': (' AND ' + filters) if filters else ''
         'filters': (' AND ' + filters) if filters else ''
     }
     }
@@ -78,15 +74,14 @@ def query(request):
     fields = [fq['field'] for fq in fqs]
     fields = [fq['field'] for fq in fqs]
     result['selected'] = facet['field'] in fields
     result['selected'] = facet['field'] in fields
   else:
   else:
-    database = 'default'
-    table = 'sample_07'    
     hql = "SELECT * FROM %s.%s" % (database, table,)
     hql = "SELECT * FROM %s.%s" % (database, table,)
     if filters:
     if filters:
       hql += ' WHERE ' + filters
       hql += ' WHERE ' + filters
 
 
   query_server = get_query_server_config(name='impala')
   query_server = get_query_server_config(name='impala')
-  query = hql_query(hql)
   db = dbms.get(request.user, query_server=query_server)
   db = dbms.get(request.user, query_server=query_server)
+  
+  query = hql_query(hql)
   handle = db.execute_and_wait(query, timeout_sec=5.0)
   handle = db.execute_and_wait(query, timeout_sec=5.0)
 
 
   if handle:
   if handle:

+ 5 - 3
apps/impala/static/js/impala-dashboard.ko.js

@@ -99,12 +99,14 @@ var Dashboard = function (vm, dashboard) {
   var self = this;
   var self = this;
   
   
   self.facets = ko.mapping.fromJS(dashboard.facets);
   self.facets = ko.mapping.fromJS(dashboard.facets);
-  self.properties = ko.observable(dashboard.properties);
+  self.properties = ko.mapping.fromJS(dashboard.properties);
 
 
 
 
-  self.fields = ko.mapping.fromJS([{'name': 'code'}, {'name': 'description'}, {'name': 'total_emp'}, {'name': 'salary'}]);
+  self.fields = ko.computed(function () {
+	return self.properties()[0].fields();
+  });
   self.fieldNames = ko.computed(function () {
   self.fieldNames = ko.computed(function () {
-	  return $.map(self.fields(), function (field) { return field.name()});
+	return $.map(self.fields(), function (field) { return field.name()});
   });
   });
 
 
   self.resultsetShowFieldList = ko.observable(true);
   self.resultsetShowFieldList = ko.observable(true);