private IEnumerable <BasicAlert> ProcessBasicAlertDefinition(IEnumerable <Label> labelCollection, AlertDefinition alertDefinition) { List <BasicAlert> alertList = new List <BasicAlert>(); foreach (var label in labelCollection) { var value = GetValueFromField(alertDefinition.compareField, label); if (alertDefinition.Valueoperator.ToUpper() == "OVER") { if (value > alertDefinition.MaxValue) { alertList.Add(new BasicAlert { AlertDefinition = alertDefinition.DefinitionName, LabelID = label.SAMPLEID, Date = DateTime.Now, FieldName = alertDefinition.compareField, Description = "Maximum allowed value: " + alertDefinition.MaxValue + ", actual value: " + value }); } } else if (alertDefinition.Valueoperator.ToUpper() == "UNDER") { if (value < alertDefinition.Minvalue) { alertList.Add(new BasicAlert { AlertDefinition = alertDefinition.DefinitionName, LabelID = label.SAMPLEID, Date = DateTime.Now, FieldName = alertDefinition.compareField, Description = "Minimum allowed value: " + alertDefinition.Minvalue + ", actual value: " + value }); } } else if (alertDefinition.Valueoperator.ToUpper() == "BETWEEN") { if (value >= alertDefinition.Minvalue && value <= alertDefinition.MaxValue) { alertList.Add(new BasicAlert { AlertDefinition = alertDefinition.DefinitionName, LabelID = label.SAMPLEID, Date = DateTime.Now, FieldName = alertDefinition.compareField, Description = "Must be between " + alertDefinition.Minvalue + " and " + alertDefinition.MaxValue + ", actual value: " + value }); } } } return(alertList); }
private IEnumerable <MethodAlert> ProcessMethodAlertDefinition(IEnumerable <Label> labelCollection, AlertDefinition alertDefinition) { // maintain a second list with only duplicates (for efficiency) var duplicateCollection = labelCollection.Where(x => x.PCHECKID != ""); // track how many failures per testing method // [0] is number of failed duplicates / [1] total number of records using that test IDictionary <string, int[]> methodErrorFreq = new Dictionary <string, int[]>(); List <MethodAlert> alertList = new List <MethodAlert>(); List <Label> failedDuplicates = new List <Label>(); foreach (var label in labelCollection) { var value = GetValueFromField(alertDefinition.compareField, label); var duplicate = duplicateCollection.FirstOrDefault <Label>(x => x.PCHECKID == label.SAMPLEID); if (duplicate != null) { // find the test that was used and use it as a basis for comparison var fieldName = FindTestUsed(alertDefinition.MineralPrefix, label); if (fieldName == null) { continue; } // try to get duplicate value for field var dupeVal = GetValueFromField(fieldName, duplicate); // create a new dictionary record if necessary if (!methodErrorFreq.Any(x => x.Key == fieldName)) { methodErrorFreq[fieldName] = new int[] { 0, 0 }; } // increment total duplicates tested with this method methodErrorFreq[fieldName][1]++; if (dupeVal != null) { // get bounds var upper = value * (1 + (alertDefinition.TolerancePercentage / 100)); var lower = value * (1 - (alertDefinition.TolerancePercentage / 100)); // check if duplicate is outside bounds if (dupeVal > upper || dupeVal < lower) { // increment frequency of errors for that method methodErrorFreq[fieldName][0]++; } } } } // final analysis var best = new KeyValuePair <string, int[]>(); foreach (var key in methodErrorFreq.Keys) { if (best.Value == null || ((double)methodErrorFreq[key][0] / (double)methodErrorFreq[key][1]) < best.Value[0]) { best = new KeyValuePair <string, int[]>(key, methodErrorFreq[key]); } } // try to clean up method name string name = best.Key; try { string pre = alertDefinition.MineralPrefix + "_"; name = best.Key.Split(new[] { pre }, StringSplitOptions.None)[1]; name = name.Split('_')[0]; } catch (Exception) { } alertList.Add(new MethodAlert { DateRaised = DateTime.Now, MethodName = name, Percentage = ((double)best.Value[0] / (double)best.Value[1]).ToString(), TotalFailures = best.Value[0].ToString(), TotalUses = best.Value[1].ToString() }); return(alertList); }
private IEnumerable <DuplicatesAlert> ProcessDuplicateAlertDefinition(IEnumerable <Label> labelCollection, AlertDefinition alertDefinition) { // maintain a second list with only duplicates (for efficiency) var duplicateCollection = labelCollection.Where(x => x.PCHECKID != ""); List <DuplicatesAlert> alertList = new List <DuplicatesAlert>(); foreach (var label in labelCollection) { // optimisation stuff // try to get the property specified for comparison var value = GetValueFromField(alertDefinition.compareField, label); if (value == null) { continue; } var duplicate = duplicateCollection.FirstOrDefault <Label>(x => x.PCHECKID == label.SAMPLEID); if (duplicate != null) { // try to get duplicate value for field var dupeVal = GetValueFromField(alertDefinition.compareField, duplicate); if (dupeVal != null) { // get bounds var upper = value * (1 + (alertDefinition.TolerancePercentage / 100)); var lower = value * (1 - (alertDefinition.TolerancePercentage / 100)); // check if duplicate is outside bounds if (dupeVal > upper || dupeVal < lower) { alertList.Add(new DuplicatesAlert { DateRaised = DateTime.Now, DespatchNo = label.DESPATCHNO, SampleID = label.SAMPLEID, ValuePrimary = value.ToString(), ValueDuplicate = dupeVal.ToString(), Difference = Math.Abs(value.Value - dupeVal.Value).ToString(), Component = alertDefinition.compareField }); } } } } return(alertList); }
private IEnumerable <RangeAlert> ProcessRangeAlertDefinition(IEnumerable <Label> labelCollection, AlertDefinition alertDefinition) { var alertList = new List <RangeAlert>(); HttpClient client = new HttpClient(); client.BaseAddress = new Uri("http://10.0.1.3:81/"); // Add an Accept header for JSON format. client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); if (alertDefinition.Valueoperator == "STANDARD_REFERENCE") { HttpResponseMessage response = client.GetAsync("http://10.0.1.3:81/api/standardReferences?pageIndex=0&pageSize=1000000").Result; if (response.IsSuccessStatusCode) { var stdRefModelRecords = response.Content.ReadAsAsync <IEnumerable <StandardReferenceModel> >().Result; // iterate over every record foreach (var rec in stdRefModelRecords) { if (rec.Ag_MEICP41s_ppm != null && rec.MIN_Ag_MEICP41s_ppm != null && rec.MAX_Ag_MEICP41s_ppm != null) { if (rec.Ag_MEICP41s_ppm < rec.MIN_Ag_MEICP41s_ppm || rec.Ag_MEICP41s_ppm > rec.MAX_Ag_MEICP41s_ppm) { alertList.Add(new RangeAlert { ErrorCause = "Ag_MEICP41s_ppm", DateRaised = DateTime.Now, ActualValue = rec.Ag_MEICP41s_ppm.ToString(), DispatchNo = rec.DESPATCHNO.ToString(), SampleID = rec.SAMPLEID, StandardID = rec.STANDARDID, StandardValue = rec.STD_Ag_MEICP41s_ppm.ToString(), Difference = Math.Abs((double)rec.Ag_MEICP41s_ppm - (double)rec.STD_Ag_MEICP41s_ppm).ToString() }); } } if (rec.Al_MEICP41s_pct != null && rec.MIN_Al_MEICP41s_pct != null && rec.MAX_Al_MEICP41s_pct != null) { if (rec.Al_MEICP41s_pct < rec.MIN_Al_MEICP41s_pct || rec.Al_MEICP41s_pct > rec.MAX_Al_MEICP41s_pct) { alertList.Add(new RangeAlert { ErrorCause = "Al_MEICP41s_ppm", DateRaised = DateTime.Now, ActualValue = rec.Al_MEICP41s_pct.ToString(), DispatchNo = rec.DESPATCHNO.ToString(), SampleID = rec.SAMPLEID, StandardID = rec.STANDARDID, StandardValue = rec.STD_Al_MEICP41s_pct.ToString(), Difference = Math.Abs((double)rec.Al_MEICP41s_pct - (double)rec.STD_Al_MEICP41s_pct).ToString() }); } } if (rec.As_MEICP41s_ppm != null && rec.MIN_As_MEICP41s_ppm != null && rec.MAX_As_MEICP41s_ppm != null) { if (rec.As_MEICP41s_ppm < rec.MIN_As_MEICP41s_ppm || rec.As_MEICP41s_ppm > rec.MAX_As_MEICP41s_ppm) { alertList.Add(new RangeAlert { ErrorCause = "As_MEICP41s_ppm", DateRaised = DateTime.Now, ActualValue = rec.As_MEICP41s_ppm.ToString(), DispatchNo = rec.DESPATCHNO.ToString(), SampleID = rec.SAMPLEID, StandardID = rec.STANDARDID, StandardValue = rec.STD_As_MEICP41s_ppm.ToString(), Difference = Math.Abs((double)rec.As_MEICP41s_ppm - (double)rec.STD_As_MEICP41s_ppm).ToString() }); } } if (rec.Au_AA25_ppm != null && rec.MIN_Au_AA25_ppm != null && rec.MAX_Au_AA25_ppm != null) { if (rec.Au_AA25_ppm < rec.MIN_Au_AA25_ppm || rec.Au_AA25_ppm > rec.MAX_Au_AA25_ppm) { alertList.Add(new RangeAlert { ErrorCause = "Au_AA25_ppm", DateRaised = DateTime.Now, ActualValue = rec.Au_AA25_ppm.ToString(), DispatchNo = rec.DESPATCHNO.ToString(), SampleID = rec.SAMPLEID, StandardID = rec.STANDARDID, StandardValue = rec.STD_Au_AA25_ppm.ToString(), Difference = Math.Abs((double)rec.Au_AA25_ppm - (double)rec.STD_Au_AA25_ppm).ToString() }); } } } } } else if (alertDefinition.Valueoperator == "CONTAMINATION_CHECK") { HttpResponseMessage response = client.GetAsync("http://10.0.1.3:81/api/ContaminationChecks?pageIndex=0&pageSize=1000000").Result; if (response.IsSuccessStatusCode) { var contaminationModelRecords = response.Content.ReadAsAsync <IEnumerable <ContaminationCheckModel> >().Result; // iterate over every record foreach (var rec in contaminationModelRecords) { if (rec.Ag_MEICP41s_ppm != null && rec.MIN_Ag_MEICP41s_ppm != null && rec.MAX_Ag_MEICP41s_ppm != null) { rec.Ag_MEICP41s_ppm = rec.Ag_MEICP41s_ppm.Replace("<", ""); if (Convert.ToDouble(rec.Ag_MEICP41s_ppm) < rec.MIN_Ag_MEICP41s_ppm || Convert.ToDouble(rec.Ag_MEICP41s_ppm) > rec.MAX_Ag_MEICP41s_ppm) { alertList.Add(new RangeAlert { ErrorCause = "Ag_MEICP41s_ppm", DateRaised = DateTime.Now, ActualValue = rec.Ag_MEICP41s_ppm.ToString(), DispatchNo = rec.DESPATCHNO.ToString(), SampleID = rec.SAMPLEID, StandardID = rec.STANDARDID, StandardValue = rec.STD_Ag_MEICP41s_ppm.ToString(), Difference = Math.Abs(Convert.ToDouble(rec.Ag_MEICP41s_ppm) - (double)rec.STD_Ag_MEICP41s_ppm).ToString() }); } } if (rec.Al_MEICP41s_pct != null && rec.MIN_Al_MEICP41s_pct != null && rec.MAX_Al_MEICP41s_pct != null) { if (rec.Al_MEICP41s_pct < rec.MIN_Al_MEICP41s_pct || rec.Al_MEICP41s_pct > rec.MAX_Al_MEICP41s_pct) { alertList.Add(new RangeAlert { ErrorCause = "Al_MEICP41s_pct", DateRaised = DateTime.Now, ActualValue = rec.Al_MEICP41s_pct.ToString(), DispatchNo = rec.DESPATCHNO.ToString(), SampleID = rec.SAMPLEID, StandardID = rec.STANDARDID, StandardValue = rec.STD_Al_MEICP41s_pct.ToString(), Difference = Math.Abs((double)rec.Al_MEICP41s_pct - (double)rec.STD_Al_MEICP41s_pct).ToString() }); } } if (rec.As_MEICP41s_ppm != null && rec.MIN_As_MEICP41s_ppm != null && rec.MAX_As_MEICP41s_ppm != null) { if (rec.As_MEICP41s_ppm < rec.MIN_As_MEICP41s_ppm || rec.As_MEICP41s_ppm > rec.MAX_As_MEICP41s_ppm) { alertList.Add(new RangeAlert { ErrorCause = "As_MEICP41s_ppm", DateRaised = DateTime.Now, ActualValue = rec.As_MEICP41s_ppm.ToString(), DispatchNo = rec.DESPATCHNO.ToString(), SampleID = rec.SAMPLEID, StandardID = rec.STANDARDID, StandardValue = rec.STD_As_MEICP41s_ppm.ToString(), Difference = Math.Abs((double)rec.As_MEICP41s_ppm - (double)rec.STD_As_MEICP41s_ppm).ToString() }); } } if (rec.Au_AA25_ppm != null && rec.MIN_Au_AA25_ppm != null && rec.MAX_Au_AA25_ppm != null) { if (rec.Au_AA25_ppm < rec.MIN_Au_AA25_ppm || rec.Au_AA25_ppm > rec.MAX_Au_AA25_ppm) { alertList.Add(new RangeAlert { ErrorCause = "Au_AA25_ppm", DateRaised = DateTime.Now, ActualValue = rec.Au_AA25_ppm.ToString(), DispatchNo = rec.DESPATCHNO.ToString(), SampleID = rec.SAMPLEID, StandardID = rec.STANDARDID, StandardValue = rec.STD_Au_AA25_ppm.ToString(), Difference = Math.Abs((double)rec.Au_AA25_ppm - (double)rec.STD_Au_AA25_ppm).ToString() }); } } } } } return(alertList); }