Exemplo n.º 1
0
def _check_status(request, notebook=None, snippet=None, operation_id=None):
    response = {'status': -1}

    if operation_id or not snippet:  # To unify with _get_snippet
        nb_doc = Document2.objects.get_by_uuid(user=request.user,
                                               uuid=operation_id
                                               or notebook['uuid'])
        notebook = Notebook(document=nb_doc).get_data()  # Used below
        snippet = notebook['snippets'][0]

    try:
        response['query_status'] = get_api(request, snippet).check_status(
            notebook, snippet)
        response['status'] = 0
    except SessionExpired:
        response['status'] = 'expired'
        raise
    except QueryExpired:
        response['status'] = 'expired'
        raise
    finally:
        if response['status'] == 0 and snippet['status'] != response[
                'query_status']:
            status = response['query_status']['status']
        elif response['status'] == 'expired':
            status = 'expired'
        else:
            status = 'failed'

        if response.get('query_status'):
            has_result_set = response['query_status'].get('has_result_set')
        else:
            has_result_set = None

        if notebook.get('dialect') or notebook['type'].startswith(
                'query') or notebook.get('isManaged'):
            nb_doc = Document2.objects.get_by_uuid(user=request.user,
                                                   uuid=operation_id
                                                   or notebook['uuid'])
            if nb_doc.can_write(request.user):
                nb = Notebook(document=nb_doc).get_data()
                if status != nb['snippets'][0][
                        'status'] or has_result_set != nb['snippets'][0].get(
                            'has_result_set'):
                    nb['snippets'][0]['status'] = status
                    if has_result_set is not None:
                        nb['snippets'][0]['has_result_set'] = has_result_set
                        nb['snippets'][0]['result']['handle'][
                            'has_result_set'] = has_result_set
                    nb_doc.update_data(nb)
                    nb_doc.save()

    return response
Exemplo n.º 2
0
Arquivo: api.py Projeto: kevinhjk/hue
def get_history(request):
  response = {'status': -1}

  doc_type = request.GET.get('doc_type')
  limit = max(request.GET.get('len', 50), 100)

  response['status'] = 0
  history = []
  for doc in Document2.objects.get_history(doc_type='query-%s' % doc_type, user=request.user).order_by('-last_modified')[:limit]:
    notebook = Notebook(document=doc).get_data()
    if 'snippets' in notebook:
      history.append({
        'name': doc.name,
        'id': doc.id,
        'uuid': doc.uuid,
        'type': doc.type,
        'data': {
            'statement_raw': notebook['snippets'][0]['statement_raw'][:1001],
            'lastExecuted':  notebook['snippets'][0]['lastExecuted'],
            'status':  notebook['snippets'][0]['status'],
            'parentUuid': notebook.get('parentUuid', '')
        } if notebook['snippets'] else {},
        'absoluteUrl': doc.get_absolute_url(),
      })
    else:
      LOG.error('Incomplete History Notebook: %s' % notebook)
  response['history'] = history
  response['message'] = _('History fetched')

  return JsonResponse(response)
Exemplo n.º 3
0
def check_status(request):
    response = {'status': -1}

    operation_id = request.POST.get('operationId')
    notebook = json.loads(request.POST.get('notebook', '{}'))
    snippet = json.loads(request.POST.get('snippet', '{}'))

    if operation_id or not snippet:  # To unify with _get_snippet
        nb_doc = Document2.objects.get_by_uuid(user=request.user,
                                               uuid=operation_id
                                               or notebook['uuid'])
        notebook = Notebook(document=nb_doc).get_data()  # Used below
        snippet = notebook['snippets'][0]

    try:
        with opentracing.tracer.start_span('notebook-check_status') as span:
            span.set_tag('user-id', request.user.username)
            span.set_tag(
                'query-id', snippet['result']['handle']['guid']
                if snippet['result'].get('handle')
                and snippet['result']['handle'].get('guid') else None)
            response['query_status'] = get_api(request, snippet).check_status(
                notebook, snippet)

        response['status'] = 0
    except SessionExpired:
        response['status'] = 'expired'
        raise
    except QueryExpired:
        response['status'] = 'expired'
        raise
    finally:
        if response['status'] == 0 and snippet['status'] != response[
                'query_status']:
            status = response['query_status']['status']
        elif response['status'] == 'expired':
            status = 'expired'
        else:
            status = 'failed'
        if response.get('query_status'):
            has_result_set = response['query_status'].get('has_result_set')
        else:
            has_result_set = None

        if notebook['type'].startswith('query') or notebook.get('isManaged'):
            nb_doc = Document2.objects.get_by_uuid(user=request.user,
                                                   uuid=operation_id
                                                   or notebook['uuid'])
            if nb_doc.can_write(request.user):
                nb = Notebook(document=nb_doc).get_data()
                if status != nb['snippets'][0][
                        'status'] or has_result_set != nb['snippets'][0].get(
                            'has_result_set'):
                    nb['snippets'][0]['status'] = status
                    if has_result_set is not None:
                        nb['snippets'][0]['has_result_set'] = has_result_set
                    nb_doc.update_data(nb)
                    nb_doc.save()

    return JsonResponse(response)
Exemplo n.º 4
0
def get_history(request):
    response = {'status': -1}

    doc_type = request.GET.get('doc_type')
    doc_text = request.GET.get('doc_text')
    page = min(int(request.GET.get('page', 1)), 100)
    limit = min(int(request.GET.get('limit', 50)), 100)
    is_notification_manager = request.GET.get('is_notification_manager',
                                              'false') == 'true'

    if is_notification_manager:
        docs = Document2.objects.get_tasks_history(user=request.user)
    else:
        docs = Document2.objects.get_history(doc_type='query-%s' % doc_type,
                                             user=request.user)

    if doc_text:
        docs = docs.filter(
            Q(name__icontains=doc_text) | Q(description__icontains=doc_text)
            | Q(search__icontains=doc_text))

    # Paginate
    docs = docs.order_by('-last_modified')
    response['count'] = docs.count()
    docs = __paginate(page, limit, queryset=docs)['documents']

    history = []
    for doc in docs:
        notebook = Notebook(document=doc).get_data()
        if 'snippets' in notebook:
            statement = notebook[
                'description'] if is_notification_manager else _get_statement(
                    notebook)
            history.append({
                'name': doc.name,
                'id': doc.id,
                'uuid': doc.uuid,
                'type': doc.type,
                'data': {
                    'statement':
                    statement[:1001] if statement else '',
                    'lastExecuted':
                    notebook['snippets'][0].get('lastExecuted', -1),
                    'status':
                    notebook['snippets'][0]['status'],
                    'parentSavedQueryUuid':
                    notebook.get('parentSavedQueryUuid', '')
                } if notebook['snippets'] else {},
                'absoluteUrl': doc.get_absolute_url(),
            })
        else:
            LOG.error('Incomplete History Notebook: %s' % notebook)
    response['history'] = sorted(history,
                                 key=lambda row: row['data']['lastExecuted'],
                                 reverse=True)
    response['message'] = _('History fetched')
    response['status'] = 0

    return JsonResponse(response)
Exemplo n.º 5
0
Arquivo: api.py Projeto: fuhmdyd/hue
def get_history(request):
    response = {'status': -1}

    doc_type = request.GET.get('doc_type')
    doc_text = request.GET.get('doc_text')
    limit = min(request.GET.get('len', 50), 100)

    docs = Document2.objects.get_history(doc_type='query-%s' % doc_type,
                                         user=request.user)

    if doc_text:
        docs = docs.filter(
            Q(name__icontains=doc_text) | Q(description__icontains=doc_text))

    history = []
    for doc in docs.order_by('-last_modified')[:limit]:
        notebook = Notebook(document=doc).get_data()
        if 'snippets' in notebook:
            try:
                statement = notebook['snippets'][0]['result']['handle'][
                    'statement']
                if type(statement) == dict:  # Old format
                    statement = notebook['snippets'][0]['statement_raw']
            except KeyError:  # Old format
                statement = notebook['snippets'][0]['statement_raw']
            history.append({
                'name': doc.name,
                'id': doc.id,
                'uuid': doc.uuid,
                'type': doc.type,
                'data': {
                    'statement':
                    statement[:1001] if statement else '',
                    'lastExecuted':
                    notebook['snippets'][0]['lastExecuted'],
                    'status':
                    notebook['snippets'][0]['status'],
                    'parentSavedQueryUuid':
                    notebook.get('parentSavedQueryUuid', '')
                } if notebook['snippets'] else {},
                'absoluteUrl': doc.get_absolute_url(),
            })
        else:
            LOG.error('Incomplete History Notebook: %s' % notebook)
    response['history'] = sorted(history,
                                 key=lambda row: row['data']['lastExecuted'],
                                 reverse=True)
    response['message'] = _('History fetched')
    response['status'] = 0

    return JsonResponse(response)
Exemplo n.º 6
0
def get_history(request):
    response = {"status": -1}

    doc_type = request.GET.get("doc_type")
    doc_text = request.GET.get("doc_text")
    limit = min(request.GET.get("len", 50), 100)

    docs = Document2.objects.get_history(doc_type="query-%s" % doc_type, user=request.user)

    if doc_text:
        docs = docs.filter(
            Q(name__icontains=doc_text) | Q(description__icontains=doc_text) | Q(search__icontains=doc_text)
        )

    history = []
    for doc in docs.order_by("-last_modified")[:limit]:
        notebook = Notebook(document=doc).get_data()
        if "snippets" in notebook:
            statement = _get_statement(notebook)
            history.append(
                {
                    "name": doc.name,
                    "id": doc.id,
                    "uuid": doc.uuid,
                    "type": doc.type,
                    "data": {
                        "statement": statement[:1001] if statement else "",
                        "lastExecuted": notebook["snippets"][0]["lastExecuted"],
                        "status": notebook["snippets"][0]["status"],
                        "parentSavedQueryUuid": notebook.get("parentSavedQueryUuid", ""),
                    }
                    if notebook["snippets"]
                    else {},
                    "absoluteUrl": doc.get_absolute_url(),
                }
            )
        else:
            LOG.error("Incomplete History Notebook: %s" % notebook)
    response["history"] = sorted(history, key=lambda row: row["data"]["lastExecuted"], reverse=True)
    response["message"] = _("History fetched")
    response["status"] = 0

    return JsonResponse(response)
Exemplo n.º 7
0
def check_status(request):
    response = {'status': -1}

    notebook = json.loads(request.POST.get('notebook', '{}'))
    snippet = json.loads(request.POST.get('snippet', '{}'))

    if not snippet:
        nb_doc = Document2.objects.get_by_uuid(user=request.user,
                                               uuid=notebook['id'])
        notebook = Notebook(document=nb_doc).get_data()
        snippet = notebook['snippets'][0]

    try:
        response['query_status'] = get_api(request, snippet).check_status(
            notebook, snippet)

        response['status'] = 0
    except SessionExpired:
        response['status'] = 'expired'
        raise
    except QueryExpired:
        response['status'] = 'expired'
        raise
    finally:
        if response['status'] == 0 and snippet['status'] != response[
                'query_status']:
            status = response['query_status']['status']
        elif response['status'] == 'expired':
            status = 'expired'
        else:
            status = 'failed'

        if notebook['type'].startswith('query') or notebook.get('isManaged'):
            nb_doc = Document2.objects.get(id=notebook['id'])
            if nb_doc.can_write(request.user):
                nb = Notebook(document=nb_doc).get_data()
                if status != nb['snippets'][0]['status']:
                    nb['snippets'][0]['status'] = status
                    nb_doc.update_data(nb)
                    nb_doc.save()

    return JsonResponse(response)
Exemplo n.º 8
0
def get_history(request):
  response = {'status': -1}

  doc_type = request.GET.get('doc_type')
  doc_text = request.GET.get('doc_text')
  limit = min(request.GET.get('len', 50), 100)

  docs = Document2.objects.get_history(doc_type='query-%s' % doc_type, user=request.user)

  if doc_text:
    docs = docs.filter(Q(name__icontains=doc_text) | Q(description__icontains=doc_text))

  history = []
  for doc in docs.order_by('-last_modified')[:limit]:
    notebook = Notebook(document=doc).get_data()
    if 'snippets' in notebook:
      try:
        statement = notebook['snippets'][0]['result']['handle']['statement']
        if type(statement) == dict: # Old format
          statement = notebook['snippets'][0]['statement_raw']
      except KeyError: # Old format
        statement = notebook['snippets'][0]['statement_raw']
      history.append({
        'name': doc.name,
        'id': doc.id,
        'uuid': doc.uuid,
        'type': doc.type,
        'data': {
            'statement': statement[:1001] if statement else '',
            'lastExecuted': notebook['snippets'][0]['lastExecuted'],
            'status':  notebook['snippets'][0]['status'],
            'parentSavedQueryUuid': notebook.get('parentSavedQueryUuid', '')
        } if notebook['snippets'] else {},
        'absoluteUrl': doc.get_absolute_url(),
      })
    else:
      LOG.error('Incomplete History Notebook: %s' % notebook)
  response['history'] = sorted(history, key=lambda row: row['data']['lastExecuted'], reverse=True)
  response['message'] = _('History fetched')
  response['status'] = 0

  return JsonResponse(response)
Exemplo n.º 9
0
def get_history(request):
    response = {'status': -1}

    doc_type = request.GET.get('doc_type')
    limit = max(request.GET.get('len', 50), 100)

    response['status'] = 0
    history = []
    for doc in Document2.objects.get_history(
            doc_type='query-%s' % doc_type,
            user=request.user).order_by('-last_modified')[:limit]:
        notebook = Notebook(document=doc).get_data()
        if 'snippets' in notebook:
            history.append({
                'name': doc.name,
                'id': doc.id,
                'uuid': doc.uuid,
                'type': doc.type,
                'data': {
                    'statement_raw':
                    notebook['snippets'][0]['statement_raw'][:1001],
                    'lastExecuted':
                    notebook['snippets'][0]['lastExecuted'],
                    'status':
                    notebook['snippets'][0]['status'],
                    'parentUuid':
                    notebook.get('parentUuid', '')
                } if notebook['snippets'] else {},
                'absoluteUrl': doc.get_absolute_url(),
            })
        else:
            LOG.error('Incomplete History Notebook: %s' % notebook)
    response['history'] = history
    response['message'] = _('History fetched')

    return JsonResponse(response)