def get_operation_due_date(forthcomingoperation):
    garden_areas = get_garden_areas(
        forthcomingoperation.area_concerned.surface.garden.id)
    alert_history = ForthcomingOperation.objects.filter(
        area_concerned__in=garden_areas, is_done=True)
    computed_due_date = services.get_due_date(forthcomingoperation,
                                              alert_history)
    return computed_due_date
def get_alert_within_notification_period(garden_id,
                                         notification_delay,
                                         area_id=None):
    """ Return an array with the active alerts and their due date
     Based on the undone alerts, their original cultural operation and en eventual postponement
     """
    if area_id:
        future_alerts = get_future_alerts_by_area(area_id)
    else:
        future_alerts = get_future_alerts(garden_id)
    past_alerts = get_past_alerts(garden_id)
    time_delta = date.today() + timedelta(days=notification_delay)
    todo = []
    for a in future_alerts:
        if services.get_due_date(a, past_alerts) < time_delta:
            todo.append(a)
    return sorted(todo, key=get_operation_due_date)
def get_max_operations_date(operations, past_operations):
    """
    Iterate over the list of given operations and return the maximum due date

    :param operations: a list of operations
    :type operations: list

    :param past_operations: a list of past operations
    :type past_operations: list

    :return: the maximum due date
    :rtype: date
    """
    max_operation_date = date.today()
    for operation in operations:
        operation_due_date = services.get_due_date(operation, past_operations)
        if max_operation_date is None or operation_due_date > max_operation_date:
            max_operation_date = operation_due_date
    return max_operation_date
def get_future_work_hours_by_week(garden_id):
    """ Compute the estimated number of hours to work by week on the garden with id garden_id """

    past_operations = queries.get_past_alerts(garden_id)
    future_operations = queries.get_future_alerts(garden_id)

    x_axis = {}
    y_axis = {}
    # Get production period. Its start date will be used as the first value of the X axis.
    production_start_date = get_min_operations_date(future_operations,
                                                    past_operations)
    # Get the production end date. It will be used as the last value of the X axis.
    production_end_date = get_max_operations_date(future_operations,
                                                  past_operations)

    x_axis, y_axis = build_statistics_axis_per_week_for_productions_date(
        production_start_date=production_start_date,
        production_end_date=production_end_date)

    for fop in future_operations:
        op_week = services.get_due_date(fop, past_operations).isocalendar()[1]
        y_axis[op_week] += from_timedelta_to_hours(
            services.get_expected_duration(fop))
    return x_axis, y_axis
def get_min_operations_date(future_operations, past_operations):
    """
    Iterate over the list of given operations and return the minimum due date

    :param operations: a list of operations
    :type operations: list

    :param past_operations: a list of past operations
    :type past_operations: list

    :return: the maximum due date
    :rtype: date
    """
    min_operation_date = date.today()
    for operation in future_operations:
        operation_due_date = services.get_due_date(operation, past_operations)
        if min_operation_date is None or operation_due_date < min_operation_date:
            min_operation_date = operation_due_date
    for operation in past_operations:
        operation_due_date = Operation.objects.get(
            original_alert=operation).execution_date
        if min_operation_date is None or operation_due_date < min_operation_date:
            min_operation_date = operation_due_date
    return min_operation_date
Exemple #6
0
def due_date(alert, garden_id):
    garden_areas = queries.get_garden_areas(garden_id)
    alert_history = ForthcomingOperation.objects.filter(
        area_concerned__in=garden_areas, is_done=True)
    computed_due_date = services.get_due_date(alert, alert_history)
    return computed_due_date