feat: Add Runtime Formula for formatting numbers#5262
Conversation
01f7c82 to
dde88d3
Compare
Coverage reportClick to see where and how coverage changed
This report was generated by python-coverage-comment-action |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
9e6a4a4 to
51d9d2d
Compare
bc9a233 to
d265c3f
Compare
jrmi
left a comment
There was a problem hiding this comment.
Hey Tsering, thanks for this PR and the new skill, it will be really usefull.
In this PR we introduce the new "time interval" data type but we don't have an ensurer for that. What do you think about adding the ensurer and support this new type in other ensurer, I thinks mainly to the "ensureString" that could return something more human friendly than {"ms":172800000}. It could be the best string representation (bay be moment has something like that?). Even the ensureInteger could return something like the number of seconds or something like that. What do you think?
More generally I left multiple comments about using the ensurer when possible. I think it could be something we add in the skill to make sure next PR use them.
Could you give an example with different separators here:
There is no ways to know what are the other parameters.
The return value here is too technical, may be it could be the number of seconds?
For the null() function may be we can be a bit more precise and say it allows to set null as value or something like that?
13d279e to
8006010
Compare
8006010 to
9206379
Compare
|
Thanks for your review @jrmi - I believe it's ready for another round. |
jrmi
left a comment
There was a problem hiding this comment.
Hey Tsering, almost there. I suggested two improvments, let me know what you think.
| if isinstance(value, timedelta): | ||
| total = int(value.total_seconds()) | ||
| if total == 0: | ||
| return "0 seconds" |
There was a problem hiding this comment.
We either:
- Want to translate that
- Just return a number so the user can make the conversion themselves with a convoluted advanced formula.
Second option is easier for us and first is better for user.
WDYT?
There was a problem hiding this comment.
Good point, I've refactored it to just return the total seconds as a string. Done in 5203b52
| if isinstance(value, timedelta): | ||
| return value | ||
|
|
||
| if isinstance(value, str): |
There was a problem hiding this comment.
If it's a string I would try to match a duration and if it doesn't work ensure a number and fall in the next case if it's possible. WDYT?
There was a problem hiding this comment.
Instead of converting to float could you use ensure_integar as I suggested to support potentially more values?
9206379 to
aef2621
Compare
|
@jrmi it's ready for another round 😄 |
f5b813c to
82dc9ac
Compare
Done in 4d4dd6f. This means we won't support fractional seconds, but I guess that should be fine since most users won't want to do millisecond level intervals. |
What is in this PR
This PR implements four new runtime formulas:
number_format(),abs(),null(),date_interval(), andto_datetime().It also adds support for better human-readable error messages by adding a new
getErrorMessage()method to theBaserowRuntimeFormulaArgumentTypeclass, as well as refactoring theRuntimeFormulaFunction::validateArgs()to raise the error message if available. The backend has been similarly refactored.Finally, a new skill
create-update-runtime-formulaAI skill has been added. This will make it easier to use an LLM to add/update a runtime formula with a simple prompt. For instance, to create thenull()runtime formula, I just prompted:Closes #4974
How to test this PR
1.
abs()This simply returns the absolute value of a number, e.g.
abs(-100)should return100.Create a Builder element, e.g. Heading, and using expert mode type in
abs(-100). You should see100rendered.2.
null()This returns a
null(orNonein the backend). This is useful when you need anullvalue to be used, e.g. when upserting anullto a row or evaluating it in a Router node, e.g.:Create a Date field in your database table and fill the field with a date value. Then in Builder or Automation, create an workflow action or a node that updates that row's field to use
null()in the service. You should observe that the row's field is correctly set to null.3.
number_format()This formats a number with the option to specify:
,).)E.g. using
number_format(12345, 2, '.', ',')should return12.345,00.4.
date_interval()This returns a timedelta that can be used by the existing
addorminusformulas to operate on a date.E.g.
now() + date_format('1 day')should add a day to the current datetime.5.
to_datetime()This takes a Moment.js datetime string with an optional date format, and converts it to a datetime objeect.
E.g.
to_datetime('2026-06-04')returnsdatetime(2026, 06, 04, 0, 0, 0).Checklist
changelog/entries/unreleasedusingchangelog/src/changelog.py