-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProjectHistoryView.cs
More file actions
385 lines (312 loc) · 14.7 KB
/
Copy pathProjectHistoryView.cs
File metadata and controls
385 lines (312 loc) · 14.7 KB
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
using System.Reflection;
using WinFormsGraphs;
namespace LinesOfCodeCounter;
internal sealed class ProjectHistoryView
{
readonly Form owner;
readonly Panel menuBar;
readonly Control[] currentDataControls;
readonly Func<string?> getFolderPath;
readonly ProjectStatsHistoryStore historyStore = new();
readonly Button buttonCurrentDataTab;
readonly Button buttonProjectHistoryTab;
readonly Panel panelProjectHistory;
readonly Label labelHistoryTitle;
readonly Label labelHistorySummary;
readonly DataGridView dataGridViewHistory;
readonly GraphView graphHistoryFiles;
readonly GraphView graphHistoryLines;
readonly GraphView graphHistoryCharacters;
readonly GraphView graphHistoryAverages;
public ProjectHistoryView(Form owner, Panel menuBar, IEnumerable<Control> currentDataControls, Func<string?> getFolderPath)
{
this.owner = owner;
this.menuBar = menuBar;
this.currentDataControls = currentDataControls?.Where(x => x != null).ToArray() ?? Array.Empty<Control>();
this.getFolderPath = getFolderPath ?? (() => "");
buttonCurrentDataTab = CreateTabButton("Current Data", 330);
buttonProjectHistoryTab = CreateTabButton("Project History", buttonCurrentDataTab.Right + 4);
buttonCurrentDataTab.Click += (_, _) => ShowCurrentDataView();
buttonProjectHistoryTab.Click += (_, _) => ShowProjectHistoryView();
menuBar.Controls.Add(buttonCurrentDataTab);
menuBar.Controls.Add(buttonProjectHistoryTab);
panelProjectHistory = CreateHistoryPanel();
labelHistoryTitle = CreateTitleLabel();
labelHistorySummary = CreateSummaryLabel();
graphHistoryFiles = CreateHistoryGraph("Files analyzed over time", "files");
graphHistoryLines = CreateHistoryGraph("Total lines over time", "lines");
graphHistoryCharacters = CreateHistoryGraph("Total characters over time", "chars");
graphHistoryAverages = CreateHistoryGraph("Average size over time", "");
dataGridViewHistory = CreateHistoryGrid();
panelProjectHistory.Controls.Add(labelHistoryTitle);
panelProjectHistory.Controls.Add(labelHistorySummary);
panelProjectHistory.Controls.Add(graphHistoryFiles);
panelProjectHistory.Controls.Add(graphHistoryLines);
panelProjectHistory.Controls.Add(graphHistoryCharacters);
panelProjectHistory.Controls.Add(graphHistoryAverages);
panelProjectHistory.Controls.Add(dataGridViewHistory);
owner.Controls.Add(panelProjectHistory);
panelProjectHistory.BringToFront();
menuBar.BringToFront();
Layout();
ShowCurrentDataView();
}
public void SaveSnapshot(CodeAnalysisResult result)
{
string? folderPath = getFolderPath();
if(string.IsNullOrWhiteSpace(folderPath) || result == null)
return;
historyStore.Add(ProjectStatsSnapshot.FromResult(folderPath, result));
}
public void Refresh()
{
string? folderPath = getFolderPath();
if(panelProjectHistory == null)
return;
if(string.IsNullOrWhiteSpace(folderPath))
{
labelHistorySummary.Text = "Choose a folder and fetch data to begin tracking project history.";
ClearHistoryGraphs();
FillHistoryGrid(Array.Empty<ProjectStatsSnapshot>());
return;
}
IReadOnlyList<ProjectStatsSnapshot> snapshots = historyStore.GetSnapshots(folderPath);
IReadOnlyList<ProjectStatsDailyPoint> daily = historyStore.GetDailyAverages(folderPath);
labelHistorySummary.Text = snapshots.Count <= 0
? $"No stored history yet for: {folderPath}"
: $"Tracking {snapshots.Count:#,0} snapshots across {daily.Count:#,0} days for: {folderPath}";
FillHistoryGrid(snapshots);
UpdateHistoryGraphs(daily);
}
public void ShowCurrentDataView()
{
SetCurrentDataVisible(true);
panelProjectHistory.Visible = false;
buttonCurrentDataTab.BackColor = Color.FromArgb(58, 58, 58);
buttonProjectHistoryTab.BackColor = Color.FromArgb(30, 30, 30);
}
public void ShowProjectHistoryView()
{
Refresh();
SetCurrentDataVisible(false);
panelProjectHistory.Visible = true;
panelProjectHistory.BringToFront();
menuBar.BringToFront();
buttonCurrentDataTab.BackColor = Color.FromArgb(30, 30, 30);
buttonProjectHistoryTab.BackColor = Color.FromArgb(58, 58, 58);
}
public void Layout()
{
if(panelProjectHistory == null)
return;
panelProjectHistory.Location = new Point(0, menuBar.Bottom);
panelProjectHistory.Size = new Size(owner.ClientSize.Width, owner.ClientSize.Height - menuBar.Height);
int margin = 12;
int width = Math.Max(320, panelProjectHistory.ClientSize.Width - (margin * 2) - SystemInformation.VerticalScrollBarWidth);
int y = 12;
labelHistoryTitle.Location = new Point(margin, y);
y += 28;
labelHistorySummary.Location = new Point(margin, y);
y += 28;
SetHistoryGraphBounds(graphHistoryFiles, margin, width, ref y);
SetHistoryGraphBounds(graphHistoryLines, margin, width, ref y);
SetHistoryGraphBounds(graphHistoryCharacters, margin, width, ref y);
SetHistoryGraphBounds(graphHistoryAverages, margin, width, ref y);
dataGridViewHistory.Bounds = new Rectangle(margin, y, width, 190);
y += dataGridViewHistory.Height + margin;
panelProjectHistory.AutoScrollMinSize = new Size(width + (margin * 2), y);
}
Panel CreateHistoryPanel() => new()
{
Location = new Point(0, menuBar.Bottom),
Size = new Size(owner.ClientSize.Width, owner.ClientSize.Height - menuBar.Height),
Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right,
BackColor = Color.FromArgb(36, 36, 36),
Visible = false,
AutoScroll = true,
};
Label CreateTitleLabel() => new()
{
AutoSize = true,
ForeColor = Color.FromArgb(251, 146, 40),
Font = new Font(owner.Font.FontFamily, 12f, FontStyle.Bold),
Text = "Project History",
};
Label CreateSummaryLabel() => new()
{
AutoSize = true,
ForeColor = Color.White,
Text = "Choose a folder and fetch data to begin tracking project history.",
};
Button CreateTabButton(string text, int x)
{
Button button = new()
{
Text = text,
Size = new Size(112, 22),
Location = new Point(x, 1),
FlatStyle = FlatStyle.Flat,
ForeColor = Color.White,
BackColor = Color.FromArgb(30, 30, 30),
Cursor = Cursors.Hand,
};
button.FlatAppearance.BorderSize = 1;
button.FlatAppearance.BorderColor = Color.FromArgb(80, 80, 80);
button.FlatAppearance.MouseOverBackColor = Color.FromArgb(48, 48, 48);
button.FlatAppearance.MouseDownBackColor = Color.FromArgb(60, 60, 60);
return button;
}
GraphView CreateHistoryGraph(string title, string unit)
{
GraphView graph = GraphPresets.Create(title, unit).Lines();
graph.Height = 118;
graph.ShowStats = true;
graph.ShowLegend = true;
graph.ShowCrosshair = true;
graph.ShowHoverTooltip = true;
graph.AutoScaleFromZero = true;
graph.Theme = GraphTheme.Dark;
return graph;
}
DataGridView CreateHistoryGrid()
{
DataGridView grid = new()
{
AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill,
AllowUserToAddRows = false,
AllowUserToDeleteRows = false,
AllowUserToResizeRows = false,
ReadOnly = true,
RowHeadersVisible = false,
ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize,
BorderStyle = BorderStyle.None,
CellBorderStyle = DataGridViewCellBorderStyle.SingleHorizontal,
SelectionMode = DataGridViewSelectionMode.FullRowSelect,
MultiSelect = false,
EnableHeadersVisualStyles = false,
};
StyleHistoryGrid(grid);
EnableDoubleBufferedGrid(grid);
return grid;
}
void SetCurrentDataVisible(bool visible)
{
for(int i = 0; i < currentDataControls.Length; i++)
currentDataControls[i].Visible = visible;
}
void FillHistoryGrid(IReadOnlyList<ProjectStatsSnapshot> snapshots)
{
dataGridViewHistory.DataSource = snapshots
.OrderByDescending(x => x.CapturedAtUtc)
.Select(x => new HistoryGridRow
{
Captured = x.CapturedAtUtc.ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss"),
Files = x.TotalFiles,
Lines = x.TotalLines,
Characters = x.TotalCharacters,
AverageLinesPerFile = Math.Round(x.AverageLinesPerFile, 2),
AverageCharactersPerLine = Math.Round(x.AverageCharactersPerLine, 2),
})
.ToList();
StyleHistoryGrid(dataGridViewHistory);
ApplyHistoryGridHeaders();
}
void ApplyHistoryGridHeaders()
{
if(dataGridViewHistory.Columns.Count <= 0)
return;
dataGridViewHistory.Columns[nameof(HistoryGridRow.Captured)].HeaderText = "Captured";
dataGridViewHistory.Columns[nameof(HistoryGridRow.Files)].HeaderText = "Files";
dataGridViewHistory.Columns[nameof(HistoryGridRow.Lines)].HeaderText = "Lines";
dataGridViewHistory.Columns[nameof(HistoryGridRow.Characters)].HeaderText = "Characters";
dataGridViewHistory.Columns[nameof(HistoryGridRow.AverageLinesPerFile)].HeaderText = "Avg Lines / File";
dataGridViewHistory.Columns[nameof(HistoryGridRow.AverageCharactersPerLine)].HeaderText = "Avg Chars / Line";
}
void UpdateHistoryGraphs(IReadOnlyList<ProjectStatsDailyPoint> daily)
{
ClearHistoryGraphs();
if(daily.Count <= 0)
return;
_ = graphHistoryFiles.SetValues("Files", daily.Select(x => (float)x.AverageFiles));
_ = graphHistoryLines.SetValues("Lines", daily.Select(x => (float)x.AverageLines));
_ = graphHistoryCharacters.SetValues("Characters", daily.Select(x => (float)x.AverageCharacters));
GraphSeries avgLines = graphHistoryAverages.SetValues("Lines / File", daily.Select(x => (float)x.AverageLinesPerFile));
avgLines.Fill = false;
avgLines.LineWidth = 1.5f;
GraphSeries avgChars = graphHistoryAverages.SetValues("Chars / Line", daily.Select(x => (float)x.AverageCharactersPerLine));
avgChars.Fill = false;
avgChars.LineWidth = 1.5f;
string start = daily[0].Day.ToString("yyyy-MM-dd");
string end = daily[^1].Day.ToString("yyyy-MM-dd");
graphHistoryFiles.Title = $"Files analyzed over time ({start} to {end})";
graphHistoryLines.Title = $"Total lines over time ({start} to {end})";
graphHistoryCharacters.Title = $"Total characters over time ({start} to {end})";
graphHistoryAverages.Title = $"Average size over time ({start} to {end})";
}
void ClearHistoryGraphs()
{
graphHistoryFiles.ClearSeries();
graphHistoryLines.ClearSeries();
graphHistoryCharacters.ClearSeries();
graphHistoryAverages.ClearSeries();
}
void SetHistoryGraphBounds(Control graph, int margin, int width, ref int y)
{
graph.Bounds = new Rectangle(margin, y, width, 118);
y += graph.Height + 10;
}
void StyleHistoryGrid(DataGridView grid)
{
Color formBack = Color.FromArgb(36, 36, 36);
Color rowBack = Color.FromArgb(38, 38, 38);
Color rowAltBack = Color.FromArgb(44, 44, 44);
Color headerBack = Color.FromArgb(24, 24, 24);
Color lineColor = Color.FromArgb(58, 58, 58);
Color textColor = Color.White;
Color dimTextColor = Color.FromArgb(210, 210, 210);
Color accentColor = Color.FromArgb(251, 146, 40);
Color selectionBack = Color.FromArgb(70, 70, 70);
grid.BackgroundColor = formBack;
grid.GridColor = lineColor;
grid.ForeColor = textColor;
grid.DefaultCellStyle.BackColor = rowBack;
grid.DefaultCellStyle.ForeColor = dimTextColor;
grid.DefaultCellStyle.SelectionBackColor = selectionBack;
grid.DefaultCellStyle.SelectionForeColor = Color.Gold;
grid.DefaultCellStyle.Font = owner.Font;
grid.DefaultCellStyle.Padding = new Padding(3, 0, 3, 0);
grid.AlternatingRowsDefaultCellStyle.BackColor = rowAltBack;
grid.AlternatingRowsDefaultCellStyle.ForeColor = dimTextColor;
grid.AlternatingRowsDefaultCellStyle.SelectionBackColor = selectionBack;
grid.AlternatingRowsDefaultCellStyle.SelectionForeColor = Color.Gold;
grid.ColumnHeadersDefaultCellStyle.BackColor = headerBack;
grid.ColumnHeadersDefaultCellStyle.ForeColor = accentColor;
grid.ColumnHeadersDefaultCellStyle.SelectionBackColor = headerBack;
grid.ColumnHeadersDefaultCellStyle.SelectionForeColor = accentColor;
grid.ColumnHeadersDefaultCellStyle.Font = owner.Font;
grid.ColumnHeadersDefaultCellStyle.Padding = new Padding(3, 2, 3, 2);
grid.RowTemplate.Height = 25;
grid.RowsDefaultCellStyle.BackColor = rowBack;
grid.RowsDefaultCellStyle.ForeColor = dimTextColor;
grid.RowsDefaultCellStyle.SelectionBackColor = selectionBack;
grid.RowsDefaultCellStyle.SelectionForeColor = Color.Gold;
grid.AdvancedCellBorderStyle.Left = DataGridViewAdvancedCellBorderStyle.None;
grid.AdvancedCellBorderStyle.Right = DataGridViewAdvancedCellBorderStyle.None;
grid.AdvancedCellBorderStyle.Top = DataGridViewAdvancedCellBorderStyle.None;
grid.AdvancedCellBorderStyle.Bottom = DataGridViewAdvancedCellBorderStyle.Single;
grid.AdvancedColumnHeadersBorderStyle.Left = DataGridViewAdvancedCellBorderStyle.None;
grid.AdvancedColumnHeadersBorderStyle.Right = DataGridViewAdvancedCellBorderStyle.None;
grid.AdvancedColumnHeadersBorderStyle.Top = DataGridViewAdvancedCellBorderStyle.None;
grid.AdvancedColumnHeadersBorderStyle.Bottom = DataGridViewAdvancedCellBorderStyle.Single;
}
void EnableDoubleBufferedGrid(DataGridView grid) => typeof(DataGridView).InvokeMember("DoubleBuffered", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.SetProperty, null, grid, new object[] { true });
sealed class HistoryGridRow
{
public string Captured { get; set; } = "";
public int Files { get; set; }
public long Lines { get; set; }
public long Characters { get; set; }
public double AverageLinesPerFile { get; set; }
public double AverageCharactersPerLine { get; set; }
}
}