-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCAD.cs
278 lines (216 loc) · 9.16 KB
/
CAD.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
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AFD.Dashboard.Models;
namespace Dievas {
public class CAD {
private readonly ConcurrentDictionary<int, IncidentDto> _incidents = new ConcurrentDictionary<int, IncidentDto>();
private readonly ConcurrentDictionary<string, UnitDto> _units = new ConcurrentDictionary<string, UnitDto>();
public IEnumerable<UnitDto> GetUnits() {
return _units.Values.OrderBy( t => t.RadioName );
}
public UnitDto GetUnitByName(string radioName) {
if (_units.ContainsKey(radioName)) return _units[radioName];
return new UnitDto() { RadioName = radioName};
}
public void AddOrUpdateUnit(UnitDto unit)
{
_units[unit.RadioName] = unit;
}
public UnitDto UpdateUnitField(string radioName, string field, string value) {
UnitDto unit = new UnitDto{ RadioName = radioName};
if (_units.ContainsKey(radioName)) unit = _units[radioName];
unit[field] = value;
_units[radioName] = unit;
return _units[radioName];
}
public UnitDto UpdateUnitStatus(string radioName, int statusId) {
UnitDto unit = new UnitDto { RadioName = radioName, StatusId = statusId};
if (_units.ContainsKey(radioName)) unit = _units[radioName];
unit.StatusId = statusId;
_units[radioName] = unit;
return _units[radioName];
}
public IEnumerable GetIncidents() {
return _incidents.Values.OrderBy( t => t.Id );
}
public IEnumerable GetActiveIncidents() {
return _incidents.Values.Where(t => t.IsActive).OrderBy( t => t.Id );
}
public IncidentDto GetIncident(int id) {
if (_incidents.ContainsKey(id)) return _incidents[id];
return new IncidentDto() { Id = id};
}
// We don't do incident # checking here, for the case where we want to add
// an existing incident with new and or updated data
public IncidentDto AddIncident(int id, IncidentDto incident){
if (incident.UnitsAssigned.Count > 1)
incident.UnitsAssigned = RemoveDuplicateAssignments(incident.UnitsAssigned);
_incidents[id] = incident;
return incident;
}
// We don't do incident # checking here, for the case where we want to add
// an existing incident with new and or updated data
public IncidentDto AddIncident(IncidentDto incident){
int id = incident.Id;
if (incident.UnitsAssigned.Count>1)
incident.UnitsAssigned = RemoveDuplicateAssignments(incident.UnitsAssigned);
_incidents[id] = incident;
return incident;
}
public List<UnitAssignmentDto> RemoveDuplicateAssignments (List<UnitAssignmentDto> unitAssignments)
{
return unitAssignments
.GroupBy(unit => unit.RadioName)
.Select(group => group.OrderByDescending(assignment => assignment.StartDateTime).First())
.ToList();
}
// Update incident Field - any updates, we copy out the value, make the update
// return the value to the dict
public IncidentDto UpdateIncidentField(int id, string field, string value){
IncidentDto incident = new IncidentDto {Id = id };
if (_incidents.ContainsKey(id)) incident = _incidents[id];
incident[field] = value;
_incidents[id] = incident;
return _incidents[id];
}
public IncidentDto AddOrUpdateIncidentUnit(int id, UnitAssignmentDto unit) {
// Update global unit
UnitDto globalUnit = GetUnitByName(unit.RadioName);
globalUnit.StatusId = unit.StatusId;
globalUnit.StatusCode = unit.StatusCode;
globalUnit.IncidentId = unit.IncidentId;
AddOrUpdateUnit(globalUnit);
// Update unit on incident
IncidentDto incident = new IncidentDto {Id = id};
if (_incidents.ContainsKey(id)) incident = _incidents[id];
// 2023-01-27 - Removed the use of IndexOf until such time as DTO object support IEquatable<T>
// var unitKey = incident.UnitsAssigned.IndexOf(unit);
var unitKey = (incident.UnitsAssigned == null ? -1 : incident.UnitsAssigned.FindIndex(u => u.RadioName == unit.RadioName));
if (unitKey < 0) {
incident.UnitsAssigned.Add(unit);
} else {
incident.UnitsAssigned[unitKey] = unit;
}
_incidents[id] = incident;
return incident;
}
public IncidentDto AddOrUpdateIncidentUnit(int id, UnitDto unit) {
// Update global unit
AddOrUpdateUnit(unit);
UnitAssignmentDto assignedUnit = new UnitAssignmentDto
{
RadioName = unit.RadioName,
StatusId = unit.StatusId,
StatusCode = unit.StatusCode,
IncidentId = id
};
// Update unit on incident
IncidentDto incident = new IncidentDto {Id = id};
if (_incidents.ContainsKey(id)) incident = _incidents[id];
// 2023-01-27 - Removed the use of IndexOf until such time as DTO object support IEquatable<T>
// var unitKey = incident.UnitsAssigned.IndexOf(assignedUnit);
// var unitKey = incident.UnitsAssigned.FindIndex(u => u.RadioName == assignedUnit.RadioName);
var unitKey = (incident.UnitsAssigned == null ? -1 : incident.UnitsAssigned.FindIndex(u => u.RadioName == assignedUnit.RadioName));
if (unitKey < 0) {
incident.UnitsAssigned.Add(assignedUnit);
} else {
incident.UnitsAssigned[unitKey] = assignedUnit;
}
_incidents[id] = incident;
return incident;
}
public IncidentDto AddOrUpdateIncidentComment(int id, CommentDto comment){
IncidentDto incident = new IncidentDto {Id = id};
if (_incidents.ContainsKey(id)) incident = _incidents[id];
// 2023-01-27 - Removed the use of IndexOf until such time as DTO Objects to IEquatable<T>
// var commentKey = incident.Comments.IndexOf(comment);
// var commentKey = incident.Comments.FindIndex(x => x.Id == comment.Id);
var commentKey = (incident.Comments == null ? -1 : incident.Comments.FindIndex(x => x.Id == comment.Id));
if (commentKey < 0) {
incident.Comments.Add(comment);
} else {
incident.Comments[commentKey] = comment;
}
_incidents[id] = incident;
return incident;
}
public int IncidentCount() {
return _incidents.Count;
}
public int PopulateUnitList(IEnumerable<UnitDto> initialUnits)
{
foreach (var unit in initialUnits)
{
_units[unit.RadioName] = unit;
}
return initialUnits.Count();
}
/// Remove's all closed incidents older than now - ageToKeep old
/// If ageToKeep is null, removes all closed incidents
public int RemoveClosedIncidents(TimeSpan? ageToKeep = null) {
if (!ageToKeep.HasValue) ageToKeep = TimeSpan.Zero;
DateTime ? timeThreshold = DateTime.Now - ageToKeep;
var oldIncidents = _incidents.Where(i =>
timeThreshold >= i.Value.IncidentEndDateTime
// timeThreshold.GreaterThanOrEqual<DateTime>(i.Value.IncidentEndDateTime)
).ToArray();
/// TODO: ADD Logging for count
/// _logger.log(LogLevel.Debug, "{} incidents older than {} slated for removal.", oldIncidents.length, timeThreshold);
int count = 0;
foreach (var oldIncident in oldIncidents) {
if (this._removeIncident(oldIncident)) count++;
}
return count;
}
/// Remove's all closed incidents older than now - ABS(hours), hours old
/// If hours is zero, removes all closed incidents
public int RemoveClosedIncidents(double hours = 0) {
if (hours > 0) hours = hours * -1;
DateTime timeThreshold = DateTime.Now.AddHours(hours);
var oldIncidents = _incidents.Where(i =>
timeThreshold >= i.Value.IncidentEndDateTime
// timeThreshold.GreaterThanOrEqual<DateTime>(i.Value.IncidentEndDateTime)
).ToArray();
/// TODO: ADD Logging for count
/// _logger.log(LogLevel.Debug, "{} incidents older than {} slated for removal.", oldIncidents.length, timeThreshold);
int count = 0;
foreach (var oldIncident in oldIncidents) {
if (this._removeIncident(oldIncident)) count++;
}
return count;
}
private bool _removeIncident(KeyValuePair<int, IncidentDto> incident){
if (_incidents.TryRemove(incident)) {
/// TODO: ADD Logging for incident removal
/// _logger.log(LogLevel.Debug, "Removed incident # {}", oldIncident.key);
return true;
} else {
/// TODO: ADD Logging for incident removal failure
/// _logger.log(LogLevel.Warn, "Failed to remove incident # {}", oldIncident.key);
}
return false;
}
private bool _removeIncident(int id, IncidentDto incident) {
if (_incidents.TryRemove(id, out incident)) {
/// TODO: ADD Logging for incident removal
/// _logger.log(LogLevel.Debug, "Removed incident # {}", oldIncident.key);
return true;
} else {
/// TODO: ADD Logging for incident removal failure
/// _logger.log(LogLevel.Warn, "Failed to remove incident # {}", oldIncident.key);
}
return false;
}
public bool RemoveIncident(int id) {
if (_incidents.ContainsKey(id)) {
IncidentDto incident = _incidents[id];
if (this._removeIncident(id, incident)) return true;
}
return false;
}
}
}