Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
protogrid:agents [2020-04-22 09:22] – [Example] jus | protogrid:agents [2023-01-16 23:16] (current) – [Example] dru | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Agents ====== | ====== Agents ====== | ||
- | |||
Agents run periodically and execute code specified by the user. | Agents run periodically and execute code specified by the user. | ||
- | |||
- | Note: Agents are only available in environments that run Protogrid 2.1 or higher. | ||
===== Setup ===== | ===== Setup ===== | ||
- | |||
To set up an agent, there are two necessary steps: | To set up an agent, there are two necessary steps: | ||
* Under Design -> Code, create an Agent Library and add your code to the // | * Under Design -> Code, create an Agent Library and add your code to the // | ||
- | * Under Design -> Proto, select the Proto you want to use and add corresponding the Agent Library | + | * Under Design -> Proto, select the Proto you want to use and add corresponding the Agent Library |
==== Example ==== | ==== Example ==== | ||
- Create a Proto with a DateTime Field and a Text Field. Copy the key of the Text Field. | - Create a Proto with a DateTime Field and a Text Field. Copy the key of the Text Field. | ||
- | - Create a user profile with rights to edit the proto created above. | + | - Create a user profile with rights to edit the proto created above but without Environment Administrator rights. |
- | - Create an Agent Library, specify the user created above as context user and copy the following code. Replace the value of //defkey_status_field// with the key from above. | + | - Create an Agent Library, specify the user created above as context user and copy the following code. Replace the value of //fieldkey_status_field// with the key from above. |
+ | - Create a Proto for parameter values with a Text Field, copy the key of the Text Field and replace // | ||
- Add this Agent Library to your Proto. | - Add this Agent Library to your Proto. | ||
- Based on this proto, create some cards where the date lies within the next 60 minutes. Wait one hour. | - Based on this proto, create some cards where the date lies within the next 60 minutes. Wait one hour. | ||
- The value in your Text Field should have been replaced by //Frist abgelaufen.// | - The value in your Text Field should have been replaced by //Frist abgelaufen.// | ||
+ | |||
<code javascript> | <code javascript> | ||
function agent_library() { | function agent_library() { | ||
- | var defkey_status_field | + | var fieldkey_status_field |
+ | var cardkey_parameter_deadline_reached = ' | ||
+ | var fieldkey_parameter_value = ' | ||
- | function | + | |
- | //sets the value of a single-value field given by field_key | + | // We recommend to use await / async syntax: https:// |
- | | + | // * card [object]: |
- | | + | // * cookie [string]: |
- | card.design_elements[i]['value'] = value; | + | // * axios [object]: |
- | break; | + | // * baseurl [string]: Contains the base url needed for API requests |
- | } | + | // * appname [string]: Contains the application name |
+ | // most URLs needed for API calls are structured like baseurl + ' | ||
+ | // return value: A boolean. If set to true, it will save the current card | ||
+ | async function on_schedule(card, | ||
+ | // Here is an example for an authenticated axios GET request: | ||
+ | | ||
+ | | ||
+ | ' | ||
+ | ' | ||
+ | } | ||
+ | | ||
+ | console.log('axios GET request finished'); | ||
+ | |||
+ | var loaded_card | ||
+ | var loaded_message = loaded_card.get_value(fieldkey_parameter_value, | ||
+ | | ||
+ | |||
+ | | ||
+ | | ||
+ | console.error(e); | ||
+ | return false; | ||
} | } | ||
- | return card; | ||
} | } | ||
- | + | ||
- | function on_schedule(card, | + | |
- | card = set_value(card, | + | |
- | return card; | + | |
- | } | + | |
- | | + | |
return { | return { | ||
- | set_value: set_value, | ||
on_schedule: | on_schedule: | ||
}; | }; | ||
} | } | ||
+ | |||
</ | </ | ||
===== Execution ====== | ===== Execution ====== | ||
+ | Agents are by default checked every hour (this can be modified for your environment, | ||
+ | |||
+ | Agents run in the context of a specific user profile which must not have Environment Administrator rights. This user's password is automatically reset during the execution process. So this user profile shall only be used for agents and not for physical users. All agent executions are logged in the // | ||
+ | |||
+ | While an agent is running in the context of a specific card, it is also possible to load and save other cards by calling the JSON API endpoints using axios. A valid axios module is given in the // | ||
+ | |||
+ | The // | ||
- | Agents are checked every hour. If any DateTime Field on a card has a smaller value than the current time and the Agent has not yet run since that time, the Agent gets triggered and the code in the Agent Library | + | ===== Best Practices for Calculation Code ====== |
+ | In the following, we have compiled some recommendations based on our experience for high stability, performance, | ||
+ | - It is recommended to use [[protogrid: | ||
+ | - Note: Use multi column indexind views at all only if the free combination of several filters is required in the user interface. | ||
+ | - Write to as few Relation Fields as possible | ||
+ | - Procedure if the calculation code requires a " | ||
+ | - If editing or deleting a Card in the user interface subsequently to calculation has to be prevented, this can be done in both cases by restricting the editing rights ([[protogrid: | ||
+ | - Read/load as few specific/ | ||
+ | - Analogous to the previous point, write as few specific/single cards as possible. Whenever possible, save several cards together. You can use the Endpoint [[protogrid: |