use crate::target::BUCKETS;
use crate::{Error, Result, Timestamp};
use serde::{Deserialize, Serialize};
pub const MAX_STAGES: usize = 16;
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Stage {
pub percent_bp: u16,
pub start_at: Timestamp,
#[serde(default)]
pub min_dwell_secs: u64,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct RolloutPlan {
pub stages: Vec<Stage>,
}
impl RolloutPlan {
pub fn immediate(at: Timestamp) -> Self {
Self { stages: vec![Stage { percent_bp: BUCKETS, start_at: at, min_dwell_secs: 0 }] }
}
pub fn validate(&self) -> Result<()> {
if self.stages.is_empty() || self.stages.len() > MAX_STAGES {
return Err(Error::invalid("rollout", format!("needs 1..={MAX_STAGES} stages")));
}
for s in &self.stages {
if s.percent_bp == 0 || s.percent_bp > BUCKETS {
return Err(Error::invalid("rollout", "stage share must be 1..=10000 basis points"));
}
}
for pair in self.stages.windows(2) {
let (a, b) = (&pair[0], &pair[1]);
if b.percent_bp <= a.percent_bp {
return Err(Error::invalid("rollout", "each stage must reach more devices than the last"));
}
if b.start_at < a.start_at.plus(a.min_dwell_secs) {
return Err(Error::invalid("rollout", "a stage starts before the previous stage's dwell ends"));
}
}
Ok(())
}
pub fn current_bp(&self, now: Timestamp, cap_bp: Option<u16>) -> u16 {
let live = self.stages.iter().filter(|s| s.start_at <= now).map(|s| s.percent_bp).max().unwrap_or(0);
cap_bp.map_or(live, |c| live.min(c))
}
pub fn includes(&self, bucket: u16, now: Timestamp, cap_bp: Option<u16>) -> bool {
bucket < self.current_bp(now, cap_bp)
}
}