Search in sources :

Example 1 with UserTaskLoad

use of org.jbpm.services.task.assignment.UserTaskLoad 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 UserTaskLoad

use of org.jbpm.services.task.assignment.UserTaskLoad in project jbpm by kiegroup.

the class TotalCompletionTimeLoadCalculator method getUserTaskLoad.

@Override
public UserTaskLoad getUserTaskLoad(User user, TaskContext context) {
    UserTaskLoad load = new UserTaskLoad(IDENTIFIER, user);
    List<User> users = Arrays.asList(user);
    List<TaskInfo> userTasks = getUserActiveTaskLists(users, context);
    if (userTasks == null || userTasks.isEmpty()) {
        load.setCalculatedLoad(new Double(0));
    } else {
        Double loadForUser = new Double(0.0);
        for (TaskInfo ti : userTasks) {
            loadForUser += getTaskDuration(ti, context);
        }
        load.setCalculatedLoad(loadForUser);
    }
    return load;
}
Also used : User(org.kie.api.task.model.User) UserTaskLoad(org.jbpm.services.task.assignment.UserTaskLoad)

Example 3 with UserTaskLoad

use of org.jbpm.services.task.assignment.UserTaskLoad 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 4 with UserTaskLoad

use of org.jbpm.services.task.assignment.UserTaskLoad 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)

Example 5 with UserTaskLoad

use of org.jbpm.services.task.assignment.UserTaskLoad in project jbpm by kiegroup.

the class TaskCountLoadCalculator method getUserTaskLoad.

@Override
public UserTaskLoad getUserTaskLoad(User user, TaskContext context) {
    UserTaskLoad load = new UserTaskLoad(getIdentifier(), user);
    TaskPersistenceContext persistenceContext = ((org.jbpm.services.task.commands.TaskContext) context).getPersistenceContext();
    Map<String, Object> params = new HashMap<>();
    params.put("owner", user.getId());
    logger.debug("DB query to be used for finding assignments :: '{}'", getSingleUserQuery());
    List<Integer> assignmentCounts = persistenceContext.queryStringWithParametersInTransaction(getSingleUserQuery(), params, ClassUtil.<List<Integer>>castClass(List.class));
    if (assignmentCounts != null && !assignmentCounts.isEmpty()) {
        load.setCalculatedLoad(new Double(assignmentCounts.get(0)));
    } else {
        load.setCalculatedLoad(new Double(0));
    }
    return load;
}
Also used : TaskContext(org.kie.api.task.TaskContext) HashMap(java.util.HashMap) TaskPersistenceContext(org.kie.internal.task.api.TaskPersistenceContext) UserTaskLoad(org.jbpm.services.task.assignment.UserTaskLoad) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

UserTaskLoad (org.jbpm.services.task.assignment.UserTaskLoad)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 TaskContext (org.kie.api.task.TaskContext)4 User (org.kie.api.task.model.User)4 Collection (java.util.Collection)3 HashMap (java.util.HashMap)3 Function (java.util.function.Function)3 Collectors (java.util.stream.Collectors)3 TaskPersistenceContext (org.kie.internal.task.api.TaskPersistenceContext)3 Logger (org.slf4j.Logger)3 LoggerFactory (org.slf4j.LoggerFactory)3 Map (java.util.Map)2 ClassUtil (org.jbpm.services.task.utils.ClassUtil)2 Cache (com.google.common.cache.Cache)1 CacheBuilder (com.google.common.cache.CacheBuilder)1 Arrays (java.util.Arrays)1 Iterator (java.util.Iterator)1 TimeUnit (java.util.concurrent.TimeUnit)1 LoadCalculator (org.jbpm.services.task.assignment.LoadCalculator)1