コード例 #1
0
def build_sketch_analysis_pipeline(
    sketch_id,
    searchindex_id,
    user_id,
    analyzer_names=None,
    analyzer_kwargs=None,
    timeline_id=None,
):
    """Build a pipeline for sketch analysis.

    If no analyzer_names is passed in then we assume auto analyzers should be
    run and get this list from the configuration. Parameters to the analyzers
    can be passed in to this function, otherwise they will be taken from the
    configuration. Either default kwargs for auto analyzers or defaults for
    manually run analyzers.

    Args:
        sketch_id (int): The ID of the sketch to analyze.
        searchindex_id (int): The ID of the searchindex to analyze.
        user_id (int): The ID of the user who started the analyzer.
        analyzer_names (list): List of analyzers to run.
        analyzer_kwargs (dict): Arguments to the analyzers.
        timeline_id (int): Optional int of the timeline to run the analyzer on.

    Returns:
        A tuple with a Celery group with analysis tasks or None if no analyzers
        are enabled and an analyzer session ID.
    """
    tasks = []

    if not analyzer_names:
        analyzer_names = current_app.config.get("AUTO_SKETCH_ANALYZERS", [])
        if not analyzer_kwargs:
            analyzer_kwargs = current_app.config.get("AUTO_SKETCH_ANALYZERS_KWARGS", {})

    # Exit early if no sketch analyzers are configured to run.
    if not analyzer_names:
        return None, None

    if not analyzer_kwargs:
        analyzer_kwargs = current_app.config.get("ANALYZERS_DEFAULT_KWARGS", {})

    if user_id:
        user = User.query.get(user_id)
    else:
        user = None

    sketch = Sketch.query.get(sketch_id)
    analysis_session = AnalysisSession(user, sketch)

    analyzers = manager.AnalysisManager.get_analyzers(analyzer_names)
    for analyzer_name, analyzer_class in analyzers:
        base_kwargs = analyzer_kwargs.get(analyzer_name, {})
        searchindex = SearchIndex.query.get(searchindex_id)

        timeline = None
        if timeline_id:
            timeline = Timeline.query.get(timeline_id)

        if not timeline:
            timeline = Timeline.query.filter_by(
                sketch=sketch, searchindex=searchindex
            ).first()

        additional_kwargs = analyzer_class.get_kwargs()
        if isinstance(additional_kwargs, dict):
            additional_kwargs = [additional_kwargs]

        kwargs_list = []
        for _kwargs in additional_kwargs:
            combined_kwargs = {**base_kwargs, **_kwargs}
            kwargs_list.append(combined_kwargs)

        if not kwargs_list:
            kwargs_list = [base_kwargs]

        for kwargs in kwargs_list:
            analysis = Analysis(
                name=analyzer_name,
                description=analyzer_name,
                analyzer_name=analyzer_name,
                parameters=json.dumps(kwargs),
                user=user,
                sketch=sketch,
                timeline=timeline,
            )
            analysis.set_status("PENDING")
            analysis_session.analyses.append(analysis)
            db_session.add(analysis)
            db_session.commit()

            tasks.append(
                run_sketch_analyzer.s(
                    sketch_id,
                    analysis.id,
                    analyzer_name,
                    timeline_id=timeline_id,
                    **kwargs
                )
            )

    # Commit the analysis session to the database.
    db_session.add(analysis_session)
    db_session.commit()

    if current_app.config.get("ENABLE_EMAIL_NOTIFICATIONS"):
        tasks.append(run_email_result_task.s(sketch_id))

    if not tasks:
        return None, None

    return chain(tasks), analysis_session
コード例 #2
0
ファイル: tasks.py プロジェクト: tee2015/timesketch
def build_sketch_analysis_pipeline(sketch_id,
                                   searchindex_id,
                                   user_id,
                                   analyzer_names=None,
                                   analyzer_kwargs=None):
    """Build a pipeline for sketch analysis.

    If no analyzer_names is passed in then we assume auto analyzers should be
    run and get this list from the configuration. Parameters to the analyzers
    can be passed in to this function, otherwise they will be taken from the
    configuration. Either default kwargs for auto analyzers or defaults for
    manually run analyzers.

    Args:
        sketch_id (int): The ID of the sketch to analyze.
        searchindex_id (int): The ID of the searchindex to analyze.
        user_id (int): The ID of the user who started the analyzer.
        analyzer_names (list): List of analyzers to run.
        analyzer_kwargs (dict): Arguments to the analyzers.

    Returns:
        Celery group with analysis tasks or None if no analyzers are enabled.
    """
    tasks = []

    if not analyzer_names:
        analyzer_names = current_app.config.get('AUTO_SKETCH_ANALYZERS', [])
        if not analyzer_kwargs:
            analyzer_kwargs = current_app.config.get(
                'AUTO_SKETCH_ANALYZERS_KWARGS', {})

    # Exit early if no sketch analyzers are configured to run.
    if not analyzer_names:
        return None

    if not analyzer_kwargs:
        analyzer_kwargs = current_app.config.get('ANALYZERS_DEFAULT_KWARGS',
                                                 {})

    if user_id:
        user = User.query.get(user_id)
    else:
        user = None

    sketch = Sketch.query.get(sketch_id)
    analysis_session = AnalysisSession(user, sketch)

    analyzers = manager.AnalysisManager.get_analyzers(analyzer_names)
    for analyzer_name, analyzer_cls in analyzers:
        if not analyzer_cls.IS_SKETCH_ANALYZER:
            continue

        kwargs = analyzer_kwargs.get(analyzer_name, {})
        searchindex = SearchIndex.query.get(searchindex_id)
        timeline = Timeline.query.filter_by(sketch=sketch,
                                            searchindex=searchindex).first()

        analysis = Analysis(name=analyzer_name,
                            description=analyzer_name,
                            analyzer_name=analyzer_name,
                            parameters=json.dumps(kwargs),
                            user=user,
                            sketch=sketch,
                            timeline=timeline)
        analysis.set_status('PENDING')
        analysis_session.analyses.append(analysis)
        db_session.add(analysis)
        db_session.commit()

        tasks.append(
            run_sketch_analyzer.s(sketch_id, analysis.id, analyzer_name,
                                  **kwargs))

    # Commit the analysis session to the database.
    db_session.add(analysis_session)
    db_session.commit()

    if current_app.config.get('ENABLE_EMAIL_NOTIFICATIONS'):
        tasks.append(run_email_result_task.s(sketch_id))

    if not tasks:
        return None

    return chain(tasks)