From e474033d9c67d1206921d40c34f890e2a280319f Mon Sep 17 00:00:00 2001 From: HojouFotytu <36724681+HojouFotytu@users.noreply.github.com> Date: Sun, 21 Jan 2024 15:59:24 +0900 Subject: [PATCH] Sales Chart --- Monitor/Pages/Login.cshtml.cs | 13 +- Monitor/Pages/MarketAnalyzer.cshtml | 2 +- Monitor/Pages/MarketAnalyzer.cshtml.cs | 138 ++++++++++--------- Monitor/Pages/SalesAnalyzer.cshtml | 6 +- Monitor/Pages/SalesAnalyzer.cshtml.cs | 21 ++- Monitor/Pages/_get/DashboardBottom.cshtml | 2 +- Monitor/Pages/_get/DashboardBottom.cshtml.cs | 1 - 7 files changed, 101 insertions(+), 82 deletions(-) diff --git a/Monitor/Pages/Login.cshtml.cs b/Monitor/Pages/Login.cshtml.cs index 3996159..a78502c 100644 --- a/Monitor/Pages/Login.cshtml.cs +++ b/Monitor/Pages/Login.cshtml.cs @@ -32,7 +32,18 @@ namespace Monitor.Pages HttpContext.Session.SetString("LoggedIn" + PTMagicConfiguration.GeneralSettings.Monitor.Port.ToString(), DateTime.UtcNow.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fff'Z'")); PTMagicConfiguration.GeneralSettings.Monitor.IsPasswordProtected = true; //PTMagicConfiguration.WriteGeneralSettings(); - + if (cbRememberMe != null) + { + if (cbRememberMe.Equals("on", StringComparison.InvariantCultureIgnoreCase)) + { + CookieOptions cookieOption = new CookieOptions(); + cookieOption.Expires = DateTime.UtcNow.AddYears(1); + + string cookieValue = EncryptionHelper.Encrypt(encryptedPassword); + + Response.Cookies.Append("PTMRememberMeKey", cookieValue, cookieOption); + } + } Response.Redirect(PTMagicConfiguration.GeneralSettings.Monitor.RootUrl); } diff --git a/Monitor/Pages/MarketAnalyzer.cshtml b/Monitor/Pages/MarketAnalyzer.cshtml index 13a2f25..304212f 100644 --- a/Monitor/Pages/MarketAnalyzer.cshtml +++ b/Monitor/Pages/MarketAnalyzer.cshtml @@ -155,7 +155,7 @@ else
-

Live Trends at @Model.PTMagicConfiguration.GeneralSettings.Application.Exchange

+

Market Trends at @Model.PTMagicConfiguration.GeneralSettings.Application.Exchange

diff --git a/Monitor/Pages/MarketAnalyzer.cshtml.cs b/Monitor/Pages/MarketAnalyzer.cshtml.cs index 9d27e60..be4faf6 100644 --- a/Monitor/Pages/MarketAnalyzer.cshtml.cs +++ b/Monitor/Pages/MarketAnalyzer.cshtml.cs @@ -4,6 +4,7 @@ using System.Linq; using Core.Main; using Core.Helper; using Core.Main.DataObjects.PTMagicData; +using System.Globalization; namespace Monitor.Pages { @@ -11,6 +12,7 @@ namespace Monitor.Pages { public List MarketTrends { get; set; } = new List(); public string TrendChartDataJSON = ""; + public double DataHours { get; set; } public void OnGet() { @@ -28,77 +30,85 @@ namespace Monitor.Pages private void BuildMarketTrendChartData() { - if (MarketTrends.Count > 0) - { - TrendChartDataJSON = "["; - int mtIndex = 0; - foreach (MarketTrend mt in MarketTrends) + List trendChartData = new List(); + if (MarketTrends.Count > 0) { - if (mt.DisplayGraph) - { - string lineColor = ""; - if (mtIndex < Constants.ChartLineColors.Length) + + int mtIndex = 0; + foreach (MarketTrend mt in MarketTrends) { - lineColor = Constants.ChartLineColors[mtIndex]; - } - else - { - lineColor = Constants.ChartLineColors[mtIndex - 20]; - } - - if (Summary.MarketTrendChanges.ContainsKey(mt.Name)) - { - List marketTrendChangeSummaries = Summary.MarketTrendChanges[mt.Name]; - - if (marketTrendChangeSummaries.Count > 0) - { - if (!TrendChartDataJSON.Equals("[")) TrendChartDataJSON += ","; - - TrendChartDataJSON += "{"; - TrendChartDataJSON += "key: '" + SystemHelper.SplitCamelCase(mt.Name) + "',"; - TrendChartDataJSON += "color: '" + lineColor + "',"; - TrendChartDataJSON += "values: ["; - - // Get trend ticks for chart - DateTime currentDateTime = new DateTime(DateTime.UtcNow.Year, DateTime.UtcNow.Month, DateTime.UtcNow.Day, DateTime.UtcNow.Hour, 0, 0); - DateTime startDateTime = currentDateTime.AddHours(-PTMagicConfiguration.GeneralSettings.Monitor.GraphMaxTimeframeHours); - DateTime endDateTime = currentDateTime; - int trendChartTicks = 0; - for (DateTime tickTime = startDateTime; tickTime <= endDateTime; tickTime = tickTime.AddMinutes(PTMagicConfiguration.GeneralSettings.Monitor.GraphIntervalMinutes)) + if (mt.DisplayGraph) { - List tickRange = marketTrendChangeSummaries.FindAll(m => m.TrendDateTime >= tickTime).OrderBy(m => m.TrendDateTime).ToList(); - if (tickRange.Count > 0) - { - MarketTrendChange mtc = tickRange.First(); - if (tickTime != startDateTime) TrendChartDataJSON += ",\n"; - if (Double.IsInfinity(mtc.TrendChange)) mtc.TrendChange = 0; + string lineColor = mtIndex < Constants.ChartLineColors.Length + ? Constants.ChartLineColors[mtIndex] + : Constants.ChartLineColors[mtIndex - 20]; - TrendChartDataJSON += "{ x: new Date('" + tickTime.ToString("yyyy-MM-ddTHH:mm:ss").Replace(".", ":") + "'), y: " + mtc.TrendChange.ToString("0.00", new System.Globalization.CultureInfo("en-US")) + "}"; - trendChartTicks++; - } + if (Summary.MarketTrendChanges.ContainsKey(mt.Name)) + { + List marketTrendChangeSummaries = Summary.MarketTrendChanges[mt.Name]; + + if (marketTrendChangeSummaries.Count > 0) + { + List trendValues = new List(); + + // Sort marketTrendChangeSummaries by TrendDateTime + marketTrendChangeSummaries = marketTrendChangeSummaries.OrderBy(m => m.TrendDateTime).ToList(); + + // Get trend ticks for chart + TimeSpan offset; + bool isNegative = PTMagicConfiguration.GeneralSettings.Application.TimezoneOffset.StartsWith("-"); + string offsetWithoutSign = PTMagicConfiguration.GeneralSettings.Application.TimezoneOffset.TrimStart('+', '-'); + + if (!TimeSpan.TryParse(offsetWithoutSign, out offset)) + { + offset = TimeSpan.Zero; // If offset is invalid, set it to zero + } + + DateTime currentDateTime = DateTime.UtcNow; + DateTime startDateTime = currentDateTime.AddHours(-PTMagicConfiguration.GeneralSettings.Monitor.GraphMaxTimeframeHours); + DateTime endDateTime = currentDateTime; + + // Ensure startDateTime doesn't exceed the available data + DateTime earliestTrendDateTime = marketTrendChangeSummaries.Min(mtc => mtc.TrendDateTime); + startDateTime = startDateTime > earliestTrendDateTime ? startDateTime : earliestTrendDateTime; + DataHours = (currentDateTime - earliestTrendDateTime).TotalHours; + + // Cache the result of SplitCamelCase(mt.Name) + string splitCamelCaseName = SystemHelper.SplitCamelCase(mt.Name); + + for (DateTime tickTime = startDateTime; tickTime <= endDateTime; tickTime = tickTime.AddMinutes(PTMagicConfiguration.GeneralSettings.Monitor.GraphIntervalMinutes)) + { + // Use binary search to find the range of items that match the condition + int index = marketTrendChangeSummaries.BinarySearch(new MarketTrendChange { TrendDateTime = tickTime }, Comparer.Create((x, y) => x.TrendDateTime.CompareTo(y.TrendDateTime))); + if (index < 0) index = ~index; + if (index < marketTrendChangeSummaries.Count) + { + MarketTrendChange mtc = marketTrendChangeSummaries[index]; + if (Double.IsInfinity(mtc.TrendChange)) mtc.TrendChange = 0; + + // Adjust tickTime to the desired timezone before converting to string + DateTime adjustedTickTime = tickTime.Add(isNegative ? -offset : offset); + trendValues.Add("{ x: new Date('" + adjustedTickTime.ToString("yyyy-MM-ddTHH:mm:ss").Replace(".", ":") + "'), y: " + mtc.TrendChange.ToString("0.00", CultureInfo.InvariantCulture) + "}"); + } + } + + // Add most recent tick + MarketTrendChange latestMtc = marketTrendChangeSummaries.Last(); + if (Double.IsInfinity(latestMtc.TrendChange)) latestMtc.TrendChange = 0; + + // Adjust latestMtc.TrendDateTime to the desired timezone before converting to string + DateTime adjustedLatestTrendDateTime = latestMtc.TrendDateTime.Add(isNegative ? -offset : offset); + trendValues.Add("{ x: new Date('" + adjustedLatestTrendDateTime.ToString("yyyy-MM-ddTHH:mm:ss").Replace(".", ":") + "'), y: " + latestMtc.TrendChange.ToString("0.00", CultureInfo.InvariantCulture) + "}"); + + // Use cached splitCamelCaseName + trendChartData.Add("{ key: '" + splitCamelCaseName + "', color: '" + lineColor + "', values: [" + string.Join(",\n", trendValues) + "] }"); + mtIndex++; + } + } } - - // Add most recent tick - List latestTickRange = marketTrendChangeSummaries.OrderByDescending(m => m.TrendDateTime).ToList(); - if (latestTickRange.Count > 0) - { - MarketTrendChange mtc = latestTickRange.First(); - if (trendChartTicks > 0) TrendChartDataJSON += ",\n"; - if (Double.IsInfinity(mtc.TrendChange)) mtc.TrendChange = 0; - - TrendChartDataJSON += "{ x: new Date('" + mtc.TrendDateTime.ToString("yyyy-MM-ddTHH:mm:ss").Replace(".", ":") + "'), y: " + mtc.TrendChange.ToString("0.00", new System.Globalization.CultureInfo("en-US")) + "}"; - } - - TrendChartDataJSON += "]"; - TrendChartDataJSON += "}"; - - mtIndex++; - } } - } } - TrendChartDataJSON += "]"; - } + TrendChartDataJSON = "[" + string.Join(",", trendChartData) + "]"; } } } diff --git a/Monitor/Pages/SalesAnalyzer.cshtml b/Monitor/Pages/SalesAnalyzer.cshtml index 0496bc6..459f086 100644 --- a/Monitor/Pages/SalesAnalyzer.cshtml +++ b/Monitor/Pages/SalesAnalyzer.cshtml @@ -379,9 +379,11 @@ @if (!Model.SalesChartDataJSON.Equals("")) { nv.addGraph(function () { - salesChart = nv.models.lineChart(); + salesChart = nv.models.multiBarChart(); var height = 300; - salesChart.useInteractiveGuideline(true); + salesChart.groupSpacing(0.5); + salesChart.showControls(false); + //salesChart.useInteractiveGuideline(true); salesChart.xAxis.tickFormat(function (d) { return d3.time.format('%m/%d')(new Date(d)); }); salesChart.yAxis.axisLabel('').tickFormat(d3.format(',.2f')); diff --git a/Monitor/Pages/SalesAnalyzer.cshtml.cs b/Monitor/Pages/SalesAnalyzer.cshtml.cs index 0f58ca4..dbe7c92 100644 --- a/Monitor/Pages/SalesAnalyzer.cshtml.cs +++ b/Monitor/Pages/SalesAnalyzer.cshtml.cs @@ -10,7 +10,7 @@ using Core.Main.DataObjects.PTMagicData; namespace Monitor.Pages { public class SalesAnalyzer : _Internal.BasePageModelSecure - { + { public ProfitTrailerData PTData = null; public MiscData MiscData { get; set; } public PropertiesData PropertiesData { get; set; } @@ -328,28 +328,25 @@ namespace Monitor.Pages { if (salesCountByDate.TryGetValue(date, out DailyStatsData dailyStatsData)) { - // Use the totalSales value directly - salesPerDayList.Add(new { x = new DateTimeOffset(date).ToUnixTimeMilliseconds(), y = dailyStatsData.TotalSales }); - - // Add daily buys to the list - buysPerDayList.Add(new { x = new DateTimeOffset(date).ToUnixTimeMilliseconds(), y = dailyStatsData.TotalBuys }); + buysPerDayList.Add(new { x = new DateTimeOffset(date).ToUnixTimeMilliseconds(), y = Convert.ToInt32(dailyStatsData.TotalBuys) }); + salesPerDayList.Add(new { x = new DateTimeOffset(date).ToUnixTimeMilliseconds(), y = Convert.ToInt32(dailyStatsData.TotalSales) }); } } // Convert the lists to a JSON string using Newtonsoft.Json SalesChartDataJSON = Newtonsoft.Json.JsonConvert.SerializeObject(new[] { + new { // New JSON object for daily buys + key = "Buys", + color = Constants.ChartLineColors[19], // Use a different color for buys + values = buysPerDayList + }, new { key = "Sales", color = Constants.ChartLineColors[1], values = salesPerDayList - }, - new { // New JSON object for daily buys - key = "Buys", - color = Constants.ChartLineColors[0], // Use a different color for buys - values = buysPerDayList } }); } - } + } } } diff --git a/Monitor/Pages/_get/DashboardBottom.cshtml b/Monitor/Pages/_get/DashboardBottom.cshtml index 1f86804..2e09d17 100644 --- a/Monitor/Pages/_get/DashboardBottom.cshtml +++ b/Monitor/Pages/_get/DashboardBottom.cshtml @@ -12,7 +12,7 @@
-

Market Trend History

+

Market Trend History

@if (!Model.TrendChartDataJSON.Equals("")) {
diff --git a/Monitor/Pages/_get/DashboardBottom.cshtml.cs b/Monitor/Pages/_get/DashboardBottom.cshtml.cs index 970e0bd..6685342 100644 --- a/Monitor/Pages/_get/DashboardBottom.cshtml.cs +++ b/Monitor/Pages/_get/DashboardBottom.cshtml.cs @@ -8,7 +8,6 @@ using Core.Main.DataObjects; using Core.Main.DataObjects.PTMagicData; using System.Globalization; using System.Text; -using System.Diagnostics; namespace Monitor.Pages {