Skip to content
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

Open
wants to merge 19 commits into
base: xdmod11.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions docs/howto-api-image-export.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
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.
aaronweeden marked this conversation as resolved.
Show resolved Hide resolved

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`

Before running the script,

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.

```python
#!/usr/bin/env python3
import os
import requests
import json
import urllib
from dotenv import load_dotenv
aaronweeden marked this conversation as resolved.
Show resolved Hide resolved

load_dotenv()

username = os.getenv('XDMOD_USERNAME')
password = os.getenv('XDMOD_PASSWORD')
site_address = ""
aaronweeden marked this conversation as resolved.
Show resolved Hide resolved
image_format = "svg"

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()
aaronweeden marked this conversation as resolved.
Show resolved Hide resolved

auth_response = auth_response.json()

header = {
'Token': auth_response['results']['token'],
'Authorization': auth_response['results']['token'],
'Content-Type': 'application/x-www-form-urlencoded'
aaronweeden marked this conversation as resolved.
Show resolved Hide resolved
}

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']):
aaronweeden marked this conversation as resolved.
Show resolved Hide resolved
aaronweeden marked this conversation as resolved.
Show resolved Hide resolved
if 'config' in chart:
chart_json = json.loads(chart['config'])
for attribute in chart_json:
if (isinstance(chart_json[attribute], dict)):
aaronweeden marked this conversation as resolved.
Show resolved Hide resolved
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['show_title'] = "y"
chart_json['format'] = image_format
chart_json['width'] = 916
chart_json['height'] = 484
aaronweeden marked this conversation as resolved.
Show resolved Hide resolved

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:
f.write(chart_response.content)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
f.write(chart_response.content)
f.write(chart_response.content)
print('Wrote ' + export_dir + '/' + chart_name)

```

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.
aaronweeden marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions docs/howto.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
aaronweeden marked this conversation as resolved.
Show resolved Hide resolved