-
Notifications
You must be signed in to change notification settings - Fork 188
/
TaskTypeUtil.java
152 lines (131 loc) · 5.99 KB
/
TaskTypeUtil.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
/*
* Copyright (c) 2010-2018 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;
import com.evolveum.midpoint.prism.PrismContext;
import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.schema.statistics.ActionsExecutedInformation;
import com.evolveum.midpoint.schema.statistics.IterativeTaskInformation;
import com.evolveum.midpoint.schema.statistics.SynchronizationInformation;
import com.evolveum.midpoint.xml.ns._public.common.common_3.*;
import org.jetbrains.annotations.NotNull;
import java.util.*;
import java.util.stream.Stream;
/**
* @author mederly
*/
public class TaskTypeUtil {
/**
* Returns a stream of the task and all of its subtasks.
*/
@NotNull
public static Stream<TaskType> getAllTasksStream(TaskType root) {
return Stream.concat(Stream.of(root),
getResolvedSubtasks(root).stream().flatMap(TaskTypeUtil::getAllTasksStream));
}
public static List<TaskType> getResolvedSubtasks(TaskType parent) {
List<TaskType> rv = new ArrayList<>();
for (ObjectReferenceType childRef : parent.getSubtaskRef()) {
//noinspection unchecked
PrismObject<TaskType> child = childRef.getObject();
if (child != null) {
rv.add(child.asObjectable());
} else {
throw new IllegalStateException("Unresolved subtaskRef in " + parent + ": " + childRef);
}
}
return rv;
}
public static void addSubtask(TaskType parent, TaskType child, PrismContext prismContext) {
parent.getSubtaskRef().add(ObjectTypeUtil.createObjectRefWithFullObject(child, prismContext));
}
//moved from GUI
public static boolean isCoordinator(TaskType task) {
return getKind(task) == TaskKindType.COORDINATOR;
}
public static boolean isPartitionedMaster(TaskType task) {
return getKind(task) == TaskKindType.PARTITIONED_MASTER;
}
@NotNull
public static TaskKindType getKind(TaskType task) {
if (task.getWorkManagement() != null && task.getWorkManagement().getTaskKind() != null) {
return task.getWorkManagement().getTaskKind();
} else {
return TaskKindType.STANDALONE;
}
}
public static int getObjectsProcessed(TaskType task) {
OperationStatsType stats = task.getOperationStats();
if (stats == null) {
return 0;
}
IterativeTaskInformationType iterativeStats = stats.getIterativeTaskInformation();
if (iterativeStats == null) {
return 0;
}
return iterativeStats.getTotalSuccessCount() + iterativeStats.getTotalFailureCount();
}
public static int getObjectsProcessedFailures(TaskType task, PrismContext prismContext) {
OperationStatsType stats = getAggregatedOperationStats(task, prismContext);
if (stats == null) {
return 0;
}
IterativeTaskInformationType iterativeStats = stats.getIterativeTaskInformation();
if (iterativeStats == null) {
return 0;
}
return iterativeStats.getTotalFailureCount();
}
public static OperationStatsType getAggregatedOperationStats(TaskType task, PrismContext prismContext) {
if (!isPartitionedMaster(task) && !isWorkStateHolder(task)) {
return task.getOperationStats();
}
IterativeTaskInformationType iterativeTaskInformation = new IterativeTaskInformationType();
SynchronizationInformationType synchronizationInformation = new SynchronizationInformationType();
ActionsExecutedInformationType actionsExecutedInformation = new ActionsExecutedInformationType();
Stream<TaskType> subTasks = TaskTypeUtil.getAllTasksStream(task);
subTasks.forEach(subTask -> {
OperationStatsType operationStatsType = subTask.getOperationStats();
if (operationStatsType != null) {
IterativeTaskInformation.addTo(iterativeTaskInformation, operationStatsType.getIterativeTaskInformation(), true);
SynchronizationInformation.addTo(synchronizationInformation, operationStatsType.getSynchronizationInformation());
ActionsExecutedInformation.addTo(actionsExecutedInformation, operationStatsType.getActionsExecutedInformation());
}
});
return new OperationStatsType(prismContext)
.iterativeTaskInformation(iterativeTaskInformation)
.synchronizationInformation(synchronizationInformation)
.actionsExecutedInformation(actionsExecutedInformation);
}
public static TaskType findChild(TaskType parent, String childOid) {
for (TaskType subtask : getResolvedSubtasks(parent)) {
if (childOid.equals(subtask.getOid())) {
return subtask;
}
}
return null;
}
public static boolean isWorkStateHolder(TaskType taskType) {
return (isCoordinator(taskType) || hasBuckets(taskType)) && !isCoordinatedWorker(taskType);
}
private static boolean hasBuckets(TaskType taskType) {
if (taskType.getWorkState() == null) {
return false;
}
if (taskType.getWorkState().getNumberOfBuckets() != null && taskType.getWorkState().getNumberOfBuckets() > 1) {
return true;
}
List<WorkBucketType> buckets = taskType.getWorkState().getBucket();
if (buckets.size() > 1) {
return true;
} else {
return buckets.size() == 1 && buckets.get(0).getContent() != null;
}
}
private static boolean isCoordinatedWorker(TaskType taskType) {
return taskType.getWorkManagement() != null && TaskKindType.WORKER == taskType.getWorkManagement().getTaskKind();
}
}