-
Notifications
You must be signed in to change notification settings - Fork 68
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added documentation for exporting saved Metric Explorer charts with the XDMoD API #1907
base: xdmod11.0
Are you sure you want to change the base?
Conversation
docs/howto-api-image-export.md
Outdated
if 'config' in chart: | ||
chart_json = json.loads(chart['config']) | ||
for attribute in chart_json: | ||
if (isinstance(chart_json[attribute], dict)): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest the chart_json[attribute]
is stored in a variable for clarity/readability because of how often it is referenced here.
docs/howto-api-image-export.md
Outdated
|
||
username = os.getenv('XDMOD_USERNAME') | ||
password = os.getenv('XDMOD_PASSWORD') | ||
site_address = "" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Super minor, but I would suggest making this clearer that the site_address
should be filled in. Either a note in the the top comment or site_address = "<XDMOD_URL_HERE>"
. I assume most people will copy-paste this script without reading it and then encounter an issue when this isn't populated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just need to add that the user needs to also pip install requests
and it'll be good to go.
docs/howto-api-image-export.md
Outdated
@@ -0,0 +1,82 @@ | |||
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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. | |
You can use the XDMoD REST API to export your saved Metric Explorer charts as images. |
SSO can also be used for authenticating through the REST API. The local account is required to run the Python script, though. I moved this line into another suggestion below.
docs/howto.md
Outdated
@@ -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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- [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) |
docs/howto-api-image-export.md
Outdated
'Token': auth_response['results']['token'], | ||
'Authorization': auth_response['results']['token'], | ||
'Content-Type': 'application/x-www-form-urlencoded' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'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' |
For consistent indentation style.
docs/howto-api-image-export.md
Outdated
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would move this above the code so all the documentation is above the code.
docs/howto-api-image-export.md
Outdated
chart_json['width'] = 916 | ||
chart_json['height'] = 484 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would make these numbers global constants.
docs/howto-api-image-export.md
Outdated
load_dotenv() | ||
|
||
username = os.getenv('XDMOD_USERNAME') | ||
password = os.getenv('XDMOD_PASSWORD') | ||
site_address = "<XDMOD_URL_HERE>" | ||
image_format = "svg" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
load_dotenv() | |
username = os.getenv('XDMOD_USERNAME') | |
password = os.getenv('XDMOD_PASSWORD') | |
site_address = "<XDMOD_URL_HERE>" | |
image_format = "svg" | |
site_address = "" | |
image_format = "svg" | |
load_dotenv() | |
username = os.getenv('XDMOD_USERNAME') | |
password = os.getenv('XDMOD_PASSWORD') |
I would suggest keeping the value of site_address
blank so people don't think they're supposed to put the URL in between the angle brackets and so that they don't think they should replace the other all caps strings nearby with their usernames and passwords. I'd also suggest grouping the global constants together and moving them to the top so all the things the user would want to edit are in one place near the top.
docs/howto-api-image-export.md
Outdated
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 | ||
``` | ||
|
||
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. Confirm the `image_format` within the script. | ||
|
||
The script will export your saved metric explorer charts to the current working directory. **\*Note:** Replace `<XDMOD_URL_HERE>` with your site address. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 | |
``` | |
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. Confirm the `image_format` within the script. | |
The script will export your saved metric explorer charts to the current working directory. **\*Note:** Replace `<XDMOD_URL_HERE>` with your site address. | |
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 `python-dotenv` and `requests` dependencies. | |
1. Create a `.env` file in the same directory as the script that contains the following contents, replacing `<username>` with your XDMoD username and `<password>` with your XDMoD password — make sure to secure this file with read-only access. | |
``` | |
XDMOD_USERNAME=<username> | |
XDMOD_PASSWORD=<password> | |
``` | |
1. Update the value of `site_address` within the script with the URL associated with your XDMoD portal. | |
1. Confirm the `image_format` within the script. |
I would also suggest making the path where the images are written a global constant.
@aaronweeden Updated per your feedback. You can now export either a single chart or all your saved charts to a specified path. |
docs/howto-api-image-export.md
Outdated
@@ -0,0 +1,93 @@ | |||
The following Python script will export your saved Metric Explorer charts. An XDMoD username and password is required to run the script. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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. |
docs/howto-api-image-export.md
Outdated
@@ -0,0 +1,93 @@ | |||
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 `python-dotenv` and `requests` dependencies. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1. Install the required `python-dotenv` and `requests` dependencies. | |
1. Install the required `python-dotenv` and `requests` Python dependencies (e.g., using `pip`). |
docs/howto-api-image-export.md
Outdated
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 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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 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. | |
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. | |
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. |
docs/howto-api-image-export.md
Outdated
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.') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.') |
The type will already be string, and None
will be the default. For simplicity, I'd stick with these.
import json | ||
import urllib | ||
import argparse | ||
from dotenv import load_dotenv |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
from dotenv import load_dotenv | |
from dotenv import load_dotenv | |
import sys |
docs/howto-api-image-export.md
Outdated
print('Specified chart not found.') | ||
exit() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
print('Specified chart not found.') | |
exit() | |
print('Specified chart not found.', file=sys.stderr) | |
sys.exit(1) |
docs/howto-api-image-export.md
Outdated
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']): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if args.name != '' and not any(chart_obj['name'] == args.name for chart_obj in saved_charts_data['data']): | |
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.') | ||
exit() | ||
|
||
for idx, chart in enumerate(saved_charts_data['data']): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for idx, chart in enumerate(saved_charts_data['data']): | |
for idx, chart in enumerate(saved_charts_data['data']): | |
if args.name is not None and args.name != chart['name']: | |
continue |
and then you don't need to wrap the writing in an if/else or needlessly fetch each chart that isn't going to be written.
image_format = 'svg' | ||
image_width = 916 | ||
image_height = 484 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if site_address == '': | |
print('Please edit the script to specify a site_address.', file=sys.stderr) | |
sys.exit(1) | |
docs/howto-api-image-export.md
Outdated
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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_path + chart_name, "wb") as f: | |
f.write(chart_response.content) |
See suggestion above with continue
.
Description
This documentation contains a Python script that uses the XDMoD API to authenticate with a local XDMoD account for the given credentials and site URL. Then the script will export the saved Metric Explorer charts into the current working directory. This documentation also describes how to edit certain parts of the script and where to find more information about the request schema for the request that exports the images.
Motivation and Context
We received a ticket asking about using the XDMoD API to export saved images for an Open XDMoD - Open OnDemand integration. This documentation will be used with the response and for future tickets that refer to issues about exporting images with the XDMoD API.
Tests performed
Tested script locally.
Checklist: