Quick start Rest API + ADLSG2

  • 29 April 2024
  • 0 replies
  • 50 views

Userlevel 4
Badge +6

Dear Community,

 

I've noticed a growing interest among clients in leveraging AIMMS' Rest API and ADLSG2 (Azure Data Lake Storage Gen 2) services. To assist you on this journey, here are some helpful pointers:

 

References:

  1. Developing an AIMMS Service:

  2. Setting up an Endpoint in AIMMS:

  3. Using AIMMS Services from Python:

  4. Connecting to and Managing ADLSG2:

 

Quick-start Guide:

Here's a simplified AIMMS application that performs the following tasks:

  • Downloads files from a fixed path on your ADSLG2.
  • Reads the files.
  • Executes optimization tasks.
  • Writes the results to local files.
  • Uploads the files back to ADLSG2.

Within the AIMMS Project:

Create a procedure that will be run as the task. Remember to add the dex::ServiceName  annotation as the endpoint name.

Procedure pr_endPointSolve {
Body: {
! Download data from ADLSG2
dex::dls::DownloadFiles(sp_fileSystem,sp_pathPrefix,sp_directory,recursive: 0);
! Reads the data from the file
dex::ReadFromFile(sp_directory + sp_inputFileName, sp_inputMappingName);
! Simple solve
solve mp_MyModel;
! Writes results to a file
dex::WriteToFile(sp_directory + sp_outputFileName, sp_outputMappingName);
! Uploads files to ADLSG2
dex::dls::UploadFile(sp_fileSystem, sp_directory + sp_outputFileName);
}
dex::ServiceName: optimizeEndPoint;
}

Publish the project on your could account!

Within a Python Script:

import requests
import time
import json

# Define constants
URL = https://YOURURL.aimms.cloud/pro-api/v2
APP_NAME = YOURAPPNAME # Replace with the APP name
APP_VERSION = 'YOURVERSION' # Replace with the APP Version
API_KEY = 'YOURAPIKEY' # Replace with the API Key you generate in the cloud portal – needs to have the task create permission
SERVICE_NAME = 'YOURSERVICENAME' # Replace with the taskname used in the AIMMS project

# Define headers
headers = {
'apiKey': API_KEY
}

# Running task
url_run = f"{URL}/tasks/{APP_NAME}/{APP_VERSION}/{SERVICE_NAME}/"
# url_run = f"{URL}/tasks/{APP_NAME}/{SERVICE_NAME}/" Leaving out the APP_VERSION will use the “latest tag” from the portal


response_submit = requests.post(url_run, headers=headers)
response_json = response_submit.json()
print(response_json)

# Get task ID
sp_id = response_json.get('id')

# Polling task status
url_poll = f"{URL}/tasks/{sp_id}/"
print("STATUS:")
state = ""
while state != "completed":
time.sleep(1)
response_poll = requests.get(url_poll, headers=headers)
state = response_poll.json().get('state')
print(state)

# Get task response
url_response = f"{URL}/tasks/{sp_id}/response"
response_task = requests.get(url_response, headers=headers)
print(response_task.text)

# Get all tasks
url_all_tasks = f"{URL}/tasks"
print("All tasks:")
params = {
'appName': APP_NAME,
'appVersion': APP_VERSION
}
response_poll = requests.get(url_all_tasks, headers=headers, params=params)
all_tasks = response_poll.json()
print(json.dumps(all_tasks, indent=4))

An output should be:

{'id': 'db64dd6b-9baa-4dd5-b32a-543d91364ea8', 'groupIndex': None, 'appName': 'FlowshopSolver', 'appVersion': '002', 'serviceName': 'flowshopSolver', 'state': 'queued', 'scheduleFor': None, 'scheduleInterval': None, 'createdAt': '2024-04-10 13:34:44.000000', 'assignedAt': None, 'runningAt': None, 'endedAt': None, 'queueTime': 0.758669, 'runTime': None, 'returnCode': None, 'errorMessage': None}

STATUS:

queued

queued

queued

queued

queued

queued

queued

queued

completed



All tasks:

{

    "total_tasks": 2,

    "current_batch": [

        {

            "id": "cbcb8911-6133-46d2-8f39-c2fb7c4d3f53",

            "groupIndex": null,

            "appName": "FlowshopSolver",

            "appVersion": "002",

            "serviceName": "flowshopSolver",

            "state": "completed",

            "scheduleFor": null,

            "scheduleInterval": null,

            "createdAt": "2024-04-08 15:56:11.000000",

            "assignedAt": "2024-04-08 15:56:22.000000",

            "runningAt": "2024-04-08 15:56:22.000000",

            "endedAt": "2024-04-08 15:56:22.000000",

            "queueTime": 11.000001,

            "runTime": 0.0,

            "returnCode": 1,

            "errorMessage": null

        },

        {

            "id": "714792f8-be04-4566-8ae9-e214de0080ea",

            "groupIndex": null,

            "appName": "FlowshopSolver",

            "appVersion": "002",

            "serviceName": "flowshopSolver",

            "state": "completed",

            "scheduleFor": null,

            "scheduleInterval": null,

            "createdAt": "2024-04-08 15:59:29.000000",

            "assignedAt": "2024-04-08 15:59:29.000000",

            "runningAt": "2024-04-08 15:59:30.000000",

            "endedAt": "2024-04-08 15:59:30.000000",

            "queueTime": 0.0,

            "runTime": 0.0,

            "returnCode": 1,

            "errorMessage": null

        }

    ]

}

 

This setup allows you to streamline interactions between AIMMS projects and external services using REST APIs. Feel free to explore these resources further and adapt them to your specific use cases.

Happy optimizing!


0 replies

Be the first to reply!

Reply


Didn't find what you were looking for? Try searching on our documentation pages:

AIMMS Developer & PRO | AIMMS How-To | AIMMS SC Navigator