def get_data(self, request): try: limit=request.query_params.get('limit', 10) offset=request.query_params.get('offset', 0) if 'advanced_config' in request.data and request.data['advanced_config']: data = run_query(query.get_advanced_config_query(request.data['advanced_config'], limit=limit, offset=offset), fetch=True) return { 'count': len(data), 'data': data } else: count, data = query.query_table( operation_steps=request.data['operation_steps'], limit=limit, offset=offset, estimate_count=True ) return { 'count': count, 'data': data } except json.decoder.JSONDecodeError as json_error: return { 'count': 0, 'data': [ { 'error': str(json_error), 'error_type': 'JSONDecodeError' } ] }
def delete(self, request, pk, format=None): saved_query_data = self.get_object(pk) table_name = saved_query_data.saved_query_db_table saved_query_data.delete() query_builder = TableQueryBuilder(table_name, "dataset") delete_sql = query_builder.delete_table(table_name, "dataset") delete_result = run_query(delete_sql) if delete_result[0]['result'] == 'success': return Response(status=status.HTTP_204_NO_CONTENT) else: return Response(delete_result, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def delete_archive(id): try: frozen_data = FrozenData.objects.get(pk=id) table_name = frozen_data.frozen_db_table frozen_data.delete() query_builder = TableQueryBuilder(table_name, "archives") delete_sql = query_builder.delete_table(table_name, "archives") delete_result = run_query(delete_sql) if delete_result[0]['result'] == 'success': delete_source(table_name) return delete_result except FrozenData.DoesNotExist: return False
def create_dataset_archive(id): try: query_data = SavedQueryData.objects.get(id=id) query_builder = TableQueryBuilder(query_data.saved_query_db_table, operation=query_data.operation) create_query = query_builder.create_table_from_query(query_data.saved_query_db_table, "dataset") create_result = run_query(create_query) if create_result[0]['result'] == 'success': query_data.status = 'c' query_data.save() return { "status": "success" } elif create_result[0]['result'] == 'error': query_data.status = 'e' query_data.logs = 'Failed to create dataset archive: ' + create_result[0]['message'] query_data.save() return { "status": "failed", "result": create_result[0]['message'] } else: query_data.status = 'e' query_data.logs = 'Failed to create dataset archive: ' + json.dumps(create_result) query_data.save() return { "status": "failed", "result": json.dumps(create_result) } except SavedQueryData.DoesNotExist: return { "status": "errored", "result": id }
def create_table_archive(id): try: frozen_data = FrozenData.objects.get(id=id) source = Source.objects.get(active_mirror_name=frozen_data.parent_db_table) schema = source.schema query_builder = TableQueryBuilder(frozen_data.parent_db_table, schema) create_query = query_builder.select().create_table_from_query(frozen_data.frozen_db_table, "archives") create_result = run_query(create_query) if create_result[0]['result'] == 'success': frozen_data.status = 'c' frozen_data.save() return { "status": "success" } elif create_result[0]['result'] == 'error': frozen_data.status = 'e' frozen_data.logs = 'Failed to create table archive: ' + create_result[0]['message'] frozen_data.save() return { "status": "failed", "result": create_result[0]['message'] } else: frozen_data.status = 'e' frozen_data.logs = 'Failed to create table archive: ' + json.dumps(create_result) frozen_data.save() return { "status": "failed", "result": json.dumps(create_result) } except FrozenData.DoesNotExist: return { "status": "errored", "result": id }