-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMetricsReportManager.cs
233 lines (202 loc) · 9.25 KB
/
MetricsReportManager.cs
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
using System;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using OfficeOpenXml;
using System.Globalization;
namespace PomodoroTimer
{
public class MetricsReportManager
{
private static string AppDataFolderPath => Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "PomodoroTimer");
private static string ReportFilePath => Path.Combine(AppDataFolderPath, "pomodoro_timer_report.csv");
private static string ReportExcelFilePath => Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "pomodoro_timer_report.xlsx");
public static void SaveMetricsReport(DateTime date, int totalTasksTime, int totalMeetingTime, int totalBreaksTime, int totalLongBreakTime, int totalLunchTime, int totalBreaksCount)
{
int totalWorkTime = totalTasksTime + totalMeetingTime;
int totalRestTime = totalBreaksTime + totalLongBreakTime + totalLunchTime;
int totalTime = totalWorkTime + totalRestTime;
// Use the culture's full day name to ensure consistency
string dayName = date.ToString("dddd", CultureInfo.InvariantCulture);
string reportLine = string.Join(",",
dayName,
date.ToString("yyyy-MM-dd"),
FormatTime(totalTasksTime),
FormatTime(totalMeetingTime),
FormatTime(totalBreaksTime),
FormatTime(totalLongBreakTime),
FormatTime(totalLunchTime),
totalBreaksCount.ToString(),
FormatTime(totalWorkTime),
FormatTime(totalRestTime),
FormatTime(totalTime),
totalTasksTime.ToString(),
totalMeetingTime.ToString(),
totalBreaksTime.ToString(),
totalLongBreakTime.ToString(),
totalLunchTime.ToString(),
totalWorkTime.ToString(),
totalRestTime.ToString(),
totalTime.ToString()
);
try
{
// Create the app data folder if it doesn't exist
Directory.CreateDirectory(AppDataFolderPath);
// Add header row if the file doesn't exist
if (!File.Exists(ReportFilePath))
{
string headerRow = "Day of the Week,Date,Total Task Time,Total Meeting Time,Total Break Time,Total Long Break Time,Total Lunch Time,Total Breaks Count,Total Work Time,Total Rest Time,Total Time,Total Task Seconds,Total Meeting Seconds,Total Break Seconds,Total Long Break Seconds,Total Lunch Seconds,Total Work Seconds,Total Rest Seconds,Total Seconds";
File.WriteAllText(ReportFilePath, headerRow + Environment.NewLine);
}
// Read all existing lines
string[] existingLines = File.Exists(ReportFilePath)
? File.ReadAllLines(ReportFilePath)
: new string[] { };
// Find if the date already exists (skip header row)
bool dateExists = false;
string dateToMatch = date.ToString("yyyy-MM-dd");
for (int i = 1; i < existingLines.Length; i++)
{
string[] fields = existingLines[i].Split(',');
if (fields.Length > 1 && fields[1] == dateToMatch)
{
existingLines[i] = reportLine;
dateExists = true;
break;
}
}
if (!dateExists)
{
// If the date doesn't exist, append the new line
Array.Resize(ref existingLines, existingLines.Length + 1);
existingLines[existingLines.Length - 1] = reportLine;
}
// Write all lines back to the file
File.WriteAllLines(ReportFilePath, existingLines);
// Update Excel report
SaveMetricsReportExcel();
}
catch (Exception ex)
{
MessageBox.Show($"Error saving metrics report: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
public static (int totalTasksTime, int totalMeetingTime, int totalBreaksTime, int totalLongBreakTime, int totalLunchTime, int totalBreaksCount, int totalTime) LoadMetricsReport(DateTime date)
{
if (!File.Exists(ReportFilePath))
{
return (0, 0, 0, 0, 0, 0, 0);
}
try
{
string dateToFind = date.ToString("yyyy-MM-dd");
string[] lines = File.ReadAllLines(ReportFilePath);
// Skip header and find matching date
var reportLine = lines.Skip(1)
.FirstOrDefault(line => line.Split(',')[1].Trim() == dateToFind);
if (reportLine != null)
{
string[] values = reportLine.Split(',');
if (values.Length >= 19)
{
return (
int.Parse(values[11]), // totalTasksTime
int.Parse(values[12]), // totalMeetingTime
int.Parse(values[13]), // totalBreaksTime
int.Parse(values[14]), // totalLongBreakTime
int.Parse(values[15]), // totalLunchTime
int.Parse(values[7]), // totalBreaksCount
int.Parse(values[18]) // totalTime
);
}
}
}
catch (Exception ex)
{
MessageBox.Show($"Error loading metrics report: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
return (0, 0, 0, 0, 0, 0, 0);
}
private static string FormatTime(int totalSeconds)
{
TimeSpan timeSpan = TimeSpan.FromSeconds(totalSeconds);
return timeSpan.ToString(@"hh\:mm\:ss");
}
private static void SaveMetricsReportExcel()
{
if (!File.Exists(ReportFilePath))
{
return;
}
try
{
while (IsExcelFileOpen(ReportExcelFilePath))
{
DialogResult result = MessageBox.Show(
$"Please close the Excel file '{ReportExcelFilePath}' to save the updated metrics.",
"File in Use",
MessageBoxButtons.RetryCancel,
MessageBoxIcon.Warning
);
if (result == DialogResult.Cancel)
{
return;
}
}
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
using (var package = new ExcelPackage(new FileInfo(ReportExcelFilePath)))
{
var worksheet = package.Workbook.Worksheets.FirstOrDefault(ws => ws.Name == "Metrics Report");
if (worksheet != null)
{
package.Workbook.Worksheets.Delete("Metrics Report");
}
worksheet = package.Workbook.Worksheets.Add("Metrics Report");
// Read all lines from CSV
string[] lines = File.ReadAllLines(ReportFilePath);
// Process header
string[] headers = lines[0].Split(',');
for (int i = 0; i < 11; i++) // Only first 11 columns for display
{
worksheet.Cells[1, i + 1].Value = headers[i];
}
// Process data rows
for (int row = 1; row < lines.Length; row++)
{
string[] values = lines[row].Split(',');
for (int col = 0; col < 11; col++) // Only first 11 columns for display
{
worksheet.Cells[row + 1, col + 1].Value = values[col];
}
}
// Apply table style
var tableRange = worksheet.Cells[1, 1, lines.Length, 11];
var table = worksheet.Tables.Add(tableRange, "MetricsReportTable");
table.TableStyle = OfficeOpenXml.Table.TableStyles.Medium9;
worksheet.Cells.AutoFitColumns();
package.Save();
}
}
catch (Exception ex)
{
MessageBox.Show($"Error saving Excel report: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private static bool IsExcelFileOpen(string filePath)
{
try
{
using (var stream = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.None))
{
stream.Close();
}
return false;
}
catch (IOException)
{
return true;
}
}
}
}