calculated fields

The recommended method of customizing your AppGini-generated application is through hooks. But sometimes you might need to add functionality not accessible through hooks. You can discuss this here.
Post Reply
angus
Veteran Member
Posts: 128
Joined: 2020-05-28 22:27

calculated fields

Post by angus » 2022-08-26 10:55

can anyone guide me on calculated fields.

I have created 24 calculated fields and initially this worked well. but now I am getting a 504 error on my site. When I look at the client connections running on the database I can see that the calculated fields are processing, even though I have not entered into a record, just opened up the table view.

does anyone know if this is how it should work? I thought it would just update the row once you save a record?
AppGini 22.13

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1807
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: calculated fields

Post by jsetzer » 2022-08-26 11:28

Calculated fields will be evaluated each time any user sees multiple records in TV or single records in DV. That's by design.

In case of 24 calculated fields and, let's say 100 visible rows per page in TV this may take seconds.

If you don't want those fields to be calculated automatically, consider removing the calculations from your model and use SQL commands or PHP calculations in before/after hooks, instead. Fields should be readonly.
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

angus
Veteran Member
Posts: 128
Joined: 2020-05-28 22:27

Re: calculated fields

Post by angus » 2022-08-26 11:31

ok thanks JSetzer I will move, we are going to have thousands of records in this table and with default I am getting 504 error. I may need to review the calculation to see if I can remove some of the joins also
AppGini 22.13

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1807
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: calculated fields

Post by jsetzer » 2022-08-26 12:54

If calculations depend on same row's data, only, from my experience it will be most performant to calculate on save in before_* hook. Also it is easier to maintain (just change SQL command) and to test (just reload page, no need to generate the app).

If there is for example a date/datetime- dependency, I usually do calculations in _init hook. So I can recalculate for all relevant records, not only for records visible in TV. You never know if or when someone opens a specific page (sorting/pagination) of TV in which specific records a listed. So I can never rely on calculated fields getting updated if calculations need dynamic data like current datetime.

The same for...
- who is the owner of a record
- date
- datetime
- values from other tables
If my calculation would require any of these, I personally would not use calculated fields but SQL in _init hook.

By using SQL you can also control which records have to be re-calculated or not. For example only update records where calc_value IS NULL or only update where status_id=5 or whatever. This can boost performance.

This is just my personal opinion. I'm sure there will be many scenarios in which calculated fields will do.
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

Post Reply