- 
                Notifications
    You must be signed in to change notification settings 
- Fork 74
Added documentation for exporting saved Metric Explorer charts with the XDMoD API #1907
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
          
     Closed
      
      
    
  
     Closed
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            19 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      b4f934a
              
                First draft at XDMoD API image export documentation
              
              
                aestoltm c1d7a8d
              
                Wordsmith
              
              
                aestoltm 7c9951e
              
                More wording adjustments
              
              
                aestoltm 1b3ccf2
              
                More editing
              
              
                aestoltm 882dad4
              
                More edits
              
              
                aestoltm a13948b
              
                Fix site address wording
              
              
                aestoltm 00b2b7c
              
                More updates
              
              
                aestoltm 3cb6496
              
                Address feedback
              
              
                aestoltm 38f05f2
              
                Address feedback
              
              
                aestoltm dbfb4df
              
                Address feedback
              
              
                aestoltm 398000e
              
                Address feedback #2
              
              
                aestoltm 8aa2f52
              
                Fix variable name
              
              
                aestoltm 7644d0f
              
                Fix more variable names
              
              
                aestoltm f0b7976
              
                Default export directory should be working directory
              
              
                aestoltm d9c4055
              
                Merge branch 'xdmod11.0' into rest_api_image_export
              
              
                aestoltm ee3b203
              
                Merge branch 'xdmod11.0' into rest_api_image_export
              
              
                aestoltm eda10eb
              
                Merge branch 'xdmod11.0' into rest_api_image_export
              
              
                aestoltm 5d9a1ec
              
                Merge branch 'xdmod11.0' into rest_api_image_export
              
              
                aestoltm dc5b3f3
              
                Address additional feedback
              
              
                aestoltm File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,98 @@ | ||||||||
| 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` Python dependencies (e.g., using `pip`). | ||||||||
| 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` at the top of the script with the URL associated with your XDMoD portal. | ||||||||
| 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. | ||||||||
|  | ||||||||
| 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 | ||||||||
| import os | ||||||||
| import requests | ||||||||
| import json | ||||||||
| import urllib | ||||||||
| import argparse | ||||||||
| from dotenv import load_dotenv | ||||||||
| import sys | ||||||||
|  | ||||||||
| site_address = '' | ||||||||
| export_dir = '.' | ||||||||
| image_format = 'svg' | ||||||||
| width = 916 | ||||||||
| height = 484 | ||||||||
|  | ||||||||
| if site_address == '': | ||||||||
| print('Please edit the script to specify a site_address.', file=sys.stderr) | ||||||||
| sys.exit(1) | ||||||||
|  | ||||||||
|         
                  aaronweeden marked this conversation as resolved.
              Show resolved
            Hide resolved | ||||||||
| 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', help='Specify the chart name of a saved chart to export.') | ||||||||
| args = parser.parse_args() | ||||||||
|  | ||||||||
| 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.', file=sys.stderr) | ||||||||
| quit(1) | ||||||||
|  | ||||||||
| 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(f'{site_address}/rest/v1/metrics/explorer/queries', headers=header) | ||||||||
| saved_charts_data = saved_charts.json() | ||||||||
|  | ||||||||
| 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']): | ||||||||
|         
                  aaronweeden marked this conversation as resolved.
              Show resolved
            Hide resolved         
                  aaronweeden marked this conversation as resolved.
              Show resolved
            Hide resolved | ||||||||
| 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: | ||||||||
| chart_parameter = chart_json[attribute] | ||||||||
| if (isinstance(chart_parameter, dict)): | ||||||||
| if 'data' in attribute: | ||||||||
| encoded_str = urllib.parse.quote_plus(str(chart_parameter['data'])) | ||||||||
| else: | ||||||||
| 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_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' | ||||||||
| chart_json['show_title'] = 'y' | ||||||||
| chart_json['format'] = image_format | ||||||||
| 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}' | ||||||||
|  | ||||||||
| with open(export_dir + '/' + chart_name, 'wb') as f: | ||||||||
| f.write(chart_response.content) | ||||||||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 
        Suggested change
       
 | ||||||||
| print('Wrote ' + export_dir + '/' + chart_name) | ||||||||
| ``` | ||||||||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.