瀏覽代碼

[metastore] Adding a related queries tab on table page

Romain Rigaux 10 年之前
父節點
當前提交
9690149e8a

+ 14 - 4
apps/metastore/src/metastore/templates/describe_table.mako

@@ -134,7 +134,6 @@ ${ require.config() }
     <div class="vertical-full">
       <div class="vertical-full row-fluid panel-container">
 
-
         <div class="assist-container left-panel" data-bind="visible: $root.isLeftPanelVisible() && $root.assistAvailable()">
           <a title="${_('Toggle Assist')}" class="pointer hide-assist" data-bind="click: function() { $root.isLeftPanelVisible(false) }">
             <i class="fa fa-chevron-left"></i>
@@ -187,6 +186,7 @@ ${ require.config() }
                 <li><a href="#sample" data-toggle="tab">${_('Sample')}</a></li>
               % endif
               <li><a href="#permissions" data-toggle="tab">${_('Permissions')}</a></li>
+              <li><a href="#queries" data-toggle="tab">${_('Queries')}</a></li>
               <li><a href="#analysis" data-toggle="tab">${_('Analyse')}</a></li>
               <li><a href="#lineage" data-toggle="tab">${_('Lineage')}</a></li>
               <li><a href="#properties" data-toggle="tab">${ _('Properties') }</a></li>
@@ -230,6 +230,9 @@ ${ require.config() }
                     ${ column_table(table.cols, "columnTable", True, 3) }
                     ${_('View more...')}
                   </div>
+                  <div>
+                    ${ _('Comments') } <i class="fa fa-comments-o"></i></a>
+                  </div>                  
                 </div>
 
                 <div class="tile">
@@ -272,6 +275,10 @@ ${ require.config() }
               </div>
               % endif
 
+              <div class="tab-pane" id="queries">
+                <pre id="queriesTable"></pre>
+              </div>
+
               <div class="tab-pane" id="properties">
                 <table class="table table-striped table-condensed">
                   <thead>
@@ -296,7 +303,6 @@ ${ require.config() }
 
           </div>
 
-
         </div>
     </div>
   </div>
@@ -416,8 +422,6 @@ var STATS_PROBLEMS = "${ _('There was a problem loading the stats.') }";
   });
 
   $(document).ready(function () {
-
-
     function selectColumn(col) {
       var _t = $("#sampleTable");
       var _col = _t.find("th").filter(function() {
@@ -447,6 +451,12 @@ var STATS_PROBLEMS = "${ _('There was a problem loading the stats.') }";
         });
     % endif
 
+    
+    // Lazy loading?
+    $.getJSON("${ url('metastore:table_queries', database=database, table=table.name) }", function (data) {
+      $("#queriesTable").text(data.queries);
+    });
+
     $('a[data-toggle="tab"]').on('shown', function (e) {
       var sortables = [];
       $(".sampleTable").not('.initialized').each(function () {

+ 1 - 0
apps/metastore/src/metastore/urls.py

@@ -30,6 +30,7 @@ urlpatterns = patterns('metastore.views',
   url(r'^table/(?P<database>\w+)/(?P<table>\w+)/metadata$', 'get_table_metadata', name='get_table_metadata'),
   url(r'^table/(?P<database>\w+)/(?P<table>\w+)/load$', 'load_table', name='load_table'),
   url(r'^table/(?P<database>\w+)/(?P<table>\w+)/read$', 'read_table', name='read_table'),
+  url(r'^table/(?P<database>\w+)/(?P<table>\w+)/queries$', 'table_queries', name='table_queries'),
   url(r'^table/(?P<database>\w+)/(?P<table>\w+)/partitions$', 'describe_partitions', name='describe_partitions'),
   url(r'^table/(?P<database>\w+)/(?P<table>\w+)/partitions/(?P<partition_spec>.+?)/read$', 'read_partition', name='read_partition'),
   url(r'^table/(?P<database>\w+)/(?P<table>\w+)/partitions/(?P<partition_spec>.+?)/browse$', 'browse_partition', name='browse_partition'),

+ 19 - 0
apps/metastore/src/metastore/views.py

@@ -19,6 +19,7 @@ import json
 import logging
 import urllib
 
+from django.db.models import Q
 from django.core.urlresolvers import reverse
 from django.shortcuts import redirect
 from django.utils.functional import wraps
@@ -28,6 +29,7 @@ from django.views.decorators.http import require_http_methods
 from desktop.context_processors import get_app_name
 from desktop.lib.django_util import JsonResponse, render
 from desktop.lib.exceptions_renderable import PopupException
+from desktop.models import Document, Document2
 
 from beeswax.design import hql_query
 from beeswax.models import SavedQuery, MetaInstall
@@ -43,6 +45,7 @@ LOG = logging.getLogger(__name__)
 
 SAVE_RESULTS_CTAS_TIMEOUT = 300         # seconds
 
+
 def check_has_write_access_permission(view_func):
   """
   Decorator ensuring that the user is not a read only user.
@@ -114,6 +117,22 @@ def get_database_metadata(request, database):
   return JsonResponse(response)
 
 
+def table_queries(request, database, table):
+  qfilter = Q(data__icontains=table) | Q(data__icontains='%s.%s' % (database, table))
+
+  response = {'status': -1, 'queries': []}
+  try:
+    queries = [d.to_dict()
+              for d in Document2.objects.filter(qfilter, owner=request.user, type='query', is_history=False)[:50]]
+    response['status'] = 0
+    response['queries'] = queries
+  except Exception, ex:
+    response['status'] = 1
+    response['data'] = _("Cannot get queries related to table %s.%s: %s") % (database, table, ex)
+
+  return JsonResponse(response)
+
+
 """
 Table Views
 """