Skip to content

Commit

Permalink
Merge pull request #17 from stackhpc/mg-fixes
Browse files Browse the repository at this point in the history
Some fixes for CLI use
  • Loading branch information
markgoddard authored Aug 1, 2023
2 parents 46d912c + c8dfa58 commit 3da671f
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 93 deletions.
27 changes: 0 additions & 27 deletions os_capacity/commands/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

from cliff.lister import Lister

from os_capacity.data import metrics
from os_capacity import prometheus
from os_capacity import utils

Expand Down Expand Up @@ -55,32 +54,6 @@ class ListResourcesGroups(Lister):

def take_action(self, parsed_args):
groups = utils.group_providers_by_type_with_capacity(self.app)
groups = list(groups) # convert iterator

metrics_to_send = []
for group in groups:
flavors = group[4].replace(", ", "-")
if not flavors:
# skip empty hosts
continue
resources = group[0]
total = group[1]
used = group[2]
free = group[3]
metrics_to_send.append(metrics.Metric(
name="resources.total", value=total,
value_meta={"flavor_resources": resources},
dimensions={"flavor": flavors}))
metrics_to_send.append(metrics.Metric(
name="resources.used", value=used,
value_meta={"flavor_resources": resources},
dimensions={"flavor": flavors}))
metrics_to_send.append(metrics.Metric(
name="resources.free", value=free,
value_meta={"flavor_resources": resources},
dimensions={"flavor": flavors}))
metrics.send_metrics(self.app.monitoring_client, metrics_to_send)

return (
('Resource Class Groups', 'Total', 'Used', 'Free', 'Flavors'),
groups)
Expand Down
41 changes: 0 additions & 41 deletions os_capacity/data/metrics.py

This file was deleted.

5 changes: 4 additions & 1 deletion os_capacity/data/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ def _parse_created(raw_created):

def get(compute_client, uuid):
url = "/servers/%s" % uuid
raw_server = compute_client.get(url).json()['server']
response = compute_client.get(url)
if not response.ok:
return None
raw_server = response.json()['server']
return Server(
uuid=raw_server['id'],
name=raw_server['name'],
Expand Down
4 changes: 2 additions & 2 deletions os_capacity/data/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@


def get_all(identity_client):
response = identity_client.get("/v3/users").json()
response = identity_client.get("/users").json()
raw_users = response['users']
return {u['id']: u['name'] for u in raw_users}


def get_all_projects(identity_client):
response = identity_client.get("/v3/projects").json()
response = identity_client.get("/projects").json()
raw_projects = response['projects']
return {u['id']: u['name'] for u in raw_projects}
29 changes: 7 additions & 22 deletions os_capacity/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import os

from os_capacity.data import flavors
from os_capacity.data import metrics
from os_capacity.data import resource_provider
from os_capacity.data import server as server_data
from os_capacity.data import users
Expand Down Expand Up @@ -144,13 +143,14 @@ def get_allocations_with_server_info(app, flat_usage=True, get_names=False):
usage = ", ".join(usage_amounts)

server = server_data.get(app.compute_client, allocation.consumer_uuid)
delta = now - server.created
days_running = delta.days + 1
if server:
delta = now - server.created
days_running = delta.days + 1

allocation_tuples.append(AllocationList(
rp_name, allocation.consumer_uuid, usage,
server.flavor_id, days_running, server.project_id,
server.user_id))
allocation_tuples.append(AllocationList(
rp_name, allocation.consumer_uuid, usage,
server.flavor_id, days_running, server.project_id,
server.user_id))

allocation_tuples.sort(key=lambda x: (x.project_id, x.user_id,
x.days * -1, x.flavor_id))
Expand Down Expand Up @@ -203,7 +203,6 @@ def get_key(allocation):
all_users = users.get_all(app.identity_client)
all_projects = users.get_all_projects(app.identity_client)

metrics_to_send = []
summary_tuples = []
for key, group in grouped_allocations.items():
grouped_usage = collections.defaultdict(int)
Expand Down Expand Up @@ -249,21 +248,7 @@ def get_key(allocation):
value_meta = {'usage_summary': usage}
dimensions['version'] = '2.0'

metrics_to_send.append(metrics.Metric(
name="usage.%s.count" % group_by,
value=grouped_usage['Count'],
value_meta=value_meta,
dimensions=dimensions))
metrics_to_send.append(metrics.Metric(
name="usage.%s.days.count" % group_by,
value=grouped_usage_days['Count'],
value_meta=value_meta,
dimensions=dimensions))

# Sort my largest current usage first
summary_tuples.sort(key=lambda x: x[1], reverse=True)

if metrics_to_send:
metrics.send_metrics(app.monitoring_client, metrics_to_send)

return summary_tuples

0 comments on commit 3da671f

Please sign in to comment.