def test_handling_rel_revisions(self):
    """Test handling of relationship revisions."""
    with factories.single_commit():
      assessment = factories.AssessmentFactory(
          finished_date=datetime.datetime(2017, 2, 20, 13, 40, 0),
          status="Completed",
      )
      control = factories.ControlFactory()
      snapshots = self._create_snapshots(assessment.audit, [control])
      rel = factories.RelationshipFactory(
          source=assessment,
          destination=snapshots[0]
      )

    rel_revision = models.Revision.query.filter(
        models.Revision.resource_type == rel.type,
        models.Revision.resource_id == rel.id,
    ).first()
    assessment_revision = models.Revision.query.filter(
        models.Revision.resource_type == assessment.type,
        models.Revision.resource_id == assessment.id,
    ).first()

    computed_attributes.compute_attributes([
        rel_revision.id, assessment_revision.id
    ])

    self.assertEqual(
        models.all_models.Attributes.query.count(),
        2,  # One entry for control and one for the control snapshot.
    )
    def test_handling_rel_revisions(self):
        """Test handling of relationship revisions."""
        with factories.single_commit():
            assessment = factories.AssessmentFactory(
                finished_date=datetime.datetime(2017, 2, 20, 13, 40, 0),
                status="Completed",
            )
            control = factories.ControlFactory()
            snapshots = self._create_snapshots(assessment.audit, [control])
            rel = factories.RelationshipFactory(source=assessment,
                                                destination=snapshots[0])

        rel_revision = models.Revision.query.filter(
            models.Revision.resource_type == rel.type,
            models.Revision.resource_id == rel.id,
        ).first()
        assessment_revision = models.Revision.query.filter(
            models.Revision.resource_type == assessment.type,
            models.Revision.resource_id == assessment.id,
        ).first()

        computed_attributes.compute_attributes(
            [rel_revision.id, assessment_revision.id])

        self.assertEqual(
            models.all_models.Attributes.query.count(),
            2,  # One entry for control and one for the control snapshot.
        )
Esempio n. 3
0
def calculate_computed_attributes(revision_ids, user_id):
    """Calculate computed attributes as deferred task."""
    with app.app_context():
        try:
            user = person.Person.query.get(user_id)
            setattr(g, '_current_user', user)
            computed_attributes.compute_attributes(revision_ids)
        except Exception as e:  # pylint: disable=broad-except
            logger.exception("Calculation of computed attributes failed: %s",
                             e.message)
Esempio n. 4
0
def compute_attributes(args):
  """Web hook to update the full text search index."""
  with benchmark("Run compute_attributes background task"):
    from ggrc.data_platform import computed_attributes
    if str(args.parameters["revision_ids"]) == "all_latest":
      revision_ids = "all_latest"
    else:
      revision_ids = [id_ for id_ in args.parameters["revision_ids"]]
    computed_attributes.compute_attributes(revision_ids)
    return app.make_response(("success", 200, [("Content-Type", "text/html")]))
Esempio n. 5
0
def compute_attributes(args):
  """Web hook to update the full text search index."""
  with benchmark("Run compute_attributes background task"):
    from ggrc.data_platform import computed_attributes
    if str(args.parameters["revision_ids"]) == "all_latest":
      revision_ids = "all_latest"
    else:
      revision_ids = [id_ for id_ in args.parameters["revision_ids"]]
    computed_attributes.compute_attributes(revision_ids)
    return app.make_response(("success", 200, [("Content-Type", "text/html")]))
Esempio n. 6
0
def calculate_computed_attributes(revision_ids, user_id):
  """Calculate computed attributes as deferred task."""
  with app.app_context():
    try:
      user = person.Person.query.get(user_id)
      setattr(g, '_current_user', user)
      computed_attributes.compute_attributes(revision_ids)
    except Exception as e:  # pylint: disable=broad-except
      logger.exception(
          "Calculation of computed attributes failed: %s", e.message
      )
Esempio n. 7
0
def compute_attributes(args):
  """Web hook to update the full text search index."""
  with benchmark("Run compute_attributes background task"):
    from ggrc.data_platform import computed_attributes
    if args.parameters["event_id"] and not args.parameters["revision_ids"]:
      rows = db.session.query(Revision.id).filter_by(
          event_id=args.parameters["event_id"],).all()
      revision_ids = [revision_id for revision_id, in rows]
    elif str(args.parameters["revision_ids"]) == "all_latest":
      revision_ids = "all_latest"
    else:
      revision_ids = [id_ for id_ in args.parameters["revision_ids"]]

    computed_attributes.compute_attributes(revision_ids)
    return app.make_response(("success", 200, [("Content-Type", "text/html")]))
Esempio n. 8
0
def compute_attributes(args):
    """Web hook to update the full text search index."""
    with benchmark("Run compute_attributes background task"):
        from ggrc.data_platform import computed_attributes
        if args.parameters["event_id"] and not args.parameters["revision_ids"]:
            rows = db.session.query(Revision.id).filter_by(
                event_id=args.parameters["event_id"], ).all()
            revision_ids = [revision_id for revision_id, in rows]
        elif str(args.parameters["revision_ids"]) == "all_latest":
            revision_ids = "all_latest"
        else:
            revision_ids = [id_ for id_ in args.parameters["revision_ids"]]

        computed_attributes.compute_attributes(revision_ids)
        return app.make_response(
            ("success", 200, [("Content-Type", "text/html")]))
Esempio n. 9
0
def compute_attributes(*_, **kwargs):
  """Web hook to update the full text search index."""
  with benchmark("Run compute_attributes background task"):
    event_id = utils.get_task_attr("event_id", kwargs)
    revision_ids = utils.get_task_attr("revision_ids", kwargs)

    if event_id and not revision_ids:
      rows = db.session.query(Revision.id).filter_by(event_id=event_id).all()
      revision_ids = [revision_id for revision_id, in rows]
    elif str(revision_ids) == "all_latest":
      revision_ids = "all_latest"
    else:
      revision_ids = list(revision_ids)

    from ggrc.data_platform import computed_attributes
    computed_attributes.compute_attributes(revision_ids)
    return app.make_response(("success", 200, [("Content-Type", "text/html")]))
Esempio n. 10
0
def compute_attributes(task):
    """Web hook to update the full text search index."""
    with benchmark("Run compute_attributes background task"):
        event_id = task.parameters.get("event_id")
        revision_ids = task.parameters.get("revision_ids")

        if event_id and not revision_ids:
            rows = db.session.query(
                revision.Revision.id).filter_by(event_id=event_id).all()
            revision_ids = [revision_id for revision_id, in rows]
        elif str(revision_ids) == "all_latest":
            revision_ids = "all_latest"
        else:
            revision_ids = list(revision_ids)

        from ggrc.data_platform import computed_attributes
        computed_attributes.compute_attributes(revision_ids)
        return app.make_response(
            ("success", 200, [("Content-Type", "text/html")]))
Esempio n. 11
0
  def setUp(self):
    self.clear_data()
    super(TestWithLastAssessmentDate, self).setUp()
    self.generator = integration.ggrc.generator.ObjectGenerator()
    self.client.get("/login")
    self.api = Api()

    program = factories.ProgramFactory(title="Last Assessment Date")

    controls = [factories.ControlFactory() for _ in range(3)]
    self.control_ids = [c.id for c in controls]
    for control in controls:
      factories.RelationshipFactory(source=program, destination=control)
    objectives = [factories.ObjectiveFactory() for _ in range(3)]
    self.objective_ids = [c.id for c in objectives]
    for objective in objectives:
      factories.RelationshipFactory(source=program, destination=objective)

    self._create_audit(program=program, title="Last Assessment Date Audit")

    c_snapshots = (db.session.query(all_models.Snapshot)
                   .filter_by(child_type="Control").all())
    o_snapshots = (db.session.query(all_models.Snapshot)
                   .filter_by(child_type="Objective").all())
    c_id_to_snap_id = {s.child_id: s.id for s in c_snapshots}
    o_id_to_snap_id = {s.child_id: s.id for s in o_snapshots}

    assessments = [factories.AssessmentFactory() for _ in range(3)]
    self.assessment_ids = [a.id for a in assessments]

    # Mappings:
    # controls[0]: assessments[0], assessments[1], assessments[2],
    # controls[1]: assessments[1], assessments[2],
    # controls[2]: assessments[2]
    # objectives[0]: assessments[0],
    # objectives[1]: assessments[0], assessments[1],
    # objectives[2]: assessments[0], assessments[1], assessments[2],

    # the first Assessment is completed earlier than the second, the third
    # Assessment is not completed.

    self._create_relationships(
        (c_id_to_snap_id[self.control_ids[0]], assessments[0]),
        (c_id_to_snap_id[self.control_ids[0]], assessments[1]),
        (c_id_to_snap_id[self.control_ids[0]], assessments[2]),
        (c_id_to_snap_id[self.control_ids[1]], assessments[1]),
        (c_id_to_snap_id[self.control_ids[1]], assessments[2]),
        (c_id_to_snap_id[self.control_ids[2]], assessments[2]),
        (o_id_to_snap_id[self.objective_ids[2]], assessments[0]),
        (o_id_to_snap_id[self.objective_ids[2]], assessments[1]),
        (o_id_to_snap_id[self.objective_ids[2]], assessments[2]),
        (o_id_to_snap_id[self.objective_ids[1]], assessments[0]),
        (o_id_to_snap_id[self.objective_ids[1]], assessments[1]),
        (o_id_to_snap_id[self.objective_ids[0]], assessments[0]),
    )

    self.finished_dates = [datetime.datetime(2017, 2, 20, 13, 40, 0),
                           datetime.datetime(2017, 3, 30, 14, 55, 0)]

    with freezegun.freeze_time(self.finished_dates[0]):
      assessments[0].status = all_models.Assessment.FINAL_STATE
      db.session.add(assessments[0])
    with freezegun.freeze_time(self.finished_dates[1]):
      assessments[1].status = all_models.Assessment.FINAL_STATE
      db.session.add(assessments[1])
    db.session.commit()

    query = all_models.Revision.query.filter_by(resource_type="Assessment")
    revision_ids = [revision.id for revision in query]
    computed_attributes.compute_attributes(revision_ids)