| OLD | NEW |
| 1 # Copyright 2016 The LUCI Authors. All rights reserved. | 1 # Copyright 2016 The LUCI Authors. All rights reserved. |
| 2 # Use of this source code is governed under the Apache License, Version 2.0 | 2 # Use of this source code is governed under the Apache License, Version 2.0 |
| 3 # that can be found in the LICENSE file. | 3 # that can be found in the LICENSE file. |
| 4 | 4 |
| 5 """Utilities for operating on instance group managers.""" | 5 """Utilities for operating on instance group managers.""" |
| 6 | 6 |
| 7 import collections |
| 7 import logging | 8 import logging |
| 8 | 9 |
| 9 from google.appengine.ext import ndb | 10 from google.appengine.ext import ndb |
| 10 | 11 |
| 11 from components import gce | 12 from components import gce |
| 12 from components import net | 13 from components import net |
| 13 from components import utils | 14 from components import utils |
| 14 | 15 |
| 15 import instance_templates | 16 import instance_templates |
| 16 import models | 17 import models |
| (...skipping 366 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 383 '/internal/queues/resize-instance-group', | 384 '/internal/queues/resize-instance-group', |
| 384 'resize-instance-group', | 385 'resize-instance-group', |
| 385 params={ | 386 params={ |
| 386 'key': instance_group_manager_key.urlsafe(), | 387 'key': instance_group_manager_key.urlsafe(), |
| 387 }, | 388 }, |
| 388 ): | 389 ): |
| 389 logging.warning( | 390 logging.warning( |
| 390 'Failed to enqueue task for InstanceGroupManager: %s', | 391 'Failed to enqueue task for InstanceGroupManager: %s', |
| 391 instance_group_manager_key, | 392 instance_group_manager_key, |
| 392 ) | 393 ) |
| 394 |
| 395 |
| 396 def count_instances(): |
| 397 """Counts the number of instances owned by each instance template. |
| 398 |
| 399 Returns: |
| 400 A dict mapping instance template name to count of instances. |
| 401 """ |
| 402 # Aggregate the number of instances owned by each instance group manager |
| 403 # created for each instance template. |
| 404 totals = collections.defaultdict(int) |
| 405 for instance_group_manager in models.InstanceGroupManager.query(): |
| 406 instance_template_name = instance_group_manager.key.parent().parent().id() |
| 407 totals[instance_template_name] += len(instance_group_manager.instances) |
| 408 return totals |
| OLD | NEW |