17
17
// You should have received a copy of the GNU Affero General Public License
18
18
// along with this program. If not, see <http://www.gnu.org/licenses/>.
19
19
20
- use std:: collections:: BTreeMap ;
21
-
22
20
use anyhow:: { bail, Context } ;
23
21
use chrono:: { DateTime , LocalResult , TimeZone , Utc } ;
24
22
25
23
const LAMBDA_SOURCE_ID_PREFIX : & str = "ingest-lambda-source-" ;
26
24
27
25
/// This duration should be large enough to prevent repeated notification
28
26
/// deliveries from causing duplicates
29
- const FILE_SOURCE_RETENTION_HOURS : usize = 6 ;
27
+ const FILE_SOURCE_RETENTION_SECONDS : i64 = 6 * 3600 ;
30
28
31
29
/// Create a source id for a Lambda file source, with the provided timestamp encoded in it
32
30
pub ( crate ) fn create_lambda_source_id ( time : DateTime < Utc > ) -> String {
@@ -44,20 +42,19 @@ fn parse_source_id_timestamp(source_id: &str) -> anyhow::Result<DateTime<Utc>> {
44
42
}
45
43
}
46
44
47
- /// Parse the provided source ids and return those where the file source is
48
- /// older than `MIN_FILE_SOURCE_RETENTION_HOURS` hours
45
+ /// Parse the provided source ids and return those where it's a Lambda file
46
+ /// source older than `FILE_SOURCE_RETENTION_SECONDS`
49
47
pub ( crate ) fn filter_prunable_lambda_source_ids < ' a > (
50
48
source_ids : impl Iterator < Item = & ' a String > ,
51
49
) -> anyhow:: Result < impl Iterator < Item = & ' a String > > {
52
- let src_timestamps = source_ids
53
- . filter ( |src_id| src_id . starts_with ( LAMBDA_SOURCE_ID_PREFIX ) )
50
+ let src_timestamps: Vec < _ > = source_ids
51
+ . filter ( |src_id| is_lambda_source_id ( src_id ) )
54
52
. map ( |src_id| Ok ( ( parse_source_id_timestamp ( src_id) ?, src_id) ) )
55
- . collect :: < anyhow:: Result < BTreeMap < _ , _ > > > ( ) ?;
53
+ . collect :: < anyhow:: Result < _ > > ( ) ?;
56
54
57
55
let prunable_sources = src_timestamps
58
56
. into_iter ( )
59
- . rev ( )
60
- . filter ( |( ts, _) | ( Utc :: now ( ) - * ts) . num_hours ( ) > FILE_SOURCE_RETENTION_HOURS as i64 )
57
+ . filter ( |( ts, _) | ( Utc :: now ( ) - * ts) . num_seconds ( ) > FILE_SOURCE_RETENTION_SECONDS )
61
58
. map ( |( _, src_id) | src_id) ;
62
59
63
60
Ok ( prunable_sources)
@@ -103,13 +100,13 @@ mod tests {
103
100
fn test_filter_old ( ) {
104
101
let source_ids: Vec < String > = ( 0 ..5 )
105
102
. map ( |i| {
106
- let hours_ago = i * FILE_SOURCE_RETENTION_HOURS * 2 ;
107
- Utc :: now ( ) - chrono:: Duration :: try_hours ( hours_ago as i64 ) . unwrap ( )
103
+ let secs_ago = i * FILE_SOURCE_RETENTION_SECONDS * 2 ;
104
+ Utc :: now ( ) - chrono:: Duration :: try_seconds ( secs_ago ) . unwrap ( )
108
105
} )
109
106
. map ( create_lambda_source_id)
110
107
. collect ( ) ;
111
108
112
- // Prune source ids that happen to be from newest to oldest
109
+ // Prune source ids, only keeping the most recent one
113
110
let prunable_sources = filter_prunable_lambda_source_ids ( source_ids. iter ( ) )
114
111
. unwrap ( )
115
112
. collect :: < HashSet < _ > > ( ) ;
@@ -118,16 +115,6 @@ mod tests {
118
115
for source_id in source_ids. iter ( ) . skip ( 1 ) {
119
116
assert ! ( prunable_sources. contains( source_id) ) ;
120
117
}
121
-
122
- // Prune source ids that happen to be from oldest to newst
123
- let prunable_sources = filter_prunable_lambda_source_ids ( source_ids. iter ( ) . rev ( ) )
124
- . unwrap ( )
125
- . collect :: < HashSet < _ > > ( ) ;
126
- assert_eq ! ( prunable_sources. len( ) , 4 ) ;
127
- assert ! ( !prunable_sources. contains( & source_ids[ 0 ] ) ) ;
128
- for source_id in source_ids. iter ( ) . skip ( 1 ) {
129
- assert ! ( prunable_sources. contains( source_id) ) ;
130
- }
131
118
}
132
119
133
120
#[ test]
0 commit comments