Skip to content

C#

C# 2

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CSVLoadAndStatisticsTest
{
    public partial class Form1 : Form
    {
        // TODO: Change this to your CSV root folder.
        // Expected structure: root\YYYY\MMDD\hhmmssff_Audit.csv
        private const string RootFolder = @"D:\csvroot";

        private CancellationTokenSource _cts;

        public Form1()
        {
            InitializeComponent();
        }

        // --------------------------
        // UI Events
        // --------------------------

        private async void btnStart_Click(object sender, EventArgs e)
        {
            btnStart.Enabled = false;
            btnCancel.Enabled = true;

            if (_cts != null)
            {
                _cts.Cancel();
                _cts.Dispose();
            }
            _cts = new CancellationTokenSource();

            DateTime from = dtpFrom.Value;
            DateTime to = dtpTo.Value;

            try
            {
                // Run aggregation on a background thread, return only results.
                AggregateResult result = await Task.Run(() => ProcessRange(from, to, _cts.Token));

                // Apply results on the UI thread in one shot.
                ApplyResultToUi(result);
            }
            catch (OperationCanceledException)
            {
                lblStatus.Text = "Canceled.";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Error");
            }
            finally
            {
                btnStart.Enabled = true;
                btnCancel.Enabled = false;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (_cts != null) _cts.Cancel();
        }

        private void btnExportDetailsCsv_Click(object sender, EventArgs e)
        {
            // Export detail rows currently displayed (last aggregation result).
            // This sample stores the last result in Tag. You can refactor as needed.
            AggregateResult r = this.Tag as AggregateResult;
            if (r == null || r.Details == null || r.Details.Count == 0)
            {
                MessageBox.Show("No detail data to export.", "Info");
                return;
            }

            using (SaveFileDialog sfd = new SaveFileDialog())
            {
                sfd.Filter = "CSV Files (*.csv)|*.csv|All Files (*.*)|*.*";
                sfd.FileName = "FixesDetails.csv";
                if (sfd.ShowDialog(this) != DialogResult.OK) return;

                try
                {
                    ExportDetailsToCsv(r.Details, sfd.FileName);
                    MessageBox.Show("Export completed.", "Info");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString(), "Error");
                }
            }
        }

        // --------------------------
        // Core Aggregation (No UI access)
        // --------------------------

        private AggregateResult ProcessRange(DateTime from, DateTime to, CancellationToken ct)
        {
            AggregateResult result = new AggregateResult();

            foreach (string path in EnumerateTargetAuditCsvFiles(from, to, ct))
            {
                ct.ThrowIfCancellationRequested();

                result.FilesRead++;

                DateTime fileTime;
                if (!TryParseTimeFromAuditFilePath(path, out fileTime))
                {
                    // Skip if file time cannot be determined from path.
                    continue;
                }

                bool firstLine = true;
                int lineNo = 0;

                foreach (string line in File.ReadLines(path))
                {
                    ct.ThrowIfCancellationRequested();

                    lineNo++;

                    if (string.IsNullOrWhiteSpace(line))
                        continue;

                    // Spec: 1st line is a datetime text. We skip it for row processing.
                    if (firstLine)
                    {
                        firstLine = false;
                        continue;
                    }

                    result.RowsRead++;

                    // NOTE: If columns may include commas inside quotes, use a real CSV parser.
                    string[] cols = line.Split(',');
                    if (cols.Length < 2)
                        continue;

                    // Expected:
                    // cols[0] = WorkNo (int)
                    // cols[1] = LP or Fix
                    // cols[2] = Fix reason (only for NG) [optional but recommended]
                    // cols[3..] = measurement values (optional)
                    int workNo = 0;
                    int.TryParse(cols[0].Trim(), out workNo);

                    string judge = cols[1].Trim();

                    if (judge.Equals("LP", StringComparison.OrdinalIgnoreCase))
                    {
                        result.OkCount++;

                        // Collect OK measurement statistics (all numeric columns from index 3).
                        AddMeasurementsToStats(cols, startIndex: 3, statsMap: result.OkStatsByIndex);
                    }
                    else if (judge.Equals("Fix", StringComparison.OrdinalIgnoreCase))
                    {
                        result.FixesCount++;

                        string reason = (cols.Length >= 3 ? cols[2].Trim() : "Unknown");
                        if (string.IsNullOrWhiteSpace(reason)) reason = "Unknown";

                        int cnt;
                        if (!result.FixesByReason.TryGetValue(reason, out cnt)) cnt = 0;
                        result.FixesByReason[reason] = cnt + 1;

                        // Collect Fix measurement statistics too (optional).
                        AddMeasurementsToStats(cols, startIndex: 3, statsMap: result.FixesStatsByIndex);

                        // Store detail row for later drill-down / export.
                        FixRecord rec = new FixRecord();
                        rec.Time = fileTime;
                        rec.WorkNo = workNo;
                        rec.Judge = "Fix";
                        rec.Reason = reason;
                        rec.FilePath = path;
                        rec.LineNo = lineNo;
                        rec.RawColumns = cols;

                        result.Details.Add(rec);
                    }
                    else
                    {
                        // Unknown judge string => skip or treat as needed.
                    }
                }
            }

            return result;
        }

        private static void AddMeasurementsToStats(string[] cols, int startIndex, Dictionary<int, StatisticsAccumulator> statsMap)
        {
            for (int i = startIndex; i < cols.Length; i++)
            {
                double v;
                string s = cols[i].Trim();

                // If your numeric format is stable (e.g., dot decimal), you can force InvariantCulture:
                // double.TryParse(s, NumberStyles.Float, CultureInfo.InvariantCulture, out v)
                if (!double.TryParse(s, NumberStyles.Float, CultureInfo.InvariantCulture, out v) &&
                    !double.TryParse(s, NumberStyles.Float, CultureInfo.CurrentCulture, out v))
                {
                    continue;
                }

                StatisticsAccumulator acc;
                if (!statsMap.TryGetValue(i, out acc))
                {
                    acc = new StatisticsAccumulator();
                    statsMap[i] = acc;
                }
                acc.Add(v);
            }
        }

        // --------------------------
        // File Enumeration & Time Parsing (Audit naming)
        // --------------------------

        private IEnumerable<string> EnumerateTargetAuditCsvFiles(DateTime from, DateTime to, CancellationToken ct)
        {
            DateTime dayFrom = from.Date;
            DateTime dayTo = to.Date;

            foreach (DateTime day in EachDay(dayFrom, dayTo))
            {
                ct.ThrowIfCancellationRequested();

                string dayFolder = BuildDayFolder(day);
                if (!Directory.Exists(dayFolder))
                    continue;

                // Rename: Inspection -> Audit
                foreach (string file in Directory.EnumerateFiles(dayFolder, "*_Audit.csv", SearchOption.TopDirectoryOnly))
                {
                    ct.ThrowIfCancellationRequested();

                    DateTime fileTime;
                    if (!TryParseTimeFromAuditFilePath(file, out fileTime))
                        continue;

                    if (fileTime >= from && fileTime <= to)
                        yield return file;
                }
            }
        }

        private static IEnumerable<DateTime> EachDay(DateTime from, DateTime to)
        {
            for (DateTime d = from.Date; d <= to.Date; d = d.AddDays(1))
                yield return d;
        }

        private static string BuildDayFolder(DateTime day)
        {
            // root\YYYY\MMDD
            return Path.Combine(
                RootFolder,
                day.Year.ToString("0000"),
                day.ToString("MMdd")
            );
        }

        private static bool TryParseTimeFromAuditFilePath(string filePath, out DateTime dt)
        {
            dt = DateTime.MinValue;

            try
            {
                // Expected: root\YYYY\MMDD\hhmmssff_Audit.csv
                string fileName = Path.GetFileName(filePath);
                if (string.IsNullOrEmpty(fileName)) return false;

                int underscore = fileName.IndexOf('_');
                if (underscore <= 0) return false;

                string timePart = fileName.Substring(0, underscore); // "hhmmssff"
                if (timePart.Length < 6) return false;

                string folderMMDD = new DirectoryInfo(Path.GetDirectoryName(filePath)).Name; // "MMDD"
                string folderYYYY = new DirectoryInfo(Path.GetDirectoryName(Path.GetDirectoryName(filePath))).Name; // "YYYY"

                int year, month, day;
                if (!int.TryParse(folderYYYY, out year)) return false;
                if (folderMMDD == null || folderMMDD.Length != 4) return false;

                if (!int.TryParse(folderMMDD.Substring(0, 2), out month)) return false;
                if (!int.TryParse(folderMMDD.Substring(2, 2), out day)) return false;

                int hh, mm, ss, ff = 0;
                if (!int.TryParse(timePart.Substring(0, 2), out hh)) return false;
                if (!int.TryParse(timePart.Substring(2, 2), out mm)) return false;
                if (!int.TryParse(timePart.Substring(4, 2), out ss)) return false;

                if (timePart.Length >= 8)
                {
                    int tmp;
                    if (int.TryParse(timePart.Substring(6, 2), out tmp))
                        ff = tmp; // 1/100 sec
                }

                int ms = ff * 10;
                dt = new DateTime(year, month, day, hh, mm, ss, ms);
                return true;
            }
            catch
            {
                return false;
            }
        }

        // --------------------------
        // UI Apply (Single shot)
        // --------------------------

        private void ApplyResultToUi(AggregateResult r)
        {
            // Keep last result for export button usage.
            this.Tag = r;

            lblStatus.Text = string.Format("Files={0}, Rows={1}", r.FilesRead, r.RowsRead);
            lblOk.Text = r.OkCount.ToString();
            lblFixes.Text = r.FixesCount.ToString(); // Rename: NGWork -> Fixes

            // Summary by reason (Fixes)
            dgvFixesSummary.Rows.Clear();
            foreach (var kv in r.FixesByReason.OrderByDescending(x => x.Value))
            {
                dgvFixesSummary.Rows.Add(kv.Key, kv.Value);
            }

            // Detail rows (Fix records)
            dgvFixesDetails.Rows.Clear();
            foreach (FixRecord rec in r.Details)
            {
                // Show main fields + optional raw columns join (or pick specific measurement columns).
                string raw = (rec.RawColumns == null) ? "" : string.Join(",", rec.RawColumns);
                dgvFixesDetails.Rows.Add(
                    rec.Time.ToString("yyyy-MM-dd HH:mm:ss.fff"),
                    rec.WorkNo,
                    rec.Reason,
                    rec.FilePath,
                    rec.LineNo,
                    raw
                );
            }

            // Optional: show one example statistics summary for OK measurement columns
            // (You can bind to another grid if you want.)
            if (r.OkStatsByIndex.Count > 0)
            {
                int firstIndex = r.OkStatsByIndex.Keys.OrderBy(x => x).First();
                StatisticsAccumulator acc = r.OkStatsByIndex[firstIndex];
                if (acc.HasData)
                {
                    lblOkStats.Text = string.Format(
                        "OK Stats (col {0}): avg={1:F3}, min={2:F3}, max={3:F3}, n={4}",
                        firstIndex, acc.Average, acc.Min, acc.Max, acc.Count);
                }
                else
                {
                    lblOkStats.Text = "OK Stats: no numeric data.";
                }
            }
            else
            {
                lblOkStats.Text = "OK Stats: no numeric columns.";
            }
        }

        // --------------------------
        // Export
        // --------------------------

        private static void ExportDetailsToCsv(List<FixRecord> details, string outputPath)
        {
            // Write UTF-8 with BOM to be Excel-friendly (optional).
            using (var sw = new StreamWriter(outputPath, false, System.Text.Encoding.UTF8))
            {
                // Header
                sw.WriteLine("Time,WorkNo,Reason,FilePath,LineNo,RawColumns");

                foreach (FixRecord rec in details)
                {
                    string raw = (rec.RawColumns == null) ? "" : string.Join("|", rec.RawColumns.Select(EscapeCsv));

                    sw.WriteLine(string.Format("{0},{1},{2},{3},{4},{5}",
                        EscapeCsv(rec.Time.ToString("yyyy-MM-dd HH:mm:ss.fff")),
                        rec.WorkNo,
                        EscapeCsv(rec.Reason),
                        EscapeCsv(rec.FilePath),
                        rec.LineNo,
                        EscapeCsv(raw)
                    ));
                }
            }
        }

        private static string EscapeCsv(string s)
        {
            if (s == null) return "";
            if (s.Contains("\"")) s = s.Replace("\"", "\"\"");
            if (s.Contains(",") || s.Contains("\n") || s.Contains("\r"))
                return "\"" + s + "\"";
            return s;
        }
    }

    // --------------------------
    // Result DTOs
    // --------------------------

    public sealed class AggregateResult
    {
        public int FilesRead { get; set; }
        public int RowsRead { get; set; }

        public int OkCount { get; set; }

        // Rename: NGWork -> Fixes
        public int FixesCount { get; set; }

        // Summary counts by reason
        public Dictionary<string, int> FixesByReason { get; set; }

        // Detail rows (Fixes)
        public List<FixRecord> Details { get; set; }

        // Measurement stats (OK and Fixes) by column index
        public Dictionary<int, StatisticsAccumulator> OkStatsByIndex { get; set; }
        public Dictionary<int, StatisticsAccumulator> FixesStatsByIndex { get; set; }

        public AggregateResult()
        {
            FixesByReason = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase);
            Details = new List<FixRecord>();
            OkStatsByIndex = new Dictionary<int, StatisticsAccumulator>();
            FixesStatsByIndex = new Dictionary<int, StatisticsAccumulator>();
        }
    }

    // Rename: NGWorkRecord -> FixRecord
    public sealed class FixRecord
    {
        public DateTime Time { get; set; }
        public int WorkNo { get; set; }
        public string Judge { get; set; }   // "NG"
        public string Reason { get; set; }
        public string FilePath { get; set; }
        public int LineNo { get; set; }
        public string[] RawColumns { get; set; }
    }

    public sealed class StatisticsAccumulator
    {
        public double Min { get; private set; }
        public double Max { get; private set; }
        public double Sum { get; private set; }
        public int Count { get; private set; }

        public StatisticsAccumulator()
        {
            Min = double.PositiveInfinity;
            Max = double.NegativeInfinity;
            Sum = 0;
            Count = 0;
        }

        public void Add(double value)
        {
            if (value < Min) Min = value;
            if (value > Max) Max = value;
            Sum += value;
            Count++;
        }

        public double Average
        {
            get { return Count == 0 ? 0 : (Sum / Count); }
        }

        public bool HasData
        {
            get { return Count > 0; }
        }
    }
}