Fix daily runtime calculation
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Pierre HUBERT 2024-12-29 14:30:32 +01:00
parent e74f7d6f6d
commit 72afa3df62
2 changed files with 15 additions and 5 deletions

View File

@ -1,7 +1,7 @@
use crate::app_config::AppConfig; use crate::app_config::AppConfig;
use crate::devices::device::{DeviceRelay, DeviceRelayID}; use crate::devices::device::{DeviceRelay, DeviceRelayID};
use crate::utils::files_utils; use crate::utils::files_utils;
use crate::utils::time_utils::{day_number, time_start_of_day}; use crate::utils::time_utils::{day_number, time_secs, time_start_of_day};
const TIME_INTERVAL: usize = 30; const TIME_INTERVAL: usize = 30;
@ -128,9 +128,19 @@ pub fn relay_total_runtime_adjusted(relay: &DeviceRelay) -> usize {
.unwrap_or(0); .unwrap_or(0);
let time_start_day = time_start_of_day().unwrap_or(1726696800); let time_start_day = time_start_of_day().unwrap_or(1726696800);
let start_time = time_start_day + reset_time as u64;
let end_time = time_start_day + 3600 * 24 + reset_time as u64; // Check if we have reached reset_time today yet or not
relay_total_runtime(relay.id, start_time, end_time).unwrap_or(3600 * 24) if time_start_day + reset_time as u64 >= time_secs() {
let start_time = time_start_day + reset_time as u64;
let end_time = time_start_day + 3600 * 24 + reset_time as u64;
relay_total_runtime(relay.id, start_time, end_time).unwrap_or(3600 * 24)
} else {
// If we have not reached reset time yet, we need to focus on previous day
let time_start_yesterday = time_start_day - 3600 * 24;
let start_time = time_start_yesterday + reset_time as u64;
let end_time = time_start_day + reset_time as u64;
relay_total_runtime(relay.id, start_time, end_time).unwrap_or(3600 * 24)
}
} }
#[cfg(test)] #[cfg(test)]

View File

@ -1,7 +1,7 @@
use chrono::prelude::*; use chrono::prelude::*;
use std::time::{SystemTime, UNIX_EPOCH}; use std::time::{SystemTime, UNIX_EPOCH};
/// Get the current time since epoch /// Get the current time since epoch, in seconds
pub fn time_secs() -> u64 { pub fn time_secs() -> u64 {
SystemTime::now() SystemTime::now()
.duration_since(UNIX_EPOCH) .duration_since(UNIX_EPOCH)