using System; using System.Collections.Generic; using System.Linq; using ApacKernel; using ApacKernel.Date; using ApacKernel.Extensions; using ApacKernel.Services.SignalR; using Microsoft.AspNet.SignalR; using Microsoft.AspNet.SignalR.Hubs; using Now.Business; using Now.Entities; using Now.Web.Hubs; namespace Now.Web.Tickers { public class NowTicker : TickerBase { protected readonly static Lazy _instance = new Lazy(() => new NowTicker(GlobalHost.ConnectionManager.GetHubContext().Clients)); private volatile RevenueNow _prevRevenueNow; private volatile IEnumerable _prevSalesSummary; private volatile IEnumerable _prevCountriesSummary; private volatile Dictionary _speedoSectors; private DateTime _lastCheckTime; public NowTicker(IHubConnectionContext clients) : base(clients) { UpdateSectors(); } public static NowTicker Instance { get { return _instance.Value; } } protected override TimeSpan UpdateInterval { get { return ApacConfig.AppSettings.NowTicks(); } } private void UpdateSectors() { var sectors = RevenueWorkflow.Instance.GetSectors(); if (sectors != null) { _speedoSectors = sectors; _lastCheckTime = DateTime.Now; } } protected override void GetAndSendData(object state = null) { var totalRevenue = CheckTotalRevenue(RevenueWorkflow.Instance.GetRevenueNow(Period.Today, true)); if (totalRevenue != null) { var now = DateTime.Now; var period = now - _lastCheckTime; if (period.Hours > 0) { UpdateSectors(); } if (_speedoSectors != null &&_speedoSectors.ContainsKey(now.Hour)) BroadcastSpeedoSectors(_speedoSectors[now.Hour]); BroadcastTotalRevenue(totalRevenue); BroadcastTTLRevenue(CheckTTLTotalRevenue(GetTTL())); BroadcastCountriesSummary(CheckCountriesSummary(RevenueWorkflow.Instance.GetCountriesStatistics(Period.Today))); BroadcastSalesSummary(CheckSalesSummary(RevenueWorkflow.Instance.GetSalesStatistics(Period.Today))); } } private Dictionary CheckTTLTotalRevenue(Dictionary data) { return data; } private Dictionary GetTTL() { return new Dictionary { {PeriodTypes.TTLM.ToString(), RevenueWorkflow.Instance.GetRevenueNow(Period.TTLM, false)}, {PeriodTypes.TTLY.ToString(), RevenueWorkflow.Instance.GetRevenueNow(Period.TTLY, false)} }; } private IEnumerable CheckCountriesSummary(IEnumerable countriesSummary) { if (_prevCountriesSummary != null && countriesSummary != null && (countriesSummary.SequenceEqual(_prevCountriesSummary) || countriesSummary.IsEmpty())) return null; _prevCountriesSummary = countriesSummary; return countriesSummary; } private IEnumerable CheckSalesSummary(IEnumerable salesSummary) { if (_prevSalesSummary != null && salesSummary != null && (salesSummary.SequenceEqual(_prevSalesSummary) || salesSummary.IsEmpty())) return null; _prevSalesSummary = salesSummary; return salesSummary; } private RevenueNow CheckTotalRevenue(RevenueNow revenueNow) { if (_prevRevenueNow != null) { if (revenueNow == null || _prevRevenueNow == revenueNow || Math.Abs( revenueNow.PerMinute - _prevRevenueNow.PerMinute ) < 10 || revenueNow.PerDay == 0) return null; } _prevRevenueNow = revenueNow; return revenueNow; } private void BroadcastSalesSummary(IEnumerable summary) { if(summary!=null) Clients.All.updateSalesSummary(summary); } private void BroadcastCountriesSummary(IEnumerable summary) { if(summary!=null) Clients.All.updateCountriesSummary(summary); } private void BroadcastTotalRevenue(RevenueNow revenue) { if(revenue!=null) Clients.All.updateTotalRevenue(revenue); } private void BroadcastTTLRevenue(Dictionary revenues) { if (revenues != null) Clients.All.updateTTLMTotalRevenue(revenues); } private void BroadcastSpeedoSectors(SpeedoSectors speedoSectors) { Clients.All.updateSpeedoSectors(speedoSectors); } } }