Skip to content

Commit 424e4aa

Browse files
committed
refactor: optimize bulk update logic (ignoring enabling enables and disabling disables)
1 parent e0d96e8 commit 424e4aa

File tree

5 files changed

+32
-22
lines changed

5 files changed

+32
-22
lines changed

app/operation/admin.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -382,19 +382,21 @@ async def bulk_set_admins_disabled(
382382
for db_admin in db_admins:
383383
await self._ensure_can_change_admin_status(db_admin, current_admin, is_disabled=is_disabled)
384384

385-
for db_admin in db_admins:
385+
admins_to_update = [db_admin for db_admin in db_admins if db_admin.is_disabled != is_disabled]
386+
387+
for db_admin in admins_to_update:
386388
db_admin.is_disabled = is_disabled
387389

388390
await db.commit()
389391

390-
for db_admin in db_admins:
392+
for db_admin in admins_to_update:
391393
modified_admin = AdminDetails.model_validate(db_admin)
392394
asyncio.create_task(notification.modify_admin(modified_admin, current_admin.username))
393395
logger.info(
394396
f'Admin "{db_admin.username}" bulk {"disabled" if is_disabled else "enabled"} by admin "{current_admin.username}"'
395397
)
396398

397-
return self._build_bulk_action_response(db_admins)
399+
return self._build_bulk_action_response(admins_to_update)
398400

399401
async def bulk_reset_admins_usage(
400402
self, db: AsyncSession, bulk_admins: BulkAdminSelection, admin: AdminDetails

app/operation/group.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -199,27 +199,30 @@ async def bulk_set_groups_disabled(
199199
for group_id in bulk_groups.ids:
200200
db_groups.append(await self.get_validated_group(db, group_id))
201201

202-
for db_group in db_groups:
202+
groups_to_update = [db_group for db_group in db_groups if db_group.is_disabled != is_disabled]
203+
204+
for db_group in groups_to_update:
203205
db_group.is_disabled = is_disabled
204206

205207
await db.commit()
206208

207-
for db_group in db_groups:
209+
for db_group in groups_to_update:
208210
await db.refresh(db_group)
209211
await load_group_attrs(db_group)
210212

211-
users = await get_users(
212-
db,
213-
group_ids=[group.id for group in db_groups],
214-
status=[UserStatus.active, UserStatus.on_hold],
215-
)
216-
await sync_users(users)
213+
if groups_to_update:
214+
users = await get_users(
215+
db,
216+
group_ids=[group.id for group in groups_to_update],
217+
status=[UserStatus.active, UserStatus.on_hold],
218+
)
219+
await sync_users(users)
217220

218-
for db_group in db_groups:
221+
for db_group in groups_to_update:
219222
group = GroupResponse.model_validate(db_group)
220223
asyncio.create_task(notification.modify_group(group, admin.username))
221224
logger.info(
222225
f'Group "{db_group.name}" bulk {"disabled" if is_disabled else "enabled"} by admin "{admin.username}"'
223226
)
224227

225-
return self._build_bulk_action_response(db_groups)
228+
return self._build_bulk_action_response(groups_to_update)

app/operation/host.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,16 +167,18 @@ async def bulk_set_hosts_disabled(
167167
for host_id in bulk_hosts.ids:
168168
db_hosts.append(await self.get_validated_host(db, host_id))
169169

170-
for db_host in db_hosts:
170+
hosts_to_update = [db_host for db_host in db_hosts if db_host.is_disabled != is_disabled]
171+
172+
for db_host in hosts_to_update:
171173
db_host.is_disabled = is_disabled
172174

173175
await db.commit()
174176

175-
for db_host in db_hosts:
177+
for db_host in hosts_to_update:
176178
await db.refresh(db_host)
177179
host = BaseHost.model_validate(db_host)
178180
asyncio.create_task(notification.modify_host(host, admin.username))
179181
await host_manager.add_host(db, db_host)
180182
logger.info(f'Host "{db_host.id}" bulk {"disabled" if is_disabled else "enabled"} by admin "{admin.username}"')
181183

182-
return self._build_bulk_action_response(db_hosts)
184+
return self._build_bulk_action_response(hosts_to_update)

app/operation/node.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -954,17 +954,18 @@ async def bulk_set_nodes_status(
954954
status: NodeStatus,
955955
) -> BulkNodesActionResponse:
956956
db_nodes = await self._get_validated_nodes(db, bulk_nodes.ids)
957+
nodes_to_update = [db_node for db_node in db_nodes if db_node.status != status]
957958

958-
for db_node in db_nodes:
959+
for db_node in nodes_to_update:
959960
payload = self._build_node_modify_payload(db_node)
960961
payload.status = status
961962
await self.modify_node(db, node_id=db_node.id, modified_node=payload, admin=admin)
962963

963964
action = "enabled" if status != NodeStatus.disabled else "disabled"
964-
for db_node in db_nodes:
965+
for db_node in nodes_to_update:
965966
logger.info(f'Node "{db_node.name}" bulk {action} by admin "{admin.username}"')
966967

967-
return self._build_bulk_action_response(db_nodes)
968+
return self._build_bulk_action_response(nodes_to_update)
968969

969970
async def bulk_reset_nodes_usage(
970971
self, db: AsyncSession, bulk_nodes: BulkNodeSelection, admin: AdminDetails

app/operation/user_template.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,14 @@ async def bulk_set_user_templates_disabled(
152152
for template_id in bulk_templates.ids:
153153
db_templates.append(await self.get_validated_user_template(db, template_id))
154154

155-
for db_template in db_templates:
155+
templates_to_update = [db_template for db_template in db_templates if db_template.is_disabled != is_disabled]
156+
157+
for db_template in templates_to_update:
156158
db_template.is_disabled = is_disabled
157159

158160
await db.commit()
159161

160-
for db_template in db_templates:
162+
for db_template in templates_to_update:
161163
await db.refresh(db_template)
162164
await load_user_template_attrs(db_template)
163165
user_template = UserTemplateResponse.model_validate(db_template)
@@ -166,4 +168,4 @@ async def bulk_set_user_templates_disabled(
166168
f'User template "{db_template.name}" bulk {"disabled" if is_disabled else "enabled"} by admin "{admin.username}"'
167169
)
168170

169-
return self._build_bulk_action_response(db_templates)
171+
return self._build_bulk_action_response(templates_to_update)

0 commit comments

Comments
 (0)