Is there any way to get the current date when writing an advanced event query?
For example, I am making a dashboard that will be used solely for generating for a daily report, and would like an Event List block that displays only events that have taken place within the last 24 hours. I can query against the creationDate property in simple and advanced queries, but querying this with a static value does not seem very useful.
Is there a way to do this? It is unclear to me what scope I have access to when writing these queries, or if I can use string templating. Using new Date()
as I might in mongodb does not appear valid.
Hi @Nate_Usher,
You can retrieve the current time with the {{currentDateTime formatStr}}
format helper, where the formatStr
is in Moment.js format. Keep in mind that the time will update only when the block is updated or rendered.
Please let me know if you have any questions on implementation or configuration.
Thanks,
Julia
1 Like
Great, thanks for pointing me in the right direction.
For posterity, I used:
creationDate > {{subtract (multiply (currentDateTime âXâ) 1000) 86400000}}
It seems the platform might be using a version of moment.js older than 2.8.4, as
{{subtract (currentDateTime âxâ) 86400000}}
(âxâ being the format string for millisecond timestamp)
crashed the block.
@Nate_Usher youâre correct about your query crashing the block; we saw the errors coming through our tracking software and shortly after you provided steps to reproduce. (Thanks!)
Your assumption as to why the block exploded is incorrect, however, as the platform is using the moment@2.24.0. Long story short, your use of {{currentDateTime}}
formatted to milliseconds was causing an infinite loop:
- Your desired helper resolved to the current time in milliseconds minus 24 hours.
- A request was kicked off to fetch events matching the query.
- The result of the query came back, which caused the block to re-render. This in turn caused your query string to re-evaluate, which returned a new value for
{{subtract (currentDateTime âxâ) 86400000}}
.
- This caused a new network request, and thus the loop begins.
Your workaround masks the issue but doesnât resolve it as {{currentDateTime}}
in seconds format changes 1000 times less frequently than when formatted to milliseconds, and a strong internet connection combined with a lightweight query resolves the loop within a second. If you throttle your internet connection to slow it down, and also keep an eye on your browserâs network requests, youâll see what I mean (but it wonât be fast enough to crash the block so quickly). You can also see the bug in action as the list refreshes when resizing your dashboard block, as thatâs kicking off another new time and thus another call.
Weâre working on a fix for this but it will be a while before we can get something live, so in the meantime, I recommend you change your âcreationDate is greater thanâ query to the following helper instead:
{{ formatDate (subtract (currentDateTime 'x') 86400000) 'YYYY-MM-DD[T]HH:mm:[00Z]' }}
Essentially what this is requesting all events from the past 24 hours, rounded down to the nearest minute. Weâre getting the timestamp of ânow minus 24 hoursâ and instead passing it up to the server in a different, but still acceptable, format. However, weâre ignoring the current timeâs seconds and milliseconds values and are instead forcing â:00â for the seconds and no value for milliseconds. This greatly mitigates the re-fetching issue so that it happens at most once per minute.
Thanks again for the steps to reproduce the issue, and we hope to have a more robust fix in our next software release. Let us know if you have any more questions.
1 Like
Your suggestion works. I appreciate the promptness and in-depth response. You guys really are developerâs developers.
The work is never done