Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
protogrid:agents [2020-04-22 09:21] – [Agents] jusprotogrid:agents [2020-12-22 16:31] – Best-Practices für grössere Berechnungen gemäss KitAjour-Design-Review eingefügt. 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.
  
Line 6: Line 5:
  
 ===== 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 //on_schedule// function.    * Under Design -> Code, create an Agent Library and add your code to the //on_schedule// function. 
Line 13: Line 11:
 ==== 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 the according rights.+  - Create a user profile with rights to edit the proto created above.
   - 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 //defkey_status_field// with the key from above.
   - 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 = "3b47a5fe-894d-4580-bc0c-563aa83d9a02"; // TODO change this value+    var defkey_status_field = "3b47a5fe-894d-4580-bc0c-563aa83d9a02";
  
     function set_value(card, field_key, value) {     function set_value(card, field_key, value) {
Line 46: Line 45:
  
 ===== 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 //agent_log// on the respective card.
  
-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 cardAgents run in the context of a specific user profileThis user's password is automatically reset during the execution process. So this user profile shall only be used for agents and not for physical usersAll agent executions are logged in the //agent_log// on the respective card.+===== Best Practices for Calculation Code ====== 
 +In the following, we have compiled some recommendations based on our experience for high stability, performance, reliability and maintainability of backend code that performs calculations on a larger number of Cards (i.e. more than 100): 
 +  - It is recommended to use [[protogrid:JSON API Database Views#Views of Dedicated Search Dialog Boxes|views of dedicated Search Dialog Boxes]] for retrieving the needed source data for calculation code. Do not use [[protogrid:Filter#Enable Multi Column Indexing for this Field|multi column indexing views]] for backend code. 
 +    - 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 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/never editedDo this only on Cards that do not (already) have a large number of Relating Cards. 
 +    - Procedure if the calculation code requires a "relation" that does not need a Relation Field in the user interface: Use a Text Field instead of a Relation FieldWrite the key of the relation to this Text Field with an individual prefix (important!). Use a Text Filter or a dedicated [[protogrid:TableView#Search Dialog Boxes|Search Dialog Box]] to retrieve Cards with the desired relation. 
 +  - 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:Role|roles]] at Proto and/or Card level). 
 +  - Read/load as few specific/single cards as possible. Work with views whenever possible. Use "include_cards" - but only if necessary. Do not hesitate to contact Protogrid Customer Support proactively in case of special requirements (e.g. certain values needed in the value of a view row or special reduction views needed). 
 +  - 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:API Endpoints#/api/v2/apps/<app_name>/cards|/api/v2/apps/<app_name>/cards]] with a list of Card objects in the POST body.
Print/export