def get_deployment_operations()

in backend/bms_app/operation/views.py [0:0]


def get_deployment_operations():
    """Return only deployed operations."""
    total_mappings = db.session.query(Operation.id, func.count()) \
        .join(OperationDetails, Operation.id == OperationDetails.operation_id) \
        .filter(Operation.operation_type == OperationType.DEPLOYMENT) \
        .group_by(Operation.id) \
        .all()
    total_mappings = dict(total_mappings)

    query = db.session.query(Wave, Operation) \
        .join(Operation, Wave.id == Operation.wave_id) \
        .filter(Operation.operation_type == OperationType.DEPLOYMENT)

    # Return only deployed operations for particular project
    project_id = request.args.get('project_id', type=int)
    if project_id:
        query = query.filter(Wave.project_id == project_id)

    deployments = query.all()

    data = []
    for row in deployments:
        data.append({
            'wave_id': row.Wave.id,
            'wave_name': row.Wave.name,
            'id': row.Operation.id,
            'started_at': row.Operation.started_at,
            'completed_at': row.Operation.completed_at,
            'status': row.Operation.status.value,
            'mappings_count': total_mappings.get(row.Operation.id, 0),
        })

    return {
        'data': data
    }