From d846cffed7e5d5ddfe48cea56ff53fe3677eb1a6 Mon Sep 17 00:00:00 2001 From: IsakNaslundBh Date: Mon, 10 Feb 2025 12:34:43 +0100 Subject: [PATCH 01/15] #503 Update csprojects to new format --- BHoM_UI/BHoM_UI.csproj | 191 ++++----------------------- BHoM_UI/Properties/AssemblyInfo.cs | 64 --------- UI_Engine/Properties/AssemblyInfo.cs | 64 --------- UI_Engine/UI_Engine.csproj | 111 +++------------- UI_oM/Properties/AssemblyInfo.cs | 64 --------- UI_oM/UI_oM.csproj | 96 +++----------- 6 files changed, 64 insertions(+), 526 deletions(-) delete mode 100644 BHoM_UI/Properties/AssemblyInfo.cs delete mode 100644 UI_Engine/Properties/AssemblyInfo.cs delete mode 100644 UI_oM/Properties/AssemblyInfo.cs diff --git a/BHoM_UI/BHoM_UI.csproj b/BHoM_UI/BHoM_UI.csproj index d333114..58da8de 100644 --- a/BHoM_UI/BHoM_UI.csproj +++ b/BHoM_UI/BHoM_UI.csproj @@ -1,36 +1,23 @@ - - - - - Debug - AnyCPU - {69F4F042-E23A-4673-AB32-3D787CFA976C} - Library - Properties - BH.UI - BHoM_UI - v4.7.2 - 512 - - - - true - full - false - ..\Build\ - DEBUG;TRACE - prompt - 4 - - - pdbonly - true - ..\Build\ - TRACE - prompt - 4 - - + + + + net472 + 8.0.0.0 + https://github.com/BHoM/BHoM_UI + 5.0.0 + BHoM + Copyright © https://github.com/BHoM + BH.UI + 8.1.0.0 + ..\Build\ + + + + + + + + $(ProgramData)\BHoM\Assemblies\Adapter_oM.dll False @@ -159,137 +146,9 @@ False - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Designer - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - {565ddbd0-5c04-4be9-b57d-ce54c2124abb} - UI_Engine - False - - - {50d4c1ed-c16c-4365-a742-938bf49ec4b0} - UI_oM - False - - - - - xcopy "$(TargetDir)$(TargetFileName)" "$(ProgramData)\BHoM\Assemblies" /Y - - + + + + + \ No newline at end of file diff --git a/BHoM_UI/Properties/AssemblyInfo.cs b/BHoM_UI/Properties/AssemblyInfo.cs deleted file mode 100644 index 84a7f04..0000000 --- a/BHoM_UI/Properties/AssemblyInfo.cs +++ /dev/null @@ -1,64 +0,0 @@ -/* - * This file is part of the Buildings and Habitats object Model (BHoM) - * Copyright (c) 2015 - 2025, the respective contributors. All rights reserved. - * - * Each contributor holds copyright over their respective contributions. - * The project versioning (Git) records all such contribution source information. - * - * - * The BHoM is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3.0 of the License, or - * (at your option) any later version. - * - * The BHoM is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this code. If not, see . - */ - -using System.Reflection; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; - -// General Information about an assembly is controlled through the following -// set of attributes. Change these attribute values to modify the information -// associated with an assembly. -[assembly: AssemblyTitle("BHoM_UI")] -[assembly: AssemblyDescription("https://github.com/BHoM/BHoM_UI")] -[assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("")] -[assembly: AssemblyProduct("BHoM_UI")] -[assembly: AssemblyCopyright("Copyright © https://github.com/BHoM")] -[assembly: AssemblyTrademark("")] -[assembly: AssemblyCulture("")] - -// Setting ComVisible to false makes the types in this assembly not visible -// to COM components. If you need to access a type in this assembly from -// COM, set the ComVisible attribute to true on that type. -[assembly: ComVisible(false)] - -// The following GUID is for the ID of the typelib if this project is exposed to COM -[assembly: Guid("69f4f042-e23a-4673-ab32-3d787cfa976c")] - -// Version information for an assembly consists of the following four values: -// -// Major Version -// Minor Version -// Build Number -// Revision -// -// You can specify all the values or you can default the Build and Revision Numbers -// by using the '*' as shown below: -// [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("8.0.0.0")] -[assembly: AssemblyFileVersion("8.1.0.0")] - - - - - - diff --git a/UI_Engine/Properties/AssemblyInfo.cs b/UI_Engine/Properties/AssemblyInfo.cs deleted file mode 100644 index 69d519a..0000000 --- a/UI_Engine/Properties/AssemblyInfo.cs +++ /dev/null @@ -1,64 +0,0 @@ -/* - * This file is part of the Buildings and Habitats object Model (BHoM) - * Copyright (c) 2015 - 2025, the respective contributors. All rights reserved. - * - * Each contributor holds copyright over their respective contributions. - * The project versioning (Git) records all such contribution source information. - * - * - * The BHoM is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3.0 of the License, or - * (at your option) any later version. - * - * The BHoM is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this code. If not, see . - */ - -using System.Reflection; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; - -// General Information about an assembly is controlled through the following -// set of attributes. Change these attribute values to modify the information -// associated with an assembly. -[assembly: AssemblyTitle("UI_Engine")] -[assembly: AssemblyDescription("https://github.com/BHoM/BHoM_UI")] -[assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("")] -[assembly: AssemblyProduct("UI_Engine")] -[assembly: AssemblyCopyright("Copyright © https://github.com/BHoM")] -[assembly: AssemblyTrademark("")] -[assembly: AssemblyCulture("")] - -// Setting ComVisible to false makes the types in this assembly not visible -// to COM components. If you need to access a type in this assembly from -// COM, set the ComVisible attribute to true on that type. -[assembly: ComVisible(false)] - -// The following GUID is for the ID of the typelib if this project is exposed to COM -[assembly: Guid("565ddbd0-5c04-4be9-b57d-ce54c2124abb")] - -// Version information for an assembly consists of the following four values: -// -// Major Version -// Minor Version -// Build Number -// Revision -// -// You can specify all the values or you can default the Build and Revision Numbers -// by using the '*' as shown below: -// [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("8.0.0.0")] -[assembly: AssemblyFileVersion("8.1.0.0")] - - - - - - diff --git a/UI_Engine/UI_Engine.csproj b/UI_Engine/UI_Engine.csproj index fe0d9fe..2681600 100644 --- a/UI_Engine/UI_Engine.csproj +++ b/UI_Engine/UI_Engine.csproj @@ -1,34 +1,20 @@ - - - - - Debug - AnyCPU - {565DDBD0-5C04-4BE9-B57D-CE54C2124ABB} - Library - Properties - BH.Engine.UI - UI_Engine - v4.7.2 - 512 - - - true - full - false - ..\Build\ - DEBUG;TRACE - prompt - 4 - - - pdbonly - true - ..\Build\ - TRACE - prompt - 4 - + + + + net472 + 8.0.0.0 + https://github.com/BHoM/BHoM_UI + 5.0.0 + BHoM + Copyright © https://github.com/BHoM + BH.Engine.UI + 8.1.0.0 + ..\Build\ + + + + + C:\ProgramData\BHoM\Assemblies\BHoM.dll @@ -60,6 +46,7 @@ C:\ProgramData\BHoM\Assemblies\Library_Engine.dll False + C:\ProgramData\BHoM\Assemblies\Reflection_Engine.dll False @@ -70,63 +57,9 @@ False C:\ProgramData\BHoM\Assemblies\Serialiser_Engine.dll - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - {50d4c1ed-c16c-4365-a742-938bf49ec4b0} - UI_oM - - - - - - - xcopy "$(TargetDir)$(TargetFileName)" "C:\ProgramData\BHoM\Assemblies" /Y - - + + + + \ No newline at end of file diff --git a/UI_oM/Properties/AssemblyInfo.cs b/UI_oM/Properties/AssemblyInfo.cs deleted file mode 100644 index 83adb25..0000000 --- a/UI_oM/Properties/AssemblyInfo.cs +++ /dev/null @@ -1,64 +0,0 @@ -/* - * This file is part of the Buildings and Habitats object Model (BHoM) - * Copyright (c) 2015 - 2025, the respective contributors. All rights reserved. - * - * Each contributor holds copyright over their respective contributions. - * The project versioning (Git) records all such contribution source information. - * - * - * The BHoM is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3.0 of the License, or - * (at your option) any later version. - * - * The BHoM is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this code. If not, see . - */ - -using System.Reflection; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; - -// General Information about an assembly is controlled through the following -// set of attributes. Change these attribute values to modify the information -// associated with an assembly. -[assembly: AssemblyTitle("UI_oM")] -[assembly: AssemblyDescription("https://github.com/BHoM/BHoM_UI")] -[assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("")] -[assembly: AssemblyProduct("UI_oM")] -[assembly: AssemblyCopyright("Copyright © https://github.com/BHoM")] -[assembly: AssemblyTrademark("")] -[assembly: AssemblyCulture("")] - -// Setting ComVisible to false makes the types in this assembly not visible -// to COM components. If you need to access a type in this assembly from -// COM, set the ComVisible attribute to true on that type. -[assembly: ComVisible(false)] - -// The following GUID is for the ID of the typelib if this project is exposed to COM -[assembly: Guid("50d4c1ed-c16c-4365-a742-938bf49ec4b0")] - -// Version information for an assembly consists of the following four values: -// -// Major Version -// Minor Version -// Build Number -// Revision -// -// You can specify all the values or you can default the Build and Revision Numbers -// by using the '*' as shown below: -// [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("8.0.0.0")] -[assembly: AssemblyFileVersion("8.1.0.0")] - - - - - - diff --git a/UI_oM/UI_oM.csproj b/UI_oM/UI_oM.csproj index 7859e79..25ea5cb 100644 --- a/UI_oM/UI_oM.csproj +++ b/UI_oM/UI_oM.csproj @@ -1,34 +1,16 @@ - - - - - Debug - AnyCPU - {50D4C1ED-C16C-4365-A742-938BF49EC4B0} - Library - Properties - BH.oM.UI - UI_oM - v4.7.2 - 512 - - - true - full - false - ..\Build\ - DEBUG;TRACE - prompt - 4 - - - pdbonly - true - ..\Build\ - TRACE - prompt - 4 - + + + + net472 + 8.0.0.0 + https://github.com/BHoM/BHoM_UI + 5.0.0 + BHoM + Copyright © https://github.com/BHoM + BH.oM.UI + 8.1.0.0 + ..\Build\ + False @@ -40,53 +22,9 @@ $(ProgramData)\BHoM\Assemblies\Geometry_oM.dll False - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - xcopy "$(TargetDir)$(TargetFileName)" "C:\ProgramData\BHoM\Assemblies" /Y - - + + + + From c61810e6c8dba37fe45e63f6f3c671606b999da5 Mon Sep 17 00:00:00 2001 From: IsakNaslundBh Date: Tue, 11 Feb 2025 11:07:18 +0100 Subject: [PATCH 02/15] Code restructuring and update to how setting a new project ID is handled --- UI_Engine/Compute/LogUsage.cs | 222 +++++++++++++++++++++++++--------- UI_oM/ProjectIDEvent.cs | 1 + 2 files changed, 169 insertions(+), 54 deletions(-) diff --git a/UI_Engine/Compute/LogUsage.cs b/UI_Engine/Compute/LogUsage.cs index 4d1bd7f..65a8441 100644 --- a/UI_Engine/Compute/LogUsage.cs +++ b/UI_Engine/Compute/LogUsage.cs @@ -33,7 +33,6 @@ using System.Reflection; using System.Text; using System.Threading.Tasks; - using System.ComponentModel; namespace BH.Engine.UI @@ -46,57 +45,35 @@ public static partial class Compute public static void LogUsage(string uiName, string uiVersion, Guid componentId, string callerName, object selectedItem, List events = null, string fileId = "", string fileName = "") { - TriggerLogUsageArgs args = new TriggerLogUsageArgs() - { - UIName = uiName, - UIVersion = uiVersion, - ComponentID = componentId, - CallerName = callerName, - SelectedItem = selectedItem, - FileID = fileId, - }; - - if (m_documentOpening) - TriggerUIOpening(args); - else - TriggerUsageLog(args); - - // If a projectID event is available, save the project code for this file - var allEvents = BH.Engine.Base.Query.AllEvents(); - if (allEvents != null) + //Special case for a component setting the project ID explicitly + HandleSetProjectId(uiName, fileId, events); + + string projectId; + lock (m_LogLock) { - ProjectIDEvent e = allEvents.OfType().Where(x => x.FileID == fileId).FirstOrDefault(); - if (e != null && !string.IsNullOrEmpty(fileId)) - m_ProjectIDPerFile[fileId] = e.ProjectID; + if (!m_ProjectIDPerFile.TryGetValue(fileId, out projectId)) + projectId = ""; } - try + if (string.IsNullOrWhiteSpace(projectId)) { - // Create the log item - UsageLogEntry info = new UsageLogEntry + TriggerLogUsageArgs args = new TriggerLogUsageArgs() { - UI = uiName, - UiVersion = uiVersion, - BHoMVersion = BHoMVersion(), - ComponentId = componentId, + UIName = uiName, + UIVersion = uiVersion, + ComponentID = componentId, CallerName = callerName, SelectedItem = selectedItem, - FileId = fileId, - FileName = fileName, - Errors = events == null ? new List() : events.Where(x => x.Type == EventType.Error).ToList() + FileID = fileId, }; - // Record the project code if it exists - if (m_ProjectIDPerFile.ContainsKey(fileId)) - info.ProjectID = m_ProjectIDPerFile[fileId]; - - // Write to the log file - string json = info.ToJson(); - StreamWriter log = GetUsageLog(uiName); - log.WriteLine(json); - log.Flush(); + if (m_documentOpening) + TriggerUIOpening(args); + else + TriggerUsageLog(args); } - catch { } + + LogToFile(uiName, uiVersion, componentId, callerName, selectedItem, events, fileId, fileName, projectId); } @@ -104,9 +81,73 @@ public static void LogUsage(string uiName, string uiVersion, Guid componentId, s /**** Helper Methods ****/ /*************************************/ - private static StreamWriter GetUsageLog(string uiName) + private static void LogToFile(string uiName, string uiVersion, Guid componentId, string callerName, object selectedItem, List events, string fileId, string fileName, string projectId) + { + try + { + Task.Run(() => + { + // Create the log item + UsageLogEntry info = new UsageLogEntry + { + UI = uiName, + UiVersion = uiVersion, + BHoMVersion = BHoMVersion(), + ComponentId = componentId, + CallerName = callerName, + SelectedItem = selectedItem, + FileId = fileId, + FileName = fileName, + ProjectID = projectId, + Errors = events == null ? new List() : events.Where(x => x.Type == EventType.Error).ToList() + }; + + string json = info.ToJson(); + + lock (m_LogLock) + { + // Write to the log file + FileStream log = GetUsageLog(uiName); + using (StreamWriter writer = new StreamWriter(log, Encoding.UTF8, 4096, true)) + { + writer.WriteLine(json); + writer.Flush(); + } + } + }); + + } + catch { } + } + + /*************************************/ + + private static void HandleSetProjectId(string uiName, string fileId, List events) { - if (m_UsageLog == null) + if (!string.IsNullOrEmpty(fileId)) + { + ProjectIDEvent projectIDEvent = events?.OfType().FirstOrDefault(); + if (projectIDEvent != null) + { + 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); + } + } + } + } + } + + /*************************************/ + + private static FileStream GetUsageLog(string uiName) + { + if (m_UsageLogStream == null) { string logFolder = Query.UsageLogFolder(); @@ -115,14 +156,13 @@ private static StreamWriter GetUsageLog(string uiName) // Create the new log file string filePath = Query.UsageLogFileName(uiName); - FileStream stream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.Read); - m_UsageLog = new StreamWriter(stream); + m_UsageLogStream = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite, FileShare.Read); - // Be ready to close the file when the UI is closed + // Be ready to close the file when the UI is closed AppDomain.CurrentDomain.ProcessExit += OnProcessExit; } - return m_UsageLog; + return m_UsageLogStream; } /*************************************/ @@ -130,9 +170,9 @@ private static StreamWriter GetUsageLog(string uiName) private static void RemoveDeprecatedLogs(string logFolder) { long currentTicks = DateTime.UtcNow.Ticks; - List logFiles = Directory.GetFiles(logFolder).Where(x => x.Contains("Usage_")).ToList(); + List logFiles = Directory.GetFiles(logFolder).Where(x => x.Contains("Usage_")).ToList(); - foreach( string file in logFiles) + foreach (string file in logFiles) { string[] parts = file.Split(new char[] { '_', '.' }); if (parts.Length >= 4) @@ -149,11 +189,84 @@ 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. - if (m_UsageLog != null) - m_UsageLog.Close(); + if (m_UsageLogStream != null) + m_UsageLogStream.Close(); TriggerUIClose(); } @@ -196,7 +309,7 @@ private static void TriggerUIOpening(TriggerLogUsageArgs e) private static void TriggerUIEndOpening() { - if(m_UIEndOpening != null) + if (m_UIEndOpening != null) m_UIEndOpening.Invoke(null, null); } @@ -204,7 +317,8 @@ private static void TriggerUIEndOpening() /**** Static Fields ****/ /*************************************/ - private static StreamWriter m_UsageLog = null; + private static object m_LogLock = new object(); + private static FileStream m_UsageLogStream = null; private static string m_BHoMVersion = null; diff --git a/UI_oM/ProjectIDEvent.cs b/UI_oM/ProjectIDEvent.cs index 4624864..8827940 100644 --- a/UI_oM/ProjectIDEvent.cs +++ b/UI_oM/ProjectIDEvent.cs @@ -36,6 +36,7 @@ public class ProjectIDEvent : Event /**** Properties ****/ /***************************************************/ + public virtual string UIName { get; set; } = ""; public virtual string ProjectID { get; set; } = ""; public virtual string FileID { get; set; } = ""; From 1050207516ceccb67c8d7f575534f925bf64bb31 Mon Sep 17 00:00:00 2001 From: IsakNaslundBh Date: Tue, 11 Feb 2025 11:08:13 +0100 Subject: [PATCH 03/15] Include filename in TriggerLogUsage --- UI_Engine/Compute/LogUsage.cs | 1 + UI_oM/TriggerLogUsageArgs.cs | 1 + 2 files changed, 2 insertions(+) diff --git a/UI_Engine/Compute/LogUsage.cs b/UI_Engine/Compute/LogUsage.cs index 65a8441..5f49d4e 100644 --- a/UI_Engine/Compute/LogUsage.cs +++ b/UI_Engine/Compute/LogUsage.cs @@ -65,6 +65,7 @@ public static void LogUsage(string uiName, string uiVersion, Guid componentId, s CallerName = callerName, SelectedItem = selectedItem, FileID = fileId, + FileName = fileName, }; if (m_documentOpening) diff --git a/UI_oM/TriggerLogUsageArgs.cs b/UI_oM/TriggerLogUsageArgs.cs index d83f0c6..e80d06d 100644 --- a/UI_oM/TriggerLogUsageArgs.cs +++ b/UI_oM/TriggerLogUsageArgs.cs @@ -38,6 +38,7 @@ public class TriggerLogUsageArgs : EventArgs, IObject public virtual string UIVersion { get; set; } = ""; public virtual Guid ComponentID { get; set; } = Guid.Empty; public virtual string FileID { get; set; } = ""; + public virtual string FileName { get; set; } = ""; } } From 7149a27df6f30b4028ea647ca99de56c7341daaf Mon Sep 17 00:00:00 2001 From: IsakNaslundBh Date: Tue, 11 Feb 2025 11:11:05 +0100 Subject: [PATCH 04/15] add ProjectId as optional input for logging to be able to log directly Useful when the method calling the logging method has the projectId available and does not need to rely on the cached values --- UI_Engine/Compute/LogUsage.cs | 47 +++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 22 deletions(-) diff --git a/UI_Engine/Compute/LogUsage.cs b/UI_Engine/Compute/LogUsage.cs index 5f49d4e..d8b1c7c 100644 --- a/UI_Engine/Compute/LogUsage.cs +++ b/UI_Engine/Compute/LogUsage.cs @@ -43,35 +43,38 @@ public static partial class Compute /**** Public Methods ****/ /*************************************/ - public static void LogUsage(string uiName, string uiVersion, Guid componentId, string callerName, object selectedItem, List events = null, string fileId = "", string fileName = "") + [PreviousVersion("8.1", "BH.Engine.UI.Compute.LogUsage(System.String, System.String, System.Guid, System.String, System.Object, System.Collections.Generic.List, System.String, System.String)")] + public static void LogUsage(string uiName, string uiVersion, Guid componentId, string callerName, object selectedItem, List events = null, string fileId = "", string fileName = "", string projectId = "") { //Special case for a component setting the project ID explicitly HandleSetProjectId(uiName, fileId, events); - string projectId; - lock (m_LogLock) - { - if (!m_ProjectIDPerFile.TryGetValue(fileId, out projectId)) - projectId = ""; - } - if (string.IsNullOrWhiteSpace(projectId)) { - TriggerLogUsageArgs args = new TriggerLogUsageArgs() + lock (m_LogLock) + { + if (!m_ProjectIDPerFile.TryGetValue(fileId, out projectId)) + projectId = ""; + } + + if (string.IsNullOrWhiteSpace(projectId)) { - UIName = uiName, - UIVersion = uiVersion, - ComponentID = componentId, - CallerName = callerName, - SelectedItem = selectedItem, - FileID = fileId, - FileName = fileName, - }; - - if (m_documentOpening) - TriggerUIOpening(args); - else - TriggerUsageLog(args); + 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); From 56c37349b8cda023a0052e516927eda49baf1fc2 Mon Sep 17 00:00:00 2001 From: IsakNaslundBh Date: Tue, 11 Feb 2025 15:50:13 +0100 Subject: [PATCH 05/15] Update csproj format for Window project --- BHoM_Windows_UI/BHoM_Windows_UI.csproj | 107 +++++---------------- BHoM_Windows_UI/Properties/AssemblyInfo.cs | 59 ------------ 2 files changed, 24 insertions(+), 142 deletions(-) delete mode 100644 BHoM_Windows_UI/Properties/AssemblyInfo.cs diff --git a/BHoM_Windows_UI/BHoM_Windows_UI.csproj b/BHoM_Windows_UI/BHoM_Windows_UI.csproj index 9b62b50..76b8cf1 100644 --- a/BHoM_Windows_UI/BHoM_Windows_UI.csproj +++ b/BHoM_Windows_UI/BHoM_Windows_UI.csproj @@ -1,35 +1,23 @@ - - - - - Debug - AnyCPU - {9BA156D1-851D-4267-A94F-6F238D17B634} - Library - Properties - BH.UI.Base.Windows - BHoM_Windows_UI - v4.7.2 - 512 - true - - - true - full - false - ..\Build\ - DEBUG;TRACE - prompt - 4 - - - pdbonly - true - ..\Build\ - TRACE - prompt - 4 - + + + + net472 + 8.0.0.0 + https://github.com/BHoM/BHoM_UI + 5.0.0 + BHoM + Copyright © https://github.com/BHoM + BH.UI.Base.Windows + 8.1.0.0 + ..\Build\ + true + + + + + + + $(ProgramData)\BHoM\Assemblies\BHoM.dll @@ -41,61 +29,14 @@ False False - - $(ProgramData)\BHoM\Assemblies\Settings_Engine.dll False False - - - - - - - - - - - - - - - - True - True - Resources.resx - - - SearchSettingsWindow.xaml - - - - - - Designer - MSBuild:Compile - - - - - {50d4c1ed-c16c-4365-a742-938bf49ec4b0} - UI_oM - - - - - ResXFileCodeGenerator - Resources.Designer.cs - - - - - - - - xcopy "$(TargetDir)$(TargetFileName)" "$(ProgramData)\BHoM\Assemblies" /Y - + + + + diff --git a/BHoM_Windows_UI/Properties/AssemblyInfo.cs b/BHoM_Windows_UI/Properties/AssemblyInfo.cs deleted file mode 100644 index 4f707bf..0000000 --- a/BHoM_Windows_UI/Properties/AssemblyInfo.cs +++ /dev/null @@ -1,59 +0,0 @@ -/* - * This file is part of the Buildings and Habitats object Model (BHoM) - * Copyright (c) 2015 - 2025, the respective contributors. All rights reserved. - * - * Each contributor holds copyright over their respective contributions. - * The project versioning (Git) records all such contribution source information. - * - * - * The BHoM is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3.0 of the License, or - * (at your option) any later version. - * - * The BHoM is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this code. If not, see . - */ - -using System.Reflection; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; - -// General Information about an assembly is controlled through the following -// set of attributes. Change these attribute values to modify the information -// associated with an assembly. -[assembly: AssemblyTitle("BHoM_Windows_UI")] -[assembly: AssemblyDescription("https://github.com/BHoM/BHoM_UI")] -[assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("BuroHappold Engineering")] -[assembly: AssemblyProduct("BHoM_Windows_UI")] -[assembly: AssemblyCopyright("Copyright © BuroHappold Engineering 2024")] -[assembly: AssemblyTrademark("")] -[assembly: AssemblyCulture("")] - -// Setting ComVisible to false makes the types in this assembly not visible -// to COM components. If you need to access a type in this assembly from -// COM, set the ComVisible attribute to true on that type. -[assembly: ComVisible(false)] - -// The following GUID is for the ID of the typelib if this project is exposed to COM -[assembly: Guid("9ba156d1-851d-4267-a94f-6f238d17b634")] - -// Version information for an assembly consists of the following four values: -// -// Major Version -// Minor Version -// Build Number -// Revision -// -// You can specify all the values or you can default the Build and Revision Numbers -// by using the '*' as shown below: -// [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("8.0.0.0")] -[assembly: AssemblyFileVersion("8.1.0.0")] - From 4e863e089ff0815d5c95d10dd6c1f175eb357404 Mon Sep 17 00:00:00 2001 From: IsakNaslundBh Date: Tue, 11 Feb 2025 15:53:44 +0100 Subject: [PATCH 06/15] fix project compliance --- BHoM_UI/BHoM_UI.csproj | 2 +- BHoM_Windows_UI/BHoM_Windows_UI.csproj | 2 +- UI_Engine/UI_Engine.csproj | 18 +++++++++--------- UI_oM/UI_oM.csproj | 2 +- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/BHoM_UI/BHoM_UI.csproj b/BHoM_UI/BHoM_UI.csproj index 58da8de..a3d3497 100644 --- a/BHoM_UI/BHoM_UI.csproj +++ b/BHoM_UI/BHoM_UI.csproj @@ -149,6 +149,6 @@ - + \ No newline at end of file diff --git a/BHoM_Windows_UI/BHoM_Windows_UI.csproj b/BHoM_Windows_UI/BHoM_Windows_UI.csproj index 76b8cf1..c167abc 100644 --- a/BHoM_Windows_UI/BHoM_Windows_UI.csproj +++ b/BHoM_Windows_UI/BHoM_Windows_UI.csproj @@ -37,6 +37,6 @@ - + diff --git a/UI_Engine/UI_Engine.csproj b/UI_Engine/UI_Engine.csproj index 2681600..5ec8116 100644 --- a/UI_Engine/UI_Engine.csproj +++ b/UI_Engine/UI_Engine.csproj @@ -17,49 +17,49 @@ - C:\ProgramData\BHoM\Assemblies\BHoM.dll + $(ProgramData)\BHoM\Assemblies\BHoM.dll False False False - C:\ProgramData\BHoM\Assemblies\BHoM_Adapter.dll + $(ProgramData)\BHoM\Assemblies\BHoM_Adapter.dll False - C:\ProgramData\BHoM\Assemblies\BHoM_Engine.dll + $(ProgramData)\BHoM\Assemblies\BHoM_Engine.dll False False - C:\ProgramData\BHoM\Assemblies\Data_Engine.dll + $(ProgramData)\BHoM\Assemblies\Data_Engine.dll False False - C:\ProgramData\BHoM\Assemblies\Data_oM.dll + $(ProgramData)\BHoM\Assemblies\Data_oM.dll False False False - C:\ProgramData\BHoM\Assemblies\Library_Engine.dll + $(ProgramData)\BHoM\Assemblies\Library_Engine.dll False - C:\ProgramData\BHoM\Assemblies\Reflection_Engine.dll + $(ProgramData)\BHoM\Assemblies\Reflection_Engine.dll False False False False - C:\ProgramData\BHoM\Assemblies\Serialiser_Engine.dll + $(ProgramData)\BHoM\Assemblies\Serialiser_Engine.dll - + \ No newline at end of file diff --git a/UI_oM/UI_oM.csproj b/UI_oM/UI_oM.csproj index 25ea5cb..f31e2eb 100644 --- a/UI_oM/UI_oM.csproj +++ b/UI_oM/UI_oM.csproj @@ -25,6 +25,6 @@ - + From a228d56372a887e03c88c184b9d4b976fdefed33 Mon Sep 17 00:00:00 2001 From: IsakNaslundBh Date: Wed, 12 Feb 2025 15:05:32 +0100 Subject: [PATCH 07/15] 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; } } From 9d6ad5d91d5b8f4c9f35ac841de40db3debecc87 Mon Sep 17 00:00:00 2001 From: IsakNaslundBh Date: Wed, 12 Feb 2025 15:07:26 +0100 Subject: [PATCH 08/15] Slight code re-organisation --- UI_Engine/Compute/LogUsage.cs | 98 ++++++++++++++++++----------------- 1 file changed, 51 insertions(+), 47 deletions(-) diff --git a/UI_Engine/Compute/LogUsage.cs b/UI_Engine/Compute/LogUsage.cs index c23d9cc..86751d5 100644 --- a/UI_Engine/Compute/LogUsage.cs +++ b/UI_Engine/Compute/LogUsage.cs @@ -91,53 +91,7 @@ public static void UpdateProjectId(string uiName, string fileID, string projectI { 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) - { - - } - } - }); + Task.Run(() => UpdateProjectIdsInLogFile(uiName, fileID, projectID)); } /*************************************/ @@ -211,6 +165,56 @@ private static void LogToFile(string uiName, string uiVersion, Guid componentId, /*************************************/ + private static void UpdateProjectIdsInLogFile(string uiName, string fileID, string projectID) + { + 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) + { + + } + } + } + + /*************************************/ + private static void HandleSetProjectId(string uiName, string fileId, List events) { if (!string.IsNullOrEmpty(fileId) && !string.IsNullOrEmpty(uiName)) From accb309cdf9725feaaccdc5d264b9377b5cdc659 Mon Sep 17 00:00:00 2001 From: IsakNaslundBh Date: Wed, 12 Feb 2025 15:18:23 +0100 Subject: [PATCH 09/15] Remove no longer required event handlers --- UI_Engine/Compute/LogUsage.cs | 31 +------------------------------ 1 file changed, 1 insertion(+), 30 deletions(-) diff --git a/UI_Engine/Compute/LogUsage.cs b/UI_Engine/Compute/LogUsage.cs index 86751d5..87673ef 100644 --- a/UI_Engine/Compute/LogUsage.cs +++ b/UI_Engine/Compute/LogUsage.cs @@ -111,7 +111,7 @@ public static void CheckLogOnUiEndOpening(string uiName, string fileId, string f UIName = uiName, FileID = fileId, FileName = fileName, - SelectedItem = "UIEndOpening" + SelectedItem = "UIEndOpening" //Need to be set to something. }; TriggerUsageLog(args); @@ -278,8 +278,6 @@ 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. if (m_UsageLogStream != null) m_UsageLogStream.Close(); - - TriggerUIClose(); } /*************************************/ @@ -300,30 +298,6 @@ private static void TriggerUsageLog(TriggerLogUsageArgs e) m_UsageLogTriggered.Invoke(null, e); //Force the data to be set if the set project ID component is being run during the script load } - /*************************************/ - - private static void TriggerUIClose() - { - if (m_UIClosed != null) - m_UIClosed.Invoke(null, null); - } - - /*************************************/ - - private static void TriggerUIOpening(TriggerLogUsageArgs e) - { - if (m_UIOpening != null) - m_UIOpening.Invoke(null, e); - } - - /*************************************/ - - private static void TriggerUIEndOpening() - { - if (m_UIEndOpening != null) - m_UIEndOpening.Invoke(null, null); - } - /*************************************/ /**** Static Fields ****/ /*************************************/ @@ -338,9 +312,6 @@ private static void TriggerUIEndOpening() private static Dictionary m_ProjectIDPerFile = new Dictionary(); public static event EventHandler m_UsageLogTriggered; - public static event EventHandler m_UIClosed; - public static event EventHandler m_UIOpening; - public static event EventHandler m_UIEndOpening; /*************************************/ } From 1dd38a27e3f48d1f0268b67a2adeb45d8b1e76c8 Mon Sep 17 00:00:00 2001 From: IsakNaslundBh Date: Wed, 12 Feb 2025 15:19:29 +0100 Subject: [PATCH 10/15] Correct name for puplic eventhandler (should not start with m_) --- UI_Engine/Compute/LogUsage.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/UI_Engine/Compute/LogUsage.cs b/UI_Engine/Compute/LogUsage.cs index 87673ef..6722b77 100644 --- a/UI_Engine/Compute/LogUsage.cs +++ b/UI_Engine/Compute/LogUsage.cs @@ -294,8 +294,8 @@ public static string BHoMVersion() private static void TriggerUsageLog(TriggerLogUsageArgs e) { - 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 + if (UsageLogTriggered != null) + UsageLogTriggered.Invoke(null, e); //Force the data to be set if the set project ID component is being run during the script load } /*************************************/ @@ -311,7 +311,7 @@ private static void TriggerUsageLog(TriggerLogUsageArgs e) private static Dictionary m_ProjectIDPerFile = new Dictionary(); - public static event EventHandler m_UsageLogTriggered; + public static event EventHandler UsageLogTriggered; /*************************************/ } From ca9e424acc4d7441311a52555e249b2e678684ec Mon Sep 17 00:00:00 2001 From: IsakNaslundBh Date: Wed, 12 Feb 2025 15:22:59 +0100 Subject: [PATCH 11/15] minor cleanup --- UI_Engine/Compute/LogUsage.cs | 3 --- 1 file changed, 3 deletions(-) diff --git a/UI_Engine/Compute/LogUsage.cs b/UI_Engine/Compute/LogUsage.cs index 6722b77..47216c3 100644 --- a/UI_Engine/Compute/LogUsage.cs +++ b/UI_Engine/Compute/LogUsage.cs @@ -304,11 +304,8 @@ private static void TriggerUsageLog(TriggerLogUsageArgs e) private static object m_LogLock = new object(); private static FileStream m_UsageLogStream = null; - private static string m_BHoMVersion = null; - private static long m_DeprecationPeriod = 28 * TimeSpan.TicksPerDay; // 28 days in ticks - private static Dictionary m_ProjectIDPerFile = new Dictionary(); public static event EventHandler UsageLogTriggered; From c2b7eff0b1c3db6fc438662f147c3a500afc24c4 Mon Sep 17 00:00:00 2001 From: IsakNaslundBh Date: Wed, 12 Feb 2025 16:20:52 +0100 Subject: [PATCH 12/15] Slight reorganisation, and additional collection to store files logged to --- UI_Engine/Compute/LogUsage.cs | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/UI_Engine/Compute/LogUsage.cs b/UI_Engine/Compute/LogUsage.cs index 47216c3..c0d0918 100644 --- a/UI_Engine/Compute/LogUsage.cs +++ b/UI_Engine/Compute/LogUsage.cs @@ -39,6 +39,12 @@ namespace BH.Engine.UI { public static partial class Compute { + /*************************************/ + /**** Public Events ****/ + /*************************************/ + + public static event EventHandler UsageLogTriggered; + /*************************************/ /**** Public Methods ****/ /*************************************/ @@ -48,16 +54,13 @@ public static void LogUsage(string uiName, string uiVersion, Guid componentId, s { //Special case for a component setting the project ID explicitly HandleSetProjectId(uiName, fileId, events); + m_FilesTriedToBeLogged.Add(fileId); //Add to files that have had anything atempted to logged to them to check for end of document opening if (string.IsNullOrWhiteSpace(projectId)) { 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 - } + m_ProjectIDPerFile.TryGetValue(fileId, out projectId); } if (!m_documentOpening) //If not during project opening, call events @@ -79,6 +82,8 @@ public static void LogUsage(string uiName, string uiVersion, Guid componentId, s } } } + else + m_ProjectIDPerFile[fileId] = projectId; LogToFile(uiName, uiVersion, componentId, callerName, selectedItem, events, fileId, fileName, projectId); } @@ -100,22 +105,23 @@ public static void CheckLogOnUiEndOpening(string uiName, string fileId, string f { 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 (m_FilesTriedToBeLogged.Contains(fileId)) //Only try to do this is something has been atempted to be logged { + string projectId; + m_ProjectIDPerFile.TryGetValue(fileId, out projectId); + if (string.IsNullOrEmpty(projectId)) //Only run if projectId is not set { - TriggerLogUsageArgs args = new TriggerLogUsageArgs() { UIName = uiName, FileID = fileId, - FileName = fileName, - SelectedItem = "UIEndOpening" //Need to be set to something. + FileName = fileName }; TriggerUsageLog(args); } + } } } @@ -307,8 +313,7 @@ private static void TriggerUsageLog(TriggerLogUsageArgs e) private static string m_BHoMVersion = null; private static long m_DeprecationPeriod = 28 * TimeSpan.TicksPerDay; // 28 days in ticks private static Dictionary m_ProjectIDPerFile = new Dictionary(); - - public static event EventHandler UsageLogTriggered; + private static HashSet m_FilesTriedToBeLogged = new HashSet(); /*************************************/ } From 81cb3458764e3f4d3dda2efb6e632d271af0fd3a Mon Sep 17 00:00:00 2001 From: IsakNaslundBh Date: Wed, 12 Feb 2025 16:22:18 +0100 Subject: [PATCH 13/15] minor comment update --- UI_Engine/Compute/LogUsage.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/UI_Engine/Compute/LogUsage.cs b/UI_Engine/Compute/LogUsage.cs index c0d0918..20eaedd 100644 --- a/UI_Engine/Compute/LogUsage.cs +++ b/UI_Engine/Compute/LogUsage.cs @@ -105,12 +105,12 @@ public static void CheckLogOnUiEndOpening(string uiName, string fileId, string f { if (fileName != null) //Only call when opening a pre-existing file, not for new files with no filename set { - if (m_FilesTriedToBeLogged.Contains(fileId)) //Only try to do this is something has been atempted to be logged + if (m_FilesTriedToBeLogged.Contains(fileId)) //Only call when file has been atempted to be logged to { string projectId; m_ProjectIDPerFile.TryGetValue(fileId, out projectId); - if (string.IsNullOrEmpty(projectId)) //Only run if projectId is not set + if (string.IsNullOrEmpty(projectId)) //Only call when projectId is not set { TriggerLogUsageArgs args = new TriggerLogUsageArgs() { From dd64984aa6e7d0b30c0f5217e2bd25d6289fcc60 Mon Sep 17 00:00:00 2001 From: IsakNaslundBh Date: Wed, 12 Feb 2025 16:26:37 +0100 Subject: [PATCH 14/15] nullchecks --- UI_Engine/Compute/LogUsage.cs | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/UI_Engine/Compute/LogUsage.cs b/UI_Engine/Compute/LogUsage.cs index 20eaedd..ff83bb8 100644 --- a/UI_Engine/Compute/LogUsage.cs +++ b/UI_Engine/Compute/LogUsage.cs @@ -90,19 +90,25 @@ public static void LogUsage(string uiName, string uiVersion, Guid componentId, s /*************************************/ - public static void UpdateProjectId(string uiName, string fileID, string projectID) + public static void UpdateProjectId(string uiName, string fileId, string projectID) { + if (string.IsNullOrWhiteSpace(uiName) || string.IsNullOrWhiteSpace(fileId)) + return; + lock (m_LogLock) { - m_ProjectIDPerFile[fileID] = projectID; + m_ProjectIDPerFile[fileId] = projectID; } - Task.Run(() => UpdateProjectIdsInLogFile(uiName, fileID, projectID)); + Task.Run(() => UpdateProjectIdsInLogFile(uiName, fileId, projectID)); } /*************************************/ public static void CheckLogOnUiEndOpening(string uiName, string fileId, string fileName) { + if (string.IsNullOrWhiteSpace(uiName) || string.IsNullOrWhiteSpace(fileId)) + return; + if (fileName != null) //Only call when opening a pre-existing file, not for new files with no filename set { if (m_FilesTriedToBeLogged.Contains(fileId)) //Only call when file has been atempted to be logged to @@ -171,7 +177,7 @@ private static void LogToFile(string uiName, string uiVersion, Guid componentId, /*************************************/ - private static void UpdateProjectIdsInLogFile(string uiName, string fileID, string projectID) + private static void UpdateProjectIdsInLogFile(string uiName, string fileId, string projectID) { lock (m_LogLock) { @@ -195,7 +201,7 @@ private static void UpdateProjectIdsInLogFile(string uiName, string fileID, stri { if (o != null) { - if (o.FileId == fileID) + if (o.FileId == fileId) o.ProjectID = projectID; } } From 7a4af154b45855efb9091079fdebed69d7c4c5b1 Mon Sep 17 00:00:00 2001 From: IsakNaslundBh Date: Fri, 14 Feb 2025 08:50:59 +0100 Subject: [PATCH 15/15] Centralise to cache is only updated in one place --- UI_Engine/Compute/LogUsage.cs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/UI_Engine/Compute/LogUsage.cs b/UI_Engine/Compute/LogUsage.cs index ff83bb8..a488ebe 100644 --- a/UI_Engine/Compute/LogUsage.cs +++ b/UI_Engine/Compute/LogUsage.cs @@ -83,23 +83,30 @@ public static void LogUsage(string uiName, string uiVersion, Guid componentId, s } } else - m_ProjectIDPerFile[fileId] = projectId; + UpdateProjectId(uiName, fileId, projectId); LogToFile(uiName, uiVersion, componentId, callerName, selectedItem, events, fileId, fileName, projectId); } /*************************************/ - public static void UpdateProjectId(string uiName, string fileId, string projectID) + public static void UpdateProjectId(string uiName, string fileId, string projectId) { if (string.IsNullOrWhiteSpace(uiName) || string.IsNullOrWhiteSpace(fileId)) return; + bool wasUpdated = true; lock (m_LogLock) { - m_ProjectIDPerFile[fileId] = projectID; + string prevPojectId; + if (m_ProjectIDPerFile.TryGetValue(fileId, out prevPojectId) && projectId == prevPojectId) //Check if the ID changed from previous cached value + wasUpdated = false; + + m_ProjectIDPerFile[fileId] = projectId; //Set the project Id to the cached dictionary } - Task.Run(() => UpdateProjectIdsInLogFile(uiName, fileId, projectID)); + + if(wasUpdated) //If changed from previous value, make sure the logfiles are updated + Task.Run(() => UpdateProjectIdsInLogFile(uiName, fileId, projectId)); } /*************************************/