use of org.kie.api.task.TaskContext in project jbpm by kiegroup.
the class TotalCompletionTimeLoadCalculator method getUserTaskLoads.
@Override
public Collection<UserTaskLoad> getUserTaskLoads(List<User> users, TaskContext context) {
Collection<UserTaskLoad> loads = new ArrayList<>();
List<TaskInfo> usersTasks = getUserActiveTaskLists(users, context);
// If there are no user tasks then everyone gets a score of 0 (zero)
if (usersTasks == null || usersTasks.isEmpty()) {
users.forEach(u -> {
loads.add(new UserTaskLoad(IDENTIFIER, u, new Double(0)));
});
} else {
users.forEach(u -> {
Double loadForUser = new Double(0.0);
//
// Get the list of tasks for the user
//
List<TaskInfo> tasksForUser = usersTasks.stream().filter(ut -> ut.getOwnerId().equals(u.getId())).collect(Collectors.toList());
//
for (TaskInfo ti : tasksForUser) {
loadForUser += getTaskDuration(ti, context) * ti.getCount();
}
UserTaskLoad load = new UserTaskLoad(IDENTIFIER, u, loadForUser);
logger.debug("User load: {}", load);
loads.add(load);
});
}
return loads;
}
use of org.kie.api.task.TaskContext in project jbpm by kiegroup.
the class TotalCompletionTimeLoadCalculator method getUserActiveTaskLists.
/**
* Retrieves a list of tasks that the users are currently assigned to, and which
* are waiting to be worked on or are being worked on
* @param users The list of users that we are interested in
* @param context The TaskContext which is associated with the new task
* @return A list of TaskInfo objects
*/
private synchronized List<TaskInfo> getUserActiveTaskLists(List<User> users, TaskContext context) {
TaskPersistenceContext taskContext = ((org.jbpm.services.task.commands.TaskContext) context).getPersistenceContext();
Map<String, Object> params = new HashMap<>();
params.put("owners", users);
return taskContext.queryStringWithParametersInTransaction(TASK_LIST_QUERY, params, ClassUtil.<List<TaskInfo>>castClass(List.class));
}
use of org.kie.api.task.TaskContext in project jbpm by kiegroup.
the class TotalCompletionTimeLoadCalculator method calculateAverageDuration.
/**
* Calculates the average duration for a target task, using a query
* against the BAMTaskSummary table
* @param context Used to retrieve a PersistenceContext
* @param processId The identifier for the process definition containing the target task
* @param deploymentId The identifier for the deployment which contains the target task
* @param name The name of the target task
* @return
*/
private Double calculateAverageDuration(TaskContext context, String processId, String deploymentId, String name) {
Double avgDur = new Double(1);
TaskPersistenceContext taskContext = ((org.jbpm.services.task.commands.TaskContext) context).getPersistenceContext();
Map<String, Object> params = new HashMap<>();
params.put("procid", processId);
params.put("depid", deploymentId);
params.put("taskname", name);
List<TaskAverageDuration> durations = taskContext.queryStringWithParametersInTransaction(TASK_AVG_DURATION, params, ClassUtil.<List<TaskAverageDuration>>castClass(List.class));
if (durations != null && !durations.isEmpty()) {
avgDur = durations.get(0).getAverageDuration();
logger.debug("Retrieved duration is {}", avgDur);
}
return avgDur;
}
use of org.kie.api.task.TaskContext in project jbpm by kiegroup.
the class TaskCountLoadCalculator method getUserTaskLoads.
@Override
public Collection<UserTaskLoad> getUserTaskLoads(List<User> users, TaskContext context) {
Collection<UserTaskLoad> userTaskLoads = new ArrayList<>();
List<String> userIds = users.stream().map(user -> {
return user.getId();
}).collect(Collectors.toList());
TaskPersistenceContext persistenceContext = ((org.jbpm.services.task.commands.TaskContext) context).getPersistenceContext();
Map<String, Object> params = new HashMap<>();
params.put("owners", userIds);
logger.debug("DB query to be used for finding assignments :: '{}'", getMultiUserQuery());
List<AssignmentImpl> assignments = persistenceContext.queryStringWithParametersInTransaction(getMultiUserQuery(), params, ClassUtil.<List<AssignmentImpl>>castClass(List.class));
Map<String, AssignmentImpl> assignmentMap = assignments.stream().collect(Collectors.toMap(assignKey, (assign) -> assign));
if (assignments != null && !assignments.isEmpty()) {
users.forEach(usr -> {
String uid = usr.getId();
if (assignmentMap.containsKey(uid)) {
Long loadValue = assignmentMap.get(uid).getCurrentlyAssigned();
userTaskLoads.add(new UserTaskLoad(getIdentifier(), usr, new Double(loadValue != null ? loadValue : 0)));
} else {
userTaskLoads.add(new UserTaskLoad(getIdentifier(), usr, new Double(0)));
}
});
} else {
users.forEach(u -> {
userTaskLoads.add(new UserTaskLoad(getIdentifier(), u, new Double(0)));
});
}
return userTaskLoads;
}
use of org.kie.api.task.TaskContext in project jbpm by kiegroup.
the class LoadBalanceAssignmentStrategy method apply.
@Override
public Assignment apply(Task task, TaskContext taskContext, String excludedUser) {
UserInfo userInfo = (UserInfo) ((org.jbpm.services.task.commands.TaskContext) taskContext).get(EnvironmentName.TASK_USER_INFO);
List<OrganizationalEntity> excluded = (getExcludedEntities(task, userInfo));
// Get the the users from the task's the potential owners, making sure that excluded users are not included
List<OrganizationalEntity> potentialOwners = task.getPeopleAssignments().getPotentialOwners().stream().filter(oe -> oe instanceof User && !excluded.contains(oe) && !oe.getId().equals(excludedUser)).collect(Collectors.toList());
// Get the users belonging to groups that are potential owners
task.getPeopleAssignments().getPotentialOwners().stream().filter(oe -> oe instanceof Group).forEach(oe -> {
Iterator<OrganizationalEntity> groupUsers = userInfo.getMembersForGroup((Group) oe);
if (groupUsers != null) {
groupUsers.forEachRemaining(user -> {
if (user != null && !excluded.contains(user) && !potentialOwners.contains(user) && !user.getId().equals(excludedUser)) {
potentialOwners.add(user);
}
});
}
});
logger.debug("Asking the load calculator [{}] for task loads for the users {}", calculator.getIdentifier(), potentialOwners);
List<User> users = potentialOwners.stream().map(entityToUser).collect(Collectors.toList());
Collection<UserTaskLoad> loads = calculator.getUserTaskLoads(users, taskContext);
UserTaskLoad lightestLoad = loads.stream().min(UserTaskLoad::compareTo).orElse(null);
return lightestLoad != null ? new Assignment(lightestLoad.getUser().getId()) : null;
}
Aggregations