/*************************************/ /**** Public Methods ****/ /*************************************/ public static void LogUsage(string uiName, string uiVersion, Guid componentId, string callerName, object selectedItem, List <Event> events = null, string fileId = "", string fileName = "") { // If a projectID event is available, save the project code for this file if (events != null) { ProjectIDEvent e = events.OfType <ProjectIDEvent>().FirstOrDefault(); if (e != null && !string.IsNullOrEmpty(fileId)) { m_ProjectIDPerFile[fileId] = e.ProjectID; } } try { // Create the log item UsageLogEntry info = new UsageLogEntry { UI = uiName, UiVersion = uiVersion, BHoMVersion = BHoMVersion(), ComponentId = componentId, CallerName = callerName, SelectedItem = selectedItem, FileId = fileId, FileName = fileName, Errors = events == null ? new List <Event>() : events.Where(x => x.Type == EventType.Error).ToList() }; // 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(); } catch { } }
/***************************************************/ /**** Public Methods ****/ /***************************************************/ public static List <UsageEntry> SummariseUsageData(this List <UsageLogEntry> logEntries) { string computer = Environment.MachineName; List <UsageEntry> dbEntries = new List <UsageEntry>(); foreach (var fileGroup in logEntries.GroupBy(x => x.FileId)) { string fileId = fileGroup.Key; string fileName = fileGroup.Select(x => x.FileName).Where(x => !string.IsNullOrEmpty(x)).FirstOrDefault(); string projectID = fileGroup.Select(x => x.ProjectID).Where(x => !string.IsNullOrEmpty(x)).FirstOrDefault(); foreach (var itemGroup in fileGroup.GroupBy(x => x.CallerName + x.SelectedItem)) { UsageLogEntry firstEntry = itemGroup.First(); dbEntries.Add(new UsageEntry { StartTime = itemGroup.Min(x => x.Time), EndTime = itemGroup.Max(x => x.Time), UI = firstEntry.UI, UiVersion = firstEntry.UiVersion, CallerName = firstEntry.CallerName, SelectedItem = firstEntry.SelectedItem, Computer = computer, BHoMVersion = firstEntry.BHoMVersion, FileId = fileId, FileName = fileName, ProjectID = projectID, NbCallingComponents = itemGroup.Select(x => x.ComponentId).Distinct().Count(), TotalNbCalls = itemGroup.Count(), Errors = itemGroup.SelectMany(x => x.Errors).GroupBy(x => x.Message).Select(g => g.First()).ToList() }); } } return(dbEntries); }