Skip to content

Commit fb4fec4

Browse files
committed
refactor: move action filtering logic to process_actions function
This separates the filtering and sorting logic from the Lambda handler, making the code more modular and enabling tests to reuse the same logic as production. Removed duplicate test-only implementation.
1 parent c0b830e commit fb4fec4

File tree

1 file changed

+29
-53
lines changed

1 file changed

+29
-53
lines changed

src/main.rs

Lines changed: 29 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use lambda_runtime::{service_fn, Error, LambdaEvent};
33
use serde::{Deserialize, Serialize};
44
use serde_json::{json, Value};
55
use std::cmp::{Eq, Ordering, PartialOrd};
6-
use std::collections::HashMap;
76

87
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq, PartialOrd, Ord)]
98
#[serde(rename_all = "lowercase")]
@@ -53,36 +52,45 @@ async fn filter_actions(event: LambdaEvent<Value>) -> Result<Value, Error> {
5352
event.payload.as_array().map(|v| v.len()).unwrap_or(0),
5453
);
5554

56-
let now = Utc::now();
57-
5855
let (value, _context) = event.into_parts();
5956
let input: Vec<Action> = serde_json::from_value(value)?;
6057

61-
let mut selected: HashMap<String, Action> = HashMap::new();
58+
let actions = process_actions(input);
59+
60+
tracing::info!("Returning {} filtered actions", actions.len());
6261

63-
for action in input {
64-
let days_since_last = (now - action.last_action_time).to_std()?.as_secs() / 86400;
65-
if days_since_last < 7 {
66-
tracing::debug!(%action.entity_id, "Skipping: last action too recent");
67-
continue;
68-
}
62+
Ok(json!(actions))
63+
}
6964

70-
if action.next_action_time > now + Duration::days(90) {
71-
tracing::debug!("Skipping (future): {}", action.entity_id);
72-
continue;
73-
}
74-
tracing::info!("Selecting action: {}", action.entity_id);
65+
fn process_actions(input: Vec<Action>) -> Vec<Action> {
66+
// ---
7567

76-
selected.entry(action.entity_id.clone()).or_insert(action);
68+
let today = Utc::now();
69+
70+
let filtered: Vec<Action> = input
71+
.into_iter()
72+
.filter(|a| {
73+
// Skip if next_action_time is more than 90 days away
74+
a.next_action_time <= today + Duration::days(90)
75+
})
76+
.filter(|a| {
77+
// Skip if last_action_time is within the past 7 days
78+
a.last_action_time <= today - Duration::days(7)
79+
})
80+
.collect();
81+
82+
// Deduplicate: keep one per entity_id (last one wins)
83+
use std::collections::HashMap;
84+
let mut map = HashMap::new();
85+
for action in filtered {
86+
map.insert(action.entity_id.clone(), action);
7787
}
7888

79-
let mut actions: Vec<Action> = selected.into_iter().map(|(_, v)| v).collect();
89+
let mut deduped: Vec<Action> = map.into_values().collect();
8090

81-
actions.sort_by_key(|a| a.priority.clone());
82-
83-
tracing::info!("Returning {} filtered actions", actions.len());
91+
deduped.sort_by(|a, b| a.priority.cmp(&b.priority));
8492

85-
Ok(json!(actions))
93+
deduped
8694
}
8795

8896
#[cfg(test)]
@@ -97,38 +105,6 @@ mod tests {
97105
let temp = DateTime::parse_from_rfc3339(s)?;
98106
Ok(temp.with_timezone(&Utc))
99107
}
100-
101-
fn process_actions(input: Vec<Action>) -> Vec<Action> {
102-
// ---
103-
104-
let today = Utc::now();
105-
106-
let filtered: Vec<Action> = input
107-
.into_iter()
108-
.filter(|a| {
109-
// Skip if next_action_time is more than 90 days away
110-
a.next_action_time <= today + chrono::Duration::days(90)
111-
})
112-
.filter(|a| {
113-
// Skip if last_action_time is within the past 7 days
114-
a.last_action_time <= today - chrono::Duration::days(7)
115-
})
116-
.collect();
117-
118-
// Deduplicate: keep one per entity_id (last one wins)
119-
use std::collections::HashMap;
120-
let mut map = HashMap::new();
121-
for action in filtered {
122-
map.insert(action.entity_id.clone(), action);
123-
}
124-
125-
let mut deduped: Vec<Action> = map.into_values().collect();
126-
127-
deduped.sort_by(|a, b| a.priority.cmp(&b.priority));
128-
129-
deduped
130-
}
131-
132108
#[test]
133109
fn test_filter_and_sort_actions() -> Result<()> {
134110
// ---

0 commit comments

Comments
 (0)