Skip to content

Commit

Permalink
fix(accumulator): Allow zero duration granularity
Browse files Browse the repository at this point in the history
  • Loading branch information
Dav1dde committed Feb 29, 2024
1 parent 5dc57aa commit 622677e
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/accumulator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,11 @@ impl UsageAccumulator {
amount: u64,
usage_unit: UsageUnit,
) {
let quantized_timestamp: DateTime<Utc> =
usage_time.duration_trunc(self.granularity).unwrap();
let quantized_timestamp = if self.granularity.is_zero() {
usage_time
} else {
usage_time.duration_trunc(self.granularity).unwrap()
};

if self.first_timestamp.is_none() {
self.first_timestamp = Some(quantized_timestamp);
Expand All @@ -75,9 +78,12 @@ impl UsageAccumulator {
/// and at least `granularity` seconds have passed since
/// the first chunk of data was added.
pub fn should_flush(&self, current_time: DateTime<Utc>) -> bool {
return self.first_timestamp.is_some()
&& self.usage_batch.keys().len() > 0
&& current_time - self.first_timestamp.unwrap() > self.granularity;
let Some(first_timestamp) = self.first_timestamp else {
return false;
};

return self.usage_batch.keys().len() > 0
&& current_time - first_timestamp >= self.granularity;
}

/// Return the current bucket and clears up the state.
Expand Down

0 comments on commit 622677e

Please sign in to comment.