/
EnvironmentalPerformanceInformation.java
479 lines (437 loc) · 20.7 KB
/
EnvironmentalPerformanceInformation.java
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
/*
* Copyright (c) 2010-2015 Evolveum
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.evolveum.midpoint.schema.statistics;
import com.evolveum.midpoint.prism.xml.XmlTypeConverter;
import com.evolveum.midpoint.util.QNameUtil;
import com.evolveum.midpoint.xml.ns._public.common.common_3.EnvironmentalPerformanceInformationType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.MappingsStatisticsEntryType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.MappingsStatisticsType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.NotificationsStatisticsEntryType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.NotificationsStatisticsType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ProvisioningStatisticsEntryType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ProvisioningStatisticsType;
import org.apache.commons.lang.StringUtils;
import javax.xml.datatype.DatatypeConstants;
import javax.xml.namespace.QName;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author Pavol Mederly
*/
public class EnvironmentalPerformanceInformation {
/*
* Thread safety: Instances of this class may be accessed from more than one thread at once.
* Updates are invoked in the context of the thread executing the task.
* Queries are invoked either from this thread, or from some observer (task manager or GUI thread).
*
* We ensure synchronization by making public methods synchronized. We don't expect much contention on this.
*/
private final EnvironmentalPerformanceInformationType startValue; // this object is concurrently read (that is thread-safe), not written
private Map<ProvisioningStatisticsKey,ProvisioningStatisticsData> provisioningData = new HashMap<>();
private Map<NotificationsStatisticsKey,GenericStatisticsData> notificationsData = new HashMap<>();
private Map<MappingsStatisticsKey,GenericStatisticsData> mappingsData = new HashMap<>();
private StatusMessage lastMessage;
public EnvironmentalPerformanceInformation(EnvironmentalPerformanceInformationType value) {
startValue = value;
}
public EnvironmentalPerformanceInformation() {
this(null);
}
public EnvironmentalPerformanceInformationType getStartValue() {
return startValue;
}
public synchronized EnvironmentalPerformanceInformationType getDeltaValue() {
EnvironmentalPerformanceInformationType rv = toEnvironmentalPerformanceInformationType();
return rv;
}
public synchronized EnvironmentalPerformanceInformationType getAggregatedValue() {
EnvironmentalPerformanceInformationType delta = toEnvironmentalPerformanceInformationType();
EnvironmentalPerformanceInformationType rv = aggregate(startValue, delta);
return rv;
}
private EnvironmentalPerformanceInformationType toEnvironmentalPerformanceInformationType() {
EnvironmentalPerformanceInformationType rv = new EnvironmentalPerformanceInformationType();
rv.setProvisioningStatistics(toProvisioningStatisticsType());
rv.setMappingsStatistics(toMappingsStatisticsType());
rv.setNotificationsStatistics(toNotificationsStatisticsType());
if (lastMessage != null) {
rv.setLastMessageTimestamp(XmlTypeConverter.createXMLGregorianCalendar(lastMessage.getDate()));
rv.setLastMessage(lastMessage.getMessage());
}
return rv;
}
private NotificationsStatisticsType toNotificationsStatisticsType() {
NotificationsStatisticsType rv = new NotificationsStatisticsType();
if (notificationsData == null) {
return rv;
}
for (Map.Entry<NotificationsStatisticsKey, GenericStatisticsData> entry : notificationsData.entrySet()) {
NotificationsStatisticsKey key = entry.getKey();
String transport = key.getTransport();
NotificationsStatisticsEntryType entryType = findNotificationsEntryType(rv.getEntry(), transport);
if (entryType == null) {
entryType = new NotificationsStatisticsEntryType();
entryType.setTransport(transport);
rv.getEntry().add(entryType);
}
setValueNotifications(entryType, key.isSuccess(), entry.getValue().getCount(),
entry.getValue().getMinDuration(), entry.getValue().getMaxDuration(), entry.getValue().getTotalDuration());
}
return rv;
}
private MappingsStatisticsType toMappingsStatisticsType() {
MappingsStatisticsType rv = new MappingsStatisticsType();
if (mappingsData == null) {
return rv;
}
for (Map.Entry<MappingsStatisticsKey, GenericStatisticsData> entry : mappingsData.entrySet()) {
MappingsStatisticsKey key = entry.getKey();
String object = key.getObjectName();
MappingsStatisticsEntryType entryType = findMappingsEntryType(rv.getEntry(), object);
if (entryType == null) {
entryType = new MappingsStatisticsEntryType();
entryType.setObject(object);
rv.getEntry().add(entryType);
}
setValueMapping(entryType, entry.getValue().getCount(),
entry.getValue().getMinDuration(), entry.getValue().getMaxDuration(), entry.getValue().getTotalDuration());
}
return rv;
}
private ProvisioningStatisticsType toProvisioningStatisticsType() {
ProvisioningStatisticsType rv = new ProvisioningStatisticsType();
if (provisioningData == null) {
return rv;
}
for (Map.Entry<ProvisioningStatisticsKey, ProvisioningStatisticsData> entry : provisioningData.entrySet()) {
ProvisioningStatisticsKey key = entry.getKey();
String resource = key.getResourceName();
QName oc = key.getObjectClass();
ProvisioningStatisticsEntryType entryType = findProvisioningEntryType(rv.getEntry(), resource, oc);
if (entryType == null) {
entryType = new ProvisioningStatisticsEntryType();
entryType.setResource(resource);
entryType.setObjectClass(oc);
rv.getEntry().add(entryType);
}
setValue(entryType, key.getOperation(), key.getStatusType(), entry.getValue().getCount(),
entry.getValue().getMinDuration(), entry.getValue().getMaxDuration(), entry.getValue().getTotalDuration());
}
return rv;
}
private static ProvisioningStatisticsEntryType findProvisioningEntryType(List<ProvisioningStatisticsEntryType> list, String resource, QName objectClass) {
for (ProvisioningStatisticsEntryType entryType : list) {
if (StringUtils.equals(entryType.getResource(), resource) && QNameUtil.match(entryType.getObjectClass(), objectClass)) {
return entryType;
}
}
return null;
}
private void setValue(ProvisioningStatisticsEntryType e, ProvisioningOperation operation, ProvisioningStatusType statusType, int count, long min, long max, long totalDuration) {
switch (operation) {
case ICF_GET:
if (statusType == ProvisioningStatusType.SUCCESS) {
e.setGetSuccess(e.getGetSuccess() + count);
} else {
e.setGetFailure(e.getGetFailure() + count);
}
break;
case ICF_SEARCH:
if (statusType == ProvisioningStatusType.SUCCESS) {
e.setSearchSuccess(e.getSearchSuccess() + count);
} else {
e.setSearchFailure(e.getSearchFailure() + count);
}
break;
case ICF_CREATE:
if (statusType == ProvisioningStatusType.SUCCESS) {
e.setCreateSuccess(e.getCreateSuccess() + count);
} else {
e.setCreateFailure(e.getCreateFailure() + count);
}
break;
case ICF_UPDATE:
if (statusType == ProvisioningStatusType.SUCCESS) {
e.setUpdateSuccess(e.getUpdateSuccess() + count);
} else {
e.setUpdateFailure(e.getUpdateFailure() + count);
}
break;
case ICF_DELETE:
if (statusType == ProvisioningStatusType.SUCCESS) {
e.setDeleteSuccess(e.getDeleteSuccess() + count);
} else {
e.setDeleteFailure(e.getDeleteFailure() + count);
}
break;
case ICF_SYNC:
if (statusType == ProvisioningStatusType.SUCCESS) {
e.setSyncSuccess(e.getSyncSuccess() + count);
} else {
e.setSyncFailure(e.getSyncFailure() + count);
}
break;
case ICF_SCRIPT:
if (statusType == ProvisioningStatusType.SUCCESS) {
e.setScriptSuccess(e.getScriptSuccess() + count);
} else {
e.setScriptFailure(e.getScriptFailure() + count);
}
break;
case ICF_GET_LATEST_SYNC_TOKEN:
case ICF_GET_SCHEMA:
if (statusType == ProvisioningStatusType.SUCCESS) {
e.setOtherSuccess(e.getOtherSuccess() + count);
} else {
e.setOtherFailure(e.getOtherFailure() + count);
}
break;
default:
throw new IllegalArgumentException("Illegal operation name: " + operation);
}
if (e.getMinTime() == null || min < e.getMinTime()) {
e.setMinTime(min);
}
if (e.getMaxTime() == null || max > e.getMaxTime()) {
e.setMaxTime(max);
}
e.setTotalTime(e.getTotalTime() + totalDuration);
}
private void setValueNotifications(NotificationsStatisticsEntryType e, boolean success, int count, long min, long max, long totalDuration) {
if (success) {
e.setCountSuccess(e.getCountSuccess() + count);
} else {
e.setCountFailure(e.getCountFailure() + count);
}
if (e.getMinTime() == null || min < e.getMinTime()) {
e.setMinTime(min);
}
if (e.getMaxTime() == null || max > e.getMaxTime()) {
e.setMaxTime(max);
}
e.setTotalTime(e.getTotalTime() + totalDuration);
}
private void setValueMapping(MappingsStatisticsEntryType e, int count, long min, long max, long totalDuration) {
e.setCount(e.getCount() + count);
if (e.getMinTime() == null || min < e.getMinTime()) {
e.setMinTime(min);
}
if (e.getMaxTime() == null || max > e.getMaxTime()) {
e.setMaxTime(max);
}
e.setTotalTime(e.getTotalTime() + totalDuration);
}
private EnvironmentalPerformanceInformationType aggregate(EnvironmentalPerformanceInformationType startValue, EnvironmentalPerformanceInformationType delta) {
if (startValue == null) {
return delta;
}
EnvironmentalPerformanceInformationType rv = new EnvironmentalPerformanceInformationType();
addTo(rv, startValue);
addTo(rv, delta);
return rv;
}
public static void addTo(EnvironmentalPerformanceInformationType rv, EnvironmentalPerformanceInformationType delta) {
addProvisioningTo(rv, delta.getProvisioningStatistics());
addMappingsTo(rv, delta.getMappingsStatistics());
addNotificationsTo(rv, delta.getNotificationsStatistics());
if (delta.getLastMessageTimestamp() != null) {
if (rv.getLastMessageTimestamp() == null || rv.getLastMessageTimestamp().compare(delta.getLastMessageTimestamp()) == DatatypeConstants.LESSER) {
rv.setLastMessageTimestamp(delta.getLastMessageTimestamp());
rv.setLastMessage(delta.getLastMessage());
}
}
}
private static void addNotificationsTo(EnvironmentalPerformanceInformationType rv, NotificationsStatisticsType delta) {
if (delta == null) {
return;
}
if (rv.getNotificationsStatistics() == null) {
rv.setNotificationsStatistics(delta.clone());
return;
}
NotificationsStatisticsType rvNST = rv.getNotificationsStatistics();
for (NotificationsStatisticsEntryType de : delta.getEntry()) {
String transport = de.getTransport();
NotificationsStatisticsEntryType e = findNotificationsEntryType(rvNST.getEntry(), transport);
if (e == null) {
e = new NotificationsStatisticsEntryType();
e.setTransport(transport);
rvNST.getEntry().add(e);
}
e.setCountSuccess(e.getCountSuccess() + de.getCountSuccess());
e.setCountFailure(e.getCountFailure() + de.getCountFailure());
int count = e.getCountSuccess() + e.getCountFailure();
e.setMinTime(min(e.getMinTime(), de.getMinTime()));
e.setMaxTime(max(e.getMaxTime(), de.getMaxTime()));
e.setTotalTime(e.getTotalTime() + de.getTotalTime());
if (count > 0) {
e.setAverageTime(e.getTotalTime() / count);
} else {
e.setAverageTime(null);
}
}
}
private static NotificationsStatisticsEntryType findNotificationsEntryType(List<NotificationsStatisticsEntryType> list, String transport) {
for (NotificationsStatisticsEntryType entry : list) {
if (StringUtils.equals(entry.getTransport(), transport)) {
return entry;
}
}
return null;
}
private static void addMappingsTo(EnvironmentalPerformanceInformationType rv, MappingsStatisticsType delta) {
if (delta == null) {
return;
}
if (rv.getMappingsStatistics() == null) {
rv.setMappingsStatistics(delta.clone());
return;
}
MappingsStatisticsType rvMST = rv.getMappingsStatistics();
for (MappingsStatisticsEntryType de : delta.getEntry()) {
String object = de.getObject();
MappingsStatisticsEntryType e = findMappingsEntryType(rvMST.getEntry(), object);
if (e == null) {
e = new MappingsStatisticsEntryType();
e.setObject(object);
rvMST.getEntry().add(e);
}
e.setCount(e.getCount() + de.getCount());
e.setMinTime(min(e.getMinTime(), de.getMinTime()));
e.setMaxTime(max(e.getMaxTime(), de.getMaxTime()));
e.setTotalTime(e.getTotalTime() + de.getTotalTime());
if (e.getCount() > 0) {
e.setAverageTime(e.getTotalTime() / e.getCount());
} else {
e.setAverageTime(null);
}
}
}
private static MappingsStatisticsEntryType findMappingsEntryType(List<MappingsStatisticsEntryType> list, String object) {
for (MappingsStatisticsEntryType lineDto : list) {
if (StringUtils.equals(lineDto.getObject(), object)) {
return lineDto;
}
}
return null;
}
private static void addProvisioningTo(EnvironmentalPerformanceInformationType rv, ProvisioningStatisticsType delta) {
if (delta == null) {
return;
}
if (rv.getProvisioningStatistics() == null) {
rv.setProvisioningStatistics(delta.clone());
return;
}
ProvisioningStatisticsType rvPST = rv.getProvisioningStatistics();
for (ProvisioningStatisticsEntryType de : delta.getEntry()) {
String resource = de.getResource();
QName oc = de.getObjectClass();
ProvisioningStatisticsEntryType e = findProvisioningEntryType(rvPST.getEntry(), resource, oc);
if (e == null) {
e = new ProvisioningStatisticsEntryType();
e.setResource(resource);
e.setObjectClass(oc);
rvPST.getEntry().add(e);
}
e.setGetSuccess(e.getGetSuccess() + de.getGetSuccess());
e.setSearchSuccess(e.getSearchSuccess() + de.getSearchSuccess());
e.setCreateSuccess(e.getCreateSuccess() + de.getCreateSuccess());
e.setUpdateSuccess(e.getUpdateSuccess() + de.getUpdateSuccess());
e.setDeleteSuccess(e.getDeleteSuccess() + de.getDeleteSuccess());
e.setSyncSuccess(e.getSyncSuccess() + de.getSyncSuccess());
e.setScriptSuccess(e.getScriptSuccess() + de.getScriptSuccess());
e.setOtherSuccess(e.getOtherSuccess() + de.getOtherSuccess());
e.setGetFailure(e.getGetFailure() + de.getGetFailure());
e.setSearchFailure(e.getSearchFailure() + de.getSearchFailure());
e.setCreateFailure(e.getCreateFailure() + de.getCreateFailure());
e.setUpdateFailure(e.getUpdateFailure() + de.getUpdateFailure());
e.setDeleteFailure(e.getDeleteFailure() + de.getDeleteFailure());
e.setSyncFailure(e.getSyncFailure() + de.getSyncFailure());
e.setScriptFailure(e.getScriptFailure() + de.getScriptFailure());
e.setOtherFailure(e.getOtherFailure() + de.getOtherFailure());
int totalCount = e.getGetSuccess() + e.getGetFailure() +
e.getSearchSuccess() + e.getSearchFailure() +
e.getCreateSuccess() + e.getCreateFailure() +
e.getUpdateSuccess() + e.getUpdateFailure() +
e.getDeleteSuccess() + e.getDeleteFailure() +
e.getSyncSuccess() + e.getSyncFailure() +
e.getScriptSuccess() + e.getScriptFailure() +
e.getOtherSuccess() + e.getOtherFailure();
e.setMinTime(min(e.getMinTime(), de.getMinTime()));
e.setMaxTime(max(e.getMaxTime(), de.getMaxTime()));
e.setTotalTime(e.getTotalTime() + de.getTotalTime());
if (totalCount > 0) {
e.setAverageTime(e.getTotalTime() / totalCount);
} else {
e.setAverageTime(null);
}
}
}
private static Long min(Long a, Long b) {
if (a == null) {
return b;
}
if (b == null) {
return a;
}
return Math.min(a, b);
}
private static Long max(Long a, Long b) {
if (a == null) {
return b;
}
if (b == null) {
return a;
}
return Math.max(a, b);
}
public synchronized void recordProvisioningOperation(String resourceOid, String resourceName, QName objectClassName, ProvisioningOperation operation, boolean success, int count, long duration) {
ProvisioningStatisticsKey key = new ProvisioningStatisticsKey(resourceOid, resourceName, objectClassName, operation, success);
ProvisioningStatisticsData data = provisioningData.get(key);
if (data == null) {
data = new ProvisioningStatisticsData();
provisioningData.put(key, data);
}
data.recordOperation(duration, count);
}
public synchronized void recordNotificationOperation(String transportName, boolean success, long duration) {
NotificationsStatisticsKey key = new NotificationsStatisticsKey(transportName, success);
GenericStatisticsData data = notificationsData.get(key);
if (data == null) {
data = new GenericStatisticsData();
notificationsData.put(key, data);
}
data.recordOperation(duration, 1);
}
public synchronized void recordMappingOperation(String objectOid, String objectName, String mappingName, long duration) {
// ignoring mapping name for now
MappingsStatisticsKey key = new MappingsStatisticsKey(objectOid, objectName);
GenericStatisticsData data = mappingsData.get(key);
if (data == null) {
data = new GenericStatisticsData();
mappingsData.put(key, data);
}
data.recordOperation(duration, 1);
}
public synchronized StatusMessage getLastMessage() {
return lastMessage;
}
public synchronized void recordState(String message) {
lastMessage = new StatusMessage(message);
}
}