Exemplo n.º 1
0
        public void Test_ExternalSupplier_FailOverService_DataNotRefreshed()
        {
            var supplier = this._supplierService.Suppliers.Where(x => x.Id == 2).First();
            IExternalInvoiceServiceManager mgr = new ExternalInvoiceServiceManager();
            var failoverInvoices = new FailoverInvoiceCollection();

            failoverInvoices.Invoices = new ExternalInvoice[]
            {
                new ExternalInvoice()
                {
                    TotalAmount = 100, Year = 2018
                }
                , new ExternalInvoice()
                {
                    TotalAmount = 100, Year = 2018
                }
            };
            //Just to simulate the data is not refreshed from past 2 months
            failoverInvoices.Timestamp = DateTime.Now.AddMonths(-2);
            mgr.EventDataNotRefreshed += delegate(object sender, ServiceManagerArgs e)
            {
                Assert.IsTrue(e.supplier.Id == 2);
            };
            var result = mgr.TryGetSpendSummaryFromFailoverService(supplier, failoverInvoices);

            Assert.IsTrue(result != null);
        }
Exemplo n.º 2
0
        private bool IsFailoverObsolete(FailoverInvoiceCollection resultFailover)
        {
            bool result = false;

            TimeSpan timeSpan = DateTime.Now - resultFailover.Timestamp;

            result = timeSpan.TotalDays > ObsoleteDaysLimit;

            return(result);
        }
Exemplo n.º 3
0
        public void Test_ExternalSupplier_FailOverService_DataRefreshed()
        {
            var supplier = this._supplierService.Suppliers.Where(x => x.Id == 2).First();
            IExternalInvoiceServiceManager mgr = new ExternalInvoiceServiceManager();
            var failoverInvoices = new FailoverInvoiceCollection();

            failoverInvoices.Invoices = new ExternalInvoice[]
            {
                new ExternalInvoice()
                {
                    TotalAmount = 100, Year = 2018
                }
                , new ExternalInvoice()
                {
                    TotalAmount = 100, Year = 2018
                }
            };
            //Just to simulate the data is refreshed recently
            failoverInvoices.Timestamp = DateTime.Now;
            var result = mgr.TryGetSpendSummaryFromFailoverService(supplier, failoverInvoices);

            Assert.IsTrue(result != null);
        }
Exemplo n.º 4
0
        public FailoverInvoiceCollection GetInvoices(int supplierId)
        {
            FailoverInvoiceCollection result = new FailoverInvoiceCollection();

            if (supplierId == 2)
            {
                IList <ExternalInvoice> list = new List <ExternalInvoice>();
                list.Add(new ExternalInvoice()
                {
                    Year = 2018, TotalAmount = 900
                });                                                                 // Only 1 Year
                result.Timestamp = DateTime.Now;
                result.Invoices  = list.ToArray();
            }
            else if (supplierId == 3)
            {
                IList <ExternalInvoice> list = new List <ExternalInvoice>();
                list.Add(new ExternalInvoice()
                {
                    Year = 2018, TotalAmount = 900
                });                                                                 // Only 1 Year
                result.Timestamp = DateTime.Now;
                result.Invoices  = list.ToArray();
            }
            else if (supplierId == 4)
            {
                IList <ExternalInvoice> list = new List <ExternalInvoice>();
                list.Add(new ExternalInvoice()
                {
                    Year = 2017, TotalAmount = 800
                });
                result.Timestamp = DateTime.Now.AddDays(-32); // Obsolete Date
                result.Invoices  = list.ToArray();
            }

            return(result);
        }
        public SpendSummary GetTotalSpend()
        {
            var watch            = Stopwatch.StartNew();
            var tokenSource      = new CancellationTokenSource();
            CancellationToken ct = tokenSource.Token;
            int counter          = 1;

            ct.ThrowIfCancellationRequested();
            bool timeOut = true;



            Task <ExternalInvoice[]> tsk = new Task <ExternalInvoice[]>(() => ExternalInvoiceService.GetInvoices(_supplier.Id.ToString()), ct);

            tsk.Start();
            while (timeOut && counter <= 3)
            {
                if (tsk.IsCompleted)
                {
                    timeOut = false;
                    break;
                }

                if (!(tsk.IsCompleted) && watch.ElapsedMilliseconds >= 30000)
                {
                    tokenSource.Cancel();
                }

                if (ct.IsCancellationRequested)
                {
                    try {
                        timeOut = false;
                        counter++;
                        ct.ThrowIfCancellationRequested();
                    }
                    catch
                    { }
                    finally
                    {
                        tokenSource = new CancellationTokenSource();
                        ct          = tokenSource.Token;
                        watch.Restart();
                        tsk = new Task <ExternalInvoice[]>(() => ExternalInvoiceService.GetInvoices(_supplier.Id.ToString()), tokenSource.Token);
                        tsk.Start();
                    }
                }
            }
            ExternalInvoice[] invoices = tsk.Result;

            if (invoices.Count() == 0)
            {
                FailoverInvoiceService    failoverInvoiceService = new FailoverInvoiceService();
                FailoverInvoiceCollection collection             = failoverInvoiceService.GetInvoices(_supplier.Id);
                invoices = collection.Invoices;
            }

            var sd = (from supplier in invoices
                      group supplier by supplier.Year into eGroup
                      select new SpendDetail()
            {
                Year = eGroup.Key,
                TotalSpend = eGroup.Sum(x => x.TotalAmount)
            });

            return(new SpendSummary()
            {
                Name = _supplier.Name,
                Years = sd.ToList()
            });
        }
        public SpendSummary TryGetSpendSummaryFromFailoverService(Supplier theSupplier, FailoverInvoiceCollection failOverInvoices)
        {
            var theSpendSummary = new SpendSummary()
            {
                Years = new List <SpendDetail>()
            };
            TimeSpan diff = DateTime.Today - failOverInvoices.Timestamp;

            if (diff.Days > 30)
            {
                EventDataNotRefreshed(this, new ServiceManagerArgs()
                {
                    supplier = theSupplier
                });
            }
            else
            {
                #region Calculate TotalSpend
                var result = failOverInvoices.Invoices.GroupBy(x => x.Year)
                             .Select(g => new
                {
                    Year       = g.Key,
                    TotalSpend = g.Sum(x => x.TotalAmount)
                }).ToList();

                result.ForEach(x => this.spendSummary.Years.Add(
                                   new SpendDetail()
                {
                    Year       = x.Year,
                    TotalSpend = x.TotalSpend
                }));
                #endregion
            }

            return(theSpendSummary);
        }