From a228d56372a887e03c88c184b9d4b976fdefed33 Mon Sep 17 00:00:00 2001 From: IsakNaslundBh Date: Wed, 12 Feb 2025 15:05:32 +0100 Subject: [PATCH] Remove triggering via events and event listener, and change how opening files is handled --- BHoM_UI/Global/DocumentListener.cs | 4 +- UI_Engine/Compute/LogUsage.cs | 203 ++++++++++++++------------- UI_Engine/Objects/DocumentLoading.cs | 3 - 3 files changed, 106 insertions(+), 104 deletions(-) diff --git a/BHoM_UI/Global/DocumentListener.cs b/BHoM_UI/Global/DocumentListener.cs index 19b72fe..5af5732 100644 --- a/BHoM_UI/Global/DocumentListener.cs +++ b/BHoM_UI/Global/DocumentListener.cs @@ -61,10 +61,12 @@ public static void OnDocumentBeginOpening(string documentName) /*************************************/ - public static void OnDocumentEndOpening(string documentName) + public static void OnDocumentEndOpening(string documentName, string uiName, string fileId) { BH.Engine.UI.Compute.SetDocumentOpeningState(false); + BH.Engine.UI.Compute.CheckLogOnUiEndOpening(uiName, fileId, documentName); + if (string.IsNullOrEmpty(documentName) || !m_OpeningTimes.ContainsKey(documentName)) return; diff --git a/UI_Engine/Compute/LogUsage.cs b/UI_Engine/Compute/LogUsage.cs index d8b1c7c..c23d9cc 100644 --- a/UI_Engine/Compute/LogUsage.cs +++ b/UI_Engine/Compute/LogUsage.cs @@ -54,32 +54,117 @@ public static void LogUsage(string uiName, string uiVersion, Guid componentId, s lock (m_LogLock) { if (!m_ProjectIDPerFile.TryGetValue(fileId, out projectId)) + { projectId = ""; + m_ProjectIDPerFile[fileId] = projectId; //Set as an indicator that the a BHoM method has been tried to be logged + } } - if (string.IsNullOrWhiteSpace(projectId)) + if (!m_documentOpening) //If not during project opening, call events { - TriggerLogUsageArgs args = new TriggerLogUsageArgs() + if (string.IsNullOrWhiteSpace(projectId)) { - UIName = uiName, - UIVersion = uiVersion, - ComponentID = componentId, - CallerName = callerName, - SelectedItem = selectedItem, - FileID = fileId, - FileName = fileName, - }; + TriggerLogUsageArgs args = new TriggerLogUsageArgs() + { + UIName = uiName, + UIVersion = uiVersion, + ComponentID = componentId, + CallerName = callerName, + SelectedItem = selectedItem, + FileID = fileId, + FileName = fileName, + }; - if (m_documentOpening) - TriggerUIOpening(args); - else TriggerUsageLog(args); + } } } LogToFile(uiName, uiVersion, componentId, callerName, selectedItem, events, fileId, fileName, projectId); } + /*************************************/ + + public static void UpdateProjectId(string uiName, string fileID, string projectID) + { + lock (m_LogLock) + { + m_ProjectIDPerFile[fileID] = projectID; + } + Task.Run(() => + { + lock (m_LogLock) + { + try + { + FileStream log = GetUsageLog(uiName); + log.Position = 0; //Set stream to start to read from top of file + List logLines = new List(); + //Read all content + using (StreamReader reader = new StreamReader(log, Encoding.UTF8, true, 4096, true)) + { + while (!reader.EndOfStream) + { + logLines.Add(reader.ReadLine()); + } + } + + //Update project ID for any items exiting before event triggered + var objects = logLines.Select(x => BH.Engine.Serialiser.Convert.FromJson(x) as UsageLogEntry).ToList(); + foreach (var o in objects) + { + if (o != null) + { + if (o.FileId == fileID) + o.ProjectID = projectID; + } + } + + //Write lines back to the file + logLines = objects.Select(x => x.ToJson()).ToList(); + log.Position = 0; + using (StreamWriter writer = new StreamWriter(log, Encoding.UTF8, 4096, true)) + { + foreach (var line in logLines) + { + writer.WriteLine(line); + } + writer.Flush(); + } + } + catch (Exception) + { + + } + } + }); + } + + /*************************************/ + + public static void CheckLogOnUiEndOpening(string uiName, string fileId, string fileName) + { + if (fileName != null) //Only call when opening a pre-existing file, not for new files with no filename set + { + string projectId; + if (m_ProjectIDPerFile.TryGetValue(fileId, out projectId)) //Only try to do this is something has been atempted to be logged + { + if (string.IsNullOrEmpty(projectId)) //Only run if projectId is not set + { + + TriggerLogUsageArgs args = new TriggerLogUsageArgs() + { + UIName = uiName, + FileID = fileId, + FileName = fileName, + SelectedItem = "UIEndOpening" + }; + + TriggerUsageLog(args); + } + } + } + } /*************************************/ /**** Helper Methods ****/ @@ -128,21 +213,12 @@ private static void LogToFile(string uiName, string uiVersion, Guid componentId, private static void HandleSetProjectId(string uiName, string fileId, List events) { - if (!string.IsNullOrEmpty(fileId)) + if (!string.IsNullOrEmpty(fileId) && !string.IsNullOrEmpty(uiName)) { - ProjectIDEvent projectIDEvent = events?.OfType().FirstOrDefault(); - if (projectIDEvent != null) + string projectId = events?.OfType().FirstOrDefault()?.ProjectID; + if (!string.IsNullOrEmpty(projectId)) { - if (string.IsNullOrEmpty(projectIDEvent.UIName) || string.IsNullOrEmpty(projectIDEvent.FileID)) - { - if (!string.IsNullOrEmpty(projectIDEvent.ProjectID)) - { - Base.Query.AllEvents().Remove(projectIDEvent); - projectIDEvent.UIName = uiName; - projectIDEvent.FileID = fileId; - Base.Compute.RecordEvent(projectIDEvent); - } - } + UpdateProjectId(uiName, fileId, projectId); } } } @@ -193,79 +269,6 @@ private static void RemoveDeprecatedLogs(string logFolder) /*************************************/ - static Compute() - { - Base.Compute.EventRecorded += EventRecorded; - } - - /*************************************/ - - private static void EventRecorded(object sender, Event e) - { - if (e != null && e is ProjectIDEvent projIdEvent) - { - if (!string.IsNullOrEmpty(projIdEvent.UIName) && !string.IsNullOrEmpty(projIdEvent.ProjectID) && !string.IsNullOrEmpty(projIdEvent.FileID)) - Task.Run(() => UpdateProjectId(projIdEvent.UIName, projIdEvent.FileID, projIdEvent.ProjectID)); - } - } - - /*************************************/ - - private static void UpdateProjectId(string uiName, string fileID, string projectID) - { - lock (m_LogLock) - { - m_ProjectIDPerFile[fileID] = projectID; - - try - { - FileStream log = GetUsageLog(uiName); - log.Position = 0; //Set stream to start to read from top of file - List logLines = new List(); - //Read all content - using (StreamReader reader = new StreamReader(log, Encoding.UTF8, true, 4096, true)) - { - while (!reader.EndOfStream) - { - logLines.Add(reader.ReadLine()); - } - } - - //Update project ID for any items exiting before event triggered - var objects = logLines.Select(x => BH.Engine.Serialiser.Convert.FromJson(x) as UsageLogEntry).ToList(); - foreach (var o in objects) - { - if (o != null) - { - if (o.FileId == fileID) - o.ProjectID = projectID; - } - } - - //Write lines back to the file - logLines = objects.Select(x => x.ToJson()).ToList(); - log.Position = 0; - using (StreamWriter writer = new StreamWriter(log, Encoding.UTF8, 4096, true)) - { - foreach (var line in logLines) - { - writer.WriteLine(line); - } - writer.Flush(); - } - } - catch (Exception) - { - - } - - - - } - } - - /*************************************/ - private static void OnProcessExit(object sender, EventArgs e) { // The file seems to be writable after the UI closed even without this but better safe than sorry. @@ -289,7 +292,7 @@ public static string BHoMVersion() private static void TriggerUsageLog(TriggerLogUsageArgs e) { - if (m_UsageLogTriggered != null && !Compute.m_documentOpening) + if (m_UsageLogTriggered != null) m_UsageLogTriggered.Invoke(null, e); //Force the data to be set if the set project ID component is being run during the script load } diff --git a/UI_Engine/Objects/DocumentLoading.cs b/UI_Engine/Objects/DocumentLoading.cs index 3e0d5bc..86d7d1d 100644 --- a/UI_Engine/Objects/DocumentLoading.cs +++ b/UI_Engine/Objects/DocumentLoading.cs @@ -34,9 +34,6 @@ public static partial class Compute public static void SetDocumentOpeningState(bool state) { - if (m_documentOpening && !state) - TriggerUIEndOpening(); - m_documentOpening = state; } }