From dbfb4df59206cece0ecdf33c38ebbe40340f0ad7 Mon Sep 17 00:00:00 2001 From: Andrew Stoltman Date: Thu, 5 Sep 2024 15:01:20 -0400 Subject: [PATCH] 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)