/// <summary> /// Helper method which is used by single flag type CSV StripMenuItemClick /// </summary> /// <param name="flagType"></param> private void FlagsToolStripMenuHelper(ReportFlag flagType) { if (dataGridView2.Rows.Count == 0) // If no rows are populated, ignore request { return; } SaveFileDialog sfd = new SaveFileDialog(); sfd.Filter = "Excel File|*.csv"; if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK) { string path = sfd.FileName; using (StreamWriter sw = new StreamWriter(File.Create(path))) { try { sw.Write(GetValuesForCSVDataIntegrityFlags(flagType)); } catch (Exception ex) { MessageBox.Show(ex.Message); } } } }
private string GetValuesForCSVDataIntegrityFlags(ReportFlag flagType) { string delimiter = ","; StringBuilder sb = new StringBuilder(); // If row is enabled, process it foreach (DataGridViewRow item in dataGridView2.Rows) { if (Convert.ToBoolean(item.Cells[0].Value) == true) // if checkbox is enabled { if (item.Cells[8].Value == null) // Ignore if there is no data in rows { continue; } int valueID = Convert.ToInt32(item.Cells[1].Value); // Get id from row List <FlagOccurrenceClass> flagList; var tuple = GetInstanceFromAllDataByDeviceID(valueID); if (tuple != null) { DataClass itemData = tuple.Item1; // DataClass object found, now get all flags and convert them in string (CSV) representation flagList = HelperGUIStats.GenerateReport(itemData, flagType); string res = HelperGUIStats.ConvertListToCSV(flagList, delimiter, itemData.deviceID); sb.Append(res); } } } if (sb.Length == 0) { throw new Exception("Couldn't get any data from Data Integrity"); } sb.Remove(sb.Length - 2, 2); // Remove last new line return(sb.ToString()); }
/// <summary> /// Generate list of selected flags, separe days too /// </summary> /// <param name="item"> DataClass object </param> /// <param name="flag"> Selected flag for report generation </param> /// <returns></returns> public static List <FlagOccurrenceClass> GenerateReportSeparateDays(DataClass item, ReportFlag flag) { List <FlagOccurrenceClass> lis = new List <FlagOccurrenceClass>(); // Get tuple based on flag var resFromFlag = GetDelegateBasedOnFlag(flag); // Assign tuple values returned by method Func <Flags, bool> calcuFunc = resFromFlag.Item1; string flagDesc = resFromFlag.Item2; Color currentCol = resFromFlag.Item3; bool res; // result from comparing flags DateTime startDate = item.startDate; // Date to display start of active flags bool endFlag = false; // This will signal end of array ReportSM state = ReportSM.STATE_ENTRY; // Inital state of SM bool newDay = false; for (int i = 0; i < item.data.Length; i++) // Loop thru every 15min value { if (i + 1 == item.data.Length) { endFlag = true; } Data15MinClass dataItem = item.data[i]; // Get data res = calcuFunc(dataItem.flags); // Get result from flags compare newDay = dataItem.date.Day != startDate.Day; // Get new day indicating flag switch (state) { case ReportSM.STATE_ENTRY: // Entry state sets startDate and chooses next state startDate = dataItem.date; if (res) { state = ReportSM.STATE_FLAGS_MATCH; } else { state = ReportSM.STATE_FLAGS_DIFFER; } break; case ReportSM.STATE_FLAGS_DIFFER: // Update startDate if new day and wait for matching flags if (newDay) { startDate = dataItem.date; } if (res) // Check if flags are matching { state = ReportSM.STATE_FLAGS_MATCH; // Goto state for matching flags startDate = dataItem.date; // Save starting date } break; case ReportSM.STATE_FLAGS_MATCH: // Wait for flag difference and write result if (endFlag) // Its end of data and we are still in flags match state, end current "array" { DateTime temp = dataItem.date.AddMinutes(15); // Add minutes so temp points to new day and "00" //lis.Add(new RowDataClass(flagDesc, currentCol, startDate.ToString("dd/MM/yyyy"), startDate.ToString("HH:mm"), temp.ToString("HH:mm"), "end")); // original lis.Add(new FlagOccurrenceClass(flagDesc, currentCol, startDate, temp)); break; } if (newDay) // Check if new day, new day restarts cycle { // It is new day, save result and start over //lis.Add(new RowDataClass(flagDesc, currentCol, startDate.ToString("dd/MM/yyyy"), startDate.ToString("HH:mm"), dataItem.date.ToString("HH:mm"), "end")); // original lis.Add(new FlagOccurrenceClass(flagDesc, currentCol, startDate, dataItem.date)); startDate = dataItem.date; break; } // Compare result if (!res) { state = ReportSM.STATE_FLAGS_DIFFER; //lis.Add(new RowDataClass(flagDesc, currentCol, startDate.ToString("dd/MM/yyyy"), startDate.ToString("HH:mm"), dataItem.date.ToString("HH:mm"), "end")); lis.Add(new FlagOccurrenceClass(flagDesc, currentCol, startDate, dataItem.date)); } break; default: throw new Exception("Corrupted state in ReportSM"); } } return(lis); }
/// <summary> /// Get method implementation, description and color based on given flag /// </summary> /// <param name="flag"></param> /// <returns></returns> private static Tuple <Func <Flags, bool>, string, Color> GetDelegateBasedOnFlag(ReportFlag flag) { Func <Flags, bool> calcuFunc; string flagDesc = ""; Color currentCol; switch (flag) // Select fucntion and color based on flag { case ReportFlag.Empty: case ReportFlag.Valid: case ReportFlag.Duplicate: flagDesc = "Duplicate"; calcuFunc = (f) => f.duplicateFlag == true; currentCol = ConfigClass.colors.colorDuplicate; break; case ReportFlag.Missing: flagDesc = "Missing"; calcuFunc = (f) => f.missingFlag == true; currentCol = ConfigClass.colors.colorMissing; break; case ReportFlag.Null: flagDesc = "Null values"; calcuFunc = (f) => f.nullFlag == true; currentCol = ConfigClass.colors.colorNull; break; case ReportFlag.Repeat: flagDesc = "Data repeat"; calcuFunc = (f) => f.dataRepeatFlag == true; currentCol = ConfigClass.colors.colorRepeat; break; case ReportFlag.Zero: flagDesc = "Zero values"; calcuFunc = (f) => f.zeroFlag == true; currentCol = ConfigClass.colors.colorZero; break; case ReportFlag.Invalid: flagDesc = "Invalid"; calcuFunc = (f) => f.invalidFlag == true; currentCol = ConfigClass.colors.colorInvalid; break; default: throw new Exception("Flag not recognized"); } return(new Tuple <Func <Flags, bool>, string, Color>(calcuFunc, flagDesc, currentCol)); }