Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add IntervalData struct #5205

Merged
merged 6 commits into from
Oct 28, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES_UNRELEASED.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ Use the template below to make assigning a version number during the release cut

### What's Changed
- Disabled Glean events recorded when the SDK is not ready for a feature ([#5185](https://github.com/mozilla/application-services/pull/5185))
- Add struct for IntervalData (behavioral targeting) ([#5205](https://github.com/mozilla/application-services/pull/5205))
69 changes: 68 additions & 1 deletion components/nimbus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ use once_cell::sync::OnceCell;
use persistence::{Database, StoreId, Writer};
use serde_derive::*;
use serde_json::{Map, Value};
use std::collections::HashSet;
use std::collections::{HashSet, VecDeque};
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};
use std::time::Instant;
use updating::{read_and_remove_pending_experiments, write_pending_experiments};
use uuid::Uuid;

Expand Down Expand Up @@ -796,6 +797,72 @@ impl NimbusTargetingHelper {

type JsonObject = Map<String, Value>;

pub struct IntervalData {
jeddai marked this conversation as resolved.
Show resolved Hide resolved
buckets: VecDeque<i32>,
starting_instant: Instant,
}

impl IntervalData {
pub fn new() -> IntervalData {
jeddai marked this conversation as resolved.
Show resolved Hide resolved
let mut data = IntervalData {
buckets: VecDeque::new(),
starting_instant: Instant::now(),
};
data.buckets.push_front(0);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both the increment and rotate handle this case, I don't think you necessarily need to push an i32 into it here in the constructor.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since they no longer handle this is this resolved?

return data;
}

fn increment(&mut self) {
match self.buckets.front_mut() {
Some(x) => *x += 1,
None => {
self.buckets.push_front(1);
}
};
}

fn rotate(&mut self, num_rotations: i32) {
for _ in 1..=num_rotations {
self.buckets.push_front(0)
jeddai marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

#[cfg(test)]
mod interval_data_unit_tests {
use super::*;

#[test]
fn increment_works_if_no_buckets_present() -> Result<()> {
let mut interval = IntervalData {
buckets: VecDeque::new(),
starting_instant: Instant::now(),
};
interval.increment();

assert!(matches!(interval.buckets[0], 1));
Ok(())
}

#[test]
fn increment_increments_front_bucket_if_it_exists() -> Result<()> {
let mut interval = IntervalData::new();
interval.increment();

assert!(matches!(interval.buckets[0], 1));
Ok(())
}

#[test]
fn rotate_adds_buckets_for_each_rotation() -> Result<()> {
let mut interval = IntervalData::new();
interval.rotate(3);

assert!(matches!(interval.buckets.len(), 4));
Ok(())
}
}

#[cfg(feature = "uniffi-bindings")]
impl UniffiCustomTypeConverter for JsonObject {
type Builtin = String;
Expand Down