Insight Scenario Access Package
Topics covered in this chapter:
Introduction
The Insight scenario access package mminsightscenario offers a high-level client for interacting with an Insight v5 server using the REST webservice interfaces. You can use mminsightscenario to create/update/delete apps, folders and scenarios; to read and write entity data, to access and update scenario and app attachments, as well as execute scenarios.
mminsightscenario can be used both inside and outside of Insight apps. When used within an Insight scenario, there is no relationship between the calls made through mminsightscenario and the currently running scenario (any reads/writes behave as though they came from outside of the scenario).
Relationship to mminsightrestapi
The mminsightscenario and mminsightrestapi packages both offer ways to interact with the Insight v5 REST webservice interface. mminsightscenario aims to offer a user-friendly API targeting the most commonly used functionality, whereas mminsightrestapi is a direct implementation of the webservice operations as Mosel subroutines. As mminsightrestapi can be more difficult to call, it is recommended developers use mminsightscenario where possible, and only use mminsightrestapi to perform tasks not supported by mminsightscenario.
If necessary, both packages can be used within the same model.
Getting Started with mminsightscenario
To use the package, import it at the top of your model, e.g.:
model mymodel imports 'mminsightscenario' namespace ins
When using the package from an Insight app, it is important to reference the package using imports and not uses, as mminsightscenario is not supplied with the Insight 5 worker and thus the app must have imported it at build-time.
All the subroutines and records defined in this package are within the ins namespace.
Before you can communicate with the Insight server, you must call the ins~login procedure to tell the package the URL of the Insight v5 server, and the client ID and secret values that will be used to authenticate your session. Where you are running Mosel without restrictions on your local account, it's recommended that you pass just the Insight URL and allow the client ID and secret to be read from the local secure storage (such as the Windows Credential Manager or Mac keychain; see the section Providing REST API credentials for instructions on configuring this).
ins~login('http://localhost:8080/') if ins~haserror then writeln_('error during login: ',ins~getlasterror) exit(1) end-if
But if this is not suitable for your use case, you can pass the client ID and secret using the insightscenario_clientid and insightscenario_secret package parameters, e.g.:
setparam('insightscenario_clientid', '<replace-this-with-client-id>') setparam('insightscenario_secret', '<replace-this-with-secret>') ins~login('http://localhost:8080/') if ins~haserror then writeln_('error during login: ',ins~getlasterror) exit(1) end-if
You can pass the server URL as a parameter as well:
setparam('insightscenario_url', 'http://localhost:8080/') setparam('insightscenario_clientid', '<replace-this-with-client-id>') setparam('insightscenario_secret', '<replace-this-with-secret>') ins~login if ins~haserror then writeln_('error during login: ',ins~getlasterror) exit(1) end-if
Or as a final alternative, all three values can be passed as subroutine arguments, e.g.:
ins~login('http://localhost:8080/', '<replace-this-with-client-id>', '<replace-this-with-secret>') if ins~haserror then writeln_('error during login: ',ins~getlasterror) exit(1) end-if
The client ID and secret are credentials giving access to your Insight 5 account - you should never hard-code them in a Mosel source file. Requests you make throught the mminsightscenario package will be performed as the user that generated the client ID and secret, and the operations that can be performed will be restricted by the authorities granted to that account.
Once you've called ins~login, you can call any of the other subroutines to send requests to the Insight server. For example, to list all the apps that are visible to your user account:
declarations apps: list of ins~app end-declarations apps := ins~getapps if ins~haserror then writeln_('error fetching apps list: ',ins~getlasterror) exit(1) end-if forall(app in apps) writeln('Found app: ', app.name)
After every call to an mminsightscenario subroutine, you should check ins~haserror to see if it succeeded. An error could be anything from a network issue interrupting communication with Insight, to the account not having the rights to access the resource requested, to someone else having deleted the resource while we were using it. If you don't want to have to handle these errors, you can set the insightscenario_failonerror parameter and your model will automatically exit when an error is encountered:
setparam('insightscenario_failonerror', true) declarations apps: list of ins~app end-declarations apps := ins~getapps ! Will not get this far if ins~getapp fails for any reason forall(app in apps) writeln('Found app: ', app.name)
Some failed requests to the Insight server will automatically be retried before an error is returned. Sometimes, this will result in a delay before you see an error message; if you want requests to fail faster, you can control the number of retries using the dmp_max_retries parameter:
setparam('dmp_max_retries', 2) ! Retry each failure twice
Reading Entity Values
When reading scenario data, you fetch all the entities you want in a single call, rather than making a separate call for each entity — this is both more efficient and ensures you are reading consistent data (i.e. the scenario isn't updated between fetches of two entities). To do this, you pass ins~getscendata a list of ins~entityfetch values created by the following functions:
Consult the individual function documentation for details on how to use each one, but they are all passed the name of the entity and a reference to a Mosel variable in the local model into which the entity will be read. So for example, to fetch the values of a scalar maxProfit and an array Prices:
declarations SCENARIO_ID='2926e9a1-9568-4116-84ed-2e75190bc651' Regions: set of string Months: set of integer Prices: dynamic array(regions, months) of real profitLimit: real end-declarations ins~getscendata(SCENARIO_ID, [ ins~getscalar('maxProfit', ->profitLimit), ins~getarray('Prices', Prices) ] ) if ins~haserror then writeln_('Error: ',ins~getlasterror) else writeln('Maximum profit: ', profitLimit) forall (region in Regions, month in Months | exists(Prices(region,month))) do writeln('Price in ', region, ' for month #', month, ': ', Prices(region,month)) end-do end-if
The ins~getscendata will convert between the value types returned by the Insight REST API, and the types of the variables in your model. The supported value types are:
- boolean
- integer
- real
- string
- text
- constant text – for sets
- any – to use the type returned by the Insight REST API
Model parameter values can be accessed as an array named 'parameters'; for example, a parameter 'DB_CONNECTION_STRING' can be read as follows:
public declarations dbConnectionString: string end-declarations ins~getscendata(SCENARIO_ID, [ ins~getarrayelt('parameters', ['DB_CONNECTION_STRING'], ->dbConnectionString) ] ) if ins~haserror then writeln_('Error: ',ins~getlasterror) end-if
Updating Entity Values
When updating scenario data, you update all the entities you want in a single call, rather than making a separate call for each entity - this is both more efficient and ensures the scenario always has consistent data (i.e. nobody can read the scenario data between the updates of two entities). To do this, you pass ins~updatescendata a list of ins~entityupdate values created by the following functions:
- ins~setscalar
- ins~setscalars
- ins~addsetelts
- ins~delsetelts
- ins~addarrayelt
- ins~addarrayelts
- ins~delarrayelt
- ins~delarrayelts
Consult the individual function documentation for details on how to use each one, but they are all passed the name of the entity and a reference to a Mosel variable in the local model into which the entity will be read. So for example, to fetch the values of a scalar maxProfit and an array prices:
declarations SCENARIO_ID='2926e9a1-9568-4116-84ed-2e75190bc651' regions: set of string months: set of integer prices: dynamic array(regions, months) of real profitLimit: real end-declarations ins~getscendata(SCENARIO_ID, [ ins~getscalar('maxProfit', ->profitLimit), ins~getarray('prices', prices) ] ) if ins~haserror then writeln_('Error: ',ins~getlasterror) else writeln('Maximum profit: ', profitLimit) forall (region in regions, month in months | exists(prices(region,month))) do writeln('Price in ', region, ' for month #', month, ': ', prices(region,month)) end-do end-if
Error handling
Clear the error state of the package, such that a future call to
ins~haserror will return false.
|
|
If the previous operation failed with an error, return a suitable error message.
|
|
If the previous operation failed with an error, return the HTTP status code.
|
|
Query whether the previous operation failed with an error.
|
Authentication
Initialize the package with the login details of the Insight server.
|
|
Removes the login details of the Insight server from this package.
|
User Management
Data Types
-
Information about a user on the Insight serverid : textThe ID of this userusername : textThis user's usernamename : textThis user's full namefirstname : textThis user's first namelastname : textThis user's last nameemail : textThe email address of this userstatus : stringThe status of this user's account (Value should be one of: ACTIVE,DELETED,DISABLED,LOCKED)See also
Subroutines
Get the user with the specified ID.
|
Apps
Data Types
-
Information about an app on the Insight server.id : textThe ID of this appname : textThe name of this apppath : textThe path within the repositoryappver : textThe model versiondataver : integerThe model data versionSee also
-
Information about a member of an app on the Insight server.id : textThe ID of this username : textThis user's full namefirstname : textThis user's first namelastname : textThis user's last nameSee also
Subroutines
Create a new app on the Insight server.
|
|
Delete an existing app from the Insight server.
|
|
Get information about an app by ID.
|
|
Get all direct app members, and users who have authority to access all apps.
|
|
Get information about multiple apps from the Insight server.
|
|
Upgrade an existing app on the Insight server.
|
App Attachments
Data Types
-
Information about a label/tag which can be applied to one or more attachments depending on its properties.id : textThe ID of this attachmentfilename : textThe file name of this attachmentdescription : textA description of this attachmenthidden : booleanWhether the attachment is hidden. This indicates the attachment is not for general user interactionlastmod : textWhen the attachment contents were last modifiedlastmodby : textThe ID of the user who last modified this attachmentappid : textThe ID of the app that this attachment belongs toparentid : textThe ID of the app or scenario that is the parent of this attachment. For app attachments, this will be the same as the appid value.parenttype : stringThe type of the parent of this attachment, either APP or SCENARIOtags : list of textThe tags that are present on this attachmentsize : realThe size of this attachment in bytesSee also
-
Information about a label/tag which can be applied to one or more attachments depending on its properties.name : textUnique name of the tagdescription : textA human-readable description for this tagmandatory : booleanWhether this tag must be present on at least one attachment belonging to the app or scenariomultifile : booleanWhether this tag can be present on multiple attachmentsSee also
Subroutines
Delete an app attachment by ID.
|
|
Get information about all the app attachments for an app.
|
|
Download the indicated attachment to a local file.
|
|
Get information about a given attachment.
|
|
Get all the attachment tags for an app.
|
|
Create a new app attachment or overwrite an existing one.
|
|
Set information about a given attachment.
|
Scenario Attachments
Subroutines
Delete a scenario attachment by ID.
|
|
Get information about all the scenario attachments for a scenario.
|
|
Download the indicated attachment to a local file.
|
|
Get information about a given attachment.
|
|
Create a new scenario attachment or overwrite an existing one.
|
|
Set information about a given attachment.
|
Scenarios
Data Types
-
Information about a scenario.id : textThe ID of this scenariocreated : textThe timestamp this scenario was createdownerid : textThe ID of the user that owns this scenarioownername : textThe full name of the user that owns this scenarioappid : textThe ID of the app that owns this scenarioparentid : textThe ID of the app or folder that is the parent of this scenario. For top-level scenarios this will be the same as the appid value.parenttype : stringThe type of the parent of this attachment, either APP or FOLDERname : textThe name of this scenariopath : textThe path within the repositorytype : textThe scenario's custom type if it has one, otherwise SCENARIO.sharestatus : stringThe share status of this scenario (one of: FULLACCESS, PRIVATE, READONLY)datastate : stringThe current state of this scenario's data (one of: LOADED, RESULTS,, RESULTS_DIRTY, UNLOADED)execduration : realThe duration of the last execution of this scenario in millisecondsexecmode : textThe execution mode for the last executionexecusername : textThe full name of the user that last executed this scenarioexecuserid : textThe ID of the user that last executed this scenariodataver : integerThe version number of the model datastatus : stringThe model status for this scenario (one of: OK, BREAK, ERROR, EXIT, INSTR, IOERR, LICERR , MATHERR, NA, NIFCT, NULL, PROB, STOP, UNKNOWN, UNKN_PF, UNKN_SYS)probstatus : stringThe problem status for this scenario (one of: INFEASIBLE, NA, OPTIMAL, OTHER, SOLUTION, UNBOUNDED, UNFINISHED, UNKNOWN)execstart : textTimestamp when this scenario last started loading or executingexecfinish : textTimestamp when this scenario last finished loading or executingSee also
Subroutines
If scenario is currently queued or executing, cancel it.
|
|
Create a copy of an existing scenario within an app.
|
|
Create a new scenario within an app.
|
|
Delete a scenario from the Insight server.
|
|
Execute a scenario and wait for it to complete.
|
|
Queue the given execution mode of the given scenario for execution.
|
|
Request information about a scenario.
|
|
Download the scenario's run log into the given local file.
|
|
Move a scenario to another folder.
|
|
Rename a scenario on the Insight server.
|
|
Update the owner and optionally share status of a scenario on the Insight server.
|
|
Update the share status of a scenario on the Insight server.
|
|
If the specified scenario is executing, wait until it completes or is cancelled. If not, return immediately.
|
Reading Scenario Data
Data Types
-
Information about an entity that is to be fetched from the Insight scenario.NoteUse the supplied functions to create a list of ins~entityfetch values and then pass to ins~getscendata to fetch atomically fetch all the entities.See also
Subroutines
Create an
ins~entityfetch record for fetching the content of an array into a Mosel variable.
|
|
Create an
ins~entityfetch record for fetching a single array element into a Mosel variable.
|
|
Create an
ins~entityfetch record for fetching a scalar entity.
|
|
Create an
ins~entityfetch record for fetching multiple scalar entities into an array.
|
|
Atomically fetch the values of one or more entities from an Insight scenario into Mosel variables.
|
|
Create an
ins~entityfetch record for fetching the content of a set entity into a Mosel variable.
|
Updating Scenario Data
Data Types
-
Information about an entity that is to be updated on the Insight scenario.NoteUse the supplied functions to create a list of ins~entityupdate values and then pass to ins~updatescendata to fetch atomically update all the entities.See also
Subroutines
Create an
ins~entityupdate record for adding a single element to an array.
|
|
Create an
ins~entityupdate record for adding multiple elements to an array.
|
|
Create an
ins~entityupdate record for adding new elements to a set.
|
|
Create an
ins~entityupdate record for removing a single element from an array.
|
|
Create an
ins~entityupdate record for removing a multiple elements from an array.
|
|
Create an
ins~entityupdate record for removing elements from a set.
|
|
Create an
ins~entityupdate record for updating a scalar entity.
|
|
Create an
ins~entityupdate record for updating multiple scalar entities.
|
|
Atomically update one or more entities in an Insight scenario.
|
Folders
Data Types
-
Information about a folder within an app.id : textThe ID of this folderappid : textThe ID of the app that this folder belongs toparentid : textThe ID of the app or folder that is the parent of this folder. For top-level folders, this will be the same as the appid value.parenttype : stringThe type of the parent of this folder, either APP or FOLDERname : textThe name of this folderpath : textThe path within the repositoryownerid : textThe ID of the user that owns this folderownername : textThe full name of the user that owns this foldersharestatus : stringThe share status of this folder (one of: FULLACCESS, PRIVATE, READONLY)See also
Subroutines
Create a new folder within an app.
|
|
Delete a folder from the Insight server.
|
|
Get the immediate child folders and scenarios of the app root.
|
|
Request information about a folder.
|
|
Get the immediate child folders and scenarios of the specified folder.
|
|
Move a folder to within another folder.
|
|
Rename a folder on the Insight server.
|
|
Update the owner and optionally share status of a folder on the Insight server.
|
|
Update the share status of a folder on the Insight server.
|
Control Parameters
© 2001-2023 Fair Isaac Corporation. All rights reserved. This documentation is the property of Fair Isaac Corporation (“FICO”). Receipt or possession of this documentation does not convey rights to disclose, reproduce, make derivative works, use, or allow others to use it except solely for internal evaluation purposes to determine whether to purchase a license to the software described in this documentation, or as otherwise set forth in a written software license agreement between you and FICO (or a FICO affiliate). Use of this documentation and the software described in it must conform strictly to the foregoing permitted uses, and no other use is permitted.