2018-05-22 10:11:50 +02:00
using System ;
using System.Collections.Generic ;
using System.Linq ;
using Microsoft.AspNetCore.Mvc.RazorPages ;
using Core.Main ;
using Core.Helper ;
using Core.Main.DataObjects ;
using Core.Main.DataObjects.PTMagicData ;
2018-12-15 22:07:29 +01:00
namespace Monitor.Pages
{
public class SalesAnalyzer : _Internal . BasePageModelSecure
{
2018-05-22 10:11:50 +02:00
public ProfitTrailerData PTData = null ;
public string TradesChartDataJSON = "" ;
public string ProfitChartDataJSON = "" ;
public IEnumerable < KeyValuePair < string , double > > TopMarkets = null ;
public DateTime MinSellLogDate = Constants . confMinDate ;
public Dictionary < DateTime , double > DailyGains = new Dictionary < DateTime , double > ( ) ;
public Dictionary < DateTime , double > MonthlyGains = new Dictionary < DateTime , double > ( ) ;
public DateTimeOffset DateTimeNow = Constants . confMinDate ;
2018-12-15 22:07:29 +01:00
public void OnGet ( )
{
2018-05-22 10:11:50 +02:00
base . Init ( ) ;
2018-12-15 22:07:29 +01:00
2018-05-22 10:11:50 +02:00
BindData ( ) ;
}
2018-12-15 22:07:29 +01:00
private void BindData ( )
{
2018-12-29 04:58:40 +01:00
PTData = new ProfitTrailerData ( PTMagicConfiguration ) ;
2018-05-22 10:11:50 +02:00
// Convert local offset time to UTC
TimeSpan offsetTimeSpan = TimeSpan . Parse ( PTMagicConfiguration . GeneralSettings . Application . TimezoneOffset . Replace ( "+" , "" ) ) ;
DateTimeNow = DateTimeOffset . UtcNow . ToOffset ( offsetTimeSpan ) ;
BuildTopMarkets ( ) ;
BuildSalesChartData ( ) ;
}
2018-12-15 22:07:29 +01:00
private void BuildTopMarkets ( )
{
2018-05-22 10:11:50 +02:00
var markets = PTData . SellLog . GroupBy ( m = > m . Market ) ;
Dictionary < string , double > topMarketsDic = new Dictionary < string , double > ( ) ;
2018-12-15 22:07:29 +01:00
foreach ( var market in markets )
{
2018-05-22 10:11:50 +02:00
double totalProfit = PTData . SellLog . FindAll ( m = > m . Market = = market . Key ) . Sum ( m = > m . Profit ) ;
topMarketsDic . Add ( market . Key , totalProfit ) ;
}
TopMarkets = new SortedDictionary < string , double > ( topMarketsDic ) . OrderByDescending ( m = > m . Value ) . Take ( PTMagicConfiguration . GeneralSettings . Monitor . MaxTopMarkets ) ;
}
2018-12-15 22:07:29 +01:00
private void BuildSalesChartData ( )
{
if ( PTData . SellLog . Count > 0 )
{
2018-05-22 10:11:50 +02:00
MinSellLogDate = PTData . SellLog . OrderBy ( sl = > sl . SoldDate ) . First ( ) . SoldDate . Date ;
DateTime graphStartDate = DateTimeNow . DateTime . Date . AddDays ( - 30 ) ;
if ( MinSellLogDate > graphStartDate ) graphStartDate = MinSellLogDate ;
int tradeDayIndex = 0 ;
string tradesPerDayJSON = "" ;
string profitPerDayJSON = "" ;
2018-12-15 22:07:29 +01:00
for ( DateTime salesDate = graphStartDate ; salesDate < = DateTimeNow . DateTime . Date ; salesDate = salesDate . AddDays ( 1 ) )
{
if ( tradeDayIndex > 0 )
{
2018-05-22 10:11:50 +02:00
tradesPerDayJSON + = ",\n" ;
profitPerDayJSON + = ",\n" ;
}
int trades = PTData . SellLog . FindAll ( t = > t . SoldDate . Date = = salesDate . Date ) . Count ;
double profit = PTData . SellLog . FindAll ( t = > t . SoldDate . Date = = salesDate . Date ) . Sum ( t = > t . Profit ) ;
double profitFiat = Math . Round ( profit * Summary . MainMarketPrice , 2 ) ;
tradesPerDayJSON + = "{x: new Date('" + salesDate . Date . ToString ( "yyyy-MM-dd" ) + "'), y: " + trades + "}" ;
profitPerDayJSON + = "{x: new Date('" + salesDate . Date . ToString ( "yyyy-MM-dd" ) + "'), y: " + profitFiat . ToString ( "0.00" , new System . Globalization . CultureInfo ( "en-US" ) ) + "}" ;
tradeDayIndex + + ;
}
TradesChartDataJSON = "[" ;
TradesChartDataJSON + = "{" ;
TradesChartDataJSON + = "key: 'Sales'," ;
TradesChartDataJSON + = "color: '" + Constants . ChartLineColors [ 0 ] + "'," ;
TradesChartDataJSON + = "values: [" + tradesPerDayJSON + "]" ;
TradesChartDataJSON + = "}" ;
TradesChartDataJSON + = "]" ;
ProfitChartDataJSON = "[" ;
ProfitChartDataJSON + = "{" ;
ProfitChartDataJSON + = "key: 'Profit in " + Summary . MainFiatCurrency + "'," ;
ProfitChartDataJSON + = "color: '" + Constants . ChartLineColors [ 1 ] + "'," ;
ProfitChartDataJSON + = "values: [" + profitPerDayJSON + "]" ;
ProfitChartDataJSON + = "}" ;
ProfitChartDataJSON + = "]" ;
2018-12-15 22:07:29 +01:00
for ( DateTime salesDate = DateTimeNow . DateTime . Date ; salesDate > = MinSellLogDate ; salesDate = salesDate . AddDays ( - 1 ) )
{
2018-05-22 10:11:50 +02:00
List < SellLogData > salesDateSales = PTData . SellLog . FindAll ( sl = > sl . SoldDate . Date = = salesDate ) ;
double salesDateProfit = salesDateSales . Sum ( sl = > sl . Profit ) ;
double salesDateStartBalance = PTData . GetSnapshotBalance ( salesDate ) ;
double salesDateGain = Math . Round ( salesDateProfit / salesDateStartBalance * 100 , 2 ) ;
DailyGains . Add ( salesDate , salesDateGain ) ;
}
DateTime minSellLogMonthDate = new DateTime ( MinSellLogDate . Year , MinSellLogDate . Month , 1 ) . Date ;
DateTime salesMonthStartDate = new DateTime ( DateTimeNow . DateTime . Year , DateTimeNow . DateTime . Month , 1 ) . Date ;
2018-12-15 22:07:29 +01:00
for ( DateTime salesMonthDate = salesMonthStartDate . Date ; salesMonthDate > = minSellLogMonthDate ; salesMonthDate = salesMonthDate . AddMonths ( - 1 ) )
{
2018-05-22 10:11:50 +02:00
List < Core . Main . DataObjects . PTMagicData . SellLogData > salesMonthSales = PTData . SellLog . FindAll ( sl = > sl . SoldDate . Date . Month = = salesMonthDate . Month & & sl . SoldDate . Date . Year = = salesMonthDate . Year ) ;
double salesDateProfit = salesMonthSales . Sum ( sl = > sl . Profit ) ;
double salesDateStartBalance = PTData . GetSnapshotBalance ( salesMonthDate ) ;
double salesDateGain = Math . Round ( salesDateProfit / salesDateStartBalance * 100 , 2 ) ;
MonthlyGains . Add ( salesMonthDate , salesDateGain ) ;
}
}
}
}
}