Explorar o código

HUE-8805 [sql] Add basic Query Analytics reporting

Romain Rigaux %!s(int64=6) %!d(string=hai) anos
pai
achega
25ce120af8

+ 3 - 6
apps/beeswax/src/beeswax/models.py

@@ -17,8 +17,8 @@
 
 import base64
 import datetime
-import logging
 import json
+import logging
 
 from django.db import models
 from django.contrib.auth.models import User
@@ -27,15 +27,12 @@ from django.urls import reverse
 from django.utils.translation import ugettext as _, ugettext_lazy as _t
 
 from enum import Enum
-
-from librdbms.server import dbms as librdbms_dbms
+from TCLIService.ttypes import TSessionHandle, THandleIdentifier, TOperationState, TOperationHandle, TOperationType
 
 from desktop.redaction import global_redaction_engine
 from desktop.lib.exceptions_renderable import PopupException
 from desktop.models import Document
-
-from TCLIService.ttypes import TSessionHandle, THandleIdentifier,\
-  TOperationState, TOperationHandle, TOperationType
+from librdbms.server import dbms as librdbms_dbms
 
 from beeswax.design import HQLdesign
 

+ 38 - 0
desktop/libs/notebook/src/notebook/management/commands/send_query_stats.py

@@ -0,0 +1,38 @@
+
+#!/usr/bin/env python
+## -*- coding: utf-8 -*-
+# 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.
+
+from django.core import mail
+from django.core.management.base import BaseCommand
+
+
+from notebook.models import Anaytics
+
+
+class Command(BaseCommand):
+  args = '<No args>'
+  help = 'Send analytical stuff.'
+
+  def handle(self, *args, **options):
+    print('Starting...')
+    stats = Anaytics.admin_stats()
+    msg = '\n\n'.join(['%s\t=\t%s' % stat for stat in stats])
+
+    mail.mail_admins(subject='Query Stats', message=msg, fail_silently=True)
+
+    print('Done.')

+ 26 - 0
desktop/libs/notebook/src/notebook/models.py

@@ -15,6 +15,7 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+import datetime
 import json
 import logging
 import math
@@ -22,9 +23,13 @@ import numbers
 import urllib
 import uuid
 
+from datetime import timedelta
+
+from django.contrib.auth.models import User
 from django.utils.html import escape
 
 from desktop.lib.i18n import smart_unicode
+from desktop.models import Document2
 
 from notebook.connectors.base import Notebook
 
@@ -459,3 +464,24 @@ def _update_property_value(properties, key, value):
   for prop in properties:
     if prop['key'] == key:
       prop.update({'value': value})
+
+
+class Anaytics():
+
+  @classmethod
+  def admin_stats(self):
+    stats = []
+    one_week = datetime.date.today() - timedelta(weeks=1)
+    one_month = datetime.date.today() - timedelta(days=30)
+    three_months = datetime.date.today() - timedelta(days=90)
+
+    stats.append(('Last modified', '1 week'))
+    stats.append(('Users', User.objects.filter(last_login__gte=one_week).count()))
+    stats.append(('Executed queries', Document2.objects.filter(last_modified__gte=one_week, is_history=True, type__startswith='query-').count()))
+    stats.append(('Saved queries', Document2.objects.filter(last_modified__gte=one_week, is_history=False, type__startswith='query-').count()))
+
+    stats.append(('\nAll', ''))
+    stats.append(('Active users 30 days', User.objects.filter(last_login__gte=one_month).count()))
+    stats.append(('Active users 90 days', User.objects.filter(last_login__gte=three_months).count()))
+
+    return stats