Preface: Utilizing CA Certificates is the best practice for Anaplan Data Integrations. The wake-up script is best leveraged with CA Certificate authentication in the individual scripts for security purposes. If you would like to learn more about CA Certificates, click on this guide which I co-authored. Note: Basic Authentication used for demo purposes.

Additionally, the wake-up script is not intended to be run 24x7x365, only during business workdays. The script should only be run to open the model for the first time. It is recommended that the script be run at most once per working day. It is important that the model goes to "sleep" when not in use to do garbage collection.


Imagine this scenario:
You have an Anaplan ecosystem as part of your organization. When you come in the morning to log into your Anaplan App or Model, you are met with a loading screen. This loading screen often takes 10 minutes plus. For data integrations, your team is exploring the use of Python!


Solution:
Create a wake up script! With the power of Python and Anaplan APIs, you can automate your model to “wake up” before you arrive to login for the day. This can save you time every morning you spend waiting for your large model to open.  


Here's how:
Creating a wake up script is a simple process!

  1. In your Anaplan model, create a process under the Actions menu
  2. Rename the process to “Wake Up Model”
  3. Do not add any actions to the process- this will be a blank “dummy” process

It should look something like this:

No alt text provided for this image

Your Anaplan model is now ready to leverage Python!

Next you should:

  1. Download Python onto your local machine or server environment
  2. Create your Get Processes Script. This is the script where we will retrieve the ID for the "Wake Up Model" process. We will use this ID in step 3.
  3. Create your Wake Up Script. We will use the IDs retrieved in step 2.

Get Processes Script:

----------------------

#Get Processes Script

# This script returns all processes in the selected workspace and model to a
# json array saved in file 'processes.json'. Reference that file for future use


# This script assumes you know your workspaceGuid and modelGuid.
# If you do not have this information, please run 'getWorkspaces.py' and
# 'getModels.py' and retrieve this information from the resulting json files.


# If you are using certificate authentication, this script assumes you have
# converted your Anaplan certificate to PEM format, and that you know the
# Anaplan account email associated with that certificate.


# This script uses Python 3 and assumes that you have the following modules
# installed: requests, base64


import requests
import base64


# Insert your workspace Guid
wGuid = ''
# Insert your model Guid
mGuid = ''
# Insert the Anaplan account email being used
username = ''


# If using cert auth, replace cert.pem with your pem converted certificate
# filename. Otherwise, remove this line.
cert = open('cert.pem').read()


# If using basic auth, insert your password. Otherwise, remove this line.
password = ''


# Uncomment your authentication method (cert or basic). Remove the other.
user = 'AnaplanCertificate ' + str(base64.b64encode((
      f'{username}:{cert}').encode('utf-8')).decode('utf-8'))


# user = 'Basic ' + str(base64.b64encode((f'{username}:{password}'
#                                         ).encode('utf-8')).decode('utf-8'))


getHeaders = {
   'Authorization': user
}


getProcesses = requests.get(f'https://api.anaplan.com/1/3/workspaces/{wGuid}/'
                           + f'models/{mGuid}/processes',
                           headers=getHeaders)

with open('processes.json', 'wb') as f:
   f.write(getProcesses.text.encode('utf-8'))

----------------------


Your output should look something like this:

No alt text provided for this image

Create your Wake Up Script: (use IDs from result of above script)

----------------------

# This script runs your selected process. Run 'processStatus.py' to retrieve
# the task metadata for the process task.


# This script assumes you know your workspaceGuid, modelGuid, and process
# metadata.
# If you do not have this information, please run 'getWorkspaces.py',
# 'getModels.py', and 'getProcesses.py' and retrieve this information from the
# resulting json files.


# If you are using certificate authentication, this script assumes you have
# converted your Anaplan certificate to PEM format, and that you know the
# Anaplan account email associated with that certificate.


# This script uses Python 3 and assumes that you have the following modules
# installed: requests, base64, json


import requests
import base64
import sys
import json


# Insert your workspace Guid
wGuid = ''
# Insert your model Guid
mGuid = ''
# Insert the Anaplan account email being used
username = ''
# Replace with your process metadata
processData = {
 "id" : "YOUR PROCESS ID FROM GET PROCESSES SCRIPT",
 "name" : "YOUR PROCESS NAME FROM GET PROCESSES SCRIPT"
}


# If using cert auth, replace cert.pem with your pem converted certificate
# filename. Otherwise, remove this line.
cert = open('cert.pem').read()


# If using basic auth, insert your password. Otherwise, remove this line.
password = 'password'


# Uncomment your authentication method (cert or basic). Remove the other.
user = 'AnaplanCertificate ' + str(base64.b64encode((
      f'{username}:{cert}').encode('utf-8')).decode('utf-8'))


# user = 'Basic ' + str(base64.b64encode((f'{username}:{password}'
#                                         ).encode('utf-8')).decode('utf-8'))


url = (f'https://api.anaplan.com/1/3/workspaces/{wGuid}/models/{mGuid}/' +
      f'processes/{processData["id"]}/tasks')


postHeaders = {
   'Authorization': user,


   'Content-Type': 'application/json'
}


# Runs an import request, and returns task metadata to 'postImport.json'
print(url)
postProcess = requests.post(url,
                           headers=postHeaders,
                           data=json.dumps({'localeName': 'en_US'}))


print(postProcess.status_code)
with open('postProcess.json', 'wb') as f:
   f.write(postProcess.text.encode('utf-8'))

----------------------

Your folder should look something like this:


No alt text provided for this image

Note: To automate this script, a scheduler will be needed to run this action on a consistent basis.