Add satisfaction check to DirectorWorker: Introduced a new field for tracking the last satisfaction check and updated the calculation logic to ensure it runs only once every 24 hours. Enhanced SQL query for updating satisfaction to include a salary gap calculation, improving the accuracy of satisfaction updates based on income and title of nobility.
All checks were successful
Deploy yourpart (blue-green) / deploy (push) Successful in 1m30s

This commit is contained in:
Torsten Schulz (local)
2026-05-07 11:30:27 +02:00
parent d0ff6cad68
commit 48a374e3ac
2 changed files with 47 additions and 4 deletions

View File

@@ -108,6 +108,7 @@ pub struct DirectorWorker {
base: BaseWorker, base: BaseWorker,
last_run: Option<Instant>, last_run: Option<Instant>,
last_resignation_check: Option<Instant>, last_resignation_check: Option<Instant>,
last_satisfaction_check: Option<Instant>,
} }
// Maximale Anzahl paralleler Produktionen pro Branch // Maximale Anzahl paralleler Produktionen pro Branch
@@ -132,6 +133,7 @@ impl DirectorWorker {
base: BaseWorker::new("DirectorWorker", pool, broker), base: BaseWorker::new("DirectorWorker", pool, broker),
last_run: None, last_run: None,
last_resignation_check: None, last_resignation_check: None,
last_satisfaction_check: None,
} }
} }
@@ -1491,6 +1493,15 @@ impl DirectorWorker {
} }
fn calculate_satisfaction(&mut self) -> Result<(), DbError> { 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 self.base
.set_current_step("DirectorWorker: calculate_satisfaction"); .set_current_step("DirectorWorker: calculate_satisfaction");
@@ -1520,6 +1531,7 @@ impl DirectorWorker {
self.base.broker.publish(message); self.base.broker.publish(message);
} }
self.last_satisfaction_check = Some(now);
Ok(()) Ok(())
} }
} }

View File

@@ -624,14 +624,45 @@ UPDATE falukant_data.director SET last_salary_payout = NOW() WHERE id = $1;
"#; "#;
pub const QUERY_UPDATE_SATISFACTION: &str = r#" pub const QUERY_UPDATE_SATISFACTION: &str = r#"
WITH new_sats AS ( WITH salary_gap AS (
SELECT d.id, ROUND(d.income::numeric / (c.title_of_nobility * POWER(1.231, AVG(k.knowledge) / 1.5)) * 100) AS new_satisfaction 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 FROM falukant_data.director d
JOIN falukant_data.knowledge k ON d.director_character_id = k.character_id 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 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#" pub const QUERY_GET_DIRECTOR_USER: &str = r#"