Improved backup schedule generator to fix some corner cases.

Add backup schedule details at router details page
This commit is contained in:
Eduardo Silva 2024-04-12 15:18:12 -03:00
parent 1f5bbf1608
commit a9215845bf
2 changed files with 53 additions and 11 deletions

View file

@ -12,23 +12,26 @@ from routerlib.backup_functions import perform_backup
def next_weekday(now, weekday, hour): def next_weekday(now, weekday, hour):
now = timezone.localtime(timezone.now())
days_ahead = weekday - now.weekday() days_ahead = weekday - now.weekday()
if days_ahead < 0 or (days_ahead == 0 and now.hour >= hour): # if backup date is for today and hour has passed, move to next week if days_ahead < 0 or (days_ahead == 0 and now.hour >= hour):
days_ahead += 7 days_ahead += 7
next_backup = now + timedelta(days=days_ahead) next_backup = now + timedelta(days=days_ahead)
return next_backup.replace(hour=hour, minute=0, second=0, microsecond=0) next_backup = next_backup.replace(hour=hour, minute=0, second=0, microsecond=0)
return next_backup
def find_next_active_day(start_date, active_days, backup_hour): def find_next_active_day(start_date, active_days, backup_hour):
for i in range(7): # Verifica os próximos 7 dias now = timezone.localtime()
potential_date = start_date + timedelta(days=i) for i in range(7):
if active_days[potential_date.weekday()]: potential_date = timezone.localtime(start_date + timedelta(days=i))
weekday = potential_date.weekday()
if active_days[weekday]:
next_active_date = potential_date.replace(hour=backup_hour, minute=0, second=0, microsecond=0) next_active_date = potential_date.replace(hour=backup_hour, minute=0, second=0, microsecond=0)
if next_active_date > timezone.now(): if next_active_date > now:
return next_active_date return next_active_date
# Se já passou a hora no primeiro dia válido, procura no dia correspondente da próxima semana if i == 0 and now.hour >= backup_hour:
if i == 0: continue
return potential_date + timedelta(days=7, hours=(backup_hour - potential_date.hour))
return None return None
@ -59,7 +62,6 @@ def calculate_next_backup(backup_profile):
potential_monthly_backup = datetime(year=now.year, month=now.month, day=backup_profile.monthly_day, potential_monthly_backup = datetime(year=now.year, month=now.month, day=backup_profile.monthly_day,
hour=backup_profile.monthly_hour, tzinfo=timezone.get_current_timezone()) hour=backup_profile.monthly_hour, tzinfo=timezone.get_current_timezone())
if potential_monthly_backup <= now: if potential_monthly_backup <= now:
# Se o dia do mês já passou, ou é hoje mas a hora já passou, agenda para o próximo mês
month_increment = 1 if now.month < 12 else -11 month_increment = 1 if now.month < 12 else -11
year_increment = 0 if now.month < 12 else 1 year_increment = 0 if now.month < 12 else 1
next_monthly_backup = potential_monthly_backup.replace(year=now.year + year_increment, next_monthly_backup = potential_monthly_backup.replace(year=now.year + year_increment,

View file

@ -117,6 +117,18 @@
<div class="card card-primary card-outline"> <div class="card card-primary card-outline">
<div class="card-header"> <div class="card-header">
<h3 class="card-title">Backup history</h3> <h3 class="card-title">Backup history</h3>
<span class="float-right">
{% if router.backupschedule %}
<a onclick="$('.backup-schedule-information').slideToggle();" href="#">
show schedule <i class="far fa-clock"></i>
</a>
{% else %}
<a onclick="alert('There\'s no backup schedule for this equipment.');" href="#">
show schedule <i class="far fa-clock"></i>
</a>
{% endif %}
</span>
</div> </div>
<div class="card-body row"> <div class="card-body row">
<div class="col-lg-12"> <div class="col-lg-12">
@ -131,6 +143,34 @@
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
{% if router.backupschedule.next_daily_backup %}
<tr class="backup-schedule-information" style="display: none;">
<td><i class="far fa-clock text-info"></i></td>
<td>Daily</td>
<td>{{ router.backupschedule.next_daily_backup }}</td>
<td><i class="far fa-clock text-info"></i></td>
<td>-</td>
</tr>
{% endif %}
{% if router.backupschedule.next_weekly_backup %}
<tr class="backup-schedule-information" style="display: none;">
<td><i class="far fa-clock text-info"></i></td>
<td>Weekly</td>
<td>{{ router.backupschedule.next_weekly_backup }}</td>
<td><i class="far fa-clock text-info"></i></td>
<td>-</td>
</tr>
{% endif %}
{% if router.backupschedule.next_monthly_backup %}
<tr class="backup-schedule-information" style="display: none;">
<td><i class="far fa-clock text-info"></i></td>
<td>Monthly</td>
<td>{{ router.backupschedule.next_monthly_backup }}</td>
<td><i class="far fa-clock text-info"></i></td>
<td>-</td>
</tr>
{% endif %}
{% for backup in router_backup_list %} {% for backup in router_backup_list %}
<tr> <tr>
<td class="min-width"> <td class="min-width">
@ -140,7 +180,7 @@
</td> </td>
<td>{{ backup.get_schedule_type_display }}</td> <td>{{ backup.get_schedule_type_display }}</td>
<td>{% if backup.finish_time %}{{ backup.finish_time }}{% else %} <td>{% if backup.finish_time %}{{ backup.finish_time }}{% else %}
<i class="far fa-clock text-warning"></i>{% endif %}</td> {% if not backup.error %}<i class="far fa-clock text-warning"></i>{% endif %}{% endif %}</td>
<td> <td>
{% if backup.success %} {% if backup.success %}
<i class="far fa-check-circle text-success"></i> <i class="far fa-check-circle text-success"></i>