def aql(self, request, name: str): """Execute AQL in a workspace.""" serializer = AqlQuerySerializer(data=request.query_params) serializer.is_valid(raise_exception=True) query_str = serializer.validated_data['query'] workspace: Workspace = get_object_or_404(Workspace, name=name) database = workspace.get_arango_db() query = ArangoQuery(database, query_str) try: cursor: Cursor = query.execute() return Response( cursor, status=status.HTTP_200_OK, ) except AQLQueryExecuteError as err: # Invalid query, time/memory limit reached, or # attempt to run a mutating query as the readonly user return Response( err.error_message, status=status.HTTP_400_BAD_REQUEST, ) except ArangoServerError as err: # Arango server errors unrelated to the client's query return Response(err.error_message, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def execute_query(task_id: int) -> None: query_task: AqlQuery = AqlQuery.objects.select_related('workspace').get(id=task_id) workspace: Workspace = query_task.workspace query_str = query_task.query # Run the query on Arango DB database = workspace.get_arango_db() query = ArangoQuery(database, query_str, time_limit_secs=60) cursor: Cursor = query.execute() # Store the results on the task object query_task.results = list(cursor) query_task.save()
def paginate_queryset(self, query: ArangoQuery, request: Request) -> List[Dict]: self._set_pre_query_params(request) paginated_query = query.paginate(self.limit, self.offset) cur: Cursor = paginated_query.execute(full_count=True) self.count = cur.statistics()['fullCount'] self._set_post_query_params() return list(cur)
def edges(self, request, parent_lookup_workspace__name: str, name: str): # Doesn't use the Network.edges method, in order to do proper pagination. workspace: Workspace = get_object_or_404( Workspace, name=parent_lookup_workspace__name) network: Network = get_object_or_404(Network, workspace=workspace, name=name) pagination = ArangoPagination() query = ArangoQuery.from_collections(workspace.get_arango_db(), network.edge_tables()) paginated_query = pagination.paginate_queryset(query, request) return pagination.get_paginated_response(paginated_query)
def get_rows(self, request, parent_lookup_workspace__name: str, name: str): workspace: Workspace = get_object_or_404(Workspace, name=parent_lookup_workspace__name) table: Table = get_object_or_404(Table, workspace=workspace, name=name) pagination = ArangoPagination() query = ArangoQuery.from_collections(workspace.get_arango_db(), [table.name]) # Attempt filtering try: query = query.filter(json.loads(request.query_params.get('filter', '{}'))) except json.JSONDecodeError as e: return Response( {'filter': [str(e)]}, status=status.HTTP_400_BAD_REQUEST, ) paginated_query = pagination.paginate_queryset(query, request) return pagination.get_paginated_response(paginated_query)
def edges(self, limit: int = 0, offset: int = 0) -> Cursor: return (ArangoQuery.from_collections(self.workspace.get_arango_db(), self.edge_tables()).paginate( limit=limit, offset=offset).execute())