From f7a1732a5242b8b77334cf80f49b68f5d34b820b Mon Sep 17 00:00:00 2001
From: Alberto Soutullo <alberto.soutullo639@gmail.com>
Date: Fri, 17 Jan 2025 17:00:13 +0100
Subject: [PATCH] =?UTF-8?q?Create=20plot=20function=20to=20use=20for=20ana?=
 =?UTF-8?q?lysis=20of=20reach=20N=C2=BA=20of=20connections?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../waku_message_log_analyzer.py              | 25 ++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)

diff --git a/src/mesh_analysis/waku_message_log_analyzer.py b/src/mesh_analysis/waku_message_log_analyzer.py
index ea0c4b0..9531db9 100644
--- a/src/mesh_analysis/waku_message_log_analyzer.py
+++ b/src/mesh_analysis/waku_message_log_analyzer.py
@@ -18,9 +18,10 @@
 from src.mesh_analysis.tracers.waku_tracer import WakuTracer
 from src.plotting.utils import add_boxplot_stat_labels
 from src.utils import file_utils, log_utils, path_utils, list_utils
+from src.utils.path_utils import check_path_exists
 
 logger = logging.getLogger(__name__)
-
+sns.set_theme()
 
 class WakuMessageLogAnalyzer:
     def __init__(self, stateful_sets: List[str], timestamp_to_analyze: str = None,
@@ -381,3 +382,25 @@ def plot_message_distribution(self, received_summary_path: Path, plot_title: str
         plt.show()
 
         return Ok(None)
+
+
+    @check_path_exists
+    def check_time_to_reach_value_plot(self, file_data_path: Path, threshold_value: int, value_name: str,
+                                       dump_path: Path) -> Result[None, str]:
+        df = pd.read_csv(file_data_path)
+        df['Time'] = pd.to_datetime(df['Time'])
+        df.set_index('Time', inplace=True)
+
+        mask = df >= threshold_value
+        first_reach = mask.idxmax()
+        first_reach[~mask.any()] = pd.NaT  # Set to NaT if the target is never reached
+        time_to_target = (first_reach - df.index[0]).dt.total_seconds() / 60
+
+        plt.figure(figsize=(10, 6))
+        plt.boxplot(time_to_target.dropna(), vert=False)
+        plt.title(f'Time to Reach {value_name} - {time_to_target.dropna().shape[0]}/{df.shape[1]}')
+        plt.xlabel('Time to Reach Target (minutes)')
+        plt.savefig(dump_path)
+        plt.show()
+
+        return Ok(None)