-
Notifications
You must be signed in to change notification settings - Fork 188
/
TaskOperationStatsUtil.java
433 lines (392 loc) · 19.2 KB
/
TaskOperationStatsUtil.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
/*
* Copyright (C) 2010-2021 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
*/
package com.evolveum.midpoint.schema.util.task;
import com.evolveum.midpoint.prism.Objectable;
import com.evolveum.midpoint.prism.PrismContext;
import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.prism.PrismObjectDefinition;
import com.evolveum.midpoint.prism.polystring.PolyString;
import com.evolveum.midpoint.prism.util.CloneUtil;
import com.evolveum.midpoint.prism.xml.XmlTypeConverter;
import com.evolveum.midpoint.schema.statistics.*;
import com.evolveum.midpoint.xml.ns._public.common.common_3.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.xml.namespace.QName;
import java.util.Collection;
import java.util.Comparator;
import java.util.Objects;
import java.util.function.Predicate;
import java.util.stream.Stream;
import static com.evolveum.midpoint.util.MiscUtil.or0;
import static java.util.Collections.singleton;
/**
* Utility methods related to task operation statistics.
*/
public class TaskOperationStatsUtil {
/**
* Returns the number of item processing failures from this task and its subtasks.
* Subtasks must be resolved into to full objects.
*
* TODO Avoid useless statistics aggregation (avoid "first aggregating, then selecting failures")
*/
public static int getItemsProcessedWithFailureFromTree(TaskType task, PrismContext prismContext) {
OperationStatsType stats = getOperationStatsFromTree(task, prismContext);
return getItemsProcessedWithFailure(stats);
}
public static int getItemsProcessedWithFailure(TaskType task) {
return getItemsProcessedWithFailure(task.getOperationStats());
}
public static int getItemsProcessedWithFailure(OperationStatsType stats) {
return stats != null ? getItemsProcessedWithFailure(stats.getIterativeTaskInformation()) : 0;
}
public static int getItemsProcessedWithFailure(IterativeTaskInformationType info) {
if (info != null) {
return getCounts(info.getPart(), OutcomeKeyedCounterTypeUtil::isFailure);
} else {
return 0;
}
}
public static int getItemsProcessedWithSuccess(TaskType task) {
return getItemsProcessedWithSuccess(task.getOperationStats());
}
public static int getItemsProcessedWithSuccess(OperationStatsType stats) {
return stats != null ? getItemsProcessedWithSuccess(stats.getIterativeTaskInformation()) : 0;
}
public static int getItemsProcessedWithSuccess(IterativeTaskInformationType info) {
if (info != null) {
return getCounts(info.getPart(), OutcomeKeyedCounterTypeUtil::isSuccess);
} else {
return 0;
}
}
public static int getItemsProcessedWithSkip(IterativeTaskInformationType info) {
if (info != null) {
return getCounts(info.getPart(), OutcomeKeyedCounterTypeUtil::isSkip);
} else {
return 0;
}
}
/**
* Provides aggregated operation statistics from this task and all its subtasks.
* Works with stored operation stats, obviously. (We have no task instances here.)
*
* Assumes that the task has all subtasks filled-in.
*
* Currently does NOT support low-level performance statistics, namely:
*
* 1. repositoryPerformanceInformation,
* 2. cachesPerformanceInformation,
* 3. operationsPerformanceInformation,
* 4. workBucketManagementPerformanceInformation,
* 5. cachingConfiguration.
*/
public static OperationStatsType getOperationStatsFromTree(TaskType task, PrismContext prismContext) {
if (!TaskWorkStateUtil.isPartitionedMaster(task) && !TaskWorkStateUtil.isWorkStateHolder(task)) {
return task.getOperationStats();
}
OperationStatsType aggregate = new OperationStatsType(prismContext)
.iterativeTaskInformation(new IterativeTaskInformationType(prismContext))
.synchronizationInformation(new SynchronizationInformationType(prismContext))
.actionsExecutedInformation(new ActionsExecutedInformationType())
.environmentalPerformanceInformation(new EnvironmentalPerformanceInformationType())
.repositoryPerformanceInformation(new RepositoryPerformanceInformationType())
.workBucketManagementPerformanceInformation(new WorkBucketManagementPerformanceInformationType());
Stream<TaskType> subTasks = TaskTreeUtil.getAllTasksStream(task);
subTasks.forEach(subTask -> {
OperationStatsType operationStatsBean = subTask.getOperationStats();
if (operationStatsBean != null) {
IterativeTaskInformation.addTo(aggregate.getIterativeTaskInformation(), operationStatsBean.getIterativeTaskInformation());
SynchronizationInformation.addTo(aggregate.getSynchronizationInformation(), operationStatsBean.getSynchronizationInformation());
ActionsExecutedInformation.addTo(aggregate.getActionsExecutedInformation(), operationStatsBean.getActionsExecutedInformation());
EnvironmentalPerformanceInformation.addTo(aggregate.getEnvironmentalPerformanceInformation(), operationStatsBean.getEnvironmentalPerformanceInformation());
RepositoryPerformanceInformationUtil.addTo(aggregate.getRepositoryPerformanceInformation(), operationStatsBean.getRepositoryPerformanceInformation());
TaskWorkBucketManagementPerformanceInformationUtil.addTo(aggregate.getWorkBucketManagementPerformanceInformation(), operationStatsBean.getWorkBucketManagementPerformanceInformation());
}
});
return aggregate;
}
/**
* Returns the number of "iterations" i.e. how many times an item was processed by this task.
* It is useful e.g. to provide average values for performance indicators.
*/
public static Integer getItemsProcessed(TaskType task) {
return getItemsProcessed(task.getOperationStats());
}
/**
* Returns the number of "iterations" i.e. how many times an item was processed by this task.
* It is useful e.g. to provide average values for performance indicators.
*/
public static Integer getItemsProcessed(OperationStatsType statistics) {
if (statistics == null || statistics.getIterativeTaskInformation() == null) {
return null;
} else {
return getCounts(statistics.getIterativeTaskInformation().getPart(), set -> true);
}
}
@SuppressWarnings("WeakerAccess")
public static IterativeTaskPartItemsProcessingInformationType getIterativeInfoForCurrentPart(OperationStatsType statistics,
StructuredTaskProgressType structuredProgress) {
return getIterativeInfoForPart(statistics,
TaskProgressUtil.getCurrentPartUri(structuredProgress));
}
private static IterativeTaskPartItemsProcessingInformationType getIterativeInfoForPart(OperationStatsType statistics,
String partUri) {
if (statistics == null || statistics.getIterativeTaskInformation() == null) {
return null;
} else {
return statistics.getIterativeTaskInformation().getPart().stream()
.filter(part -> Objects.equals(part.getPartUri(), partUri))
.findFirst().orElse(null);
}
}
@SuppressWarnings("unused")
public static int getItemsProcessedForCurrentPart(OperationStatsType statistics,
StructuredTaskProgressType structuredTaskProgress) {
return getItemsProcessed(
getIterativeInfoForCurrentPart(statistics, structuredTaskProgress));
}
public static int getItemsProcessed(IterativeTaskPartItemsProcessingInformationType info) {
if (info == null) {
return 0;
} else {
return getCounts(singleton(info), set -> true);
}
}
public static int getErrors(IterativeTaskPartItemsProcessingInformationType info) {
if (info == null) {
return 0;
} else {
return getCounts(singleton(info), OutcomeKeyedCounterTypeUtil::isFailure);
}
}
public static double getProcessingTime(IterativeTaskPartItemsProcessingInformationType info) {
if (info == null) {
return 0;
} else {
return getProcessingTime(singleton(info), set -> true);
}
}
/**
* Returns sum of `count` values from processing information conforming to given predicate.
*/
private static int getCounts(Collection<IterativeTaskPartItemsProcessingInformationType> parts,
Predicate<ProcessedItemSetType> itemSetFilter) {
return parts.stream()
.flatMap(component -> component.getProcessed().stream())
.filter(Objects::nonNull)
.filter(itemSetFilter)
.mapToInt(p -> or0(p.getCount()))
.sum();
}
private static double getProcessingTime(Collection<IterativeTaskPartItemsProcessingInformationType> parts,
Predicate<ProcessedItemSetType> itemSetFilter) {
return parts.stream()
.flatMap(component -> component.getProcessed().stream())
.filter(Objects::nonNull)
.filter(itemSetFilter)
.mapToDouble(p -> or0(p.getDuration()))
.sum();
}
/**
* Returns object that was last successfully processed by given task.
*/
public static String getLastSuccessObjectName(TaskType task) {
OperationStatsType stats = task.getOperationStats();
if (stats == null || stats.getIterativeTaskInformation() == null) {
return null;
} else {
return getLastProcessedObjectName(stats.getIterativeTaskInformation(), OutcomeKeyedCounterTypeUtil::isSuccess);
}
}
/**
* Returns object that was last processed by given task in item set defined by the filter.
*/
public static String getLastProcessedObjectName(IterativeTaskInformationType info,
Predicate<ProcessedItemSetType> itemSetFilter) {
if (info == null) {
return null;
}
ProcessedItemType lastSuccess = info.getPart().stream()
.flatMap(component -> component.getProcessed().stream())
.filter(itemSetFilter)
.map(ProcessedItemSetType::getLastItem)
.filter(Objects::nonNull)
.max(Comparator.nullsFirst(Comparator.comparing(item -> XmlTypeConverter.toMillis(item.getEndTimestamp()))))
.orElse(null);
return lastSuccess != null ? lastSuccess.getName() : null;
}
/**
* Returns display name for given object, e.g. fullName for a user, displayName for a role,
* and more detailed description for a shadow.
*/
public static <O extends ObjectType> String getDisplayName(PrismObject<O> object) {
if (object == null) {
return null;
}
O objectable = object.asObjectable();
if (objectable instanceof UserType) {
return PolyString.getOrig(((UserType) objectable).getFullName());
} else if (objectable instanceof AbstractRoleType) {
return PolyString.getOrig(((AbstractRoleType) objectable).getDisplayName());
} else if (objectable instanceof ShadowType) {
ShadowType shadow = (ShadowType) objectable;
String objectName = PolyString.getOrig(shadow.getName());
QName oc = shadow.getObjectClass();
String ocName = oc != null ? oc.getLocalPart() : null;
return objectName + " (" + shadow.getKind() + " - " + shadow.getIntent() + " - " + ocName + ")";
} else {
return null;
}
}
/**
* Returns the type name for an object.
* (This really belongs somewhere else, not here.)
*/
public static QName getObjectType(ObjectType object, PrismContext prismContext) {
if (object == null) {
return null;
}
PrismObjectDefinition<?> objectDef = object.asPrismObject().getDefinition();
if (objectDef != null) {
return objectDef.getTypeName();
}
Class<? extends Objectable> clazz = object.asPrismObject().getCompileTimeClass();
if (clazz == null) {
return null;
}
PrismObjectDefinition<?> defFromRegistry = prismContext.getSchemaRegistry().findObjectDefinitionByCompileTimeClass(clazz);
if (defFromRegistry != null) {
return defFromRegistry.getTypeName();
} else {
return ObjectType.COMPLEX_TYPE;
}
}
public static boolean isEmpty(EnvironmentalPerformanceInformationType info) {
return info == null ||
(isEmpty(info.getProvisioningStatistics())
&& isEmpty(info.getMappingsStatistics())
&& isEmpty(info.getNotificationsStatistics())
&& info.getLastMessage() == null
&& info.getLastMessageTimestamp() == null);
}
public static boolean isEmpty(NotificationsStatisticsType notificationsStatistics) {
return notificationsStatistics == null || notificationsStatistics.getEntry().isEmpty();
}
public static boolean isEmpty(MappingsStatisticsType mappingsStatistics) {
return mappingsStatistics == null || mappingsStatistics.getEntry().isEmpty();
}
public static boolean isEmpty(ProvisioningStatisticsType provisioningStatistics) {
return provisioningStatistics == null || provisioningStatistics.getEntry().isEmpty();
}
/**
* Computes a sum of two operation statistics.
* Returns a modifiable object, independent from the source ones.
*/
public static OperationStatsType sum(OperationStatsType a, OperationStatsType b) {
if (a == null) {
return CloneUtil.clone(b);
} else {
OperationStatsType sum = CloneUtil.clone(a);
addTo(sum, b);
return sum;
}
}
/**
* Adds an statistics increment into given aggregate statistic information.
*/
private static void addTo(@NotNull OperationStatsType aggregate, @Nullable OperationStatsType increment) {
if (increment == null) {
return;
}
if (increment.getEnvironmentalPerformanceInformation() != null) {
if (aggregate.getEnvironmentalPerformanceInformation() == null) {
aggregate.setEnvironmentalPerformanceInformation(new EnvironmentalPerformanceInformationType());
}
EnvironmentalPerformanceInformation.addTo(aggregate.getEnvironmentalPerformanceInformation(), increment.getEnvironmentalPerformanceInformation());
}
IterativeTaskInformation.addTo(aggregate.getIterativeTaskInformation(), increment.getIterativeTaskInformation());
if (increment.getSynchronizationInformation() != null) {
if (aggregate.getSynchronizationInformation() == null) {
aggregate.setSynchronizationInformation(new SynchronizationInformationType());
}
SynchronizationInformation.addTo(aggregate.getSynchronizationInformation(), increment.getSynchronizationInformation());
}
if (increment.getActionsExecutedInformation() != null) {
if (aggregate.getActionsExecutedInformation() == null) {
aggregate.setActionsExecutedInformation(new ActionsExecutedInformationType());
}
ActionsExecutedInformation.addTo(aggregate.getActionsExecutedInformation(), increment.getActionsExecutedInformation());
}
if (increment.getRepositoryPerformanceInformation() != null) {
if (aggregate.getRepositoryPerformanceInformation() == null) {
aggregate.setRepositoryPerformanceInformation(new RepositoryPerformanceInformationType());
}
RepositoryPerformanceInformationUtil.addTo(aggregate.getRepositoryPerformanceInformation(), increment.getRepositoryPerformanceInformation());
}
if (increment.getCachesPerformanceInformation() != null) {
if (aggregate.getCachesPerformanceInformation() == null) {
aggregate.setCachesPerformanceInformation(new CachesPerformanceInformationType());
}
CachePerformanceInformationUtil.addTo(aggregate.getCachesPerformanceInformation(), increment.getCachesPerformanceInformation());
}
}
public static String format(OperationStatsType statistics) {
if (statistics == null) {
return "null";
}
StringBuilder sb = new StringBuilder();
if (statistics.getIterativeTaskInformation() != null) {
sb.append("Iterative task information\n\n")
.append(IterativeTaskInformation.format(statistics.getIterativeTaskInformation()))
.append("\n");
}
if (statistics.getActionsExecutedInformation() != null) {
sb.append("Actions executed\n\n")
.append(ActionsExecutedInformation.format(statistics.getActionsExecutedInformation()))
.append("\n");
}
// if (statistics.getSynchronizationInformation() != null) {
// sb.append("Synchronization information:\n")
// .append(SynchronizationInformation.format(statistics.getSynchronizationInformation()))
// .append("\n");
// }
if (statistics.getEnvironmentalPerformanceInformation() != null) {
sb.append("Environmental performance information\n\n")
.append(EnvironmentalPerformanceInformation.format(statistics.getEnvironmentalPerformanceInformation()))
.append("\n");
}
if (statistics.getRepositoryPerformanceInformation() != null) {
sb.append("Repository performance information\n\n")
.append(RepositoryPerformanceInformationUtil.format(statistics.getRepositoryPerformanceInformation()))
.append("\n");
}
if (statistics.getCachesPerformanceInformation() != null) {
sb.append("Cache performance information\n\n")
.append(CachePerformanceInformationUtil.format(statistics.getCachesPerformanceInformation()))
.append("\n");
}
if (statistics.getWorkBucketManagementPerformanceInformation() != null) {
sb.append("Work bucket management performance information\n\n")
.append(TaskWorkBucketManagementPerformanceInformationUtil.format(statistics.getWorkBucketManagementPerformanceInformation()))
.append("\n");
}
if (statistics.getOperationsPerformanceInformation() != null) {
sb.append("Methods performance information\n\n")
.append(OperationsPerformanceInformationUtil.format(statistics.getOperationsPerformanceInformation()))
.append("\n");
}
return sb.toString();
}
public static long getWallClockTime(IterativeTaskPartItemsProcessingInformationType info) {
return new WallClockTimeComputer(info.getExecution())
.getSummaryTime();
}
public static Double toSeconds(Long time) {
return time != null ? time / 1000.0 : null;
}
}