Search in sources :

Example 1 with TaskContext

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;
}
Also used : TaskContext(org.kie.api.task.TaskContext) Arrays(java.util.Arrays) Logger(org.slf4j.Logger) Collection(java.util.Collection) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) ClassUtil(org.jbpm.services.task.utils.ClassUtil) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) UserTaskLoad(org.jbpm.services.task.assignment.UserTaskLoad) ArrayList(java.util.ArrayList) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Map(java.util.Map) CacheBuilder(com.google.common.cache.CacheBuilder) Cache(com.google.common.cache.Cache) TaskPersistenceContext(org.kie.internal.task.api.TaskPersistenceContext) User(org.kie.api.task.model.User) ArrayList(java.util.ArrayList) UserTaskLoad(org.jbpm.services.task.assignment.UserTaskLoad)

Example 2 with TaskContext

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));
}
Also used : TaskContext(org.kie.api.task.TaskContext) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List) TaskPersistenceContext(org.kie.internal.task.api.TaskPersistenceContext)

Example 3 with TaskContext

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;
}
Also used : TaskContext(org.kie.api.task.TaskContext) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List) TaskPersistenceContext(org.kie.internal.task.api.TaskPersistenceContext)

Example 4 with TaskContext

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;
}
Also used : TaskContext(org.kie.api.task.TaskContext) Logger(org.slf4j.Logger) Collection(java.util.Collection) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) ClassUtil(org.jbpm.services.task.utils.ClassUtil) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) UserTaskLoad(org.jbpm.services.task.assignment.UserTaskLoad) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) TaskPersistenceContext(org.kie.internal.task.api.TaskPersistenceContext) User(org.kie.api.task.model.User) TaskContext(org.kie.api.task.TaskContext) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) TaskPersistenceContext(org.kie.internal.task.api.TaskPersistenceContext) UserTaskLoad(org.jbpm.services.task.assignment.UserTaskLoad) ArrayList(java.util.ArrayList) List(java.util.List)

Example 5 with TaskContext

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;
}
Also used : TaskContext(org.kie.api.task.TaskContext) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) Task(org.kie.api.task.model.Task) Assignment(org.kie.internal.task.api.assignment.Assignment) EnvironmentName(org.kie.api.runtime.EnvironmentName) UserInfo(org.kie.internal.task.api.UserInfo) Collection(java.util.Collection) LoggerFactory(org.slf4j.LoggerFactory) LoadCalculator(org.jbpm.services.task.assignment.LoadCalculator) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) UserTaskLoad(org.jbpm.services.task.assignment.UserTaskLoad) ArrayList(java.util.ArrayList) OrganizationalEntity(org.kie.api.task.model.OrganizationalEntity) List(java.util.List) Group(org.kie.api.task.model.Group) AssignmentStrategy(org.kie.internal.task.api.assignment.AssignmentStrategy) InternalPeopleAssignments(org.kie.internal.task.api.model.InternalPeopleAssignments) User(org.kie.api.task.model.User) Group(org.kie.api.task.model.Group) User(org.kie.api.task.model.User) OrganizationalEntity(org.kie.api.task.model.OrganizationalEntity) UserInfo(org.kie.internal.task.api.UserInfo) Assignment(org.kie.internal.task.api.assignment.Assignment) UserTaskLoad(org.jbpm.services.task.assignment.UserTaskLoad)

Aggregations

ArrayList (java.util.ArrayList)8 List (java.util.List)8 TaskContext (org.kie.api.task.TaskContext)8 HashMap (java.util.HashMap)6 TaskPersistenceContext (org.kie.internal.task.api.TaskPersistenceContext)6 User (org.kie.api.task.model.User)5 Logger (org.slf4j.Logger)5 LoggerFactory (org.slf4j.LoggerFactory)5 Map (java.util.Map)4 Collectors (java.util.stream.Collectors)4 UserTaskLoad (org.jbpm.services.task.assignment.UserTaskLoad)4 Collection (java.util.Collection)3 Iterator (java.util.Iterator)3 Function (java.util.function.Function)3 ClassUtil (org.jbpm.services.task.utils.ClassUtil)3 EnvironmentName (org.kie.api.runtime.EnvironmentName)3 Group (org.kie.api.task.model.Group)3 OrganizationalEntity (org.kie.api.task.model.OrganizationalEntity)3 Task (org.kie.api.task.model.Task)3 UserInfo (org.kie.internal.task.api.UserInfo)3