diff --git a/src/worker/director.rs b/src/worker/director.rs index 7fd779e..5abb39f 100644 --- a/src/worker/director.rs +++ b/src/worker/director.rs @@ -108,6 +108,7 @@ pub struct DirectorWorker { base: BaseWorker, last_run: Option, last_resignation_check: Option, + last_satisfaction_check: Option, } // Maximale Anzahl paralleler Produktionen pro Branch @@ -132,6 +133,7 @@ impl DirectorWorker { base: BaseWorker::new("DirectorWorker", pool, broker), last_run: None, last_resignation_check: None, + last_satisfaction_check: None, } } @@ -1491,6 +1493,15 @@ impl DirectorWorker { } fn calculate_satisfaction(&mut self) -> Result<(), DbError> { + let now = Instant::now(); + let should_run = match self.last_satisfaction_check { + None => true, + Some(last) => now.saturating_duration_since(last) >= Duration::from_secs(24 * 60 * 60), + }; + if !should_run { + return Ok(()); + } + self.base .set_current_step("DirectorWorker: calculate_satisfaction"); @@ -1520,6 +1531,7 @@ impl DirectorWorker { self.base.broker.publish(message); } + self.last_satisfaction_check = Some(now); Ok(()) } } diff --git a/src/worker/sql.rs b/src/worker/sql.rs index 9198ef6..334b19b 100644 --- a/src/worker/sql.rs +++ b/src/worker/sql.rs @@ -624,14 +624,45 @@ UPDATE falukant_data.director SET last_salary_payout = NOW() WHERE id = $1; "#; pub const QUERY_UPDATE_SATISFACTION: &str = r#" -WITH new_sats AS ( - SELECT d.id, ROUND(d.income::numeric / (c.title_of_nobility * POWER(1.231, AVG(k.knowledge) / 1.5)) * 100) AS new_satisfaction +WITH salary_gap AS ( + SELECT + d.id, + d.employer_user_id, + COALESCE(d.satisfaction, 100)::int AS old_satisfaction, + ( + 100.0 + - LEAST( + 200.0, + (d.income::float8 / GREATEST(1.0, (c.title_of_nobility::float8 * POWER(1.231, AVG(k.knowledge) / 1.5)))) * 100.0 + ) + ) AS gap_percent FROM falukant_data.director d JOIN falukant_data.knowledge k ON d.director_character_id = k.character_id JOIN falukant_data.character c ON c.id = d.director_character_id - GROUP BY d.id, c.title_of_nobility, d.income + GROUP BY d.id, d.employer_user_id, d.satisfaction, d.income, c.title_of_nobility +), +new_sats AS ( + SELECT + sg.id, + sg.employer_user_id, + GREATEST( + 0, + sg.old_satisfaction + - CASE + WHEN sg.gap_percent <= 0 THEN 0 + WHEN sg.gap_percent < 20 THEN 1 + WHEN sg.gap_percent < 50 THEN 2 + ELSE 3 + END + )::int AS new_satisfaction + FROM salary_gap sg ) -UPDATE falukant_data.director dir SET satisfaction = ns.new_satisfaction FROM new_sats ns WHERE dir.id = ns.id AND dir.satisfaction IS DISTINCT FROM ns.new_satisfaction RETURNING dir.employer_user_id; +UPDATE falukant_data.director dir + SET satisfaction = ns.new_satisfaction + FROM new_sats ns + WHERE dir.id = ns.id + AND dir.satisfaction IS DISTINCT FROM ns.new_satisfaction +RETURNING dir.employer_user_id; "#; pub const QUERY_GET_DIRECTOR_USER: &str = r#"