Ejemplo n.º 1
0
 private static void StartReconsiliation()
 {
     if (!Directory.Exists(ConfigurationManager.AppSettings["TempFolderPath"]))
     {
         Directory.CreateDirectory(ConfigurationManager.AppSettings["TempFolderPath"]);
     }
     if (!Directory.Exists(ConfigurationManager.AppSettings["OutputFolderPath"]))
     {
         Directory.CreateDirectory(ConfigurationManager.AppSettings["OutputFolderPath"]);
     }
     if (!Directory.Exists(ConfigurationManager.AppSettings["PersistentFolderPath"]))
     {
         Directory.CreateDirectory(ConfigurationManager.AppSettings["PersistentFolderPath"]);
     }
     Logger.Info($"Start Reconsiliate Trade at {DateTime.Now:MM/dd/yyyy hh:mm}");
     try
     {
         WebTradeMonitor p = new WebTradeMonitor();
         Logger.Info("Clean up Temp Folder...");
         CleanUpTempFolder();
         p.Run();
         p.LogCount();
         if (_isIceSilent == true && p.Futures + p.Swap > 0)
         {
             Logger.Info("Sending First Record of Trade for today...");
             using (EmailController email = new EmailController())
             {
                 email.SendResultEmail(p.CountToHTML(),
                                       "First record of trade for today Appears", null);
             }
             _isIceSilent = false;
         }
         if (p.Futures + p.Swap == 0)
         {
             _isIceSilent = true;
             Logger.Info("Next Business Day, the ICE Trade has been Reset");
         }
         var ICEResult = ExcelProcessor.Extract().ToList();
         if (!EnableComparison)
         {
             Logger.Info("Non Comparison Mode");
             Logger.Info("Saving To Local...");
             foreach (var dt in ICEResult)
             {
                 dt.OWSaveToCSV("");
             }
             if (!EnableEmail)
             {
                 Logger.Info("Saving Count Result...");
                 p.OutputCountToFile();
             }
             else
             {
                 Logger.Info("Add Count Result To Email...");
                 using (EmailController email = new EmailController())
                 {
                     email.SendResultEmail(p.CountToHTML(), "", null);
                 }
             }
         }
         else
         {
             Logger.Info("Start Comparison...");
             OracleDBMonitor db         = new OracleDBMonitor();
             int             queryDelay = 0;
             int.TryParse(ConfigurationManager.AppSettings["DBQueryDelay"], out queryDelay);
             Logger.Info($"Wait to query Database, waiting time = {queryDelay} seconds");
             Thread.Sleep(queryDelay * 1000);
             var DBResult = db.QueryDB();
             db.LogCount();
             var comparator = new ICEOpenLinkComparator();
             var diff       = comparator.Diff(ICEResult, DBResult);
             diff.ForEach(d => d.DataTableCorrectDate("Trade Date"));
             if (diff.All(d => d.Rows.Count == 0))
             {
                 Logger.Info("Reconsiliation Matches, No Alert Send");
             }
             else if (EnableEmail)
             {
                 //to-do
                 Logger.Info("Email Enabled...");
                 using (EmailController email = new EmailController())
                 {
                     Logger.Info("Add Count Result To Email...");
                     Logger.Info("Add Comparison Result To Email...");
                     var attachmentPaths = diff.Select(d => ProjectPath + d.OWSaveToCSV("_Diff")).ToList();
                     Logger.Info("Add Comparison Result To Attachment...");
                     email.SendResultEmail(p.CountToHTML()
                                           + db.CountToHTML()
                                           + Environment.NewLine
                                           + BuildComparisonResultBody(diff)
                                           + Environment.NewLine
                                           + HelperFunctions
                                           .WrapParagraphToHTML(comparator.ExcludedRecords)
                                           , "", attachmentPaths);
                 }
             }
             if (EnableSaveLocal)
             {
                 Logger.Info("Saving To Local...");
                 p.OutputCountToFile();
                 DBResult.ForEach(d => d.OWSaveToCSV("_DB"));
                 ICEResult.ForEach(d => d.OWSaveToCSV("_ICE"));
             }
         }
     }
     catch (Exception ex)
     {
         Logger.Error(ex, "Skip This Round");
         return;
     }
 }
Ejemplo n.º 2
0
 private CookieContainer RequestSSOCookie()
 {
     try
     {
         var             cookies = ReadCookiesFromDisk(null);
         HttpWebResponse response;
         #region Validate Existing SSO Token
         if (null != cookies)
         {
             var cookie = new CookieContainer();
             cookie.SetCookies(new Uri(Url), cookies);
             response = MakeHttpRequest(Url
                                        + ConfigurationManager.AppSettings["PrincipalUrl"]
                                        + Urlpostfix(), null, cookie);
             if (GetResponseString(response).Contains("Valid Token"))
             {
                 response.Close();
                 return(cookie);
             }
             else
             {
                 File.Delete(_defaultCookiePath);
                 OutputTo(ConfigurationManager.AppSettings["OTPExpiredAlertMessage"],
                          ConfigurationManager.AppSettings["OTPExpiredAlertMessage"]);
             }
             response.Close();
         }
         #endregion
         #region Get SSO Token
         DateTime requestTime = DateTime.Now;
         string   SSOUrl      = ConfigurationManager.AppSettings["SSOUrl"];
         string   post        = "";
         BuildPostLoad(out post, null);
         byte[] encodedPost = System.Text.Encoding.UTF8.GetBytes(post);
         response = MakeHttpRequest(Url + SSOUrl + Urlpostfix(), encodedPost, null);
         if (GetResponseString(response).Contains("Re-login with 2FA passcode"))
         {
             Logger.Info("OTP Expired, Get OTP from Outlook");
             string otp = "";
             using (EmailController email = new EmailController())
             {
                 otp = email.GetOTP(requestTime).ToString();
             }
             if (otp == "")
             {
                 Logger.Info("Please enter OTP:");
                 otp = Console.ReadLine();
             }
             else
             {
                 Logger.Info("OTP successfully load from Outlook");
             }
             response.Close();
             BuildPostLoad(out post, otp);
             encodedPost = System.Text.Encoding.UTF8.GetBytes(post);
             response    = MakeHttpRequest(Url + SSOUrl + Urlpostfix(), encodedPost, null);
         }
         CookieContainer collection = new CookieContainer();
         string          sso        = GetCookieHeader(response);
         if (!sso.Contains("iceSsoCookie"))
         {
             return(RequestSSOCookie());
         }
         WriteCookiesToDisk(null, sso);
         collection.SetCookies(new Uri(Url), sso);
         response.Close();
         #endregion
         Logger.Info("SSO Cookie is Ready");
         return(collection);
     }
     catch (Exception ex)
     {
         throw new MonitorException("Request SSO Cookie Failed", ex);
     }
 }