-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathDatasetRaceway.cs
314 lines (250 loc) · 11.6 KB
/
DatasetRaceway.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
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
// Copyright (c) The University of Dundee 2018-2019
// This file is part of the Research Data Management Platform (RDMP).
// RDMP is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
// RDMP is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along with RDMP. If not, see <https://www.gnu.org/licenses/>.
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Rdmp.Core.CommandExecution;
using Rdmp.Core.Curation.Data;
using Rdmp.Core.Curation.Data.Dashboarding;
using Rdmp.Core.DataQualityEngine.Data;
using Rdmp.Core.Icons.IconProvision;
using Rdmp.Core.Providers;
using Rdmp.Core.Repositories;
using Rdmp.Core.ReusableLibraryCode.Icons.IconProvision;
using Rdmp.UI.DashboardTabs.Construction;
using Rdmp.UI.ItemActivation;
using Rdmp.UI.Refreshing;
using Rdmp.UI.TestsAndSetup.ServicePropogation;
namespace Rdmp.UI.Raceway;
/// <summary>
/// Allows you to quickly view the timespan of each of your datasets, which sections of your datasets are failing validation (e.g. 'Prescribing' 2001-2002 records are all failing
/// validation but 2003 onwards are fine) and identify any gaps in your record coverage.
///
/// <para>Each dataset appears as a green/red bar along a shared axis (See RacewayRenderAreaUI). You can switch from viewing all months for which you have data, only
/// the last decade, year or last 6 months.</para>
///
/// <para>By default the row height of each bar in a dataset indicates the proportion of records in that month relative to the average number of records per month, this allows you to see for
/// example the gradual increase in volume of records per month in a dataset and identify any periods where it doubles (may indicate duplication) or a hole appears. If you tick 'Ignore
/// Row Counts' then full bars will appear only, this lets you identify which datasets are responsible for sparse errors (e.g. if 'Biochemistry' has 1,00,000,000 and some records have
/// dates sprinkled between 1900-01-01 and 2000-01-01 then these will appear on the axis but won't be visible due to how sparse the number of error records are).</para>
/// </summary>
public partial class DatasetRaceway : RDMPUserControl, IDashboardableControl
{
private ToolStripButton btnAddCatalogue = new("Add Catalogue") { Name = "btnAddCatalogue" };
private ToolStripButton btnRemoveAll = new("Clear", FamFamFamIcons.delete_multi.ImageToBitmap())
{ Name = "btnRemoveAll" };
private ToolStripButton btnAddExtractableDatasetPackage =
new("Add Package") { Name = "btnAddExtractableDatasetPackage" };
private ToolStripLabel toolStripLabel1 = new("Show Period") { Name = "toolStripLabel1" };
private ToolStripComboBox ddShowPeriod = new() { Name = "ddShowPeriod", Size = new Size(121, 25) };
private ToolStripButton cbIgnoreRowCounts = new() { Name = "cbIgnoreRowCounts" };
public DatasetRaceway()
{
InitializeComponent();
btnAddCatalogue.Click += btnAddCatalogue_Click;
btnRemoveAll.Click += btnRemoveAll_Click;
ddShowPeriod.DropDownClosed += ddShowPeriod_DropDownClosed;
cbIgnoreRowCounts.CheckOnClick = true;
cbIgnoreRowCounts.CheckedChanged += cbIgnoreRowCounts_CheckedChanged;
btnAddExtractableDatasetPackage.Click += btnAddExtractableDatasetPackage_Click;
ddShowPeriod.ComboBox.DataSource = Enum.GetValues(typeof(RacewayShowPeriod));
btnRemoveAll.Image = FamFamFamIcons.delete_multi.ImageToBitmap();
_ignoreRowCounts = CatalogueIcons.RowCounts_Ignore.ImageToBitmap();
_respectRowCounts = CatalogueIcons.RowCounts_Respect.ImageToBitmap();
}
private DashboardControl _dashboardControlDatabaseRecord;
private DatasetRacewayObjectCollection _collection;
private IActivateItems _activator;
private bool _isEditmodeOn;
private bool isFirstTime = true;
private Bitmap _ignoreRowCounts;
private Bitmap _respectRowCounts;
public void GenerateChart()
{
CommonFunctionality.ResetChecks();
var allCatalogues = _collection.GetCatalogues();
var cataloguesToAdd = new Dictionary<Catalogue, Dictionary<DateTime, ArchivalPeriodicityCount>>();
DQERepository dqeRepository;
try
{
dqeRepository = new DQERepository(_activator.RepositoryLocator.CatalogueRepository);
}
catch (NotSupportedException e)
{
CommonFunctionality.Fatal("Failed to get DQE Repository", e);
return;
}
foreach (var cata in allCatalogues.OrderBy(c => c.Name))
{
var eval = dqeRepository.GetMostRecentEvaluationFor(cata);
Dictionary<DateTime, ArchivalPeriodicityCount> dictionary = null;
if (eval != null)
dictionary = PeriodicityState.GetPeriodicityCountsForEvaluation(eval, true);
cataloguesToAdd.Add(cata, dictionary);
}
//every month seen in every dataset ever
var buckets = GetBuckets(cataloguesToAdd);
racewayRenderArea.AddTracks(_activator, cataloguesToAdd, buckets, _collection.IgnoreRows);
racewayRenderArea.Refresh();
Invalidate();
}
private DateTime[] GetBuckets(
Dictionary<Catalogue, Dictionary<DateTime, ArchivalPeriodicityCount>> cataloguesDictionary)
{
var buckets = new List<DateTime>();
foreach (var dictionary in cataloguesDictionary.Values)
if (dictionary != null)
foreach (var dt in dictionary.Keys)
{
//if it is before the start or after today
if (dt < GetUserPickedStartDate() || dt.Date > DateTime.Now.Date)
continue;
if (!buckets.Contains(dt))
buckets.Add(dt);
}
return buckets.OrderBy(static d => d).ToArray();
}
private DateTime GetUserPickedStartDate()
{
return _collection.ShowPeriod switch
{
RacewayShowPeriod.AllTime => DateTime.MinValue,
RacewayShowPeriod.LastDecade => DateTime.Now.AddYears(-10),
RacewayShowPeriod.LastYear => DateTime.Now.AddYears(-1),
RacewayShowPeriod.LastSixMonths => DateTime.Now.AddMonths(-6),
_ => throw new ArgumentOutOfRangeException()
};
}
public enum RacewayShowPeriod
{
AllTime,
LastDecade,
LastYear,
LastSixMonths
}
public void RefreshBus_RefreshObject(object sender, RefreshObjectEventArgs e)
{
}
public void SetCollection(IActivateItems activator, IPersistableObjectCollection collection)
{
_activator = activator;
_collection = (DatasetRacewayObjectCollection)collection;
SetItemActivator(activator);
btnAddCatalogue.Image = _activator.CoreIconProvider.GetImage(RDMPConcept.Catalogue, OverlayKind.Import)
.ImageToBitmap();
btnAddExtractableDatasetPackage.Image = _activator.CoreIconProvider
.GetImage(RDMPConcept.ExtractableDataSetPackage, OverlayKind.Import).ImageToBitmap();
ddShowPeriod.ComboBox.SelectedItem = _collection.ShowPeriod;
cbIgnoreRowCounts.Checked = _collection.IgnoreRows;
UpdateIgnoreRowCountCheckBoxIconAndText();
if (isFirstTime)
{
isFirstTime = false;
racewayRenderArea.RequestDeletion += c =>
{
_collection.RemoveCatalogue(c);
SaveCollectionChanges();
};
racewayRenderArea.NotifyEditModeChange(_isEditmodeOn);
}
CommonFunctionality.Add(btnAddCatalogue);
CommonFunctionality.Add(btnAddExtractableDatasetPackage);
CommonFunctionality.Add(btnRemoveAll);
CommonFunctionality.Add(toolStripLabel1);
CommonFunctionality.Add(ddShowPeriod);
CommonFunctionality.Add(cbIgnoreRowCounts);
GenerateChart();
}
public IPersistableObjectCollection GetCollection() => _collection;
public string GetTabName() => Text;
public string GetTabToolTip() => null;
public IPersistableObjectCollection ConstructEmptyCollection(DashboardControl databaseRecord)
{
_dashboardControlDatabaseRecord = databaseRecord;
return new DatasetRacewayObjectCollection();
}
public void NotifyEditModeChange(bool isEditModeOn)
{
_isEditmodeOn = isEditModeOn;
racewayRenderArea.NotifyEditModeChange(isEditModeOn);
CommonFunctionality.ToolStrip.Visible = isEditModeOn;
}
private void btnAddCatalogue_Click(object sender, EventArgs e)
{
if (_activator.SelectObjects(new DialogArgs
{
TaskDescription = "Choose which new Catalogues should be represented in the diagram."
},
_activator.RepositoryLocator.CatalogueRepository.GetAllObjects<Catalogue>()
.Except(_collection.GetCatalogues())
.ToArray(),
out var selected))
{
foreach (var catalogue in selected)
AddCatalogue(catalogue);
SaveCollectionChanges();
GenerateChart();
}
}
private void SaveCollectionChanges()
{
_dashboardControlDatabaseRecord.SaveCollectionState(_collection);
}
private void btnRemoveAll_Click(object sender, EventArgs e)
{
_collection.ClearDatabaseObjects();
SaveCollectionChanges();
GenerateChart();
}
private void btnAddExtractableDatasetPackage_Click(object sender, EventArgs e)
{
if (_activator.CoreChildProvider is not DataExportChildProvider dataExportChildProvider)
return;
if (Activator.SelectObject(new DialogArgs
{
TaskDescription = "Choose a Package. All Catalogues in the Package will be added to the diagram."
}, dataExportChildProvider.AllPackages, out var packageToAdd))
{
var contents =
_activator.RepositoryLocator.DataExportRepository.GetAllDataSets(packageToAdd,
dataExportChildProvider.ExtractableDataSets);
foreach (var cata in contents.Select(ds => ds.Catalogue))
if (!_collection.GetCatalogues().Contains(cata))
AddCatalogue((Catalogue)cata);
SaveCollectionChanges();
GenerateChart();
}
}
private void AddCatalogue(Catalogue cata)
{
_collection.AddCatalogue(cata);
}
private void ddShowPeriod_DropDownClosed(object sender, EventArgs e)
{
if (ddShowPeriod.SelectedItem == null)
return;
var newPeriod = (RacewayShowPeriod)ddShowPeriod.SelectedItem;
//no change
if (newPeriod == _collection.ShowPeriod)
return;
_collection.ShowPeriod = newPeriod;
GenerateChart();
SaveCollectionChanges();
}
private void cbIgnoreRowCounts_CheckedChanged(object sender, EventArgs e)
{
UpdateIgnoreRowCountCheckBoxIconAndText();
_collection.IgnoreRows = cbIgnoreRowCounts.Checked;
GenerateChart();
SaveCollectionChanges();
}
private void UpdateIgnoreRowCountCheckBoxIconAndText()
{
cbIgnoreRowCounts.Image = cbIgnoreRowCounts.Checked ? _ignoreRowCounts : _respectRowCounts;
}
}