From bde9b98f11da7c8eeb20d4cada92d9ccd1206cf2 Mon Sep 17 00:00:00 2001 From: HojouFotytu <36724681+HojouFotytu@users.noreply.github.com> Date: Sat, 30 Dec 2023 14:46:45 +0900 Subject: [PATCH] Dashboard Bottom Sales Overview changes --- Core/DataObjects/PTMagicData.cs | 25 +++ Core/DataObjects/ProfitTrailerData.cs | 177 +++++++++++-------- Monitor/Pages/_get/DashboardBottom.cshtml | 110 +++++++----- Monitor/Pages/_get/DashboardBottom.cshtml.cs | 3 + 4 files changed, 195 insertions(+), 120 deletions(-) diff --git a/Core/DataObjects/PTMagicData.cs b/Core/DataObjects/PTMagicData.cs index fa6f69b..a7d6bde 100644 --- a/Core/DataObjects/PTMagicData.cs +++ b/Core/DataObjects/PTMagicData.cs @@ -431,6 +431,31 @@ namespace Core.Main.DataObjects.PTMagicData public double TotalCost { get; set; } public double SoldPrice { get; set; } public double SoldValue { get; set; } + public double TotalSales { get; set; } + } + + public class StatsData + { + public double SalesToday { get; set; } + public double ProfitToday { get; set; } + public double ProfitPercToday { get; set; } + public double SalesYesterday { get; set; } + public double ProfitYesterday { get; set; } + public double ProfitPercYesterday { get; set; } + public double SalesWeek { get; set; } + public double ProfitWeek { get; set; } + public double ProfitPercWeek { get; set; } + public double SalesMonth { get; set; } + public double ProfitMonth { get; set; } + public double ProfitPercMonth { get; set; } + public double TotalProfit { get; set; } + public double TotalSales { get; set; } + public double TotalProfitPerc { get; set; } + public double FundingToday { get; set; } + public double FundingYesterday { get; set; } + public double FundingWeek { get; set; } + public double FundingMonth { get; set; } + public double FundingTotal { get; set; } } public class PTStrategy diff --git a/Core/DataObjects/ProfitTrailerData.cs b/Core/DataObjects/ProfitTrailerData.cs index ca04707..10b8231 100644 --- a/Core/DataObjects/ProfitTrailerData.cs +++ b/Core/DataObjects/ProfitTrailerData.cs @@ -16,16 +16,22 @@ namespace Core.Main.DataObjects { private SummaryData _summary = null; private Properties _properties = null; + private List _stats = null; private List _sellLog = new List(); private List _dcaLog = new List(); private List _buyLog = new List(); private string _ptmBasePath = ""; private PTMagicConfiguration _systemConfiguration = null; private TransactionData _transactionData = null; - private DateTime _buyLogRefresh = DateTime.UtcNow, _sellLogRefresh = DateTime.UtcNow, _dcaLogRefresh = DateTime.UtcNow, _summaryRefresh = DateTime.UtcNow, _propertiesRefresh = DateTime.UtcNow; - private volatile object _buyLock = new object(), _sellLock = new object(), _dcaLock = new object(), _summaryLock = new object(), _propertiesLock = new object(); + private DateTime _statsRefresh = DateTime.UtcNow,_buyLogRefresh = DateTime.UtcNow, _sellLogRefresh = DateTime.UtcNow, _dcaLogRefresh = DateTime.UtcNow, _summaryRefresh = DateTime.UtcNow, _propertiesRefresh = DateTime.UtcNow; + private volatile object _statsLock = new object(),_buyLock = new object(), _sellLock = new object(), _dcaLock = new object(), _summaryLock = new object(), _propertiesLock = new object(); private TimeSpan? _offsetTimeSpan = null; - + + public void DoLog(string message) + { + // Implement your logging logic here + Console.WriteLine(message); + } // Constructor public ProfitTrailerData(PTMagicConfiguration systemConfiguration) { @@ -96,86 +102,82 @@ namespace Core.Main.DataObjects return _properties; } } + + public List Stats + { + get + { + if (_stats == null || DateTime.UtcNow > _statsRefresh) + { + lock (_statsLock) + { + if (_stats == null || DateTime.UtcNow > _statsRefresh) + { + dynamic statsDataJson = GetDataFromProfitTrailer("/api/v2/data/stats"); + JObject statsDataJObject = statsDataJson as JObject; + JObject basicSection = (JObject)statsDataJObject["basic"]; + _stats = new List { BuildStatsData(basicSection) }; + _statsRefresh = DateTime.UtcNow.AddSeconds(_systemConfiguration.GeneralSettings.Monitor.RefreshSeconds - 1); + } + } + } + return _stats; + } + } + + public List SellLog { get { + if (_sellLog == null || (DateTime.UtcNow > _sellLogRefresh)) { - lock (_sellLock) - { - // Thread double locking - if (_sellLog == null || (DateTime.UtcNow > _sellLogRefresh)) + lock (_sellLock) { - _sellLog.Clear(); - - // Page through the sales data summarizing it. - bool exitLoop = false; - int pageIndex = 1; - - int maxPages = _systemConfiguration.GeneralSettings.Monitor.MaxSalesRecords; - int requestedPages = 0; - - while (!exitLoop && requestedPages < maxPages) + // Thread double locking + if (_sellLog == null || (DateTime.UtcNow > _sellLogRefresh)) { - var sellDataPage = GetDataFromProfitTrailer("/api/v2/data/sales?Page=1&perPage=1&sort=SOLDDATE&sortDirection=DESCENDING&page=" + pageIndex); - if (sellDataPage != null && sellDataPage.data.Count > 0) - { - // Add sales data page to collection - this.BuildSellLogData(sellDataPage); - pageIndex++; - requestedPages++; + _sellLog.Clear(); + - } - else + // Page through the sales data summarizing it. + bool exitLoop = false; + int pageIndex = 1; + + // 1 record per page to allow user to set max records to retrieve + int maxPages = _systemConfiguration.GeneralSettings.Monitor.MaxSalesRecords; + int requestedPages = 0; + + while (!exitLoop && requestedPages < maxPages) { - // All data retrieved - exitLoop = true; + var sellDataPage = GetDataFromProfitTrailer("/api/v2/data/sales?Page=1&perPage=1&sort=SOLDDATE&sortDirection=DESCENDING&page=" + pageIndex); + if (sellDataPage != null && sellDataPage.data.Count > 0) + { + // Add sales data page to collection + this.BuildSellLogData(sellDataPage); + pageIndex++; + requestedPages++; + Console.WriteLine($"Importing sale: {pageIndex}"); + + } + else + { + // All data retrieved + exitLoop = true; + } } + + // Update sell log refresh time + _sellLogRefresh = DateTime.UtcNow.AddSeconds(_systemConfiguration.GeneralSettings.Monitor.RefreshSeconds -1); } - - // Update sell log refresh time - _sellLogRefresh = DateTime.UtcNow.AddSeconds(_systemConfiguration.GeneralSettings.Monitor.RefreshSeconds -1); } } - } - return _sellLog; } } - public List SellLogToday - { - get - { - return SellLog.FindAll(sl => sl.SoldDate.Date == LocalizedTime.DateTime.Date); - } - } - - public List SellLogYesterday - { - get - { - return SellLog.FindAll(sl => sl.SoldDate.Date == LocalizedTime.DateTime.AddDays(-1).Date); - } - } - - public List SellLogLast7Days - { - get - { - return SellLog.FindAll(sl => sl.SoldDate.Date >= LocalizedTime.DateTime.AddDays(-7).Date); - } - } - - public List SellLogLast30Days - { - get - { - return SellLog.FindAll(sl => sl.SoldDate.Date >= LocalizedTime.DateTime.AddDays(-30).Date); - } - } - + public List DCALog { get @@ -313,15 +315,18 @@ namespace Core.Main.DataObjects response.Close(); - // Parse the JSON and build the data sets + if (!arrayReturned) - { - return JObject.Parse(rawBody); - } - else - { - return JArray.Parse(rawBody); - } + { + return JObject.Parse(rawBody); + } + else + { + return JArray.Parse(rawBody); + } + + + } private SummaryData BuildSummaryData(dynamic PTData) @@ -349,6 +354,32 @@ namespace Core.Main.DataObjects BaseUrl = PTProperties.baseUrl }; } + private StatsData BuildStatsData(dynamic statsDataJson) + { + return new StatsData() + { + SalesToday = statsDataJson["totalSalesToday"], + ProfitToday = statsDataJson["totalProfitToday"], + ProfitPercToday = statsDataJson["totalProfitPercToday"], + SalesYesterday = statsDataJson["totalSalesYesterday"], + ProfitYesterday = statsDataJson["totalProfitYesterday"], + ProfitPercYesterday = statsDataJson["totalProfitPercYesterday"], + SalesWeek = statsDataJson["totalSalesWeek"], + ProfitWeek = statsDataJson["totalProfitWeek"], + ProfitPercWeek = statsDataJson["totalProfitPercWeek"], + SalesMonth = statsDataJson["totalSalesThisMonth"], + ProfitMonth = statsDataJson["totalProfitThisMonth"], + ProfitPercMonth = statsDataJson["totalProfitPercThisMonth"], + TotalProfit = statsDataJson["totalProfit"], + TotalSales = statsDataJson["totalSales"], + TotalProfitPerc = statsDataJson["totalProfitPerc"], + FundingToday = statsDataJson["totalFundingToday"], + FundingYesterday = statsDataJson["totalFundingYesterday"], + FundingWeek = statsDataJson["totalFundingWeek"], + FundingMonth = statsDataJson["totalFundingThisMonth"], + FundingTotal = statsDataJson["totalFunding"] + }; + } private void BuildSellLogData(dynamic rawSellLogData) { foreach (var rsld in rawSellLogData.data) diff --git a/Monitor/Pages/_get/DashboardBottom.cshtml b/Monitor/Pages/_get/DashboardBottom.cshtml index 823b6f6..d15c6d4 100644 --- a/Monitor/Pages/_get/DashboardBottom.cshtml +++ b/Monitor/Pages/_get/DashboardBottom.cshtml @@ -62,7 +62,7 @@
-
+

@@ -113,46 +113,56 @@
-
+

Sales Overviewmore

@{ - double totalProfit = 0; - totalProfit = Model.PTData.SellLog.Sum(s => s.Profit); - double totalProfitFiat = Math.Round(totalProfit * Model.Summary.MainMarketPrice, 2); - double percentGain = Math.Round(totalProfit / Model.PTMagicConfiguration.GeneralSettings.Application.StartBalance * 100, 2); - string percentGainText = percentGain.ToString("#,#0.00", new System.Globalization.CultureInfo("en-US")) + "%"; - if (Model.PTData.TransactionData.Transactions.Count > 0) - { - percentGainText = ""; - } - double todaysProfit = 0; - todaysProfit = Model.PTData.SellLogToday.Sum(s => s.Profit); - double todaysStartBalance = Model.PTData.GetSnapshotBalance(Model.DateTimeNow.DateTime); - double todaysProfitFiat = Math.Round(todaysProfit * Model.Summary.MainMarketPrice, 2); - double todaysPercentGain = Math.Round(todaysProfit / todaysStartBalance * 100, 2); + var overviewStats = Model.StatsData.FirstOrDefault(); // todaysStats is a new variable + + var todaysSales = overviewStats.SalesToday; + var todaysProfit = overviewStats.ProfitToday; + var todaysFunding = overviewStats.FundingToday; + var todaysPercentGain = overviewStats.ProfitPercToday; + var todaysFundingGain = todaysPercentGain * ((todaysProfit - todaysFunding) / todaysProfit); + + + var yesterdaysSales = overviewStats.SalesYesterday; + var yesterdaysProfit = overviewStats.ProfitYesterday; + var yesterdaysFunding = overviewStats.FundingYesterday; + var yesterdaysPercentGain = overviewStats.ProfitPercYesterday; + var yesterdaysFundingGain = yesterdaysPercentGain * ((yesterdaysProfit + yesterdaysFunding) / yesterdaysProfit); + + var last7DaysSales = overviewStats.SalesWeek; + var last7DaysProfit = overviewStats.ProfitWeek; + var last7DaysFunding = overviewStats.FundingWeek; + var last7DaysPercentGain = overviewStats.ProfitPercWeek; + var last7DaysFundingGain = last7DaysPercentGain * ((last7DaysProfit + last7DaysFunding) / last7DaysProfit); + + var last30DaysSales = overviewStats.SalesMonth; + var last30DaysProfit = overviewStats.ProfitMonth; + var last30DaysFunding = overviewStats.FundingMonth; + var last30DaysPercentGain = overviewStats.ProfitPercMonth; + var last30DaysFundingGain = last30DaysPercentGain * ((last30DaysProfit + last30DaysFunding) / last30DaysProfit); + + var totalSales = overviewStats.TotalSales; + var totalProfit = overviewStats.TotalProfit; + var totalFunding = overviewStats.FundingTotal; + var totalProfitPercent = overviewStats.TotalProfitPerc; + var totalFundingGain = totalProfitPercent * ((totalProfit + totalFunding) / totalProfit); + + double todaysProfitFiat = Math.Round((todaysProfit + todaysFunding) * Model.Summary.MainMarketPrice, 2); + double yesterdaysProfitFiat = Math.Round((yesterdaysProfit + yesterdaysFunding) * Model.Summary.MainMarketPrice, 2); + double last7DaysProfitFiat = Math.Round((last7DaysProfit + last7DaysFunding) * Model.Summary.MainMarketPrice, 2); + double last30DaysProfitFiat = Math.Round((last30DaysProfit + last30DaysFunding) * Model.Summary.MainMarketPrice, 2); + double totalProfitFiat = Math.Round((totalProfit + totalFunding) * Model.Summary.MainMarketPrice, 2); + - double yesterdaysProfit = 0; - yesterdaysProfit = Model.PTData.SellLogYesterday.Sum(s => s.Profit); - double yesterdaysStartBalance = Model.PTData.GetSnapshotBalance(Model.DateTimeNow.DateTime.AddDays(-1)); - double yesterdaysProfitFiat = Math.Round(yesterdaysProfit * Model.Summary.MainMarketPrice, 2); - double yesterdaysPercentGain = Math.Round(yesterdaysProfit / yesterdaysStartBalance * 100, 2); - double last7DaysProfit = 0; - last7DaysProfit = Model.PTData.SellLogLast7Days.Sum(s => s.Profit); - double last7DaysStartBalance = Model.PTData.GetSnapshotBalance(Model.DateTimeNow.DateTime.AddDays(-7)); - double last7DaysProfitFiat = Math.Round(last7DaysProfit * Model.Summary.MainMarketPrice, 2); - double last7DaysPercentGain = Math.Round(last7DaysProfit / last7DaysStartBalance * 100, 2); - double last30DaysProfit = 0; - last30DaysProfit = Model.PTData.SellLogLast30Days.Sum(s => s.Profit); - double last30DaysStartBalance = Model.PTData.GetSnapshotBalance(Model.DateTimeNow.DateTime.AddDays(-30)); - double last30DaysProfitFiat = Math.Round(last30DaysProfit * Model.Summary.MainMarketPrice, 2); - double last30DaysPercentGain = Math.Round(last30DaysProfit / last30DaysStartBalance * 100, 2); } @@ -160,45 +170,51 @@ - + + - + - - + + + - + - - + + + - + - - + + + - + - - + + + - + - - + + +
Sales Profit @Model.Summary.MainMarketProfit @Model.Summary.MainFiatCurrencyFunding@Model.Summary.MainFiatCurrency Gain
Today@Model.PTData.SellLogToday.Count@overviewStats.SalesToday @todaysProfit.ToString("#,#0.00000000", new System.Globalization.CultureInfo("en-US"))@Html.Raw(Model.MainFiatCurrencySymbol + todaysProfitFiat.ToString("#,#0.00", new System.Globalization.CultureInfo("en-US")))@todaysPercentGain.ToString("#,#0.00", new System.Globalization.CultureInfo("en-US"))%@todaysFunding.ToString("#,#0.00000000", new System.Globalization.CultureInfo("en-US"))@Html.Raw(todaysProfitFiat.ToString("#,#0.00", new System.Globalization.CultureInfo("en-US")))@todaysFundingGain.ToString("#,#0.00", new System.Globalization.CultureInfo("en-US"))%
Yesterday@Model.PTData.SellLogYesterday.Count@yesterdaysSales @yesterdaysProfit.ToString("#,#0.00000000", new System.Globalization.CultureInfo("en-US"))@Html.Raw(Model.MainFiatCurrencySymbol + yesterdaysProfitFiat.ToString("#,#0.00", new System.Globalization.CultureInfo("en-US")))@yesterdaysPercentGain.ToString("#,#0.00", new System.Globalization.CultureInfo("en-US"))%@yesterdaysFunding.ToString("#,#0.00000000", new System.Globalization.CultureInfo("en-US"))@Html.Raw(yesterdaysProfitFiat.ToString("#,#0.00", new System.Globalization.CultureInfo("en-US")))@yesterdaysFundingGain.ToString("#,#0.00", new System.Globalization.CultureInfo("en-US"))%
Last 7 Days@Model.PTData.SellLogLast7Days.Count@last7DaysSales @last7DaysProfit.ToString("#,#0.00000000", new System.Globalization.CultureInfo("en-US"))@Html.Raw(Model.MainFiatCurrencySymbol + last7DaysProfitFiat.ToString("#,#0.00", new System.Globalization.CultureInfo("en-US")))@last7DaysPercentGain.ToString("#,#0.00", new System.Globalization.CultureInfo("en-US"))%@last7DaysFunding.ToString("#,#0.00000000", new System.Globalization.CultureInfo("en-US"))@Html.Raw(last7DaysProfitFiat.ToString("#,#0.00", new System.Globalization.CultureInfo("en-US")))@last7DaysFundingGain.ToString("#,#0.00", new System.Globalization.CultureInfo("en-US"))%
Last 30 Days@Model.PTData.SellLogLast30Days.Count@last30DaysSales @last30DaysProfit.ToString("#,#0.00000000", new System.Globalization.CultureInfo("en-US"))@Html.Raw(Model.MainFiatCurrencySymbol + last30DaysProfitFiat.ToString("#,#0.00", new System.Globalization.CultureInfo("en-US")))@last30DaysPercentGain.ToString("#,#0.00", new System.Globalization.CultureInfo("en-US"))%@last30DaysFunding.ToString("#,#0.00000000", new System.Globalization.CultureInfo("en-US"))@Html.Raw(last30DaysProfitFiat.ToString("#,#0.00", new System.Globalization.CultureInfo("en-US")))@last30DaysFundingGain.ToString("#,#0.00", new System.Globalization.CultureInfo("en-US"))%
Total@Model.PTData.SellLog.Count@totalSales @totalProfit.ToString("#,#0.00000000", new System.Globalization.CultureInfo("en-US"))@Html.Raw(Model.MainFiatCurrencySymbol + totalProfitFiat.ToString("#,#0.00", new System.Globalization.CultureInfo("en-US")))@Html.Raw(percentGainText)@totalFunding.ToString("#,#0.00000000", new System.Globalization.CultureInfo("en-US"))@Html.Raw(totalProfitFiat.ToString("#,#0.00", new System.Globalization.CultureInfo("en-US")))@totalFundingGain.ToString("#,#0.00", new System.Globalization.CultureInfo("en-US"))%
diff --git a/Monitor/Pages/_get/DashboardBottom.cshtml.cs b/Monitor/Pages/_get/DashboardBottom.cshtml.cs index 0ff5157..3f5dbea 100644 --- a/Monitor/Pages/_get/DashboardBottom.cshtml.cs +++ b/Monitor/Pages/_get/DashboardBottom.cshtml.cs @@ -13,6 +13,7 @@ namespace Monitor.Pages public class DashboardBottomModel : _Internal.BasePageModelSecureAJAX { public ProfitTrailerData PTData = null; + public List StatsData { get; set; } public List MarketTrends { get; set; } = new List(); public string TrendChartDataJSON = ""; public string ProfitChartDataJSON = ""; @@ -26,12 +27,14 @@ namespace Monitor.Pages base.Init(); BindData(); + BuildAssetDistributionData(); } private void BindData() { PTData = this.PtDataObject; + StatsData = this.PTData.Stats; // Cleanup temp files FileHelper.CleanupFilesMinutes(PTMagicMonitorBasePath + "wwwroot" + System.IO.Path.DirectorySeparatorChar + "assets" + System.IO.Path.DirectorySeparatorChar + "tmp" + System.IO.Path.DirectorySeparatorChar, 5);