From 3c7c21afcd3d47afcf649de49930fd649bd2215a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Reynir=20Bj=C3=B6rnsson?= Date: Wed, 15 Jan 2025 13:36:31 +0100 Subject: [PATCH] Improve console_of_string --- bin/builder_db_app.ml | 24 ++++++++++++++---------- lib/model.ml | 24 ++++++++++++++---------- 2 files changed, 28 insertions(+), 20 deletions(-) diff --git a/bin/builder_db_app.ml b/bin/builder_db_app.ml index 0ade5e4..e6f4283 100644 --- a/bin/builder_db_app.ml +++ b/bin/builder_db_app.ml @@ -759,20 +759,24 @@ module Asn = struct let console_of_cs, console_to_cs = projections_of console end +(* NOTE: this function is duplicatedi in lib/model.ml *) let console_of_string data = - let lines = String.split_on_char '\n' data in (* remove last empty line *) - let lines = - match List.rev lines with - | "" :: lines -> List.rev lines - | _ -> lines + let data = + if String.ends_with ~suffix:"\n" data then + String.sub data 0 (String.length data - 2) + else data in + let lines = String.split_on_char '\n' data in List.map (fun line -> - match String.split_on_char ':' line with - | ts :: tail -> - let delta = float_of_string (String.sub ts 0 (String.length ts - 1)) in - Int64.to_int (Duration.of_f delta), String.concat ":" tail - | _ -> assert false) + match String.index line ':' with + | i -> + (* the timestamp is of the form "%fs", e.g. 0.867s; so chop off the 's' *) + let delta = float_of_string (String.sub line 0 (i - 1)) in + let delta = Int64.to_int (Duration.of_f delta) in + let line = String.sub line i (String.length line - i) in + delta, line + | exception Not_found -> assert false) lines let extract_full () datadir dest uuid = diff --git a/lib/model.ml b/lib/model.ml index e8c6f15..b911e89 100644 --- a/lib/model.ml +++ b/lib/model.ml @@ -524,20 +524,24 @@ let add_build Unix.closedir dh; Lwt.return (Ok ()) +(* NOTE: this function is duplicatedi in bin/builder_db_app.ml *) let console_of_string data = - let lines = String.split_on_char '\n' data in (* remove last empty line *) - let lines = - match List.rev lines with - | "" :: lines -> List.rev lines - | _ -> lines + let data = + if String.ends_with ~suffix:"\n" data then + String.sub data 0 (String.length data - 2) + else data in + let lines = String.split_on_char '\n' data in List.map (fun line -> - match String.split_on_char ':' line with - | ts :: tail -> - let delta = float_of_string (String.sub ts 0 (String.length ts - 1)) in - Int64.to_int (Duration.of_f delta), String.concat ":" tail - | _ -> assert false) + match String.index line ':' with + | i -> + (* the timestamp is of the form "%fs", e.g. 0.867s; so chop off the 's' *) + let delta = float_of_string (String.sub line 0 (i - 1)) in + let delta = Int64.to_int (Duration.of_f delta) in + let line = String.sub line i (String.length line - i) in + delta, line + | exception Not_found -> assert false) lines let exec_of_build datadir uuid (module Db : CONN) =