From 9c9b6c5749d1e7b5e17c18bb3bd41635f8549da4 Mon Sep 17 00:00:00 2001 From: djbadders <34887832+djbadders@users.noreply.github.com> Date: Sun, 8 Aug 2021 17:29:37 +0100 Subject: [PATCH 1/2] Fix possible exception when PTM last run file is being written --- Monitor/_Internal/BasePageModel.cs | 339 +++++++++++++++-------------- 1 file changed, 178 insertions(+), 161 deletions(-) diff --git a/Monitor/_Internal/BasePageModel.cs b/Monitor/_Internal/BasePageModel.cs index b3ee870..d05f2eb 100644 --- a/Monitor/_Internal/BasePageModel.cs +++ b/Monitor/_Internal/BasePageModel.cs @@ -1,5 +1,6 @@ using System; using System.IO; +using System.Threading; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.Extensions.Configuration; @@ -15,170 +16,186 @@ using Core.Main.DataObjects; namespace Monitor._Internal { - public class BasePageModel : PageModel - { - public string PTMagicBasePath = ""; - public string PTMagicMonitorBasePath = ""; - public PTMagicConfiguration PTMagicConfiguration = null; - public Summary Summary { get; set; } = new Summary(); - public LogHelper Log = null; - public string LatestVersion = ""; - public string CurrentBotVersion = ""; - public string NotifyHeadline = ""; - public string NotifyMessage = ""; - public string NotifyType = ""; - - public string MainFiatCurrencySymbol = "$"; - private volatile object _ptDataLock = new object(); - private static ProfitTrailerData _ptData = null; - - // Profit Trailer data accessor object - public ProfitTrailerData PtDataObject + public class BasePageModel : PageModel { - get - { - if (_ptData == null) + public string PTMagicBasePath = ""; + public string PTMagicMonitorBasePath = ""; + public PTMagicConfiguration PTMagicConfiguration = null; + public Summary Summary { get; set; } = new Summary(); + public LogHelper Log = null; + public string LatestVersion = ""; + public string CurrentBotVersion = ""; + public string NotifyHeadline = ""; + public string NotifyMessage = ""; + public string NotifyType = ""; + + public string MainFiatCurrencySymbol = "$"; + private volatile object _ptDataLock = new object(); + private static ProfitTrailerData _ptData = null; + + // Profit Trailer data accessor object + public ProfitTrailerData PtDataObject { - lock (_ptDataLock) - { - _ptData = new ProfitTrailerData(PTMagicConfiguration); - } + get + { + if (_ptData == null) + { + lock (_ptDataLock) + { + _ptData = new ProfitTrailerData(PTMagicConfiguration); + } + } + + return _ptData; + } } - return _ptData; - } + public void PreInit() + { + PTMagicMonitorBasePath = Directory.GetCurrentDirectory(); + if (!System.IO.File.Exists(PTMagicMonitorBasePath + Path.DirectorySeparatorChar + "appsettings.json")) + { + PTMagicMonitorBasePath += Path.DirectorySeparatorChar + "Monitor"; + } + + if (!PTMagicMonitorBasePath.EndsWith(Path.DirectorySeparatorChar)) + { + PTMagicMonitorBasePath += Path.DirectorySeparatorChar; + } + + IConfiguration config = new ConfigurationBuilder() + .SetBasePath(PTMagicMonitorBasePath) + .AddJsonFile("appsettings.json", false) + .Build(); + + PTMagicBasePath = config.GetValue("PTMagicBasePath"); + + if (!PTMagicBasePath.EndsWith(Path.DirectorySeparatorChar)) + { + PTMagicBasePath += Path.DirectorySeparatorChar; + } + + PTMagicConfiguration = new PTMagicConfiguration(PTMagicBasePath); + + IServiceProvider logProvider = ServiceHelper.BuildLoggerService(PTMagicBasePath); + Log = logProvider.GetRequiredService(); + + bool exitLoop = false; + + while (!exitLoop) + { + try + { + // Try to read the current runtime summary, but may be being written, so retry if necessary. + Summary = JsonConvert.DeserializeObject(System.IO.File.ReadAllText(PTMagicBasePath + Constants.PTMagicPathData + Path.DirectorySeparatorChar + "LastRuntimeSummary.json")); + exitLoop = true; + } + catch (IOException e) + { + // Squash exception and try again, as file was locked. + Thread.Sleep(250); + } + } + + if (Summary.CurrentGlobalSetting == null) Summary.CurrentGlobalSetting = PTMagicConfiguration.AnalyzerSettings.GlobalSettings.Find(s => s.SettingName.IndexOf("default", StringComparison.InvariantCultureIgnoreCase) > -1); + + MainFiatCurrencySymbol = SystemHelper.GetCurrencySymbol(Summary.MainFiatCurrency); + + try + { + // Get latest release from GitHub + if (!String.IsNullOrEmpty(HttpContext.Session.GetString("LatestVersion"))) + { + LatestVersion = HttpContext.Session.GetString("LatestVersion"); + } + else + { + LatestVersion = BaseAnalyzer.GetLatestGitHubRelease(Log, Summary.Version); + HttpContext.Session.SetString("LatestVersion", LatestVersion); + } + } + catch { } + + try + { + // Get current bot version + if (!String.IsNullOrEmpty(HttpContext.Session.GetString("CurrentBotVersion"))) + { + CurrentBotVersion = HttpContext.Session.GetString("CurrentBotVersion"); + } + else + { + string ptMagicBotDllPath = PTMagicBasePath + "PTMagic.dll"; + if (System.IO.File.Exists(ptMagicBotDllPath)) + { + FileVersionInfo ptMagicDllInfo = FileVersionInfo.GetVersionInfo(ptMagicBotDllPath); + + CurrentBotVersion = ptMagicDllInfo.ProductVersion.Substring(0, ptMagicDllInfo.ProductVersion.LastIndexOf(".")); + HttpContext.Session.SetString("CurrentBotVersion", CurrentBotVersion); + } + else + { + CurrentBotVersion = Summary.Version; + } + } + + } + catch + { + CurrentBotVersion = Summary.Version; + } + } + + protected string GetStringParameter(string paramName, string defaultValue) + { + string result = defaultValue; + + if (HttpContext.Request.Query.ContainsKey(paramName)) + { + result = HttpContext.Request.Query[paramName]; + } + else if (HttpContext.Request.Method.Equals("POST") && HttpContext.Request.Form.ContainsKey(paramName)) + { + result = HttpContext.Request.Form[paramName]; + } + + return result; + } + + /// + /// Holt einen Url-Parameter als Integer, wenn vorhanden. + /// + /// Name des Parameters + /// Defaultvalue, wenn Parameter nicht vorhanden ist. + /// Der Wert des Parameters als Integer. + protected int GetIntParameter(string paramName, int defaultValue) + { + int result = defaultValue; + + if (HttpContext.Request.Query.ContainsKey(paramName)) + { + try + { + result = Int32.Parse(HttpContext.Request.Query[paramName]); + } + catch + { + result = defaultValue; + } + } + else if (HttpContext.Request.Method.Equals("POST") && HttpContext.Request.Form.ContainsKey(paramName)) + { + try + { + result = Int32.Parse(HttpContext.Request.Form[paramName]); + } + catch + { + result = defaultValue; + } + } + + return result; + } } - - public void PreInit() - { - PTMagicMonitorBasePath = Directory.GetCurrentDirectory(); - if (!System.IO.File.Exists(PTMagicMonitorBasePath + Path.DirectorySeparatorChar + "appsettings.json")) - { - PTMagicMonitorBasePath += Path.DirectorySeparatorChar + "Monitor"; - } - - if (!PTMagicMonitorBasePath.EndsWith(Path.DirectorySeparatorChar)) - { - PTMagicMonitorBasePath += Path.DirectorySeparatorChar; - } - - IConfiguration config = new ConfigurationBuilder() - .SetBasePath(PTMagicMonitorBasePath) - .AddJsonFile("appsettings.json", false) - .Build(); - - PTMagicBasePath = config.GetValue("PTMagicBasePath"); - - if (!PTMagicBasePath.EndsWith(Path.DirectorySeparatorChar)) - { - PTMagicBasePath += Path.DirectorySeparatorChar; - } - - PTMagicConfiguration = new PTMagicConfiguration(PTMagicBasePath); - - IServiceProvider logProvider = ServiceHelper.BuildLoggerService(PTMagicBasePath); - Log = logProvider.GetRequiredService(); - - Summary = JsonConvert.DeserializeObject(System.IO.File.ReadAllText(PTMagicBasePath + Constants.PTMagicPathData + Path.DirectorySeparatorChar + "LastRuntimeSummary.json")); - if (Summary.CurrentGlobalSetting == null) Summary.CurrentGlobalSetting = PTMagicConfiguration.AnalyzerSettings.GlobalSettings.Find(s => s.SettingName.IndexOf("default", StringComparison.InvariantCultureIgnoreCase) > -1); - - MainFiatCurrencySymbol = SystemHelper.GetCurrencySymbol(Summary.MainFiatCurrency); - - try - { - // Get latest release from GitHub - if (!String.IsNullOrEmpty(HttpContext.Session.GetString("LatestVersion"))) - { - LatestVersion = HttpContext.Session.GetString("LatestVersion"); - } - else - { - LatestVersion = BaseAnalyzer.GetLatestGitHubRelease(Log, Summary.Version); - HttpContext.Session.SetString("LatestVersion", LatestVersion); - } - } - catch { } - - try - { - // Get current bot version - if (!String.IsNullOrEmpty(HttpContext.Session.GetString("CurrentBotVersion"))) - { - CurrentBotVersion = HttpContext.Session.GetString("CurrentBotVersion"); - } - else - { - string ptMagicBotDllPath = PTMagicBasePath + "PTMagic.dll"; - if (System.IO.File.Exists(ptMagicBotDllPath)) - { - FileVersionInfo ptMagicDllInfo = FileVersionInfo.GetVersionInfo(ptMagicBotDllPath); - - CurrentBotVersion = ptMagicDllInfo.ProductVersion.Substring(0, ptMagicDllInfo.ProductVersion.LastIndexOf(".")); - HttpContext.Session.SetString("CurrentBotVersion", CurrentBotVersion); - } - else - { - CurrentBotVersion = Summary.Version; - } - } - - } - catch - { - CurrentBotVersion = Summary.Version; - } - } - - protected string GetStringParameter(string paramName, string defaultValue) - { - string result = defaultValue; - - if (HttpContext.Request.Query.ContainsKey(paramName)) - { - result = HttpContext.Request.Query[paramName]; - } - else if (HttpContext.Request.Method.Equals("POST") && HttpContext.Request.Form.ContainsKey(paramName)) - { - result = HttpContext.Request.Form[paramName]; - } - - return result; - } - - /// - /// Holt einen Url-Parameter als Integer, wenn vorhanden. - /// - /// Name des Parameters - /// Defaultvalue, wenn Parameter nicht vorhanden ist. - /// Der Wert des Parameters als Integer. - protected int GetIntParameter(string paramName, int defaultValue) - { - int result = defaultValue; - - if (HttpContext.Request.Query.ContainsKey(paramName)) - { - try - { - result = Int32.Parse(HttpContext.Request.Query[paramName]); - } - catch - { - result = defaultValue; - } - } - else if (HttpContext.Request.Method.Equals("POST") && HttpContext.Request.Form.ContainsKey(paramName)) - { - try - { - result = Int32.Parse(HttpContext.Request.Form[paramName]); - } - catch - { - result = defaultValue; - } - } - - return result; - } - } } From 309e4dfe7d02ba63296732db2007e4c1d927e7b4 Mon Sep 17 00:00:00 2001 From: djbadders <34887832+djbadders@users.noreply.github.com> Date: Sun, 8 Aug 2021 17:30:01 +0100 Subject: [PATCH 2/2] Implement paged retrieval of sales dat --- Core/DataObjects/ProfitTrailerData.cs | 28 +++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/Core/DataObjects/ProfitTrailerData.cs b/Core/DataObjects/ProfitTrailerData.cs index 6cc70b1..b49fa63 100644 --- a/Core/DataObjects/ProfitTrailerData.cs +++ b/Core/DataObjects/ProfitTrailerData.cs @@ -108,7 +108,28 @@ namespace Core.Main.DataObjects if (_sellLog == null || (DateTime.UtcNow > _sellLogRefresh)) { _sellLog.Clear(); - this.BuildSellLogData(GetDataFromProfitTrailer("/api/v2/data/sales")); + + // Page through the sales data summarizing it. + bool exitLoop = false; + int pageIndex = 1; + + while (!exitLoop) + { + var sellDataPage = GetDataFromProfitTrailer("/api/v2/data/sales?perPage=5000&sort=SOLDDATE&sortDirection=ASCENDING&page=" + pageIndex); + if (sellDataPage != null && sellDataPage.data.Count > 0) + { + // Add sales data page to collection + this.BuildSellLogData(sellDataPage); + pageIndex++; + } + else + { + // All data retrieved + exitLoop = true; + } + } + + // Update sell log refresh time _sellLogRefresh = DateTime.UtcNow.AddSeconds(_systemConfiguration.GeneralSettings.Monitor.RefreshSeconds - 1); } } @@ -265,7 +286,10 @@ namespace Core.Main.DataObjects private dynamic GetDataFromProfitTrailer(string callPath, bool arrayReturned = false) { string rawBody = ""; - string url = string.Format("{0}{1}?token={2}", _systemConfiguration.GeneralSettings.Application.ProfitTrailerMonitorURL, callPath, _systemConfiguration.GeneralSettings.Application.ProfitTrailerServerAPIToken); + string url = string.Format("{0}{1}{2}token={3}", _systemConfiguration.GeneralSettings.Application.ProfitTrailerMonitorURL, + callPath, + callPath.Contains("?") ? "&" : "?", + _systemConfiguration.GeneralSettings.Application.ProfitTrailerServerAPIToken); // Get the data from PT Debug.WriteLine(String.Format("{0} - Calling '{1}'", DateTime.UtcNow, url));