Skip to content

Commit

Permalink
MAINT: code review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
anish-mudaraddi committed Feb 7, 2024
1 parent 5f868db commit 02d3222
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 26 deletions.
18 changes: 10 additions & 8 deletions MonitoringTools/usr/local/bin/limits_to_influx.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ def get_limit_prop_string(limit_details):
:return: a data string of scraped info
"""
# all limit properties are integers so add 'i' for each value
return ",".join([f"{limit}={val}i" for limit, val in limit_details.items()])
limit_strings = []
for limit, val in limit_details.items():
limit_strings.append(f"{limit}={val}i")
return ",".join(limit_strings)


def extract_limits(limits_dict) -> Dict:
Expand Down Expand Up @@ -72,7 +75,7 @@ def extract_limits(limits_dict) -> Dict:
return parsed_limits


def get_limits_for_project(instance, project_id) -> Dict:
def get_limits_for_project(instance: str, project_id) -> Dict:
"""
Get limits for a project. This is currently using openstack-cli
This will be rewritten to instead use openstacksdk
Expand All @@ -95,7 +98,7 @@ def is_valid_project(project: Project) -> bool:
:return: boolean, True if project should be accounted for in limits
"""
invalid_strings = ["_rally", "844"]
return all(s not in project["name"] for s in invalid_strings)
return all(string not in project["name"] for string in invalid_strings)


def get_all_limits(instance: str) -> str:
Expand All @@ -105,11 +108,10 @@ def get_all_limits(instance: str) -> str:
:return: A data string of scraped info
"""
conn = openstack.connect(cloud=instance)
limit_details = {
project["name"]: get_limits_for_project(instance, project["id"])
for project in conn.list_projects()
if is_valid_project(project)
}
limit_details = {}
for project in conn.list_projects():
if is_valid_project(project):
limit_details[project["name"]] = get_limits_for_project(instance, project["id"])
return convert_to_data_string(instance, limit_details)


Expand Down
35 changes: 17 additions & 18 deletions MonitoringTools/usr/local/bin/slottifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from send_metric_utils import parse_args, run_scrape


def get_hv_info(hypervisor: Dict, aggregate_info, service_info) -> Dict:
def get_hv_info(hypervisor: Dict, aggregate_info: Dict, service_info: Dict) -> Dict:
"""
Helper function to get hv information on cores/memory available
:param hypervisor: a dictionary holding info on hypervisor
Expand Down Expand Up @@ -39,7 +39,7 @@ def get_hv_info(hypervisor: Dict, aggregate_info, service_info) -> Dict:
return hv_info


def get_flavor_requirements(flavor) -> Dict:
def get_flavor_requirements(flavor: Dict) -> Dict:
"""
Helper function to get flavor memory/ram/gpu requirements for a VM of that type to be built on a hv
:param flavor: flavor to get requirements from
Expand Down Expand Up @@ -112,7 +112,7 @@ def convert_to_data_string(instance: str, slots_dict: Dict) -> str:
return data_string


def calculate_slots_on_hv(flavor_name, flavor_reqs, hv_info) -> SlottifierEntry:
def calculate_slots_on_hv(flavor_name: str, flavor_reqs: Dict, hv_info: Dict) -> SlottifierEntry:
"""
Helper function that calculates available slots for a flavor on a given hypervisor
:param flavor_name: name of flavor
Expand Down Expand Up @@ -202,7 +202,7 @@ def get_openstack_resources(instance: str) -> Dict:


def get_all_hv_info_for_aggregate(
aggregate, all_compute_services, all_hypervisors
aggregate: Dict, all_compute_services: List, all_hypervisors: List
) -> List:
"""
helper function to get all useful info from hypervisors belonging to a given aggregate
Expand All @@ -215,28 +215,27 @@ def get_all_hv_info_for_aggregate(

valid_hvs = []
for host in aggregate["hosts"]:
host_compute_service = next(
(cs for cs in all_compute_services if cs["host"] == host), None
)

host_compute_service = None
for cs in all_compute_services:
if cs["host"] == host:
host_compute_service = cs

if not host_compute_service:
continue

hv = next(
(
hv
for hv in all_hypervisors
if host_compute_service["host"] == hv["name"]
),
None,
)
if not hv:
hv_obj = None
for hv in all_hypervisors:
if host_compute_service["host"] == hv["name"]:
hv_obj = hv
if not hv_obj:
continue

valid_hvs.append(get_hv_info(hv, aggregate, host_compute_service))
valid_hvs.append(get_hv_info(hv_obj, aggregate, host_compute_service))
return valid_hvs


def update_slots(flavors, host_info_list, slots_dict) -> Dict:
def update_slots(flavors: List, host_info_list: List, slots_dict: Dict) -> Dict:
"""
update total slots by calculating slots available for a set of flavors on a set of hosts
:param flavors: a list of flavors
Expand Down

0 comments on commit 02d3222

Please sign in to comment.