Skip to content

Commit dbfb4df

Browse files
committed
Address feedback
1 parent 38f05f2 commit dbfb4df

File tree

2 files changed

+40
-29
lines changed

2 files changed

+40
-29
lines changed

docs/howto-api-image-export.md

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,39 @@
1-
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.
2-
3-
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:
4-
5-
```shell
6-
pip install python-dotenv
7-
pip install requests
8-
```
9-
1+
The following Python script will export your saved Metric Explorer charts. An XDMoD username and password is required to run the script.
102
Before running the script,
11-
12-
1. Install the required dependencies listed above.
13-
1. Create a `.env` file with your local XDMoD account credentials in the same directory as the script.
14-
1. Update `site_address` within the script with site address associated with your XDMoD instance.
3+
1. Install the required `python-dotenv` and `requests` dependencies.
4+
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.
5+
```
6+
XDMOD_USERNAME=<username>
7+
XDMOD_PASSWORD=<password>
8+
```
9+
1. Update the value of `site_address` within the script with the URL associated with your XDMoD portal.
10+
1. Update the value of `export_path` within the script with the desired path to export the images.
1511
1. Confirm the `image_format` within the script.
1612
17-
The script will export your saved metric explorer charts to the current working directory. **\*Note:** Replace `<XDMOD_URL_HERE>` with your site address.
13+
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.
1814
1915
```python
2016
#!/usr/bin/env python3
2117
import os
2218
import requests
2319
import json
2420
import urllib
21+
import argparse
2522
from dotenv import load_dotenv
2623
27-
load_dotenv()
24+
site_address = ''
25+
export_path = ''
26+
image_format = 'svg'
27+
image_width = 916
28+
image_height = 484
2829
30+
load_dotenv()
2931
username = os.getenv('XDMOD_USERNAME')
3032
password = os.getenv('XDMOD_PASSWORD')
31-
site_address = "<XDMOD_URL_HERE>"
32-
image_format = "svg"
33+
34+
parser = argparse.ArgumentParser(description='Export XDMoD saved Metric Explorer charts with the REST API.')
35+
parser.add_argument('-n', '--name',type=str, default='', help='Specify the chart name of a saved chart to export.')
36+
args = parser.parse_args()
3337
3438
session = requests.Session()
3539
@@ -42,14 +46,18 @@ if auth_response.status_code != 200:
4246
auth_response = auth_response.json()
4347
4448
header = {
45-
'Token': auth_response['results']['token'],
46-
'Authorization': auth_response['results']['token'],
47-
'Content-Type': 'application/x-www-form-urlencoded'
49+
'Token': auth_response['results']['token'],
50+
'Authorization': auth_response['results']['token'],
51+
'Content-Type': 'application/x-www-form-urlencoded'
4852
}
4953
50-
saved_charts = session.get(f'{site_address}/rest/v1/metrics/explorer/queries', headers=header, cookies=session.cookies)
54+
saved_charts = session.get(f'{site_address}/rest/v1/metrics/explorer/queries', headers=header)
5155
saved_charts_data = saved_charts.json()
5256
57+
if args.name != '' and not any(chart_obj['name'] == args.name for chart_obj in saved_charts_data['data']):
58+
print('Specified chart not found.')
59+
exit()
60+
5361
for idx, chart in enumerate(saved_charts_data['data']):
5462
if 'config' in chart:
5563
chart_json = json.loads(chart['config'])
@@ -69,14 +77,17 @@ for idx, chart in enumerate(saved_charts_data['data']):
6977
chart_json['controller_module'] = "metric_explorer"
7078
chart_json['show_title'] = "y"
7179
chart_json['format'] = image_format
72-
chart_json['width'] = 916
73-
chart_json['height'] = 484
80+
chart_json['width'] = image_width
81+
chart_json['height'] = image_height
7482
75-
chart_response = session.post(f'{site_address}/controllers/metric_explorer.php', data=chart_json, headers=header, cookies=session.cookies)
83+
chart_response = session.post(f'{site_address}/controllers/metric_explorer.php', data=chart_json, headers=header)
7684
chart_name = f"{chart['name']}.{image_format}" if ('name' in chart) else f"xdmod_API_export_{idx}.{image_format}"
7785
78-
with open(chart_name, "wb") as f:
79-
f.write(chart_response.content)
86+
if args.name != '' and args.name == chart['name']:
87+
with open(export_path + chart_name, "wb") as f:
88+
f.write(chart_response.content)
89+
exit()
90+
else:
91+
with open(export_path + chart_name, "wb") as f:
92+
f.write(chart_response.content)
8093
```
81-
82-
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.

docs/howto.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ The Open XDMoD HOWTOs are "how to" documents on specific subjects.
88
- [Change Metric Explorer Colors](howto-colors.html)
99
- [Enable Node Utilization Statistics](howto-node-utilization.html)
1010
- [Reconstruct Slurm Accounting Logs](howto-reconstruct-slurm.html)
11-
- [Export Saved Metric Explorer Charts Through the XDMOD API](howto-api-image-export.md)
11+
- [Export Saved Metric Explorer Charts Through the REST API](howto-api-image-export.md)

0 commit comments

Comments
 (0)