#![allow(dead_code)]
#[derive(Debug, Clone)]
pub struct BatchCountViewConfig {
pub low_threshold: u32,
pub high_threshold: u32,
pub opacity: f32,
}
impl Default for BatchCountViewConfig {
fn default() -> Self {
Self { low_threshold: 4, high_threshold: 32, opacity: 0.6 }
}
}
#[derive(Debug, Clone)]
pub struct BatchCountView {
pub config: BatchCountViewConfig,
pub enabled: bool,
}
impl Default for BatchCountView {
fn default() -> Self {
Self { config: BatchCountViewConfig::default(), enabled: false }
}
}
pub fn bcv_enable(view: &mut BatchCountView) {
view.enabled = true;
}
pub fn bcv_disable(view: &mut BatchCountView) {
view.enabled = false;
}
pub fn bcv_set_thresholds(view: &mut BatchCountView, low: u32, high: u32) {
view.config.low_threshold = low;
view.config.high_threshold = high.max(low + 1);
}
pub fn bcv_count_to_color(count: u32, config: &BatchCountViewConfig) -> [f32; 4] {
let lo = config.low_threshold as f32;
let hi = config.high_threshold as f32;
let t = ((count as f32 - lo) / (hi - lo)).clamp(0.0, 1.0);
let r = t.min(0.5) * 2.0;
let g = (1.0 - t).min(0.5) * 2.0;
[r, g, 0.0, config.opacity]
}
pub fn bcv_is_hot(count: u32, config: &BatchCountViewConfig) -> bool {
count >= config.high_threshold
}
pub fn bcv_efficiency(count: u32, config: &BatchCountViewConfig) -> f32 {
let lo = config.low_threshold.max(1) as f32;
(count as f32 / lo).min(1.0)
}
pub fn bcv_to_json(view: &BatchCountView) -> String {
format!(
r#"{{"low_threshold":{},"high_threshold":{},"enabled":{}}}"#,
view.config.low_threshold, view.config.high_threshold, view.enabled
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_default_disabled() {
let v = BatchCountView::default();
assert!(!v.enabled);
}
#[test]
fn test_enable_disable() {
let mut v = BatchCountView::default();
bcv_enable(&mut v);
assert!(v.enabled);
bcv_disable(&mut v);
assert!(!v.enabled);
}
#[test]
fn test_set_thresholds() {
let mut v = BatchCountView::default();
bcv_set_thresholds(&mut v, 2, 20);
assert_eq!(v.config.low_threshold, 2);
assert_eq!(v.config.high_threshold, 20);
}
#[test]
fn test_high_threshold_minimum() {
let mut v = BatchCountView::default();
bcv_set_thresholds(&mut v, 10, 5);
assert!(v.config.high_threshold > v.config.low_threshold);
}
#[test]
fn test_count_to_color_low() {
let cfg = BatchCountViewConfig::default();
let c = bcv_count_to_color(cfg.low_threshold, &cfg);
assert!(c[1] >= c[0]);
}
#[test]
fn test_count_to_color_high() {
let cfg = BatchCountViewConfig::default();
let c = bcv_count_to_color(cfg.high_threshold, &cfg);
assert!(c[0] >= c[1]);
}
#[test]
fn test_is_hot() {
let cfg = BatchCountViewConfig::default();
assert!(bcv_is_hot(cfg.high_threshold, &cfg));
}
#[test]
fn test_is_not_hot() {
let cfg = BatchCountViewConfig::default();
assert!(!bcv_is_hot(cfg.low_threshold, &cfg));
}
#[test]
fn test_to_json_contains_thresholds() {
let v = BatchCountView::default();
let json = bcv_to_json(&v);
assert!(json.contains("low_threshold"));
}
}