From 943ef13752f14655827e39ab2dfaf5ddb6d20fae Mon Sep 17 00:00:00 2001 From: Samuele Locatelli Date: Fri, 28 Nov 2025 10:07:38 +0100 Subject: [PATCH] - update comments on statsCollector - update yaml robocopy html --- .gitlab-ci.yml | 2 +- IOB-MAN.Core/StatsCollector.cs | 114 +++++++++++++++++++++++++-------- IOB-MAN/IOB-MAN.csproj | 2 +- 3 files changed, 91 insertions(+), 27 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 88c1ceb..8f92268 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -19,7 +19,7 @@ variables: - | net use A: /delete net use A: $env:NET_SHARE_X /u:$env:NET_USER_X $XDRIVE_PASSWD - ROBOCOPY doxygen A:\$env:APP_NAME /MIR /XF .git* /XD .git + ROBOCOPY doxygen/html A:\$env:APP_NAME /MIR /XF .git* /XD .git SLEEP 2 net use A: /delete diff --git a/IOB-MAN.Core/StatsCollector.cs b/IOB-MAN.Core/StatsCollector.cs index 778f118..60a85ab 100644 --- a/IOB-MAN.Core/StatsCollector.cs +++ b/IOB-MAN.Core/StatsCollector.cs @@ -1,21 +1,36 @@ -using System; +/// +/// StatsCollector provides real-time performance and operational statistics for the application. +/// +/// This class collects and tracks execution metrics (e.g., duration, frequency, last call) for various functions. +/// It is used to generate detailed reports on application behavior, including startup time, uptime, and function performance. +/// +/// Key Features: +/// - Tracks execution duration and frequency of functions (e.g., service checks, data scans). +/// - Calculates uptime and startup timestamp. +/// - Formats time durations in readable units (ns, ms, sec, min). +/// - Uses thread-safe concurrent dictionaries to ensure safe access in multi-threaded environments. +/// - Exposes a dictionary of statistics for use in reporting or logging. +/// using System.Collections.Concurrent; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace IOB_MAN.Core { /// - /// Oggetto collezione statistiche x report status applicazione + /// Object responsible for collecting and reporting application-level statistics. + /// Used to monitor function execution times, frequency, and runtime behavior. /// public class StatsCollector { #region Public Properties /// - /// Calcola uptime del processo + /// Calculates and returns the current uptime of the application process. + /// + /// Format: + /// - If over 1 day: "DD d HHh MMm" + /// - Otherwise: "HHh MMm SS s" + /// + /// Example: "2d 3h 45m" or "1h 23m 15 s" /// public static string UptimeCurr { @@ -23,7 +38,8 @@ namespace IOB_MAN.Core { string answ = "ND"; var upT = DateTime.Now.Subtract(StartTime); - // formatto secondo durata... + + // Format based on duration if (upT.TotalDays > 1) { answ = $"{upT.Days:00} d {upT.Hours:00}h {upT.Minutes:00}m"; @@ -41,27 +57,49 @@ namespace IOB_MAN.Core #region Public Methods /// - /// Restituisce l'oggetto statistiche come dizionario di tutti gli eventi + /// Returns a dictionary containing all collected statistics for each function. + /// + /// Each entry includes: + /// - The count of executions. + /// - The average execution duration (formatted). + /// - The timestamp of the last execution. + /// + /// Example: + /// { + /// "ScanIOB": "15 x 2.345 sec", + /// "ScanIOBLast": "2025-04-05 10:30:22", + /// "Startup": "2025-04-05 09:00:00", + /// "Uptime": "2d 3h 45m" + /// } /// - /// + /// A dictionary of function-level statistics. public static Dictionary CurrentInfo() { Dictionary result = new Dictionary(); - // ciclo sugli oggetti calcolando durate e ultima call + + // Iterate over all tracked functions foreach (var item in Counter) { - string sRec = $"{Counter[item.Key]} x {formElaps((Duration[item.Key]) / Counter[item.Key])}"; - result.Add(item.Key, sRec); + string executionCount = $"{Counter[item.Key]} x {formElaps((Duration[item.Key]) / Counter[item.Key])}"; + result.Add(item.Key, executionCount); result.Add($"{item.Key}Last", $"{LastCall[item.Key]:yyyy-MM-dd HH:mm:ss}"); } - // infine metto stata avvio e uptime + + // Add startup and uptime metadata result.Add("Startup", $"{StartTime:yyyy-MM-dd HH:mm:ss}"); result.Add("Uptime", UptimeCurr); + return result; } /// - /// Reset collezione statistiche + /// Resets all internal statistics to their initial state. + /// + /// Actions: + /// - Sets the new start time to current moment. + /// - Clears all counters, durations, and last-call timestamps. + /// + /// Use case: Called during application restart, config reload, or debug reset. /// public static void ResetData() { @@ -72,12 +110,24 @@ namespace IOB_MAN.Core } /// - /// Upsert statistica gestita + /// Updates the execution statistics for a specific function. + /// + /// Parameters: + /// - functionName: Name of the function (e.g., "DoScan", "CheckMemory"). + /// - elaps: Duration of the function execution (e.g., 123.45 ms). + /// + /// Behavior: + /// - Accumulates duration and execution count. + /// - Records the current timestamp as the last call. + /// + /// Thread Safety: + /// - Uses concurrent dictionaries to safely handle updates from multiple threads. /// - /// - /// + /// The name of the function being executed. + /// The elapsed time of the function execution. public static void UpdateStat(string functionName, TimeSpan elaps) { + // Accumulate execution duration if (!Duration.ContainsKey(functionName)) { Duration.TryAdd(functionName, elaps); @@ -87,6 +137,7 @@ namespace IOB_MAN.Core Duration[functionName] += elaps; } + // Increment execution counter if (!Counter.ContainsKey(functionName)) { Counter.TryAdd(functionName, 1); @@ -96,7 +147,7 @@ namespace IOB_MAN.Core Counter[functionName]++; } - // salvo anche ultima call ad adesso + // Record timestamp of last execution DateTime adesso = DateTime.Now; if (!LastCall.ContainsKey(functionName)) { @@ -113,22 +164,23 @@ namespace IOB_MAN.Core #region Private Fields /// - /// Dizionario contatori esecuzione + /// Thread-safe dictionary tracking how many times each function has been executed. /// private static ConcurrentDictionary Counter = new ConcurrentDictionary(); /// - /// Dizionario durate esecuzoine + /// Thread-safe dictionary storing the total duration (summed) of each function's execution. /// private static ConcurrentDictionary Duration = new ConcurrentDictionary(); /// - /// Dizionario durate esecuzoine + /// Thread-safe dictionary storing the timestamp of the last execution of each function. /// private static ConcurrentDictionary LastCall = new ConcurrentDictionary(); /// - /// DataOra avvio processo + /// Timestamp when the application started. + /// Used to calculate uptime and startup time. /// private static DateTime StartTime = DateTime.Now; @@ -136,10 +188,22 @@ namespace IOB_MAN.Core #region Private Methods + /// + /// Formats a time span into a human-readable string based on magnitude. + /// + /// Format logic: + /// - Less than 1 ms → "X.XXX ns" + /// - 1 ms to 1 sec → "X.X ms" + /// - 1 sec to 300 sec → "X.X sec" + /// - Over 300 sec → "X.X min" + /// + /// Example: 123.456 ms → "123.456 ms", 1.234 sec → "1.234 sec", 360 sec → "6.0 min" + /// + /// The time span to format. + /// Formatted time string. private static string formElaps(TimeSpan ts) { string answ = ""; - // formatto secondo durata if (ts.Milliseconds < 1) { answ = $"{ts.TotalMilliseconds:N3} ns"; @@ -161,4 +225,4 @@ namespace IOB_MAN.Core #endregion Private Methods } -} \ No newline at end of file +} diff --git a/IOB-MAN/IOB-MAN.csproj b/IOB-MAN/IOB-MAN.csproj index 5b5f6ac..4db27cb 100644 --- a/IOB-MAN/IOB-MAN.csproj +++ b/IOB-MAN/IOB-MAN.csproj @@ -8,7 +8,7 @@ enable true enable - 4.0.2511.2809 + 4.0.2511.2810 Debug;Release;Remote_DEBUG false