forked from netwrix/pingcastle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPingCastleReportHistoryCollection.cs
181 lines (163 loc) · 4.82 KB
/
PingCastleReportHistoryCollection.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
//
// Copyright (c) Ping Castle. All rights reserved.
// https://www.pingcastle.com
//
// Licensed under the Non-Profit OSL. See LICENSE file in the project root for full license information.
//
using PingCastle.Data;
using System;
using System.Collections.Generic;
namespace PingCastle.Healthcheck
{
public class PingCastleReportHistoryCollection<T> : ICollection<T> where T : IPingCastleReport
{
Dictionary<string, Dictionary<DateTime, T>> data { get; set; }
public PingCastleReportHistoryCollection()
{
data = new Dictionary<string, Dictionary<DateTime, T>>();
}
public void Add(T item)
{
// taking the more recent report
var sid = item.Domain.DomainSID;
if (!data.ContainsKey(sid))
{
data[sid] = new Dictionary<DateTime, T>();
}
data[sid][item.GenerationDate] = item;
}
public void Clear()
{
data.Clear();
}
public bool Contains(T item)
{
var sid = item.Domain.DomainSID;
return data.ContainsKey(sid) && data[sid].ContainsKey(item.GenerationDate);
}
public void CopyTo(T[] array, int arrayIndex)
{
foreach (var value in data.Values)
{
value.Values.CopyTo(array, arrayIndex);
arrayIndex += value.Count;
}
}
public int Count
{
get
{
int count = 0;
foreach (var value in data.Values)
{
count += value.Count;
}
return count;
}
}
public bool IsReadOnly
{
get { return false; }
}
public bool Remove(T item)
{
var sid = item.Domain.DomainSID;
if (!data.ContainsKey(sid))
return false;
var value = data[sid];
if (!value.ContainsKey(item.GenerationDate))
return false;
value.Remove(item.GenerationDate);
return true;
}
public IEnumerator<T> GetEnumerator()
{
throw new NotImplementedException();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
public PingCastleReportCollection<T> ToLatestReportCollection()
{
var output = new PingCastleReportCollection<T>();
foreach (var sid in data.Keys)
{
DateTime maxDate = DateTime.MinValue;
foreach (var date in data[sid].Keys)
{
if (maxDate < date)
maxDate = date;
}
output.Add(data[sid][maxDate]);
}
output.EnrichInformation();
return output;
}
public IEnumerable<KeyValuePair<string, DateTime>> GetKeyPoints()
{
var output = new List<KeyValuePair<string, DateTime>>();
foreach (var sid in data.Keys)
{
foreach (var date in data[sid].Keys)
{
output.Add(new KeyValuePair<string, DateTime>(sid, date));
}
}
return output;
}
public T GetItem(KeyValuePair<string, DateTime> key)
{
return data[key.Key][key.Value];
}
public PingCastleReportCollection<T> GetDataReportAtDate(DateTime dateToIssueReport)
{
var output = new PingCastleReportCollection<T>();
foreach (var sid in data.Keys)
{
DateTime min = DateTime.MinValue;
foreach (var date in data[sid].Keys)
{
if (date > min && date <= dateToIssueReport)
min = date;
}
if (min != DateTime.MinValue)
output.Add(data[sid][min]);
}
output.EnrichInformation();
return output;
}
public DateTime MinDate
{
get
{
DateTime min = DateTime.MaxValue;
foreach (var sid in data.Keys)
{
foreach (var date in data[sid].Keys)
{
if (date < min)
min = date;
}
}
return min;
}
}
public DateTime MaxDate
{
get
{
DateTime max = DateTime.MinValue;
foreach (var sid in data.Keys)
{
foreach (var date in data[sid].Keys)
{
if (date > max)
max = date;
}
}
return max;
}
}
}
}