@@ -5,6 +5,7 @@ use std::pin::Pin;
55use std:: sync:: Arc ;
66
77use async_compression:: tokio:: write:: GzipEncoder ;
8+ use chrono:: TimeZone ;
89use futures:: io:: { BufReader , BufWriter } ;
910use futures:: { Stream , StreamExt , TryStreamExt } ;
1011use log:: { error, info, warn} ;
@@ -798,6 +799,22 @@ async fn pushlog_handler(
798799 ) . into ( ) )
799800}
800801
802+ fn file_name_to_file_msec ( fn_path : & str ) -> Result < i64 , String > {
803+ let filename = Path :: new ( fn_path)
804+ . file_name ( )
805+ . and_then ( |f| f. to_str ( ) )
806+ . ok_or_else ( || format ! ( "Invalid UTF-8 in file name '{}'" , fn_path) ) ?;
807+
808+ let without_ext = filename
809+ . strip_suffix ( ".log2" )
810+ . ok_or_else ( || format ! ( "Invalid file extension in '{}'" , filename) ) ?;
811+
812+ let datetime = chrono:: NaiveDateTime :: parse_from_str ( without_ext, "%Y-%m-%dT%H-%M-%S-%3f" )
813+ . map_err ( |e| format ! ( "Failed to parse '{}': {}" , filename, e) ) ?;
814+
815+ Ok ( chrono:: Utc . from_utc_datetime ( & datetime) . timestamp_millis ( ) )
816+ }
817+
801818pub async fn getlog_handler (
802819 site_path : & str ,
803820 params : & GetLog2Params ,
@@ -844,17 +861,13 @@ pub async fn getlog_handler(
844861 match & params. since {
845862 GetLog2Since :: DateTime ( date_time) => {
846863 let since_ms = date_time. epoch_msec ( ) ;
847- let file_name_to_msec = |file_name : & str | file_name
848- . strip_suffix ( ".log2" )
849- . and_then ( |file| shvproto:: DateTime :: from_iso_str ( file) . ok ( ) . as_ref ( ) . map ( shvproto:: DateTime :: epoch_msec) )
850- . unwrap_or ( i64:: MAX ) ;
851864
852865 log_files
853866 . iter ( )
854867 . map ( DirEntry :: file_name)
855868 . enumerate ( )
856869 . rev ( )
857- . find ( |( _, file) | file_name_to_msec ( & file. to_string_lossy ( ) ) < since_ms)
870+ . find ( |( _, file) | file_name_to_file_msec ( & file. to_string_lossy ( ) ) . ok ( ) . unwrap_or ( i64 :: MAX ) < since_ms)
858871 . map ( |( idx, _) | idx)
859872 . unwrap_or ( 0 )
860873 }
0 commit comments