(self, sketch_id)
return self.to_json(sessions)
Handles POST request to the resource.

        Returns:
            A string with the response from running the analyzer.
Handles POST request to the resource.
Handles POST request to the resource .
def post(self, sketch_id):
        """Handles POST request to the resource.

        Returns:
            A string with the response from running the analyzer.
        """
        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, 'write'):
            return abort(
                HTTP_STATUS_CODE_FORBIDDEN,
                'User does not have write permission on the sketch.')

        form = request.json
        if not form:
            form = request.data

        if not form:
            return abort(
                HTTP_STATUS_CODE_FORBIDDEN,
                'Unable to run an analyzer without any data submitted.')

        timeline_ids = form.get('timeline_ids')
        if not timeline_ids:
            return abort(
                HTTP_STATUS_CODE_BAD_REQUEST,
                'Need to provide a timeline ID')

        sketch_timelines = {t.id for t in sketch.timelines}
        if not set(timeline_ids).issubset(sketch_timelines):
            return abort(
                HTTP_STATUS_CODE_FORBIDDEN,
                'Timeline is not part of this sketch')

        analyzer_names = form.get('analyzer_names')
        if analyzer_names:
            if not isinstance(analyzer_names, (tuple, list)):
                return abort(
                    HTTP_STATUS_CODE_BAD_REQUEST,
                    'Analyzer names needs to be a list of analyzers.')

        analyzer_kwargs = form.get('analyzer_kwargs')
        if analyzer_kwargs:
            if not isinstance(analyzer_kwargs, dict):
                return abort(
                    HTTP_STATUS_CODE_BAD_REQUEST,
                    'Kwargs needs to be a dictionary of parameters.')

        analyzers = []
        all_analyzers = [
            x for x, _ in analyzer_manager.AnalysisManager.get_analyzers()]
        for analyzer in analyzer_names:
            for correct_name in all_analyzers:
                if fnmatch.fnmatch(correct_name, analyzer):
                    METRICS['analyzer_run'].labels(name=correct_name).inc()
                    analyzers.append(correct_name)

        if not analyzers:
            return abort(
                HTTP_STATUS_CODE_BAD_REQUEST, 'No analyzers found to run.')

        # Import here to avoid circular imports.
        # pylint: disable=import-outside-toplevel
        from timesketch.lib import tasks

        # TODO: Change to run on Timeline instead of Index
        sessions = []
        for timeline_id in timeline_ids:
            timeline = Timeline.query.get(timeline_id)
            if not timeline:
                continue
            searchindex_id = timeline.searchindex.id
            searchindex_name = timeline.searchindex.index_name

            try:
                analyzer_group, session = tasks.build_sketch_analysis_pipeline(
                    sketch_id=sketch_id, searchindex_id=searchindex_id,
                    user_id=current_user.id, analyzer_names=analyzers,
                    analyzer_kwargs=analyzer_kwargs, timeline_id=timeline_id)
            except KeyError as e:
                logger.warning(
                    'Unable to build analyzer pipeline, analyzer does not '
                    'exists. Error message: {0!s}'.format(e))
                continue

            if analyzer_group:
                pipeline = (tasks.run_sketch_init.s(
                    [searchindex_name]) | analyzer_group)
                pipeline.apply_async()

            sessions.append(session)

        return self.to_json(sessions)
def post ( self , sketch_id ) : 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 , 'write' ) : return abort ( HTTP_STATUS_CODE_FORBIDDEN , 'User does not have write permission on the sketch.' ) form = request . json if not form : form = request . data if not form : return abort ( HTTP_STATUS_CODE_FORBIDDEN , 'Unable to run an analyzer without any data submitted.' ) timeline_ids = form . get ( 'timeline_ids' ) if not timeline_ids : return abort ( HTTP_STATUS_CODE_BAD_REQUEST , 'Need to provide a timeline ID' ) sketch_timelines = { t . id for t in sketch . timelines } if not set ( timeline_ids ) . issubset ( sketch_timelines ) : return abort ( HTTP_STATUS_CODE_FORBIDDEN , 'Timeline is not part of this sketch' ) analyzer_names = form . get ( 'analyzer_names' ) if analyzer_names : if not isinstance ( analyzer_names , ( tuple , list ) ) : return abort ( HTTP_STATUS_CODE_BAD_REQUEST , 'Analyzer names needs to be a list of analyzers.' ) analyzer_kwargs = form . get ( 'analyzer_kwargs' ) if analyzer_kwargs : if not isinstance ( analyzer_kwargs , dict ) : return abort ( HTTP_STATUS_CODE_BAD_REQUEST , 'Kwargs needs to be a dictionary of parameters.' ) analyzers = [ ] all_analyzers = [ x for x , _ in analyzer_manager . AnalysisManager . get_analyzers ( ) ] for analyzer in analyzer_names : for correct_name in all_analyzers : if fnmatch . fnmatch ( correct_name , analyzer ) : METRICS [ 'analyzer_run' ] . labels ( name = correct_name ) . inc ( ) analyzers . append ( correct_name ) if not analyzers : return abort ( HTTP_STATUS_CODE_BAD_REQUEST , 'No analyzers found to run.' ) # Import here to avoid circular imports. # pylint: disable=import-outside-toplevel from timesketch . lib import tasks # TODO: Change to run on Timeline instead of Index sessions = [ ] for timeline_id in timeline_ids : timeline = Timeline . query . get ( timeline_id ) if not timeline : continue searchindex_id = timeline . searchindex . id searchindex_name = timeline . searchindex . index_name try : analyzer_group , session = tasks . build_sketch_analysis_pipeline ( sketch_id = sketch_id , searchindex_id = searchindex_id , user_id = current_user . id , analyzer_names = analyzers , analyzer_kwargs = analyzer_kwargs , timeline_id = timeline_id ) except KeyError as e : logger . warning ( 'Unable to build analyzer pipeline, analyzer does not ' 'exists. Error message: {0!s}' . format ( e ) ) continue if analyzer_group : pipeline = ( tasks . run_sketch_init . s ( [ searchindex_name ] ) | analyzer_group ) pipeline . apply_async ( ) sessions . append ( session ) return self . to_json ( sessions )