Skip to content

Commit

Permalink
fix: use metrics_data in process, not filepath to process metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
bdromard committed Oct 1, 2024
1 parent 47665c7 commit 33e4a84
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 27 deletions.
29 changes: 8 additions & 21 deletions boagent/api/process.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,19 @@
from json import load
from collections import defaultdict
from .exceptions import InvalidPIDException


class Process:
def __init__(self, metrics_file_path, pid):
self.metrics_file_path = metrics_file_path
def __init__(self, metrics_data, pid):
self.metrics_data = metrics_data
self.validate_pid(pid)
self._pid = pid
self.process_info = self.get_process_info()

@property
def processed_metrics(self):
"""The metrics in JSON format parsed from the metrics file path."""

with open(self.metrics_file_path, "r") as metrics_data_file:
processed_metrics = load(metrics_data_file)
return processed_metrics

def validate_pid(self, value):

timestamps = [
timestamp
for timestamp in self.processed_metrics["raw_data"]["power_data"][
"raw_data"
]
for timestamp in self.metrics_data["raw_data"]["power_data"]["raw_data"]
]
consumers = [timestamp["consumers"] for timestamp in timestamps]
pids = set([process["pid"] for consumer in consumers for process in consumer])
Expand All @@ -46,9 +35,7 @@ def get_process_info(self):

timestamps = [
timestamp
for timestamp in self.processed_metrics["raw_data"]["power_data"][
"raw_data"
]
for timestamp in self.metrics_data["raw_data"]["power_data"]["raw_data"]
]
consumers = [timestamp["consumers"] for timestamp in timestamps]
process_info = [
Expand All @@ -71,7 +58,7 @@ def process_exe(self):

def get_total_ram_in_bytes(self):

ram_data = self.processed_metrics["raw_data"]["hardware_data"]["rams"]
ram_data = self.metrics_data["raw_data"]["hardware_data"]["rams"]
total_ram_in_bytes = (
sum(ram_unit["capacity"] for ram_unit in ram_data) * 1073741824
)
Expand All @@ -81,12 +68,12 @@ def get_total_ram_in_bytes(self):
def get_disk_usage_in_bytes(self):

disk_total_bytes = int(
self.processed_metrics["raw_data"]["power_data"]["raw_data"][1]["host"][
self.metrics_data["raw_data"]["power_data"]["raw_data"][1]["host"][
"components"
]["disks"][0]["disk_total_bytes"]
)
disk_available_bytes = int(
self.processed_metrics["raw_data"]["power_data"]["raw_data"][1]["host"][
self.metrics_data["raw_data"]["power_data"]["raw_data"][1]["host"][
"components"
]["disks"][0]["disk_available_bytes"]
)
Expand Down Expand Up @@ -135,7 +122,7 @@ def storage_shares(self):
def get_component_embedded_impact_shares(self, queried_component, component_shares):

component = f"{queried_component}-1"
component_impacts_data = self.processed_metrics["raw_data"]["boaviztapi_data"][
component_impacts_data = self.metrics_data["raw_data"]["boaviztapi_data"][
"verbose"
][component]["impacts"]
component_embedded_impact_shares = list()
Expand Down
4 changes: 2 additions & 2 deletions tests/api/test_api_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ def test_read_query_with_measure_power_and_fetch_hardware_verbose(

@patch("boagent.api.api.get_metrics")
def test_get_process_embedded_impacts_with_success(self, mocked_get_metrics):
mocked_get_metrics.return_value = mock_get_metrics_verbose
mocked_get_metrics.return_value = self.get_metrics_verbose
params = {
"process_id": 3099,
"start_time": "1717500637.2979465",
Expand All @@ -209,7 +209,7 @@ def test_get_process_embedded_impacts_with_error_if_pid_not_found_in_metrics_dat
self, mocked_get_metrics
):

mocked_get_metrics.return_value = mock_get_metrics_verbose
mocked_get_metrics.return_value = self.get_metrics_verbose
params = {
"process_id": 1234,
"start_time": "1717500637.2979465",
Expand Down
11 changes: 7 additions & 4 deletions tests/api/test_api_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,13 @@ def setUp(self):
with open(mock_boaviztapi_response_not_verbose, "r") as boaviztapi_data:
self.boaviztapi_data = json.load(boaviztapi_data)

with open(mock_get_metrics_verbose) as get_metrics_verbose:
with open(mock_get_metrics_verbose, "r") as get_metrics_verbose:
self.get_metrics_verbose = json.load(get_metrics_verbose)

self.process = Process(mock_get_metrics_verbose, self.pid)
with open(mock_get_metrics_verbose_no_hdd, "r") as get_metrics_verbose_no_hdd:
self.get_metrics_verbose_no_hdd = json.load(get_metrics_verbose_no_hdd)

self.process = Process(self.get_metrics_verbose, self.pid)

@patch("boagent.api.api.query_machine_impact_data")
def test_get_total_embedded_impacts_for_host(
Expand Down Expand Up @@ -87,7 +90,7 @@ def test_validate_pid_with_error_if_process_id_not_in_metrics(self):
)

with self.assertRaises(InvalidPIDException) as context_manager:
self.process = Process(mock_get_metrics_verbose, 1234)
self.process = Process(self.get_metrics_verbose, 1234)

self.assertEqual(context_manager.exception.message, expected_error_message)

Expand Down Expand Up @@ -260,7 +263,7 @@ def test_get_all_components_embedded_impact_values(self):
def test_get_components_embedded_impact_values_with_hdd_absent_from_get_metrics(
self,
):
self.process = Process(mock_get_metrics_verbose_no_hdd, self.pid)
self.process = Process(self.get_metrics_verbose_no_hdd, self.pid)
process_embedded_impacts = self.process.embedded_impact_values
self.assertIn("process_cpu_embedded_impact_values", process_embedded_impacts)
self.assertIn("process_ram_embedded_impact_values", process_embedded_impacts)
Expand Down

0 comments on commit 33e4a84

Please sign in to comment.