Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
protogrid:agents [2020-12-22 16:31] – Best-Practices für grössere Berechnungen gemäss KitAjour-Design-Review eingefügt. dru | 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. | ||
Line 19: | Line 18: | ||
<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 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 is executed in the context of this card (for Date-Only Fields, the time taken into account is said date, 00:00 GMT). Agents run in the context of a specific user profile. 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 // | + | Agents are by default |
+ | |||
+ | Agents run in the context of a specific user profile | ||
+ | |||
+ | 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 // | ||
===== Best Practices for Calculation Code ====== | ===== Best Practices for Calculation Code ====== | ||
In the following, we have compiled some recommendations based on our experience for high stability, performance, | In the following, we have compiled some recommendations based on our experience for high stability, performance, | ||
- It is recommended to use [[protogrid: | - It is recommended to use [[protogrid: | ||
- | | + | |
- Write to as few Relation Fields as possible in the code. Do this only if it is needed for the user interface (e.g. for filtering TableViews). Do this only on Cards that are rarely/ | - Write to as few Relation Fields as possible in the code. Do this only if it is needed for the user interface (e.g. for filtering TableViews). Do this only on Cards that are rarely/ | ||
- | | + | |
- 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: | - 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/ | + | - Read/load as few specific/ |
- Analogous to the previous point, write as few specific/ | - Analogous to the previous point, write as few specific/ |