| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
use super::shared::{CssClass, DbValue, ParseableEnum}; |
| 16 |
use crate::id_types::{ProblemId, ProjectId, TaskId}; |
| 17 |
use chrono::{DateTime, Utc}; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
pub use painhours::Priority as ProblemBand; |
| 22 |
use serde::{Deserialize, Serialize}; |
| 23 |
use strum_macros::EnumString; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Default, EnumString)] |
| 30 |
pub enum ProblemStatus { |
| 31 |
|
| 32 |
#[strum(serialize = "Open")] |
| 33 |
#[default] |
| 34 |
Open, |
| 35 |
|
| 36 |
#[strum(serialize = "Promoted")] |
| 37 |
Promoted, |
| 38 |
|
| 39 |
#[strum(serialize = "Dismissed")] |
| 40 |
Dismissed, |
| 41 |
|
| 42 |
#[strum(serialize = "Resolved")] |
| 43 |
Resolved, |
| 44 |
} |
| 45 |
|
| 46 |
impl ProblemStatus { |
| 47 |
|
| 48 |
pub fn as_str(&self) -> &'static str { |
| 49 |
match self { |
| 50 |
ProblemStatus::Open => "Open", |
| 51 |
ProblemStatus::Promoted => "Promoted", |
| 52 |
ProblemStatus::Dismissed => "Dismissed", |
| 53 |
ProblemStatus::Resolved => "Resolved", |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
pub fn is_settled(&self) -> bool { |
| 60 |
!matches!(self, ProblemStatus::Open) |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
impl ParseableEnum for ProblemStatus {} |
| 65 |
|
| 66 |
impl DbValue for ProblemStatus { |
| 67 |
fn db_value(&self) -> &'static str { |
| 68 |
match self { |
| 69 |
ProblemStatus::Open => "Open", |
| 70 |
ProblemStatus::Promoted => "Promoted", |
| 71 |
ProblemStatus::Dismissed => "Dismissed", |
| 72 |
ProblemStatus::Resolved => "Resolved", |
| 73 |
} |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
impl CssClass for ProblemStatus { |
| 78 |
fn css_class(&self) -> &'static str { |
| 79 |
match self { |
| 80 |
ProblemStatus::Open => "problem-open", |
| 81 |
ProblemStatus::Promoted => "problem-promoted", |
| 82 |
ProblemStatus::Dismissed => "problem-dismissed", |
| 83 |
ProblemStatus::Resolved => "problem-resolved", |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
#[derive(Debug, Clone, Serialize, Deserialize)] |
| 90 |
#[serde(rename_all = "camelCase")] |
| 91 |
pub struct Problem { |
| 92 |
|
| 93 |
pub id: ProblemId, |
| 94 |
|
| 95 |
pub source: String, |
| 96 |
|
| 97 |
pub source_ref: String, |
| 98 |
|
| 99 |
pub title: String, |
| 100 |
|
| 101 |
pub body: String, |
| 102 |
|
| 103 |
pub pain: u8, |
| 104 |
|
| 105 |
pub scale: u8, |
| 106 |
|
| 107 |
pub status: ProblemStatus, |
| 108 |
|
| 109 |
pub project_id: Option<ProjectId>, |
| 110 |
|
| 111 |
pub tags: Vec<String>, |
| 112 |
|
| 113 |
|
| 114 |
pub promoted_task_id: Option<TaskId>, |
| 115 |
|
| 116 |
pub created_at: DateTime<Utc>, |
| 117 |
|
| 118 |
pub updated_at: DateTime<Utc>, |
| 119 |
|
| 120 |
pub settled_at: Option<DateTime<Utc>>, |
| 121 |
|
| 122 |
|
| 123 |
pub last_seen_at: DateTime<Utc>, |
| 124 |
} |
| 125 |
|
| 126 |
impl Problem { |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
fn age_anchor(&self) -> DateTime<Utc> { |
| 133 |
if self.status.is_settled() { |
| 134 |
self.settled_at.unwrap_or(self.updated_at) |
| 135 |
} else { |
| 136 |
Utc::now() |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
pub fn age_weeks(&self) -> u32 { |
| 142 |
painhours::age_weeks(self.created_at, self.age_anchor()) |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
pub fn painhours(&self) -> u32 { |
| 149 |
painhours::painhours(self.pain, self.scale, self.age_weeks()) |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
pub fn band(&self) -> ProblemBand { |
| 154 |
ProblemBand::from_painhours(self.painhours()) |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
pub fn age(&self) -> String { |
| 159 |
painhours::age_label(self.created_at, Utc::now()) |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
pub fn is_stale(&self, source_last_pulled_at: DateTime<Utc>) -> bool { |
| 167 |
self.last_seen_at < source_last_pulled_at |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
#[derive(Debug, Clone, Serialize, Deserialize)] |
| 176 |
pub struct NewProblem { |
| 177 |
pub source: String, |
| 178 |
pub source_ref: String, |
| 179 |
pub title: String, |
| 180 |
pub body: String, |
| 181 |
pub pain: u8, |
| 182 |
pub scale: u8, |
| 183 |
pub project_id: Option<ProjectId>, |
| 184 |
pub tags: Vec<String>, |
| 185 |
|
| 186 |
pub created_at: DateTime<Utc>, |
| 187 |
|
| 188 |
pub updated_at: DateTime<Utc>, |
| 189 |
|
| 190 |
|
| 191 |
pub resolved_upstream: bool, |
| 192 |
} |
| 193 |
|
| 194 |
#[cfg(test)] |
| 195 |
mod tests { |
| 196 |
use super::*; |
| 197 |
use chrono::Duration; |
| 198 |
|
| 199 |
fn problem(status: ProblemStatus, age: Duration) -> Problem { |
| 200 |
let now = Utc::now(); |
| 201 |
Problem { |
| 202 |
id: ProblemId::new(), |
| 203 |
source: "wam".into(), |
| 204 |
source_ref: "abc123".into(), |
| 205 |
title: "it breaks".into(), |
| 206 |
body: String::new(), |
| 207 |
pain: 3, |
| 208 |
scale: 3, |
| 209 |
status, |
| 210 |
project_id: None, |
| 211 |
tags: vec![], |
| 212 |
promoted_task_id: None, |
| 213 |
created_at: now - age, |
| 214 |
updated_at: now, |
| 215 |
settled_at: status.is_settled().then_some(now - age + Duration::days(1)), |
| 216 |
last_seen_at: now, |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
#[test] |
| 221 |
fn problem_status_db_value_round_trips() { |
| 222 |
for st in [ |
| 223 |
ProblemStatus::Open, |
| 224 |
ProblemStatus::Promoted, |
| 225 |
ProblemStatus::Dismissed, |
| 226 |
ProblemStatus::Resolved, |
| 227 |
] { |
| 228 |
assert_eq!(ProblemStatus::from_str_or_default(st.db_value()), st); |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
#[test] |
| 233 |
fn unknown_status_falls_back_to_open() { |
| 234 |
assert_eq!( |
| 235 |
ProblemStatus::from_str_or_default("bogus"), |
| 236 |
ProblemStatus::Open |
| 237 |
); |
| 238 |
} |
| 239 |
|
| 240 |
#[test] |
| 241 |
fn only_open_is_unsettled() { |
| 242 |
assert!(!ProblemStatus::Open.is_settled()); |
| 243 |
assert!(ProblemStatus::Promoted.is_settled()); |
| 244 |
assert!(ProblemStatus::Dismissed.is_settled()); |
| 245 |
assert!(ProblemStatus::Resolved.is_settled()); |
| 246 |
} |
| 247 |
|
| 248 |
#[test] |
| 249 |
fn open_problems_climb_with_age() { |
| 250 |
let fresh = problem(ProblemStatus::Open, Duration::days(1)); |
| 251 |
let old = problem(ProblemStatus::Open, Duration::days(60)); |
| 252 |
assert!(old.painhours() > fresh.painhours()); |
| 253 |
} |
| 254 |
|
| 255 |
#[test] |
| 256 |
fn settling_freezes_the_score() { |
| 257 |
|
| 258 |
|
| 259 |
let age = Duration::days(90); |
| 260 |
let open = problem(ProblemStatus::Open, age); |
| 261 |
for settled in [ |
| 262 |
ProblemStatus::Promoted, |
| 263 |
ProblemStatus::Dismissed, |
| 264 |
ProblemStatus::Resolved, |
| 265 |
] { |
| 266 |
let p = problem(settled, age); |
| 267 |
assert_eq!(p.age_weeks(), 1, "{settled:?} must freeze at settled_at"); |
| 268 |
assert!(p.painhours() < open.painhours(), "{settled:?}"); |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
#[test] |
| 273 |
fn settled_without_a_timestamp_falls_back_to_updated_at() { |
| 274 |
let mut p = problem(ProblemStatus::Promoted, Duration::days(90)); |
| 275 |
p.settled_at = None; |
| 276 |
|
| 277 |
assert_eq!(p.age_weeks(), 13); |
| 278 |
} |
| 279 |
|
| 280 |
#[test] |
| 281 |
fn staleness_is_relative_to_the_last_pull() { |
| 282 |
let p = problem(ProblemStatus::Open, Duration::days(1)); |
| 283 |
assert!(!p.is_stale(p.last_seen_at)); |
| 284 |
assert!(p.is_stale(p.last_seen_at + Duration::seconds(1))); |
| 285 |
} |
| 286 |
} |
| 287 |
|