(self, sketch_id)
return jsonify(schema)
Handles POST request to the resource.

        Handler for /api/v1/sketches/<int:sketch_id>/aggregation/explore/

        Args:
            sketch_id: Integer primary key for a sketch database model

        Returns:
            JSON with aggregation results
Handles POST request to the resource.
Handles POST request to the resource .
def post(self, sketch_id):
        """Handles POST request to the resource.

        Handler for /api/v1/sketches/<int:sketch_id>/aggregation/explore/

        Args:
            sketch_id: Integer primary key for a sketch database model

        Returns:
            JSON with aggregation results
        """
        form = forms.AggregationExploreForm.build(request)
        if not form.validate_on_submit():
            abort(
                HTTP_STATUS_CODE_BAD_REQUEST,
                'Not able to run aggregation, unable to validate form data.')

        sketch = Sketch.query.get_with_acl(sketch_id)
        if not sketch:
            abort(
                HTTP_STATUS_CODE_NOT_FOUND, 'No sketch found with this ID.')

        if not sketch.has_permission(current_user, 'read'):
            abort(HTTP_STATUS_CODE_FORBIDDEN,
                  'User does not have read access controls on sketch.')

        if sketch.get_status.status == 'archived':
            abort(
                HTTP_STATUS_CODE_BAD_REQUEST,
                'Not able to run aggregation on an archived sketch.')

        sketch_indices = {
            t.searchindex.index_name
            for t in sketch.timelines
            if t.get_status.status.lower() == 'ready'
        }

        aggregation_dsl = form.aggregation_dsl.data
        aggregator_name = form.aggregator_name.data

        if aggregator_name:
            if isinstance(form.aggregator_parameters.data, dict):
                aggregator_parameters = form.aggregator_parameters.data
            else:
                aggregator_parameters = json.loads(
                    form.aggregator_parameters.data)

            agg_class = aggregator_manager.AggregatorManager.get_aggregator(
                aggregator_name)
            if not agg_class:
                return {}
            if not aggregator_parameters:
                aggregator_parameters = {}

            indices = aggregator_parameters.pop('index', sketch_indices)
            indices, timeline_ids = lib_utils.get_validated_indices(
                indices, sketch)

            if not (indices or timeline_ids):
                abort(
                    HTTP_STATUS_CODE_BAD_REQUEST, 'No indices to aggregate on')

            aggregator = agg_class(
                sketch_id=sketch_id, indices=indices,
                timeline_ids=timeline_ids)

            chart_type = aggregator_parameters.pop('supported_charts', None)
            chart_color = aggregator_parameters.pop('chart_color', '')
            chart_title = aggregator_parameters.pop(
                'chart_title', aggregator.chart_title)

            time_before = time.time()
            try:
                result_obj = aggregator.run(**aggregator_parameters)
            except NotFoundError:
                abort(
                    HTTP_STATUS_CODE_NOT_FOUND,
                    'Attempting to run an aggregation on a non-existing '
                    'index, index: {0:s} and parameters: {1!s}'.format(
                        ','.join(indices), aggregator_parameters))
            except ValueError as exc:
                abort(
                    HTTP_STATUS_CODE_BAD_REQUEST,
                    'Unable to run the aggregation, with error: {0!s}'.format(
                        exc))
            time_after = time.time()

            aggregator_description = aggregator.describe

            buckets = result_obj.to_dict()
            buckets['buckets'] = buckets.pop('values')
            result = {
                'aggregation_result': {
                    aggregator_name: buckets
                }
            }
            meta = {
                'method': 'aggregator_run',
                'chart_type': chart_type,
                'name': aggregator_description.get('name'),
                'description': aggregator_description.get('description'),
                'es_time': time_after - time_before,
            }

            if chart_type:
                meta['vega_spec'] = result_obj.to_chart(
                    chart_name=chart_type,
                    chart_title=chart_title, color=chart_color)
                meta['vega_chart_title'] = chart_title

        elif aggregation_dsl:
            # pylint: disable=unexpected-keyword-arg
            result = self.datastore.client.search(
                index=','.join(sketch_indices), body=aggregation_dsl, size=0)

            meta = {
                'es_time': result.get('took', 0),
                'es_total_count': result.get('hits', {}).get('total', 0),
                'timed_out': result.get('timed_out', False),
                'method': 'aggregator_query',
                'max_score': result.get('hits', {}).get('max_score', 0.0)
            }
        else:
            abort(
                HTTP_STATUS_CODE_BAD_REQUEST,
                'An aggregation DSL or a name for an aggregator name needs '
                'to be provided!')

        result_keys = set(result.keys()) - self.REMOVE_FIELDS
        objects = [result[key] for key in result_keys]
        schema = {'meta': meta, 'objects': objects}

        # Update the last activity of a sketch.
        utils.update_sketch_last_activity(sketch)

        return jsonify(schema)
def post ( self , sketch_id ) : form = forms . AggregationExploreForm . build ( request ) if not form . validate_on_submit ( ) : abort ( HTTP_STATUS_CODE_BAD_REQUEST , 'Not able to run aggregation, unable to validate form data.' ) sketch = Sketch . query . get_with_acl ( sketch_id ) if not sketch : abort ( HTTP_STATUS_CODE_NOT_FOUND , 'No sketch found with this ID.' ) if not sketch . has_permission ( current_user , 'read' ) : abort ( HTTP_STATUS_CODE_FORBIDDEN , 'User does not have read access controls on sketch.' ) if sketch . get_status . status == 'archived' : abort ( HTTP_STATUS_CODE_BAD_REQUEST , 'Not able to run aggregation on an archived sketch.' ) sketch_indices = { t . searchindex . index_name for t in sketch . timelines if t . get_status . status . lower ( ) == 'ready' } aggregation_dsl = form . aggregation_dsl . data aggregator_name = form . aggregator_name . data if aggregator_name : if isinstance ( form . aggregator_parameters . data , dict ) : aggregator_parameters = form . aggregator_parameters . data else : aggregator_parameters = json . loads ( form . aggregator_parameters . data ) agg_class = aggregator_manager . AggregatorManager . get_aggregator ( aggregator_name ) if not agg_class : return { } if not aggregator_parameters : aggregator_parameters = { } indices = aggregator_parameters . pop ( 'index' , sketch_indices ) indices , timeline_ids = lib_utils . get_validated_indices ( indices , sketch ) if not ( indices or timeline_ids ) : abort ( HTTP_STATUS_CODE_BAD_REQUEST , 'No indices to aggregate on' ) aggregator = agg_class ( sketch_id = sketch_id , indices = indices , timeline_ids = timeline_ids ) chart_type = aggregator_parameters . pop ( 'supported_charts' , None ) chart_color = aggregator_parameters . pop ( 'chart_color' , '' ) chart_title = aggregator_parameters . pop ( 'chart_title' , aggregator . chart_title ) time_before = time . time ( ) try : result_obj = aggregator . run ( * * aggregator_parameters ) except NotFoundError : abort ( HTTP_STATUS_CODE_NOT_FOUND , 'Attempting to run an aggregation on a non-existing ' 'index, index: {0:s} and parameters: {1!s}' . format ( ',' . join ( indices ) , aggregator_parameters ) ) except ValueError as exc : abort ( HTTP_STATUS_CODE_BAD_REQUEST , 'Unable to run the aggregation, with error: {0!s}' . format ( exc ) ) time_after = time . time ( ) aggregator_description = aggregator . describe buckets = result_obj . to_dict ( ) buckets [ 'buckets' ] = buckets . pop ( 'values' ) result = { 'aggregation_result' : { aggregator_name : buckets } } meta = { 'method' : 'aggregator_run' , 'chart_type' : chart_type , 'name' : aggregator_description . get ( 'name' ) , 'description' : aggregator_description . get ( 'description' ) , 'es_time' : time_after - time_before , } if chart_type : meta [ 'vega_spec' ] = result_obj . to_chart ( chart_name = chart_type , chart_title = chart_title , color = chart_color ) meta [ 'vega_chart_title' ] = chart_title elif aggregation_dsl : # pylint: disable=unexpected-keyword-arg result = self . datastore . client . search ( index = ',' . join ( sketch_indices ) , body = aggregation_dsl , size = 0 ) meta = { 'es_time' : result . get ( 'took' , 0 ) , 'es_total_count' : result . get ( 'hits' , { } ) . get ( 'total' , 0 ) , 'timed_out' : result . get ( 'timed_out' , False ) , 'method' : 'aggregator_query' , 'max_score' : result . get ( 'hits' , { } ) . get ( 'max_score' , 0.0 ) } else : abort ( HTTP_STATUS_CODE_BAD_REQUEST , 'An aggregation DSL or a name for an aggregator name needs ' 'to be provided!' ) result_keys = set ( result . keys ( ) ) - self . REMOVE_FIELDS objects = [ result [ key ] for key in result_keys ] schema = { 'meta' : meta , 'objects' : objects } # Update the last activity of a sketch. utils . update_sketch_last_activity ( sketch ) return jsonify ( schema )