diff --git a/server/db/days.rs b/server/db/days.rs index f4ad104..fc46989 100644 --- a/server/db/days.rs +++ b/server/db/days.rs @@ -4,7 +4,7 @@ //! In-memory indexes by calendar day. -use crate::recording::{self, Time}; +use base::time::{Duration, Time, TIME_UNITS_PER_SEC}; use failure::Error; use log::{error, trace}; use smallvec::SmallVec; @@ -30,12 +30,12 @@ impl Key { let mut my_tm = time::strptime(self.as_ref(), "%Y-%m-%d").expect("days must be parseable"); my_tm.tm_utcoff = 1; // to the time crate, values != 0 mean local time. my_tm.tm_isdst = -1; - let start = Time(my_tm.to_timespec().sec * recording::TIME_UNITS_PER_SEC); + let start = Time(my_tm.to_timespec().sec * TIME_UNITS_PER_SEC); my_tm.tm_hour = 0; my_tm.tm_min = 0; my_tm.tm_sec = 0; my_tm.tm_mday += 1; - let end = Time(my_tm.to_timespec().sec * recording::TIME_UNITS_PER_SEC); + let end = Time(my_tm.to_timespec().sec * TIME_UNITS_PER_SEC); start..end } } @@ -64,7 +64,7 @@ pub struct StreamValue { /// The total duration recorded on this day. This can be 0; because frames' durations are taken /// from the time of the next frame, a recording that ends unexpectedly after a single frame /// will have 0 duration of that frame and thus the whole recording. - pub duration: recording::Duration, + pub duration: Duration, } impl Value for StreamValue { @@ -84,7 +84,7 @@ impl Value for StreamValue { pub struct SignalValue { /// `states[i]` represents the amount of time spent in state `i+1`. /// (The signal is the unknown state, 0, for the remainder of the time.) - pub states: SmallVec<[u32; 4]>, + pub states: SmallVec<[u64; 4]>, } impl Value for SignalValue { @@ -99,7 +99,7 @@ impl Value for SignalValue { // add to new state. let s = &mut self.states[c.new_state as usize - 1]; let n = s - .checked_add(c.duration) + .checked_add(u64::try_from(c.duration.0).unwrap()) .unwrap_or_else(|| panic!("add range violation: s={:?} c={:?}", s, c)); *s = n; } @@ -115,7 +115,7 @@ impl Value for SignalValue { ); let s = &mut self.states[c.old_state as usize - 1]; let n = s - .checked_sub(c.duration) + .checked_sub(u64::try_from(c.duration.0).unwrap()) .unwrap_or_else(|| panic!("sub range violation: s={:?} c={:?}", s, c)); *s = n; } @@ -137,7 +137,7 @@ impl Value for SignalValue { #[derive(Debug)] pub struct SignalChange { /// The duration of time being altered. - duration: u32, + duration: Duration, /// The state of the given range before this change. old_state: i16, @@ -204,7 +204,7 @@ impl Map { Err(ref e) => { error!( "Unable to fill first day key from {:?}->{:?}: {}; will ignore.", - r.start, my_tm, e + r, my_tm, e ); return; } @@ -218,12 +218,12 @@ impl Map { my_tm.tm_sec = 0; my_tm.tm_mday += 1; let boundary = my_tm.to_timespec(); - let boundary_90k = boundary.sec * recording::TIME_UNITS_PER_SEC; + let boundary_90k = boundary.sec * TIME_UNITS_PER_SEC; // Adjust the first day. let first_day_delta = StreamValue { recordings: sign, - duration: recording::Duration(sign * (cmp::min(r.end.0, boundary_90k) - r.start.0)), + duration: Duration(sign * (cmp::min(r.end.0, boundary_90k) - r.start.0)), }; self.adjust_day(day, first_day_delta); @@ -247,7 +247,7 @@ impl Map { }; let second_day_delta = StreamValue { recordings: sign, - duration: recording::Duration(sign * (r.end.0 - boundary_90k)), + duration: Duration(sign * (r.end.0 - boundary_90k)), }; self.adjust_day(day, second_day_delta); } @@ -260,18 +260,16 @@ impl Map { /// /// This function swallows/logs date formatting errors because they shouldn't happen and there's /// not much that can be done about them. (The database operation has already gone through.) - pub(crate) fn adjust(&mut self, mut r: Range, old_state: i16, new_state: i16) { + pub(crate) fn adjust(&mut self, mut r: Range