From b4f934a84a24cb0c6dae561026268d9d78f96405 Mon Sep 17 00:00:00 2001 From: Andrew Stoltman Date: Thu, 22 Aug 2024 15:05:49 -0400 Subject: [PATCH 01/14] First draft at XDMoD API image export documentation --- docs/howto-api-image-export.md | 63 ++++++++++++++++++++++++++++++++++ docs/howto.md | 1 + 2 files changed, 64 insertions(+) create mode 100644 docs/howto-api-image-export.md diff --git a/docs/howto-api-image-export.md b/docs/howto-api-image-export.md new file mode 100644 index 0000000000..379cdf11c3 --- /dev/null +++ b/docs/howto-api-image-export.md @@ -0,0 +1,63 @@ +You can use the XDMoD API to export your saved metric explorer charts. A local XDMoD account is required to authenticate through the API. + +The following python script can be used to export the saved metric explorer charts. The `dotenv` library is recommend when authenticating through XDMoD API. You can install the `dotenv` library using: +`$ pip install python-dotenv` + +The script will write the images to the current working directory. The format of the returned image can be changed, the default used here is 'svg'. + +```python +#!/usr/bin/env python3 +import os +import requests +import json +import urllib +from dotenv import load_dotenv + +load_dotenv() + +username = os.getenv('XDMOD_USERNAME') +password = os.getenv('XDMOD_PASSWORD') + +session = requests.Session() + +auth_response = session.post('YOUR_CENTER_URL/rest/auth/login', auth=(username, password)) + +if auth_response.status_code != 200: + print('Authentication failed. Check provided credentials and check if you have a local XDMoD account') + quit() + +auth_response = auth_response.json() + +header = { + 'Token': auth_response['results']['token'], + 'Authorization': auth_response['results']['token'], + 'Content-Type': 'application/x-www-form-urlencoded' +} + +saved_charts = session.get('YOUR_CENTER_URL/rest/v1/metrics/explorer/queries', headers=header, cookies=session.cookies) +saved_charts_data = saved_charts.json() + +for idx, chart in enumerate(saved_charts_data['data']): + if 'config' in chart: + chart_json = json.loads(chart['config']) + for attribute in chart_json: + if (isinstance(chart_json[attribute], dict)): + if 'data' in attribute: + encoded_str = urllib.parse.quote_plus(str(chart_json[attribute]['data'])) + else: + encoded_str = urllib.parse.quote_plus(str(chart_json[attribute])) + encoded_str = encoded_str.replace('%27','%22').replace('False', 'false').replace('True', 'true').replace('None', 'null') + chart_json[attribute] = encoded_str + if chart_json[attribute] in (True, False, None): + chart_json[attribute] = str(chart_json[attribute]).replace('False', 'false').replace('True', 'true').replace('None', 'null') + + chart_json['operation'] = "get_data" + chart_json['controller_module'] = "metric_explorer" + chart_json['format'] = "svg" + + chart_response = session.post('YOUR_CENTER_URL/controllers/metric_explorer.php', data=chart_json, headers=header, cookies=session.cookies) + chart_name = f"{chart['name']}.{chart_json['format']}" if ('name' in chart) else f"xdmod_API_export_{idx}.{chart_json['format']}" + + with open(chart_name, "w") as f: + f.write(chart_response.text) +``` diff --git a/docs/howto.md b/docs/howto.md index c42b89c37c..998e1d7564 100644 --- a/docs/howto.md +++ b/docs/howto.md @@ -8,3 +8,4 @@ The Open XDMoD HOWTOs are "how to" documents on specific subjects. - [Change Metric Explorer Colors](howto-colors.html) - [Enable Node Utilization Statistics](howto-node-utilization.html) - [Reconstruct Slurm Accounting Logs](howto-reconstruct-slurm.html) +- [Export Saved Metric Explorer Charts Through the XDMOD API](howto-api-image-export.md) From c1d7a8d0fff105ecd57ff89c5cc6c806659e5c14 Mon Sep 17 00:00:00 2001 From: Andrew Stoltman Date: Thu, 22 Aug 2024 16:03:03 -0400 Subject: [PATCH 02/14] Wordsmith --- docs/howto-api-image-export.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/docs/howto-api-image-export.md b/docs/howto-api-image-export.md index 379cdf11c3..2dd7e5ba04 100644 --- a/docs/howto-api-image-export.md +++ b/docs/howto-api-image-export.md @@ -1,9 +1,10 @@ -You can use the XDMoD API to export your saved metric explorer charts. A local XDMoD account is required to authenticate through the API. +You can use the XDMoD API to image export your saved metric explorer charts. A local XDMoD account is **required** to authenticate through the API. + +The following python script can be used to export your saved metric explorer charts. The `dotenv` library is recommend when authenticating through XDMoD API. You can install the `dotenv` library using: -The following python script can be used to export the saved metric explorer charts. The `dotenv` library is recommend when authenticating through XDMoD API. You can install the `dotenv` library using: `$ pip install python-dotenv` -The script will write the images to the current working directory. The format of the returned image can be changed, the default used here is 'svg'. +Running the script will export your saved metric explorer charts to the current working directory. The format of the exported image can be changed, the default used here is `svg`. Refer to the XDMoD Metric Explorer Tab Controller [API documentation](rest.html#tag/Metric-Explorer/paths/~1controllers~1metric_explorer.php/post) section for more information. The operation used here is `get_data`. ```python #!/usr/bin/env python3 From 7c9951eb60caf104f459295f4a1c3982679bf9f1 Mon Sep 17 00:00:00 2001 From: Andrew Stoltman Date: Thu, 22 Aug 2024 16:44:46 -0400 Subject: [PATCH 03/14] More wording adjustments --- docs/howto-api-image-export.md | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/docs/howto-api-image-export.md b/docs/howto-api-image-export.md index 2dd7e5ba04..a7579f2f39 100644 --- a/docs/howto-api-image-export.md +++ b/docs/howto-api-image-export.md @@ -4,7 +4,12 @@ The following python script can be used to export your saved metric explorer cha `$ pip install python-dotenv` -Running the script will export your saved metric explorer charts to the current working directory. The format of the exported image can be changed, the default used here is `svg`. Refer to the XDMoD Metric Explorer Tab Controller [API documentation](rest.html#tag/Metric-Explorer/paths/~1controllers~1metric_explorer.php/post) section for more information. The operation used here is `get_data`. +Before running the script, + +1. Replace `YOUR_CENTER_URL` within the script with the appropriate information +1. Create a `.env` file with your local XDMoD account credentials in the same directory as the script + +Running the script will export your saved metric explorer charts to the current working directory. The format of the exported image can be changed, the default used here is `svg`. ```python #!/usr/bin/env python3 @@ -55,6 +60,8 @@ for idx, chart in enumerate(saved_charts_data['data']): chart_json['operation'] = "get_data" chart_json['controller_module'] = "metric_explorer" chart_json['format'] = "svg" + chart_json['width'] = 916 + chart_json['height'] = 484 chart_response = session.post('YOUR_CENTER_URL/controllers/metric_explorer.php', data=chart_json, headers=header, cookies=session.cookies) chart_name = f"{chart['name']}.{chart_json['format']}" if ('name' in chart) else f"xdmod_API_export_{idx}.{chart_json['format']}" @@ -62,3 +69,5 @@ for idx, chart in enumerate(saved_charts_data['data']): with open(chart_name, "w") as f: f.write(chart_response.text) ``` + +The format of the exported image can be changed, the default used here is `svg`. Refer to the XDMoD [Metric Explorer Tab Controller API](rest.html#tag/Metric-Explorer/paths/~1controllers~1metric_explorer.php/post) `get_data` operation information on the request body schema. From 1b3ccf2071aacf7b4563de2717a3356bd997fcc9 Mon Sep 17 00:00:00 2001 From: Andrew Stoltman Date: Thu, 22 Aug 2024 17:14:31 -0400 Subject: [PATCH 04/14] More editing --- docs/howto-api-image-export.md | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/docs/howto-api-image-export.md b/docs/howto-api-image-export.md index a7579f2f39..71f1771c36 100644 --- a/docs/howto-api-image-export.md +++ b/docs/howto-api-image-export.md @@ -6,10 +6,11 @@ The following python script can be used to export your saved metric explorer cha Before running the script, -1. Replace `YOUR_CENTER_URL` within the script with the appropriate information 1. Create a `.env` file with your local XDMoD account credentials in the same directory as the script +1. Declare `center_url` within the script with the appropriate information +1. Confirm the `image_format` within the script. -Running the script will export your saved metric explorer charts to the current working directory. The format of the exported image can be changed, the default used here is `svg`. +Running the script will export your saved metric explorer charts to the current working directory. ```python #!/usr/bin/env python3 @@ -23,10 +24,12 @@ load_dotenv() username = os.getenv('XDMOD_USERNAME') password = os.getenv('XDMOD_PASSWORD') +center_url = "" +image_format = "svg" session = requests.Session() -auth_response = session.post('YOUR_CENTER_URL/rest/auth/login', auth=(username, password)) +auth_response = session.post(f'{center_url}/rest/auth/login', auth=(username, password)) if auth_response.status_code != 200: print('Authentication failed. Check provided credentials and check if you have a local XDMoD account') @@ -40,7 +43,7 @@ header = { 'Content-Type': 'application/x-www-form-urlencoded' } -saved_charts = session.get('YOUR_CENTER_URL/rest/v1/metrics/explorer/queries', headers=header, cookies=session.cookies) +saved_charts = session.get(f'{center_url}/rest/v1/metrics/explorer/queries', headers=header, cookies=session.cookies) saved_charts_data = saved_charts.json() for idx, chart in enumerate(saved_charts_data['data']): @@ -59,15 +62,16 @@ for idx, chart in enumerate(saved_charts_data['data']): chart_json['operation'] = "get_data" chart_json['controller_module'] = "metric_explorer" - chart_json['format'] = "svg" + chart_json['show_title'] = "y" + chart_json['format'] = image_format chart_json['width'] = 916 chart_json['height'] = 484 - chart_response = session.post('YOUR_CENTER_URL/controllers/metric_explorer.php', data=chart_json, headers=header, cookies=session.cookies) - chart_name = f"{chart['name']}.{chart_json['format']}" if ('name' in chart) else f"xdmod_API_export_{idx}.{chart_json['format']}" + chart_response = session.post(f'{center_url}/controllers/metric_explorer.php', data=chart_json, headers=header, cookies=session.cookies) + chart_name = f"{chart['name']}.{image_format}" if ('name' in chart) else f"xdmod_API_export_{idx}.{image_format}" - with open(chart_name, "w") as f: - f.write(chart_response.text) + with open(chart_name, "wb") as f: + f.write(chart_response.content) ``` -The format of the exported image can be changed, the default used here is `svg`. Refer to the XDMoD [Metric Explorer Tab Controller API](rest.html#tag/Metric-Explorer/paths/~1controllers~1metric_explorer.php/post) `get_data` operation information on the request body schema. +The defualt image format is `svg`, but `png` and `pdf` formats are also supported. Refer to the XDMoD [Metric Explorer Tab Controller API](rest.html#tag/Metric-Explorer/paths/~1controllers~1metric_explorer.php/post) `get_data` operation information on the request body schema. From 882dad40dc10ea3abe76e3b1166e1b0f52ec136c Mon Sep 17 00:00:00 2001 From: Andrew Stoltman Date: Thu, 22 Aug 2024 17:18:11 -0400 Subject: [PATCH 05/14] More edits --- docs/howto-api-image-export.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/howto-api-image-export.md b/docs/howto-api-image-export.md index 71f1771c36..2863c414b3 100644 --- a/docs/howto-api-image-export.md +++ b/docs/howto-api-image-export.md @@ -1,6 +1,6 @@ You can use the XDMoD API to image export your saved metric explorer charts. A local XDMoD account is **required** to authenticate through the API. -The following python script can be used to export your saved metric explorer charts. The `dotenv` library is recommend when authenticating through XDMoD API. You can install the `dotenv` library using: +The following Python script will export your saved metric explorer charts. The `dotenv` library is recommended when authenticating through XDMoD API. You can install the `dotenv` library using: `$ pip install python-dotenv` @@ -10,7 +10,7 @@ Before running the script, 1. Declare `center_url` within the script with the appropriate information 1. Confirm the `image_format` within the script. -Running the script will export your saved metric explorer charts to the current working directory. +The script will export your saved metric explorer charts to the current working directory. ```python #!/usr/bin/env python3 @@ -74,4 +74,4 @@ for idx, chart in enumerate(saved_charts_data['data']): f.write(chart_response.content) ``` -The defualt image format is `svg`, but `png` and `pdf` formats are also supported. Refer to the XDMoD [Metric Explorer Tab Controller API](rest.html#tag/Metric-Explorer/paths/~1controllers~1metric_explorer.php/post) `get_data` operation information on the request body schema. +The default image format is `svg`, but `png` and `pdf` formats are also supported. Refer to the XDMoD [Metric Explorer Tab Controller API](rest.html#tag/Metric-Explorer/paths/~1controllers~1metric_explorer.php/post) `get_data` operation information on the request body schema. From a13948b2ebef0bc2e29cbacc6cc28e9d41ef0425 Mon Sep 17 00:00:00 2001 From: Andrew Stoltman Date: Thu, 22 Aug 2024 17:24:22 -0400 Subject: [PATCH 06/14] Fix site address wording --- docs/howto-api-image-export.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/howto-api-image-export.md b/docs/howto-api-image-export.md index 2863c414b3..086ea9ab95 100644 --- a/docs/howto-api-image-export.md +++ b/docs/howto-api-image-export.md @@ -7,7 +7,7 @@ The following Python script will export your saved metric explorer charts. The ` Before running the script, 1. Create a `.env` file with your local XDMoD account credentials in the same directory as the script -1. Declare `center_url` within the script with the appropriate information +1. Update `site_address` within the script with site address associated with your XDMoD instance. 1. Confirm the `image_format` within the script. The script will export your saved metric explorer charts to the current working directory. @@ -24,12 +24,12 @@ load_dotenv() username = os.getenv('XDMOD_USERNAME') password = os.getenv('XDMOD_PASSWORD') -center_url = "" +site_address = "" image_format = "svg" session = requests.Session() -auth_response = session.post(f'{center_url}/rest/auth/login', auth=(username, password)) +auth_response = session.post(f'{site_address}/rest/auth/login', auth=(username, password)) if auth_response.status_code != 200: print('Authentication failed. Check provided credentials and check if you have a local XDMoD account') @@ -43,7 +43,7 @@ header = { 'Content-Type': 'application/x-www-form-urlencoded' } -saved_charts = session.get(f'{center_url}/rest/v1/metrics/explorer/queries', headers=header, cookies=session.cookies) +saved_charts = session.get(f'{site_address}/rest/v1/metrics/explorer/queries', headers=header, cookies=session.cookies) saved_charts_data = saved_charts.json() for idx, chart in enumerate(saved_charts_data['data']): @@ -67,7 +67,7 @@ for idx, chart in enumerate(saved_charts_data['data']): chart_json['width'] = 916 chart_json['height'] = 484 - chart_response = session.post(f'{center_url}/controllers/metric_explorer.php', data=chart_json, headers=header, cookies=session.cookies) + chart_response = session.post(f'{site_address}/controllers/metric_explorer.php', data=chart_json, headers=header, cookies=session.cookies) chart_name = f"{chart['name']}.{image_format}" if ('name' in chart) else f"xdmod_API_export_{idx}.{image_format}" with open(chart_name, "wb") as f: From 00b2b7cdca7c7595a888925cbcd81f12647ef40f Mon Sep 17 00:00:00 2001 From: Andrew Stoltman Date: Thu, 22 Aug 2024 17:46:52 -0400 Subject: [PATCH 07/14] More updates --- docs/howto-api-image-export.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/howto-api-image-export.md b/docs/howto-api-image-export.md index 086ea9ab95..9be7061287 100644 --- a/docs/howto-api-image-export.md +++ b/docs/howto-api-image-export.md @@ -74,4 +74,4 @@ for idx, chart in enumerate(saved_charts_data['data']): f.write(chart_response.content) ``` -The default image format is `svg`, but `png` and `pdf` formats are also supported. Refer to the XDMoD [Metric Explorer Tab Controller API](rest.html#tag/Metric-Explorer/paths/~1controllers~1metric_explorer.php/post) `get_data` operation information on the request body schema. +The default image format is `svg`, but `png` and `pdf` formats are also supported. Refer to the XDMoD [Metric Explorer Tab Controller API](rest.html#tag/Metric-Explorer/paths/~1controllers~1metric_explorer.php/post) `get_data` operation for more information on the request body schema. From 3cb64960b667a7d6f8243ab60fbc0816d5a01aa5 Mon Sep 17 00:00:00 2001 From: Andrew Stoltman Date: Fri, 23 Aug 2024 09:19:26 -0400 Subject: [PATCH 08/14] Address feedback --- docs/howto-api-image-export.md | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/docs/howto-api-image-export.md b/docs/howto-api-image-export.md index 9be7061287..0a52a1e728 100644 --- a/docs/howto-api-image-export.md +++ b/docs/howto-api-image-export.md @@ -50,15 +50,16 @@ for idx, chart in enumerate(saved_charts_data['data']): if 'config' in chart: chart_json = json.loads(chart['config']) for attribute in chart_json: - if (isinstance(chart_json[attribute], dict)): + chart_parameter = chart_json[attribute] + if (isinstance(chart_parameter, dict)): if 'data' in attribute: - encoded_str = urllib.parse.quote_plus(str(chart_json[attribute]['data'])) + encoded_str = urllib.parse.quote_plus(str(chart_parameter['data'])) else: - encoded_str = urllib.parse.quote_plus(str(chart_json[attribute])) + encoded_str = urllib.parse.quote_plus(str(chart_parameter)) encoded_str = encoded_str.replace('%27','%22').replace('False', 'false').replace('True', 'true').replace('None', 'null') chart_json[attribute] = encoded_str - if chart_json[attribute] in (True, False, None): - chart_json[attribute] = str(chart_json[attribute]).replace('False', 'false').replace('True', 'true').replace('None', 'null') + if chart_parameter in (True, False, None): + chart_json[attribute] = str(chart_parameter).replace('False', 'false').replace('True', 'true').replace('None', 'null') chart_json['operation'] = "get_data" chart_json['controller_module'] = "metric_explorer" From 38f05f24f797f06e89027db88fd97e5edde1cd16 Mon Sep 17 00:00:00 2001 From: Andrew Stoltman Date: Fri, 23 Aug 2024 10:15:53 -0400 Subject: [PATCH 09/14] Address feedback --- docs/howto-api-image-export.md | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/docs/howto-api-image-export.md b/docs/howto-api-image-export.md index 0a52a1e728..048a80f7d8 100644 --- a/docs/howto-api-image-export.md +++ b/docs/howto-api-image-export.md @@ -1,16 +1,20 @@ You can use the XDMoD API to image export your saved metric explorer charts. A local XDMoD account is **required** to authenticate through the API. -The following Python script will export your saved metric explorer charts. The `dotenv` library is recommended when authenticating through XDMoD API. You can install the `dotenv` library using: +The following Python script will export your saved metric explorer charts. The `dotenv` and `requests` libraries are used when authenticating through the XDMoD API. You can install these libraries through: -`$ pip install python-dotenv` +```shell +pip install python-dotenv +pip install requests +``` Before running the script, -1. Create a `.env` file with your local XDMoD account credentials in the same directory as the script +1. Install the required dependencies listed above. +1. Create a `.env` file with your local XDMoD account credentials in the same directory as the script. 1. Update `site_address` within the script with site address associated with your XDMoD instance. 1. Confirm the `image_format` within the script. -The script will export your saved metric explorer charts to the current working directory. +The script will export your saved metric explorer charts to the current working directory. **\*Note:** Replace `` with your site address. ```python #!/usr/bin/env python3 @@ -24,7 +28,7 @@ load_dotenv() username = os.getenv('XDMOD_USERNAME') password = os.getenv('XDMOD_PASSWORD') -site_address = "" +site_address = "" image_format = "svg" session = requests.Session() From dbfb4df59206cece0ecdf33c38ebbe40340f0ad7 Mon Sep 17 00:00:00 2001 From: Andrew Stoltman Date: Thu, 5 Sep 2024 15:01:20 -0400 Subject: [PATCH 10/14] Address feedback --- docs/howto-api-image-export.md | 67 ++++++++++++++++++++-------------- docs/howto.md | 2 +- 2 files changed, 40 insertions(+), 29 deletions(-) diff --git a/docs/howto-api-image-export.md b/docs/howto-api-image-export.md index 048a80f7d8..172f3ea914 100644 --- a/docs/howto-api-image-export.md +++ b/docs/howto-api-image-export.md @@ -1,20 +1,16 @@ -You can use the XDMoD API to image export your saved metric explorer charts. A local XDMoD account is **required** to authenticate through the API. - -The following Python script will export your saved metric explorer charts. The `dotenv` and `requests` libraries are used when authenticating through the XDMoD API. You can install these libraries through: - -```shell -pip install python-dotenv -pip install requests -``` - +The following Python script will export your saved Metric Explorer charts. An XDMoD username and password is required to run the script. Before running the script, - -1. Install the required dependencies listed above. -1. Create a `.env` file with your local XDMoD account credentials in the same directory as the script. -1. Update `site_address` within the script with site address associated with your XDMoD instance. +1. Install the required `python-dotenv` and `requests` dependencies. +1. Create a `.env` file in the same directory as the script that contains the following contents, replacing `` with your XDMoD username and `` with your XDMoD password — make sure to secure this file with read-only access. + ``` + XDMOD_USERNAME= + XDMOD_PASSWORD= + ``` +1. Update the value of `site_address` within the script with the URL associated with your XDMoD portal. +1. Update the value of `export_path` within the script with the desired path to export the images. 1. Confirm the `image_format` within the script. -The script will export your saved metric explorer charts to the current working directory. **\*Note:** Replace `` with your site address. +The default image format is `svg`, but `png` and `pdf` formats are also supported. Refer to the XDMoD [Metric Explorer Tab Controller API](rest.html#tag/Metric-Explorer/paths/~1controllers~1metric_explorer.php/post) `get_data` operation for more information on the request body schema. ```python #!/usr/bin/env python3 @@ -22,14 +18,22 @@ import os import requests import json import urllib +import argparse from dotenv import load_dotenv -load_dotenv() +site_address = '' +export_path = '' +image_format = 'svg' +image_width = 916 +image_height = 484 +load_dotenv() username = os.getenv('XDMOD_USERNAME') password = os.getenv('XDMOD_PASSWORD') -site_address = "" -image_format = "svg" + +parser = argparse.ArgumentParser(description='Export XDMoD saved Metric Explorer charts with the REST API.') +parser.add_argument('-n', '--name',type=str, default='', help='Specify the chart name of a saved chart to export.') +args = parser.parse_args() session = requests.Session() @@ -42,14 +46,18 @@ if auth_response.status_code != 200: auth_response = auth_response.json() header = { - 'Token': auth_response['results']['token'], - 'Authorization': auth_response['results']['token'], - 'Content-Type': 'application/x-www-form-urlencoded' + 'Token': auth_response['results']['token'], + 'Authorization': auth_response['results']['token'], + 'Content-Type': 'application/x-www-form-urlencoded' } -saved_charts = session.get(f'{site_address}/rest/v1/metrics/explorer/queries', headers=header, cookies=session.cookies) +saved_charts = session.get(f'{site_address}/rest/v1/metrics/explorer/queries', headers=header) saved_charts_data = saved_charts.json() +if args.name != '' and not any(chart_obj['name'] == args.name for chart_obj in saved_charts_data['data']): + print('Specified chart not found.') + exit() + for idx, chart in enumerate(saved_charts_data['data']): if 'config' in chart: chart_json = json.loads(chart['config']) @@ -69,14 +77,17 @@ for idx, chart in enumerate(saved_charts_data['data']): chart_json['controller_module'] = "metric_explorer" chart_json['show_title'] = "y" chart_json['format'] = image_format - chart_json['width'] = 916 - chart_json['height'] = 484 + chart_json['width'] = image_width + chart_json['height'] = image_height - chart_response = session.post(f'{site_address}/controllers/metric_explorer.php', data=chart_json, headers=header, cookies=session.cookies) + chart_response = session.post(f'{site_address}/controllers/metric_explorer.php', data=chart_json, headers=header) chart_name = f"{chart['name']}.{image_format}" if ('name' in chart) else f"xdmod_API_export_{idx}.{image_format}" - with open(chart_name, "wb") as f: - f.write(chart_response.content) + if args.name != '' and args.name == chart['name']: + with open(export_path + chart_name, "wb") as f: + f.write(chart_response.content) + exit() + else: + with open(export_path + chart_name, "wb") as f: + f.write(chart_response.content) ``` - -The default image format is `svg`, but `png` and `pdf` formats are also supported. Refer to the XDMoD [Metric Explorer Tab Controller API](rest.html#tag/Metric-Explorer/paths/~1controllers~1metric_explorer.php/post) `get_data` operation for more information on the request body schema. diff --git a/docs/howto.md b/docs/howto.md index 998e1d7564..57d102fbb3 100644 --- a/docs/howto.md +++ b/docs/howto.md @@ -8,4 +8,4 @@ The Open XDMoD HOWTOs are "how to" documents on specific subjects. - [Change Metric Explorer Colors](howto-colors.html) - [Enable Node Utilization Statistics](howto-node-utilization.html) - [Reconstruct Slurm Accounting Logs](howto-reconstruct-slurm.html) -- [Export Saved Metric Explorer Charts Through the XDMOD API](howto-api-image-export.md) +- [Export Saved Metric Explorer Charts Through the REST API](howto-api-image-export.md) From 398000e5fe981e22df55e9ffe8aa1ea39a72185c Mon Sep 17 00:00:00 2001 From: Andrew Stoltman Date: Thu, 5 Sep 2024 16:15:58 -0400 Subject: [PATCH 11/14] Address feedback #2 --- docs/howto-api-image-export.md | 44 ++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/docs/howto-api-image-export.md b/docs/howto-api-image-export.md index 172f3ea914..4db1911dcf 100644 --- a/docs/howto-api-image-export.md +++ b/docs/howto-api-image-export.md @@ -1,16 +1,18 @@ -The following Python script will export your saved Metric Explorer charts. An XDMoD username and password is required to run the script. +The following Python script can be used to export your saved Metric Explorer charts to image files using the XDMoD REST API. An XDMoD username and password are required to run the script. Before running the script, -1. Install the required `python-dotenv` and `requests` dependencies. +1. Install the required `python-dotenv` and `requests` Python dependencies (e.g., using `pip`). 1. Create a `.env` file in the same directory as the script that contains the following contents, replacing `` with your XDMoD username and `` with your XDMoD password — make sure to secure this file with read-only access. ``` XDMOD_USERNAME= XDMOD_PASSWORD= ``` -1. Update the value of `site_address` within the script with the URL associated with your XDMoD portal. -1. Update the value of `export_path` within the script with the desired path to export the images. -1. Confirm the `image_format` within the script. +1. Update the value of `site_address` at the top of the script with the URL associated with your XDMoD portal. +1. Update the value of `export_path` at the top of the script with the desired directory path where the images will be written. +1. Confirm the desired values for `image_format`, `width`, and `height` at the top of the script. The default image format is `svg`, but `png` and `pdf` formats are also supported. -The default image format is `svg`, but `png` and `pdf` formats are also supported. Refer to the XDMoD [Metric Explorer Tab Controller API](rest.html#tag/Metric-Explorer/paths/~1controllers~1metric_explorer.php/post) `get_data` operation for more information on the request body schema. +By default, the script will download all of your saved Metric Explorer charts. You can have it instead download a single chart by providing the `-n` or `--name` option followed by the name of the saved chart. + +Refer to the XDMoD [Metric Explorer Tab Controller REST API](rest.html#tag/Metric-Explorer/paths/~1controllers~1metric_explorer.php/post) `get_data` operation for more information on the REST request body schema. ```python #!/usr/bin/env python3 @@ -20,19 +22,24 @@ import json import urllib import argparse from dotenv import load_dotenv +import sys site_address = '' -export_path = '' +export_dir = '.' image_format = 'svg' image_width = 916 image_height = 484 +if site_address == '': + print('Please edit the script to specify a site_address.', file=sys.stderr) + sys.exit(1) + load_dotenv() username = os.getenv('XDMOD_USERNAME') password = os.getenv('XDMOD_PASSWORD') parser = argparse.ArgumentParser(description='Export XDMoD saved Metric Explorer charts with the REST API.') -parser.add_argument('-n', '--name',type=str, default='', help='Specify the chart name of a saved chart to export.') +parser.add_argument('-n', '--name', help='Specify the chart name of a saved chart to export.') args = parser.parse_args() session = requests.Session() @@ -40,8 +47,8 @@ session = requests.Session() auth_response = session.post(f'{site_address}/rest/auth/login', auth=(username, password)) if auth_response.status_code != 200: - print('Authentication failed. Check provided credentials and check if you have a local XDMoD account') - quit() + print('Authentication failed. Check provided credentials.', file=sys.stderr) + quit(1) auth_response = auth_response.json() @@ -54,11 +61,13 @@ header = { saved_charts = session.get(f'{site_address}/rest/v1/metrics/explorer/queries', headers=header) saved_charts_data = saved_charts.json() -if args.name != '' and not any(chart_obj['name'] == args.name for chart_obj in saved_charts_data['data']): - print('Specified chart not found.') - exit() +if args.name is not None and not any(chart_obj['name'] == args.name for chart_obj in saved_charts_data['data']): + print('Specified chart not found.', file=sys.stderr) + exit(1) for idx, chart in enumerate(saved_charts_data['data']): + if args.name is not None and args.name != chart['name']: + continue if 'config' in chart: chart_json = json.loads(chart['config']) for attribute in chart_json: @@ -83,11 +92,6 @@ for idx, chart in enumerate(saved_charts_data['data']): chart_response = session.post(f'{site_address}/controllers/metric_explorer.php', data=chart_json, headers=header) chart_name = f"{chart['name']}.{image_format}" if ('name' in chart) else f"xdmod_API_export_{idx}.{image_format}" - if args.name != '' and args.name == chart['name']: - with open(export_path + chart_name, "wb") as f: - f.write(chart_response.content) - exit() - else: - with open(export_path + chart_name, "wb") as f: - f.write(chart_response.content) + with open(export_dir + '/' + chart_name, "wb") as f: + f.write(chart_response.content) ``` From 8aa2f523e277f7a01a3e067b926ecbcc7b9f7396 Mon Sep 17 00:00:00 2001 From: Andrew Stoltman Date: Thu, 5 Sep 2024 16:53:12 -0400 Subject: [PATCH 12/14] Fix variable name --- docs/howto-api-image-export.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/howto-api-image-export.md b/docs/howto-api-image-export.md index 4db1911dcf..f1fdd5410c 100644 --- a/docs/howto-api-image-export.md +++ b/docs/howto-api-image-export.md @@ -7,7 +7,7 @@ Before running the script, XDMOD_PASSWORD= ``` 1. Update the value of `site_address` at the top of the script with the URL associated with your XDMoD portal. -1. Update the value of `export_path` at the top of the script with the desired directory path where the images will be written. +1. Update the value of `export_dir` at the top of the script with the desired directory path where the images will be written. 1. Confirm the desired values for `image_format`, `width`, and `height` at the top of the script. The default image format is `svg`, but `png` and `pdf` formats are also supported. By default, the script will download all of your saved Metric Explorer charts. You can have it instead download a single chart by providing the `-n` or `--name` option followed by the name of the saved chart. From 7644d0fa5cc57b02ee0c911ef328eb7b033f2da1 Mon Sep 17 00:00:00 2001 From: Andrew Stoltman Date: Thu, 5 Sep 2024 16:57:37 -0400 Subject: [PATCH 13/14] Fix more variable names --- docs/howto-api-image-export.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/howto-api-image-export.md b/docs/howto-api-image-export.md index f1fdd5410c..9188b2efe2 100644 --- a/docs/howto-api-image-export.md +++ b/docs/howto-api-image-export.md @@ -25,10 +25,10 @@ from dotenv import load_dotenv import sys site_address = '' -export_dir = '.' +export_dir = '' image_format = 'svg' -image_width = 916 -image_height = 484 +width = 916 +height = 484 if site_address == '': print('Please edit the script to specify a site_address.', file=sys.stderr) @@ -86,8 +86,8 @@ for idx, chart in enumerate(saved_charts_data['data']): chart_json['controller_module'] = "metric_explorer" chart_json['show_title'] = "y" chart_json['format'] = image_format - chart_json['width'] = image_width - chart_json['height'] = image_height + chart_json['width'] = width + chart_json['height'] = height chart_response = session.post(f'{site_address}/controllers/metric_explorer.php', data=chart_json, headers=header) chart_name = f"{chart['name']}.{image_format}" if ('name' in chart) else f"xdmod_API_export_{idx}.{image_format}" From f0b7976bc5272ff7cf98efcbb928cd11f6c99acc Mon Sep 17 00:00:00 2001 From: Andrew Stoltman Date: Fri, 22 Nov 2024 14:08:01 -0500 Subject: [PATCH 14/14] Default export directory should be working directory --- docs/howto-api-image-export.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/howto-api-image-export.md b/docs/howto-api-image-export.md index 9188b2efe2..fe57ee17c4 100644 --- a/docs/howto-api-image-export.md +++ b/docs/howto-api-image-export.md @@ -25,7 +25,7 @@ from dotenv import load_dotenv import sys site_address = '' -export_dir = '' +export_dir = '.' image_format = 'svg' width = 916 height = 484