Using the Wave API for Backups

Following our last post on using the Wave API to create targeted campaigns, we will now use the API to backup Wave assets as document sObjects.

The Wave API allows us to access the metadata around Wave assets, whether they are JSON files behind dashboards and lenses, or metadata around Wave Apps. To do a backup of dashboards and lenses, the asset endpoints can be used. Specifically they are /wave/dashboards and /wave/lenses.

Step 1: Get the Dashboard IDs

To start, the IDs are collected for all the dashboards available to the user using Apex:

//Generic REST GET method
    global static string makeGetRequest(string endpoint, String sessionId){
        HttpRequest req = new HttpRequest();
        req.setEndpoint('https://' + System.URL.getSalesforceBaseUrl().getHost() + '/services/data/v37.0/wave/' + endpoint);
        req.setHeader('Authorization', 'Bearer ' + sessionId);
        req.setMethod('GET');
        
        HttpResponse res = new HttpResponse();
        Http http = new Http();
        res = http.send(req);
        
        return res.getBody();
    }

string response = makeGetRequest('dashboards'); //actually make the REST call

If a user has access to many dashboards, sometimes you need to page through them to collect all the user’s dashboards. You can do this by using the query arguments “page” and “pageSize” in the API URL (more on this here). Once a response is received with a list of all the dashboards available to the current user, we can parse the response to find the ID of each dashboard:

Map<String, Object> dashboardsDeserialized = (Map<String, Object>)JSON.deserializeUntyped(response);
List<Object> dashboards = (List<Object>);
dashboardsDeserialized.get('dashboards'); //dashboards are represented in a List

List<String> dashboardIds = new List<String>(); //this will be the resulting list of dashboardIds

for (Object dash : dashboards){
    dashboardIds.add(((String)((Map<String, Object>)dash).get('id')));
}

Step 2: Get the State of Each Dashboard

Once we have our list of dashboardIds, we can iterate over them to get the “state” node from each underlying JSON file. This node is the primary one defining all the queries and widgets on your dashboard. The other nodes are based on system defined properties which we cannot use later when restoring our dashboards and hence they will be ignored here. To do this, the future annotation for methods will be used which has a much greater limit of Http callouts than a typical synchronous method.

    @future(callout=true)
    global static void getDashboardJsonsAndSave(List dashIds, string sessionId){
        
        List<Document> docsToSave = new List<Document>();
        for (String dashId: dashIds){
            String dashResponse = makeRequest('dashboards/' + dashId, 'GET', sessionId);
            Map<String, Object> jsonDeserial;
            try{
        		jsonDeserial = ((Map<String, Object>)JSON.deserializeUntyped(dashResponse));
            }
            catch(Exception e){
                continue;
            }
             
            String fileName = (String)jsonDeserial.get('name');
            String stateNode = JSON.serialize(jsonDeserial.get('state'));
            
            Document d = new Document();
            d.Name = fileName + '_' + system.today().format() + '.json';
            d.Body = Blob.valueOf(stateNode);
            d.ContentType = 'application/json';
            d.Type = 'json';
            d.FolderId = '00lB0000000mh2T'; //A folder was created beforehand specifically for Wave backups
            docsToSave.add(d);            
        }
        insert docsToSave;
    }

Now that you have the dashboard IDs, you can call the method:

getDashboardJsonsAndSave(dashboardIds, UserInfo.getSessionId());

Implementation Notes

  • This process can be extended to download other assets such as lenses and dataset XMD JSON files. After the actual download of assets is completed, it can also save these JSON files in version control systems such as Git.
  • Since this is an async method, it will be queued to run when resources are available. The Apex process can be monitored by going into Setup->Monitor->Apex Jobs.
  • After the process completes, the user’s dashboards are stored in a folder with the name “<dashboard Name>_<system date>.json”.
  • This process could be run on a scheduled basis using the Apex Scheduler, or by a user invoking the process. Note that if a user invokes the process, it’s still an asynchronous method, and will run when resources are available.

About the Author

Tomasz Bacewicz is a Senior Consultant dedicated to providing solutions in accelerating & optimizing implementation of Wave. Tomasz is a member of the Analytics Cloud’s ASG team.

About the Advanced Solutions Group

A specialized analytics team in the Analytics Cloud product organization that provides world-class technical leadership and guidance focused on empowering the Wave ecosystem. ASG helps drive rapid value by leveraging deep technical knowledge of the Wave platform and apps.

Leave your comments...

Using the Wave API for Backups