Continuous Integration/Continuous Delivery – Using Python and REST APIs for SAS Visual Analytics reports

10

With increasing interest in Continuous Integration/Continuous Delivery (CI/CD), many SAS Users want to know what can be done for Visual Analytics reports. In this article, I will explain how to use Python and SAS Viya REST APIs to extract a report from a SAS Viya environment and import it into another environment. For those trying to understand the secret behind CI/CD and DevOps, here it is:

What you do tomorrow will be better than what you did yesterday because you gained more experience today!

About Continuous Integration/Continuous Delivery

If you apply this principle to code development, it means that you may update your code every day, test it, deploy it, and start the process all over again the following day. You might feel like Sisyphus rolling his boulder around for eternity. This is where CI/CD can help. In the code deployment process, you have many recurrent tasks that can be automated to reduce repetitiveness and boredom. CI/CD is a paradigm where improvements to code are pushed, tested, validated, and deployed to production in a continuous automated manner.

About ModelOps and AnalyticOps

I hope you now have a better understanding of what CI/CD is. You might now wonder how CI/CD relates to Visual Analytics reports, models, etc. With the success of DevOps which describes the Development Operations for software development, companies have moved to the CI/CD paradigms for operations not related to software development. This is why you hear about ModelOps, AnalyticOps... Wait a second, what is the difference between writing or generating code for a model or report versus writing code for software? You create a model, you test it, you validate it, and finally deploy it. You create a report, you test it, you validate it, and then you deploy it. Essentially, the processes are the same. This is why we apply CI/CD techniques to models, reports, and many other business-related tasks.

About tools

As with many methodologies like CI/CD, tools are developed to help users through the process. There are many tools available and some of them are more widely used. SAS offers SAS Workflow Manager for building workflows to help with ModelOps. Additionally, you have surely heard about Git and maybe even Jenkins.

  • Git is a version control system that is used by many companies with some popular implementations: GitHub, GitLab, BitBucket.
  • Jenkins is an automation program that is designed to build action flows to ease the CI/CD process.

With these tools, you have the needed architecture to start your CI/CD journey.

The steps

With a basic understanding of the CI/CD world; you might ask yourself: How does this apply to reports?

When designing a report in an environment managed by DevOps principles, here are the steps to deploy the report from a development environment to production:

  1. Design the report in development environment.
  2. Validate the report with Business stakeholders.
  3. Export the report from the development environment.
  4. Save a version of the report.
  5. Import the report into the test environment.
  6. Test the report into the test environment.
  7. Import the report into the production environment.
  8. Monitor the report usage and performance.

Note: In some companies, the development and test environments are the same. In this case, steps 4 to 6 are not required.

Walking through the steps, we identify steps 1 and 2 are manual. The other steps can be automated as part of the CI/CD process. I will not explain how to build a pipeline in Jenkins or other tools in this post. I will nevertheless provide you with the Python code to extract, import, and test a report.

The code

To perform the steps described in the previous section, you can use different techniques and programming languages. I’ve chosen to use Python and REST APIs. You might wonder why I've chosen these and not the sas-admin CLI or another language. The reason is quite simple: my Chinese zodiac sign is a snake!

Jokes aside, I opted for Python because:

  • It is easy to read and understand.
  • There is no need to compile.
  • It can run on different operating systems without adaptation.
  • The developer.sas.com site provides examples.

I’ve created four Python files:

  1. getReport.py: to extract the report from an environment.
  2. postReport.py: to create the report in an environment.
  3. testReport.py: to test the report in an environment.
  4. functions.py: contains the functions that are used in the other Python files.

All the files are available on GitHub.

The usage

Before you use the above code, you should configure your SAS Viya environment to enable access to REST APIs. Please follow the first step from this article to register your client.

You should also make sure the environment executing the Python code has Python 3 with the requests package installed. If you're missing the requests package, you will get an error message when executing the Python files.

Get the report

Now that your environment is set up, you can execute the getReport code to extract the report content from your source environment. Below is the command line arguments to pass to execute the code:

python3 getReport.py -a myAdmin -p myAdminPW -sn http://va85.gel.sas.com -an app -as appsecret -rl "/Users/sbxxab/My Folder" -rn CarsReport -o /tmp/CICD/

The parameters are:

  • a - the user that is used to connect to the SAS Viya environment.
  • p - the password of the user.
  • sn - the URL of the SAS Viya environment.
  • an - the name of the application that was defined to enable REST APIs.
  • as - the secret to access the REST APIs.
  • rl - the report location should be between quotes if it contains white spaces.
  • rn - the report name should be between quotes if it contains white spaces.
  • o - the output location that will be used.

The output location should ideally be a Git repository. This allows a commit as the next step in the CI/CD process to keep a history log of any report changes.

The output generated by getReport is a JSON file which has the following structure:

{
"name": "CarsReport",
"location": "/Users/sbxxab/My Folder",
"content": {
"@element": "SASReport",
"xmlns": "http://www.sas.com/sasreportmodel/bird-4.2.4",
"label": "CarsReport",
"dateCreated": "2020-01-14T08:10:31Z",
"createdApplicationName": "SAS Visual Analytics 8.5",
"dateModified": "2020-02-17T15:33:13Z",
"lastModifiedApplicationName": "SAS Visual Analytics 8.5",
"createdVersion": "4.2.4",
"createdLocale": "en",
"nextUniqueNameIndex": 94,
 
...
 
}

From the response:

  • The name value is the report name.
  • The location value is the folder location in the SAS Content.
  • The content value is the BIRD representation of the report.

The generated file is not a result of the direct extraction of the report in the SAS environment. It is a combination of multiple elements to build a file containing the required information to import the report in the target environment.

Version the report

The next step in the process is to commit the file within the Git repository. You can use the git commit command followed by a git push to upload the content to a remote repository. Here are some examples:

# Saving the report in the local repository
git commit -m "Save CarsReport"
 
# Pushing the report to a remote repository after a local commit
git push https://gitlab.sas.com/myRepository.git

Promote the report

As soon as you have saved a version of the report, you can import the report in the target environment (production). This is where the postReport comes into play. Here is a sample command line:

python3 postReport.py -a myAdmin -p myAdminPW -sn http://va85.gel.sas.com -an app -as appsecret -i /tmp/CICD/CarsReport.json

The parameters are:

  • a - the user that is used to connect to the SAS Viya environment.
  • p - the password of the user.
  • sn - the URL of the SAS Viya environment.
  • an - the name of the application that was defined to enable REST APIs.
  • as - the secret to access the REST APIs.
  • i - the input JSON file which contains the output of the getReport.py.

The execution of the code returns nothing except in the case of an error.

Testing the report

You now have access to the report in the production environment. A good practice is to test/validate access to the report. While testing manually in an interface is possible, it's best to automate. Sure, you could validate one report, but what if you had twenty? Use the testReport script to verify the report. Below are the command line arguments to execute the code:

python3 testReport.py -a myAdmin -p myAdminPW -sn http://va85.gel.sas.com -an app -as appsecret -i /tmp/CICD/CarsReport.json

The parameters are:

  • a - the user that is used to connect to the SAS Viya environment.
  • p - the password of the user.
  • sn - the URL of the SAS Viya environment.
  • an - the name of the application that was defined to enable REST APIs.
  • as - the secret to access the REST APIs.
  • i - the input JSON file which contains the output of the getReport.py

The testReport connects to SAS Visual Analytics using REST APIs and generates an image of the first section of the report. The image generation process produces an SVG image of little interest. The most compelling part of the test is the duration of the execution. This gives us an indication of the report's performance.

Throughout the CI/CD process, validation is important and it is also interesting to get a benchmark for the report. This is why the testReport populates a .perf file for the report. The response file has the following structure:

{
"name": "CarsReport",
"location": "/Users/sbxxab/My Folder",
"performance": [
{
"testDate": "2020-03-26T08:55:37.296Z",
"duration": 7.571
},
{
"testDate": "2020-03-26T08:55:56.449Z",
"duration": 8.288
}
]
}

From the response:

  • The name value is the report name.
  • The location value is the folder location in the SAS Content.
  • The performance array contains the date time stamp of the test and the time needed to generate the image.

The file updates for each execution of the testReport code.

Conclusion

CI/CD is important to SAS. Many SAS users need solutions to automate their deployment processes for code, reports, models, etc. This is not something to fear because SAS Viya has the tools required to integrate into CI/CD pipelines. As you have seen in this post, we write code to ease the integration. Even if you don’t have CI/CD tools like Jenkins to orchestrate the deployment, you can execute the different Python files to promote content from one environment to another and test the deployed content.

If you want to get more information about ModelOps, I recommend to have a look at this series.

Share

About Author

Xavier Bizoux

Principal Technical Architect

Xavier is recognized for his expertise in business intelligence, administration, data visualization and programming. He joined SAS Technical Support in Belgium where he covered data management, business intelligence and SAS solutions. Since 2008, his main focus became mastering SAS business intelligence from both the visualization and architecture perspectives. In 2018, Xavier took on a new challenge and joined the Global Enablement and Learning team, part of SAS R&D, which seeks to disseminate knowledge with our internal colleagues, SAS partners, and ultimately our customers to achieve actionable insights through data visualization.

10 Comments

  1. David Mitrovic on

    Hi Xavier, great post but I have a question about the following arguments:
    -an app
    -as appsecret

    What do you mean with this arguments? How I can recreate this in our Viya environment?

    Thanks in advance.

    • Xavier Bizoux
      Xavier Bizoux on

      Hi David,

      When you are using REST API's, you need to register your application within SAS Viya. When doing so, you need to register a client and during the registration process you specify an Application Name and an Application Secret. You can find more information about registering the client from https://developer.sas.com/reference/auth/#register. The following blog might also be of interest to you: https://blogs.sas.com/content/sgf/2019/01/25/authentication-to-sas-viya/

      Please let me know if you have questions.

      Regards,

      Xavier

      • David Mitrovic on

        Hi Xavier,

        Thanks, we can connect with REST-API to the environment with the command

        python3 postReport.py -a user-p password -sn https://servername -an sas.cli -as "" -i /tmp/reports/report.json

        to get the report but when we try to upload the report with some changes (location and libraries) if the report exists in the destination folder the function "createReport" first delete the existing report and after uploads the new report. Is possible to overwrite in case of the report exists? In our client we work with the URI references of the reports and if this changes we can't send the Production report URI to the users.

        Thanks in advance for your help!

          • David Mitrovic on

            Thanks Xavier,
            Don't worry, I will try to install the viyaRestpy package from its github because when I try to use it it does not appear in the available modules even when installing it correctly. I do not know if it will influence that it is a RHEL7.

            I will update with my progress

            • David Mitrovic on

              Hi Xavier,

              I have the python modules already installed and when I try to run the programs get_report.py or update_report.py I have the following message about the Metadata folder:
              Folder '/Users/username/My Folder' doesn't exist.

              I have tried with "/My Folder" or "/folders/folders/{uri}" and no ones works.

              Thanks in advance for your help.

              • Xavier Bizoux

                Hi David,
                Are you able to get the authentication token?
                Would you please send me the code you are using? That would greatly help in understanding the issue?
                Regards,
                Xavier

                • David Mitrovic on

                  Yes Xavier, I'm running the following code:

                  python3 update_report.py -u username -p password -sn https://servername -an sas.cli -as "" -i /tmp/reports/report_input.json

                  And the answer is:

                  Folder '/Users/username/My Folder' doesn't exist.

                  I have defined the environment variable id_token with a valid token for the REST-API.

                  Thanks for your help.

                  • Xavier Bizoux

                    Hi David,

                    The command line seems to be correct. To be 100% sure, could you please confirm that you have changed username and password by real values.

                    python3 update_report.py -u toto -p titi -sn https://servername -an sas.cli -as "" -i /tmp/reports/report_input.json

                    The library relies on a specific authentication mechanism. You have the options to pass the userid and password through the command line or use an authentication token generated by the sas-admin cli.
                    I would also like to know how you extracted the report information to the report_input.json file. The input file should have a specific structure in order to be imported successfully.

                    Would that be an option to plan a meeting together in order to go through the steps together?

                    Regards,

                    Xavier

Leave A Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to Top