Skip to content

Commit

Permalink
Write to central issue tables from zoncolan_cli
Browse files Browse the repository at this point in the history
Reviewed By: fahndrich

Differential Revision: D58112427

fbshipit-source-id: d8bb3636d88391d0f9a2e431582f806e620d0939
  • Loading branch information
alexblanck authored and facebook-github-bot committed Jun 5, 2024
1 parent 74c2f19 commit 8c0e21d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
3 changes: 2 additions & 1 deletion sapp/cli_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import click_log
import IPython
from click import argument, option, Parameter, Path
from tools.sapp.sapp.models import Run
from traitlets.config import Config

from .analysis_output import AnalysisOutput
Expand Down Expand Up @@ -198,7 +199,7 @@ def analyze(
AddFeatures(add_feature),
ModelGenerator(),
TrimTraceGraph(),
DatabaseSaver(ctx.database, PrimaryKeyGenerator(), dry_run),
DatabaseSaver(ctx.database, Run, PrimaryKeyGenerator(), dry_run),
]
# pyre-fixme[6]: Expected
# `List[tools.sapp.sapp.pipeline.PipelineStep[typing.Any, typing.Any]]` for 1st
Expand Down
22 changes: 17 additions & 5 deletions sapp/pipeline/database_saver.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import collections
import logging
from typing import List, Optional, Tuple, Type
from typing import Generic, List, Optional, Tuple, Type, TypeVar

from ..bulk_saver import BulkSaver
from ..db import DB
Expand All @@ -32,20 +32,22 @@

log: logging.Logger = logging.getLogger("sapp")

TRun = TypeVar("TRun", bound=Run)

# pyre-fixme[13]: Attribute `summary` is never initialized.
class DatabaseSaver(PipelineStep[TraceGraph, RunSummary]):
RUN_MODEL = Run

# pyre-fixme[13]: Attribute `summary` is never initialized.
class DatabaseSaver(PipelineStep[TraceGraph, RunSummary], Generic[TRun]):
def __init__(
self,
database: DB,
run_model: Type[TRun],
primary_key_generator: Optional[PrimaryKeyGenerator] = None,
dry_run: bool = False,
extra_saving_classes: Optional[List[Type[object]]] = None,
) -> None:
self.dbname: str = database.dbname
self.database = database
self.run_model = run_model
self.primary_key_generator: PrimaryKeyGenerator = (
primary_key_generator or PrimaryKeyGenerator()
)
Expand Down Expand Up @@ -126,12 +128,17 @@ def _save(self) -> RunSummary:
run_id = self.summary["run"].id.resolved()
log.info("Created run: %d", run_id)

# Get full list of issues before bulk_saver prunes them during _prepare
issues = self.bulk_saver.get_items_to_add(Issue)

self.bulk_saver.save_all(self.database)

self._save_central_issues(self.summary["run"], issues)

# Now that the run is finished, fetch it from the DB again and set its
# status to FINISHED.
with self.database.make_session() as session:
run = session.query(self.RUN_MODEL).filter_by(id=run_id).one()
run = session.query(self.run_model).filter_by(id=run_id).one()
run.status = RunStatus.FINISHED
session.add(run)
session.commit()
Expand Down Expand Up @@ -163,3 +170,8 @@ def _get_dry_run_summary(self) -> RunSummary:
collections.Counter(issue.code for issue in self.graph.get_issues())
),
)

def _save_central_issues(self, run: TRun, issues: List[Issue]) -> None:
"""Subclasses may implement this to save issue data to a second location before
the run status is changed to FINISHED"""
pass
1 change: 1 addition & 0 deletions sapp/tests/fake_object_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ def issue(
callable_id=callable.id,
status=status,
detected_time=now.timestamp(),
first_instance_id=DBID(10072),
)
if self.graph:
# pyre-fixme[6]: For 1st param expected `Issue` but got `Munch`.
Expand Down

0 comments on commit 8c0e21d

Please sign in to comment.