【发布时间】:2021-09-01 20:47:45
【问题描述】:
我想在活跃员工列表中平均分配警报/通知列表,以确保每个人都有相似的职责。我目前收到要分配的警报列表和活动用户以及每个人当前分配的活动数量。截至目前,我只设法以与 for 循环迭代相同的顺序实现分布,但我想不出一种方法来均匀地做到这一点。
例如:
- e1 有 1 个警报
- e2 有 1 个警报
- e3 有 3 个警报
我想将任务分配给 e1 和 e2,直到它们达到 3,然后循环可以正常继续,直到它用完警报。
Employee 是一个包含 {"id": "oid", "alerts": int} 的字典 unassignedAlerts 只是 ObjectIds 的列表
代码:
for alert in unassignedAlerts:
for employee in activeEmployees:
if employee ["alerts"] > maxAlerts:
maxAlerts = employee["alerts"]
elif employee ["_id"] in activeUsers:
operations.append(
UpdateOne({"_id": alert}, {"$set": {"employeeResponsible": employee["_id"], "status": "assigned"}})
)
employee["alerts"] += 1
感谢任何想法。
【问题讨论】:
标签: python arrays mongodb loops load-balancing