From 43f747db86a8cfeecf550498973ee7fc4adc91a5 Mon Sep 17 00:00:00 2001 From: Shiv Date: Tue, 14 Mar 2023 20:42:59 +0530 Subject: [PATCH 1/3] Docstrings and fstrings changes Signed-off-by: Shiv --- remote_monitoring_demo/kruize/kruize.py | 76 +++++++++++++++---------- 1 file changed, 46 insertions(+), 30 deletions(-) diff --git a/remote_monitoring_demo/kruize/kruize.py b/remote_monitoring_demo/kruize/kruize.py index a32c8bf0..fa87d6eb 100644 --- a/remote_monitoring_demo/kruize/kruize.py +++ b/remote_monitoring_demo/kruize/kruize.py @@ -18,9 +18,7 @@ import subprocess import requests import json -import os -import time -import shutil + def form_kruize_url(cluster_type): global URL @@ -43,14 +41,17 @@ def form_kruize_url(cluster_type): print("IP = ", SERVER_IP) URL = "http://" + str(SERVER_IP) + ":" + str(AUTOTUNE_PORT) - print ("\nKRUIZE AUTOTUNE URL = ", URL) + print (f"\nKRUIZE AUTOTUNE URL = {URL}") -# Description: This function validates the input json and posts the experiment using createExperiment API to Kruize -# Input Parameters: experiment input json def create_experiment(input_json_file): + """ + Description: This function validates the input json and posts + the experiment using createExperiment API to Kruize + Input Parameters: experiment input json + """ - json_file = open(input_json_file, "r") + json_file = open(input_json_file) input_json = json.loads(json_file.read()) print("\n************************************************************") print(input_json) @@ -68,15 +69,18 @@ def create_experiment(input_json_file): print("\nCreating the experiment...") url = URL + "/createExperiment" - print("URL = ", url) + print(f"URL = {url}") response = requests.post(url, json=input_json) - print("Response status code = ", response.status_code) + print(f"Response status code = {response.status_code}") print(response.text) -# Description: This function validates the result json and posts the experiment results using updateResults API to Kruize -# Input Parameters: resource usage metrics json def update_results(result_json_file): + """ + Description: This function validates the result json and + posts the experiment results using updateResults API to Kruize + Input Parameters: resource usage metrics json + """ # read the json json_file = open(result_json_file, "r") @@ -86,60 +90,72 @@ def update_results(result_json_file): print("\nUpdating the results...") url = URL + "/updateResults" - print("URL = ", url) + print(f"URL = {url}") response = requests.post(url, json=result_json) - print("Response status code = ", response.status_code) + print(f"Response status code = {response.status_code}") print(response.text) return response -# Description: This function obtains the recommendations from Kruize using listRecommendations API -# Input Parameters: experiment name -def list_recommendations(experiment_name): +def list_recommendations(experiment_name, deployment_name, namespace): + """ + Description: This function obtains the recommendations + from Kruize using listRecommendations API + Input Parameters: experiment name, deployment name and namespace + """ print("\nListing the recommendations...") url = URL + "/listRecommendations" - print("URL = ", url) + print(f"URL = {url}") - PARAMS = {'experiment_name': experiment_name} + PARAMS = {'experiment_name': experiment_name, 'deployment_name': deployment_name, 'namespace': namespace} response = requests.get(url = url, params = PARAMS) - print("Response status code = ", response.status_code) + print(f"Response status code = {response.status_code}") return response.json() -# Description: This function creates a performance profile using the Kruize createPerformanceProfile API -# Input Parameters: performance profile json def create_performance_profile(perf_profile_json_file): + """ + Description: This function creates a performance profile + using the Kruize createPerformanceProfile API + Input Parameters: performance profile json + """ - json_file = open(perf_profile_json_file, "r") + json_file = open(perf_profile_json_file) perf_profile_json = json.loads(json_file.read()) print("\nCreating performance profile...") url = URL + "/createPerformanceProfile" - print("URL = ", url) + print(f"URL = {url}") response = requests.post(url, json=perf_profile_json) - print("Response status code = ", response.status_code) + print(f"Response status code = {response.status_code}") print(response.text) return response -# Description: This function obtains the experiments and result metrics from Kruize using listExperiments API def list_experiments(): + """ + Description: This function obtains the experiments and + result metrics from Kruize using listExperiments API + """ print("\nListing the experiments...") url = URL + "/listExperiments" - print("URL = ", url) + print(f"URL = {url}") response = requests.get(url = url) - print("Response status code = ", response.status_code) + print(f"Response status code = {response.status_code}") return response.json() -# Description: This function combines the metric results and recommendations into a single json -# Input parameters: result json file, recommendations json def combine_jsons(result_json, reco_json): + """ + Description: This function combines the metric + results and recommendations into a single json + Input parameters: result json file, recommendations json + """ - input_json = open(result_json, "r") + input_json = open(result_json) data = json.loads(input_json.read()) exp = "quarkus-resteasy-autotune-min-http-response-time-db" From 86a96f9ff2aa431ed1bf389eca649a842e3d2fb6 Mon Sep 17 00:00:00 2001 From: Shiv Date: Tue, 14 Mar 2023 20:59:46 +0530 Subject: [PATCH 2/3] using default file permissions --- remote_monitoring_demo/demo.py | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/remote_monitoring_demo/demo.py b/remote_monitoring_demo/demo.py index 052e406a..4d7b64af 100644 --- a/remote_monitoring_demo/demo.py +++ b/remote_monitoring_demo/demo.py @@ -17,12 +17,11 @@ from kruize.kruize import * import sys, getopt import json -import os import time def generate_json(find_arr, json_file, filename, i): - with open(json_file, 'r') as file: + with open(json_file) as file: data = file.read() for find in find_arr: @@ -40,13 +39,11 @@ def main(argv): json_data = json.load(open(create_exp_json_file)) find.append(json_data[0]['experiment_name']) - find.append(json_data[0]['kubernetes_objects'][0]['name']) - find.append(json_data[0]['kubernetes_objects'][0]['namespace']) - - print(find) + find.append(json_data[0]['deployment_name']) + find.append(json_data[0]['namespace']) try: - opts, args = getopt.getopt(argv,"h:c:") + opts, _ = getopt.getopt(argv,"h:c:") except getopt.GetoptError: print("demo.py -c ") sys.exit(2) @@ -74,11 +71,11 @@ def main(argv): create_experiment(tmp_create_exp_json_file) if i == 0: - json_data = json.load(open(tmp_create_exp_json_file)) + json_data = json.load(open(create_exp_json_file)) experiment_name = json_data[0]['experiment_name'] - deployment_name = json_data[0]['kubernetes_objects'][0]['name'] - namespace = json_data[0]['kubernetes_objects'][0]['namespace'] + deployment_name = json_data[0]['deployment_name'] + namespace = json_data[0]['namespace'] print("Experiment name = ", experiment_name) print("Deployment name = ", deployment_name) @@ -99,16 +96,16 @@ def main(argv): # Sleep time.sleep(5) - reco = list_recommendations(experiment_name) + reco = list_recommendations(experiment_name, deployment_name, namespace) recommendations_json_arr.append(reco) # Dump the results & recommendations into json files with open('recommendations_data.json', 'w') as f: - json.dump(recommendations_json_arr, f, indent=4) + json.dump(recommendations_json_arr, f) list_exp_json = list_experiments() with open('usage_data.json', 'w') as f: - json.dump(list_exp_json, f, indent=4) + json.dump(list_exp_json, f) if __name__ == '__main__': From b7f6e248b3481ec4839106da143ffe6c96242f5b Mon Sep 17 00:00:00 2001 From: Shiv Date: Wed, 15 Mar 2023 18:08:53 +0530 Subject: [PATCH 3/3] updating few lines as per the latest branch --- remote_monitoring_demo/demo.py | 19 +++++++++++-------- remote_monitoring_demo/kruize/kruize.py | 4 ++-- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/remote_monitoring_demo/demo.py b/remote_monitoring_demo/demo.py index 4d7b64af..fbfb4643 100644 --- a/remote_monitoring_demo/demo.py +++ b/remote_monitoring_demo/demo.py @@ -39,8 +39,11 @@ def main(argv): json_data = json.load(open(create_exp_json_file)) find.append(json_data[0]['experiment_name']) - find.append(json_data[0]['deployment_name']) - find.append(json_data[0]['namespace']) + find.append(json_data[0]['kubernetes_objects'][0]['name']) + find.append(json_data[0]['kubernetes_objects'][0]['namespace']) + + print(find) + try: opts, _ = getopt.getopt(argv,"h:c:") @@ -71,11 +74,11 @@ def main(argv): create_experiment(tmp_create_exp_json_file) if i == 0: - json_data = json.load(open(create_exp_json_file)) + json_data = json.load(open(tmp_create_exp_json_file)) experiment_name = json_data[0]['experiment_name'] - deployment_name = json_data[0]['deployment_name'] - namespace = json_data[0]['namespace'] + deployment_name = json_data[0]['kubernetes_objects'][0]['name'] + namespace = json_data[0]['kubernetes_objects'][0]['namespace'] print("Experiment name = ", experiment_name) print("Deployment name = ", deployment_name) @@ -96,16 +99,16 @@ def main(argv): # Sleep time.sleep(5) - reco = list_recommendations(experiment_name, deployment_name, namespace) + reco = list_recommendations(experiment_name) recommendations_json_arr.append(reco) # Dump the results & recommendations into json files with open('recommendations_data.json', 'w') as f: - json.dump(recommendations_json_arr, f) + json.dump(recommendations_json_arr, f, indent=4) list_exp_json = list_experiments() with open('usage_data.json', 'w') as f: - json.dump(list_exp_json, f) + json.dump(list_exp_json, f, indent=4) if __name__ == '__main__': diff --git a/remote_monitoring_demo/kruize/kruize.py b/remote_monitoring_demo/kruize/kruize.py index fa87d6eb..517e175a 100644 --- a/remote_monitoring_demo/kruize/kruize.py +++ b/remote_monitoring_demo/kruize/kruize.py @@ -97,7 +97,7 @@ def update_results(result_json_file): print(response.text) return response -def list_recommendations(experiment_name, deployment_name, namespace): +def list_recommendations(experiment_name): """ Description: This function obtains the recommendations from Kruize using listRecommendations API @@ -108,7 +108,7 @@ def list_recommendations(experiment_name, deployment_name, namespace): url = URL + "/listRecommendations" print(f"URL = {url}") - PARAMS = {'experiment_name': experiment_name, 'deployment_name': deployment_name, 'namespace': namespace} + PARAMS = {'experiment_name': experiment_name} response = requests.get(url = url, params = PARAMS) print(f"Response status code = {response.status_code}")