Ejemplo n.º 1
0
def _query_result(request, notebook, max_rows):
  snippet = notebook['snippets'][0]
  snippet['statement'] = notebook['snippets'][0]['statement_raw']

  query_execute = _execute_notebook(request, notebook, snippet)

  history_uuid = query_execute['history_uuid']
  status = _check_status(request, operation_id=history_uuid)
  if status['query_status']['status'] == 'available':
    res = _fetch_result_data(request, operation_id=history_uuid, rows=max_rows)
    return res['result']

  return None
Ejemplo n.º 2
0
def query_result(request, notebook):
    snippet = notebook['snippets'][0]
    snippet['statement'] = notebook['snippets'][0]['statement_raw']

    query_execute = _execute_notebook(request, notebook, snippet)

    history_uuid = query_execute['history_uuid']
    status = _check_status(request, operation_id=history_uuid)
    if status['query_status']['status'] == 'available':
        response = _fetch_result_data(request, operation_id=history_uuid)
        return response['result']

    return 'Query result has expired or could not be found'
Ejemplo n.º 3
0
    def check_status(self, request, operation_id):
        from notebook.api import _check_status

        return _check_status(request, operation_id=operation_id)
Ejemplo n.º 4
0
def _make_unfurl_payload(request, url, id_type, doc, doc_type):
  doc_data = json.loads(doc.data)
  statement = doc_data['snippets'][0]['statement_raw'] or 'No statement' if id_type == 'editor' else doc_data.get('statement_raw', '')
  dialect = doc_data.get('dialect') or doc_data.get('type', '') if id_type == 'editor' else doc.extra

  file_status = False
  result_section = None

  if id_type == 'editor':
    max_rows = 2
    unfurl_result = 'Query result has expired or could not be found'

    try:
      status = _check_status(request, operation_id=doc_data['uuid'])
      if status['query_status']['status'] == 'available':
        fetch_result = _query_result(request, json.loads(doc.data), max_rows)
        if fetch_result is not None:
          unfurl_result = _make_result_table(fetch_result)
          file_status = True
    except:
      pass

    result_section = {
      "type": "section",
      "text": {
        "type": "mrkdwn",
        "text": "*Query result:*\n```{result}```".format(result=unfurl_result),
      }
    }

  payload_data = {
    'url': url,
    'name': doc.name,
    'doc_type': doc_type,
    'dialect': dialect,
    'user': doc.owner.get_full_name() or doc.owner.username,
    'query': statement if len(statement) < 150 else (statement[:150] + '...'),
  }

  payload = {
    url: {
      "color": "#025BA6",
      "blocks": [
        {
          "type": "section",
          "text": {
            "type": "mrkdwn",
            "text": "\n*<{url}|Open {name} {doc_type} of {dialect} dialect created by {user} in Hue>*".format(**payload_data),
          }
        },
        {
          "type": "divider"
				},
        {
          "type": "section",
          "text": {
            "type": "mrkdwn",
            "text": "*Statement:*\n```{query}```".format(**payload_data)
          }
        },
      ]
    }
  }

  if result_section is not None:
    payload[url]['blocks'].append(result_section)

  return {'payload': payload, 'file_status': file_status}
Ejemplo n.º 5
0
    def check_status(self, request, operation_id):
        from notebook.api import _check_status  # Cyclic dependency

        return _check_status(request, operation_id=operation_id)
Ejemplo n.º 6
0
def _make_unfurl_payload(url, id_type, doc, doc_type):
    doc_data = json.loads(doc.data)
    statement = doc_data['snippets'][0][
        'statement_raw'] if id_type == 'editor' else doc_data['statement_raw']
    dialect = doc_data['dialect'] if id_type == 'editor' else doc.extra
    created_by = doc.owner.get_full_name() or doc.owner.username

    # Mock request for query execution and fetch result
    user = rewrite_user(User.objects.get(username=doc.owner.username))
    request = MockRequest(user=user)

    if id_type == 'editor':
        try:
            status = _check_status(request, operation_id=doc_data['uuid'])
            if status['query_status']['status'] == 'available':
                fetch_result = query_result(request, json.loads(doc.data))
                unfurl_result = _make_result_table(fetch_result) if isinstance(
                    fetch_result, dict) else fetch_result
        except:
            unfurl_result = 'Query result has expired or could not be found'
    else:
        unfurl_result = 'Result is not available for Gist'

    payload = {
        url: {
            "color":
            "#025BA6",
            "blocks": [{
                "type": "section",
                "text": {
                    "type": "mrkdwn",
                    "text": "\n*<{}|Hue - SQL {}>*".format(url, doc_type)
                }
            }, {
                "type": "divider"
            }, {
                "type": "section",
                "text": {
                    "type":
                    "mrkdwn",
                    "text":
                    "*Statement:*\n```{}```".format(
                        statement if len(statement) < 150 else (
                            statement[:150] + '...'))
                }
            }, {
                "type":
                "section",
                "fields": [{
                    "type": "mrkdwn",
                    "text": "*Dialect:*\n{}".format(dialect)
                }, {
                    "type": "mrkdwn",
                    "text": "*Created by:*\n{}".format(created_by)
                }]
            }, {
                "type": "section",
                "text": {
                    "type": "mrkdwn",
                    "text": "*Query result:*\n```{}```".format(unfurl_result),
                }
            }]
        }
    }

    return payload
Ejemplo n.º 7
0
def _make_unfurl_payload(request, url, id_type, doc, doc_type):
  doc_data = json.loads(doc.data)
  statement = doc_data['snippets'][0]['statement_raw'] if id_type == 'editor' else doc_data['statement_raw']
  dialect = doc_data['dialect'] if id_type == 'editor' else doc.extra
  created_by = doc.owner.get_full_name() or doc.owner.username
  name = doc.name or dialect

  file_status = False

  if id_type == 'editor':
    max_rows = 2
    unfurl_result = 'Query result has expired or could not be found'
    try:
      status = _check_status(request, operation_id=doc_data['uuid'])
      if status['query_status']['status'] == 'available':
        fetch_result = _query_result(request, json.loads(doc.data), max_rows)
        if fetch_result is not None:
          unfurl_result = _make_result_table(fetch_result)
          file_status = True
    except:
      pass
  else:
    unfurl_result = 'Result is not available for Gist'

  payload = {
    url: {
      "color": "#025BA6",
      "blocks": [
        {
          "type": "section",
          "text": {
            "type": "mrkdwn",
            "text": "\n*<{}|Open {} {} in Hue - SQL Editor>*".format(url, name, doc_type)
          }
        },
        {
          "type": "divider"
				},
        {
          "type": "section",
          "text": {
            "type": "mrkdwn",
            "text": "*Statement:*\n```{}```".format(statement if len(statement) < 150 else (statement[:150] + '...'))
          }
        },
        {
          "type": "section",
          "fields": [
            {
              "type": "mrkdwn",
              "text": "*Dialect:*\n{}".format(dialect)
            },
            {
              "type": "mrkdwn",
              "text": "*Created by:*\n{}".format(created_by)
            }
          ]
        },
        {
          "type": "section",
          "text": {
            "type": "mrkdwn",
            "text": "*Query result:*\n```{}```".format(unfurl_result),
          }
				}
      ]
    }
  }

  return {'payload': payload, 'file_status': file_status}