YAQL as a first class citizen - Support YAQL expressions in rule criteria
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 6.5k
- Forks
- 787
- PR merge metrics
- No merged PRs in 30d
Description
Problem
The timediff_lt and timediff_gt functions expect a specific format. When processing events sent from an external applications to a generic web hook, it's not possible to control the format of the date in the payload. In my specific use case, a unix timestamp is being sent.
I tested the code from https://github.com/StackStorm/st2/blob/master/st2common/st2common/operators.py#L248-L271 which calls dateutil.parser.parse under the hood. The result is as follows
>>> d= dateutil.parser.parse(str(1566394298))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/st2/virtualenv/local/lib/python2.7/site-packages/dateutil/parser/_parser.py", line 1358, in parse
return DEFAULTPARSER.parse(timestr, **kwargs)
File "/st2/virtualenv/local/lib/python2.7/site-packages/dateutil/parser/_parser.py", line 654, in parse
ret = self._build_naive(res, default)
File "/st2/virtualenv/local/lib/python2.7/site-packages/dateutil/parser/_parser.py", line 1226, in _build_naive
if cday > monthrange(cyear, cmonth)[1]:
File "/usr/lib/python2.7/calendar.py", line 121, in monthrange
day1 = weekday(year, month, 1)
File "/usr/lib/python2.7/calendar.py", line 113, in weekday
return datetime.date(year, month, day).weekday()
ValueError: year is out of range
I looked into using Jinja to work around this issue but could not find any reference to acquiring the current time. Had YAQL been available, I could have written the following expression to evaluate if the time stamp was older than 60 seconds.
int(now().format("%s")) - int($.payload.body.nagios_epoch) < 60
Feature
I would like to have YAQL available in the rules criteria to provide greater flexibility in complex evaluations. A flow on effect from this change would mean that people who use YAQL exclusively in the workflows could maintain consistency and use YAQL in other parts of the StackStorm solution.
If this feature is considered of value, I would be willing to implement it with a bit of assistance from a StackStorm dev.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reading st2common/st2common/operators.py around lines 248-271, then trace how rule criteria are evaluated. The change is complete when YAQL expressions, including the timestamp comparison example, can be used in rule criteria; the payload does not name a test file, so identify the relevant criteria tests while tracing the evaluation path.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend, devops
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100