Exemplo n.º 1
0
 /// <summary>
 /// caller should send the same date of dailylog and record
 /// </summary>
 /// <param name="log"></param>
 /// <param name="record"></param>
 /// <returns></returns>
 public bool InsertOrReplaceRecord(DailyLog log, CurrencyRate record)
 {
     try
     {
         if (log.CurrenciesRate == null)
         {
             log.CurrenciesRate = new List <CurrencyRate>();
         }
         var existing = log.CurrenciesRate.Where(c => c.Currency == record.Currency);
         if (!existing.Any())
         {
             log.CurrenciesRate.Add(record);
             record.Date = log.Date;// replace date in record
             return(true);
         }
         else
         {
             foreach (var rec in existing)
             {
                 log.CurrenciesRate.Remove(rec);
             }
             log.CurrenciesRate.Add(record);
             record.Date = log.Date;// replace date in record
             return(true);
         }
     }
     catch (Exception ex)
     {
         ExceptionHandling.LogException(ex);
         return(false);
     }
 }
Exemplo n.º 2
0
        public DailyLog GetOrCreateDailyLog(DateTime date)
        {
            var log = _log.Log.Where(l => l.Date == date);

            if (log.Count() > 0)
            {
                return(log.First());
            }
            else
            {
                var newLog = new DailyLog {
                    Date = date, CurrenciesRate = new List <CurrencyRate>()
                };
                _log.Log.Add(newLog);
                return(newLog);
            }
        }
Exemplo n.º 3
0
        public static void Patching(List <string> currencies, List <DailyLog> log)
        {
            var      allDates       = log.Select(x => x.Date).ToList();
            DateTime lowerBoundary  = allDates.OrderBy(x => x).First().AddDays(-10);//6/1/2018
            DateTime higherBoundary = allDates.OrderByDescending(x => x).First();
            List <Task <List <DailyLog> > > taskList = new List <Task <List <DailyLog> > >();

            foreach (var currency in currencies)
            {
                taskList.Add(APIExecutePatching(currency, lowerBoundary, higherBoundary));
            }
            var task = Task.WhenAll(taskList);

            task.Wait();
            var             result = task.Result;
            List <DailyLog> logs   = new List <DailyLog>();

            foreach (var date in result)
            {
                logs = logs.Concat(date).ToList();
            }

            List <DailyLog> merged      = new List <DailyLog>();
            var             resultGroup = logs.GroupBy(x => x.Date);

            foreach (var group in resultGroup)
            {
                if (group.Key != default(DateTime))// we do not need error currency pair, we think that service should return empty list if currency is error.
                {
                    DailyLog groupLog = new DailyLog();
                    groupLog.Date           = group.Key;
                    groupLog.CurrenciesRate = group.SelectMany(x => x.CurrenciesRate).ToList();
                    merged.Add(groupLog);
                }
            }

            //TODO : after we merge we need to specify algorithm to patch date to Buy_SAP and Sell_SAP (dont forget to error currency pair)
            merged = merged.OrderBy(x => x.Date).ToList();
            for (int i = 0; i < merged.Count; i++)
            {
                DateTime dateLower  = DateTime.MinValue;
                DateTime dateHigher = DateTime.MaxValue;
                if (i == merged.Count - 1)
                {
                    dateLower = merged[i].Date;
                }
                else
                {
                    dateLower  = merged[i].Date;
                    dateHigher = merged[i + 1].Date;
                }
                var patchedLog = log.Where(l => l.Date.Date.CompareTo(dateLower.Date) >= 0 && l.Date.Date.CompareTo(dateHigher.Date) < 0);
                foreach (var daylog in patchedLog)
                {
                    foreach (var currency in merged[i].CurrenciesRate)
                    {
                        var currencyToPatch = daylog.CurrenciesRate.FirstOrDefault(x => x.Currency == currency.Currency);//replace unknown currency
                        if (currencyToPatch != null)
                        {
                            currencyToPatch.Buy_SAP       = currency.Buy;
                            currencyToPatch.Sell_SAP      = currency.Sell;
                            currencyToPatch.isAPIComplete = true;
                            currencyToPatch.isSyncSAP     = false;
                        }
                        else //new currency needed when patching
                        {
                            daylog.CurrenciesRate.Add(new CurrencyRate
                            {
                                Buy           = 0,
                                Sell          = 0,
                                Buy_SAP       = currency.Buy,
                                Sell_SAP      = currency.Sell,
                                Currency      = currency.Currency,
                                isAPIComplete = true,
                                isSyncSAP     = false,
                                Date          = daylog.Date
                            });
                        }
                    }
                }
            }
        }
Exemplo n.º 4
0
        public static async Task <Result <List <DailyLog> > > GetRatePatching(string currency, DateTime dateStart, DateTime dateEnd)
        {
            log.Debug("GetRate Patching is run for " + currency + " " + dateStart.ToString("dd/MM/yyyy") + " to " + dateEnd.ToString("dd/MM/yyyy"));
            var query = new NameValueCollection();

            query["start_period"] = dateStart.ToString("yyyy-MM-dd", new CultureInfo("en-US"));
            query["end_period"]   = dateEnd.ToString("yyyy-MM-dd", new CultureInfo("en-US"));
            query["currency"]     = currency;

            var header = new NameValueCollection();

            header["api-key"] = Appconfig.BOTAPIKey;
            var callResult = await RESTServiceCall.GetJSONAsync(Appconfig.BOTServiceEndPoint, query, header);

            dynamic response = callResult;

            if (response.result.success == "true")
            {
                try
                {
                    var output = new List <DailyLog>();
                    foreach (var data in response.result.data["data_detail"])
                    {
                        string period        = data.period;
                        string currency_pair = (string)data.currency_id;
                        string buy           = data[Appconfig.BuyValue];
                        if (String.IsNullOrWhiteSpace(buy))
                        {
                            buy = "0";
                        }
                        string sell = data[Appconfig.SellValue];
                        if (String.IsNullOrWhiteSpace(sell))
                        {
                            sell = "0";
                        }
                        log.Debug("GetRatePatching Extracting Data complete at " + period + " " + currency_pair + " " + buy + " " + sell);
                        if (!String.IsNullOrWhiteSpace(period))
                        {
                            var      date = DateTime.ParseExact(period, "yyyy-MM-dd", new CultureInfo("en-US"));
                            DailyLog log  = new DailyLog()
                            {
                                CurrenciesRate = new List <CurrencyRate>
                                {
                                    new CurrencyRate {
                                        Date = date, Buy = Convert.ToDecimal(buy), Sell = Convert.ToDecimal(sell), isAPIComplete = true, isSyncSAP = false, Currency = currency_pair
                                    }
                                }
                                ,
                                Date = date
                            };
                            output.Add(log);
                        }
                    }
                    return(new Result <List <DailyLog> > {
                        Success = true, Data = output
                    });
                }
                catch (Exception ex)
                {
                    log.Debug("GetRate REST calling pass but extracting data error" + ex.Message);
                    ExceptionHandling.LogException(ex);
                    return(new Result <List <DailyLog> > {
                        Success = false, Failure = FailureType.UnexpectedServiceBehaviorError, Message = ex.Message
                    });
                }
            }
            else
            {
                string errorMsg = string.Empty;
                JToken errors   = response.result["error"] as JToken;
                foreach (dynamic error in errors)
                {
                    errorMsg += string.Format(@" {0}:{1} ", error.code, error.message);
                }
                log.Debug("GetRate REST calling ERROR Occured" + errorMsg);
                return(new Result <List <DailyLog> > {
                    Success = false, Failure = FailureType.DatabaseConnectionError, Message = errorMsg
                });
            }
        }