using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace crusherScanner
{
internal class Reconciliation
{
///
/// Get a list of jobs in the working dir that may be reconcilable
///
public static void CheckJobs(System.ComponentModel.DoWorkEventArgs e)
{
if (e.Argument!=null)
{
ForceReconciliation((string)e.Argument);
}
var jobs = new HashSet();
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();
}
///
/// Remove old reconciled jobs once they are older than 24 hours.
///
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);
}
}
}
///
/// Copy job file from working to output and make a backup (temporary 24hr)
///
/// Jobfile
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);
}
}
///
/// Check if given job can be reconciled automatically
///
/// Full path to job csv file
/// True if could be reconciled
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 CheckJobReconcilableDiscrepencies(string jobPath)
{
string[] lines = File.ReadAllLines(jobPath);
HashSet discrepencies = new HashSet();
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;
}
}
}