Reconciliation.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace crusherScanner
  7. {
  8. internal class Reconciliation
  9. {
  10. /// <summary>
  11. /// Get a list of jobs in the working dir that may be reconcilable
  12. /// </summary>
  13. public static void CheckJobs(System.ComponentModel.DoWorkEventArgs e)
  14. {
  15. if (e.Argument!=null)
  16. {
  17. ForceReconciliation((string)e.Argument);
  18. }
  19. var jobs = new HashSet<string>();
  20. foreach (string jobPath in SupportFunctions.GetFilesFromPath(Properties.Settings.Default.OreDefWorkFile, "ML??????*.csv"))
  21. {
  22. if(!CheckJobReconcilable(jobPath))
  23. {
  24. jobs.Add(jobPath);
  25. }
  26. else
  27. {
  28. string[] job = jobPath.Split('\\');
  29. ForceReconciliation(job[job.Length-1]);
  30. }
  31. }
  32. e.Result = jobs;
  33. PruneBackupJobFiles();
  34. }
  35. /// <summary>
  36. /// Remove old reconciled jobs once they are older than 24 hours.
  37. /// </summary>
  38. public static void PruneBackupJobFiles()
  39. {
  40. //Remove Recond_ML??????.csv once they are 24 hr old.
  41. foreach (string jobPath in SupportFunctions.GetFilesFromPath(Properties.Settings.Default.OreDefWorkFile, "Recond_ML??????*.csv"))
  42. {
  43. DateTime lastMoved = DateTime.MinValue;
  44. lastMoved = File.GetLastWriteTime(jobPath);
  45. if (lastMoved.AddHours(24) < DateTime.Now)
  46. {
  47. File.Delete(jobPath);
  48. }
  49. }
  50. }
  51. /// <summary>
  52. /// Copy job file from working to output and make a backup (temporary 24hr)
  53. /// </summary>
  54. /// <param name="job">Jobfile</param>
  55. public static void ForceReconciliation(string job)
  56. {
  57. string JobPath = Properties.Settings.Default.OreDefWorkFile;
  58. string ReconPath = Properties.Settings.Default.OreDefOutFile;
  59. if (File.Exists($"{JobPath}\\{job}"))
  60. {
  61. File.Copy($"{JobPath}\\{job}", $"{JobPath}\\Recond_{job}");
  62. File.Move($"{JobPath}\\{job}", $"{ReconPath}\\{job}");
  63. File.SetLastWriteTime($"{JobPath}\\Recond_{job}", DateTime.Now);
  64. }
  65. }
  66. /// <summary>
  67. /// Check if given job can be reconciled automatically
  68. /// </summary>
  69. /// <param name="jobPath">Full path to job csv file</param>
  70. /// <returns>True if could be reconciled</returns>
  71. private static bool CheckJobReconcilable(string jobPath)
  72. {
  73. string[] lines = File.ReadAllLines(jobPath);
  74. foreach (string line in lines)
  75. {
  76. if (line.Contains(','))
  77. {
  78. string[] RawSampleData = line.Split(',');
  79. if (RawSampleData.Length == 9)
  80. {
  81. if(RawSampleData[0] != "SAMPLEID" && RawSampleData[0] != RawSampleData[8])
  82. {
  83. return false;
  84. }
  85. }
  86. }
  87. }
  88. return true;
  89. }
  90. public static HashSet<string> CheckJobReconcilableDiscrepencies(string jobPath)
  91. {
  92. string[] lines = File.ReadAllLines(jobPath);
  93. HashSet<string> discrepencies = new HashSet<string>();
  94. foreach (string line in lines)
  95. {
  96. if (line.Contains(','))
  97. {
  98. string[] RawSampleData = line.Split(',');
  99. if (RawSampleData.Length == 9)
  100. {
  101. if (RawSampleData[0] != "SAMPLEID" && RawSampleData[0] != RawSampleData[8] && RawSampleData[8] != "INS")
  102. {
  103. discrepencies.Add(RawSampleData[0]);
  104. }
  105. }
  106. }
  107. }
  108. return discrepencies;
  109. }
  110. }
  111. }