workload_analytics_api.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #!/usr/bin/env python
  2. # Licensed to Cloudera, Inc. under one
  3. # or more contributor license agreements. See the NOTICE file
  4. # distributed with this work for additional information
  5. # regarding copyright ownership. Cloudera, Inc. licenses this file
  6. # to you under the Apache License, Version 2.0 (the
  7. # "License"); you may not use this file except in compliance
  8. # with the License. You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. import logging
  18. import json
  19. from django.utils.translation import ugettext as _
  20. from django.views.decorators.http import require_POST
  21. from desktop.lib.django_util import JsonResponse
  22. from desktop.lib.i18n import force_unicode
  23. from metadata.workload_analytics_client import WorkfloadAnalyticsClient
  24. LOG = logging.getLogger(__name__)
  25. def error_handler(view_fn):
  26. def decorator(*args, **kwargs):
  27. try:
  28. return view_fn(*args, **kwargs)
  29. except Exception, e:
  30. LOG.exception(e)
  31. response = {
  32. 'status': -1,
  33. 'message': force_unicode(e)
  34. }
  35. return JsonResponse(response, status=500)
  36. return decorator
  37. @require_POST
  38. @error_handler
  39. def get_impala_query(request):
  40. response = {'status': -1}
  41. cluster_id = json.loads(request.POST.get('cluster_id'))
  42. query_id = json.loads(request.POST.get('query_id'))
  43. client = WorkfloadAnalyticsClient(request.user)
  44. data = client.get_impala_query(cluster_id=cluster_id, query_id=query_id)
  45. if data:
  46. response['status'] = 0
  47. response['data'] = data
  48. else:
  49. response['message'] = 'Workload Analytics: %s' % data['details']
  50. return JsonResponse(response)
  51. @require_POST
  52. @error_handler
  53. def get_cluster_id(request):
  54. response = {'status': -1}
  55. crn = json.loads(request.POST.get('crn'))
  56. client = WorkfloadAnalyticsClient(request.user)
  57. data = client.list_uploads()
  58. if data:
  59. env = [_env for _env in data['environments'] if _env['crn'] == crn]
  60. if not env:
  61. response['message'] = 'Workload Analytics: %s environment not found' % crn
  62. else:
  63. response['status'] = 0
  64. response['data'] = env[0]
  65. else:
  66. response['message'] = 'Workload Analytics: %s' % data['details']
  67. return JsonResponse(response)
  68. @require_POST
  69. @error_handler
  70. def get_environment(request):
  71. response = {'status': -1}
  72. crn = json.loads(request.POST.get('crn'))
  73. client = WorkfloadAnalyticsClient(request.user)
  74. data = client.list_environments()
  75. if data:
  76. env = [_env for _env in data['environments'] if _env['crn'] == crn]
  77. if not env:
  78. response['message'] = 'Workload Analytics: %s environment not found' % crn
  79. else:
  80. response['status'] = 0
  81. response['data'] = env[0]
  82. else:
  83. response['message'] = 'Workload Analytics: %s' % data['details']
  84. return JsonResponse(response)
  85. @require_POST
  86. @error_handler
  87. def get_operation_execution_details(request):
  88. response = {'status': -1}
  89. operation_id = json.loads(request.POST.get('operation_id'))
  90. client = WorkfloadAnalyticsClient(request.user)
  91. data = client.get_operation_execution_details(operation_id=operation_id)
  92. if data:
  93. response['status'] = 0
  94. response['data'] = data
  95. else:
  96. response['message'] = 'Workload Analytics: %s' % data['details']
  97. return JsonResponse(response)