| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace crusherScanner
- {
- internal class Reconciliation
- {
- /// <summary>
- /// Get a list of jobs in the working dir that may be reconcilable
- /// </summary>
- public static void CheckJobs(System.ComponentModel.DoWorkEventArgs e)
- {
- if (e.Argument!=null)
- {
- ForceReconciliation((string)e.Argument);
- }
- var jobs = new HashSet<string>();
- foreach (string jobPath in SupportFunctions.GetFilesFromPath(Properties.Settings.Default.OreDefWorkFile, "ML??????*.csv"))
- {
- if(!CheckJobReconcilable(jobPath))
- {
- jobs.Add(jobPath);
- }
- else
- {
- string[] job = jobPath.Split('\\');
- ForceReconciliation(job[job.Length-1]);
- }
- }
- e.Result = jobs;
- PruneBackupJobFiles();
- }
- /// <summary>
- /// Remove old reconciled jobs once they are older than 24 hours.
- /// </summary>
- public static void PruneBackupJobFiles()
- {
- //Remove Recond_ML??????.csv once they are 24 hr old.
- foreach (string jobPath in SupportFunctions.GetFilesFromPath(Properties.Settings.Default.OreDefWorkFile, "Recond_ML??????*.csv"))
- {
- DateTime lastMoved = DateTime.MinValue;
- lastMoved = File.GetLastWriteTime(jobPath);
- if (lastMoved.AddHours(24) < DateTime.Now)
- {
- File.Delete(jobPath);
- }
- }
- }
- /// <summary>
- /// Copy job file from working to output and make a backup (temporary 24hr)
- /// </summary>
- /// <param name="job">Jobfile</param>
- public static void ForceReconciliation(string job)
- {
- string JobPath = Properties.Settings.Default.OreDefWorkFile;
- string ReconPath = Properties.Settings.Default.OreDefOutFile;
-
- if (File.Exists($"{JobPath}\\{job}"))
- {
- File.Copy($"{JobPath}\\{job}", $"{JobPath}\\Recond_{job}");
- File.Move($"{JobPath}\\{job}", $"{ReconPath}\\{job}");
- File.SetLastWriteTime($"{JobPath}\\Recond_{job}", DateTime.Now);
- }
- }
- /// <summary>
- /// Check if given job can be reconciled automatically
- /// </summary>
- /// <param name="jobPath">Full path to job csv file</param>
- /// <returns>True if could be reconciled</returns>
- private static bool CheckJobReconcilable(string jobPath)
- {
- string[] lines = File.ReadAllLines(jobPath);
- foreach (string line in lines)
- {
- if (line.Contains(','))
- {
- string[] RawSampleData = line.Split(',');
- if (RawSampleData.Length == 9)
- {
- if(RawSampleData[0] != "SAMPLEID" && RawSampleData[0] != RawSampleData[8])
- {
- return false;
- }
- }
- }
- }
- return true;
- }
- public static HashSet<string> CheckJobReconcilableDiscrepencies(string jobPath)
- {
- string[] lines = File.ReadAllLines(jobPath);
- HashSet<string> discrepencies = new HashSet<string>();
- foreach (string line in lines)
- {
- if (line.Contains(','))
- {
- string[] RawSampleData = line.Split(',');
- if (RawSampleData.Length == 9)
- {
- if (RawSampleData[0] != "SAMPLEID" && RawSampleData[0] != RawSampleData[8] && RawSampleData[8] != "INS")
- {
- discrepencies.Add(RawSampleData[0]);
- }
- }
- }
- }
- return discrepencies;
- }
- }
- }
|