Esempio n. 1
0
def calculate_incidents_response_cost(db_session=None):
    """
    Calculates and saves the response cost for all incidents.
    """
    for project in project_service.get_all(db_session=db_session):
        response_cost_type = incident_cost_type_service.get_default(
            db_session=db_session, project_id=project.id)
        if response_cost_type is None:
            log.warning(
                f"A default cost type for response cost does not exist in the {project.name} project. Response costs won't be calculated."
            )
            continue

        # we want to update the response cost of all incidents, all the time
        incidents = incident_service.get_all(db_session=db_session,
                                             project_id=project.id)
        for incident in incidents:
            try:
                # we get the response cost for the given incident
                incident_response_cost = get_by_incident_id_and_incident_cost_type_id(
                    db_session=db_session,
                    incident_id=incident.id,
                    incident_cost_type_id=response_cost_type.id,
                )

                if incident_response_cost is None:
                    # we create the response cost if it doesn't exist
                    incident_cost_type = IncidentCostTypeRead.from_orm(
                        response_cost_type)
                    incident_cost_in = IncidentCostCreate(
                        incident_cost_type=incident_cost_type, project=project)
                    incident_response_cost = create(
                        db_session=db_session,
                        incident_cost_in=incident_cost_in)

                # we calculate the response cost amount
                amount = calculate_incident_response_cost(
                    incident.id, db_session)

                # we don't need to update the cost amount if it hasn't changed
                if incident_response_cost.amount == amount:
                    continue

                # we save the new incident cost amount
                incident_response_cost.amount = amount
                incident.incident_costs.append(incident_response_cost)
                db_session.add(incident)
                db_session.commit()

                log.debug(
                    f"Response cost amount for {incident.name} incident has been updated in the database."
                )

            except Exception as e:
                # we shouldn't fail to update all incidents when one fails
                log.exception(e)
Esempio n. 2
0
def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column("incident_cost_type",
                  sa.Column("category", sa.String(), nullable=True))

    # we set the category of the default response incident cost type
    db_session = SessionLocal()
    incident_cost_type = incident_cost_type_service.get_default(
        db_session=db_session)
    if incident_cost_type:
        incident_cost_type_in = IncidentCostTypeUpdate(
            id=incident_cost_type.id,
            name=incident_cost_type.name,
            category="Primary")
        incident_cost_type_service.update(
            db_session=db_session,
            incident_cost_type=incident_cost_type,
            incident_cost_type_in=incident_cost_type_in,
        )