Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Lens] Formula: Date math #112851

Closed
flash1293 opened this issue Sep 22, 2021 · 6 comments · Fixed by #143632
Closed

[Lens] Formula: Date math #112851

flash1293 opened this issue Sep 22, 2021 · 6 comments · Fixed by #143632
Assignees
Labels
enhancement New value added to drive a business result Feature:Lens Team:Visualizations Visualization editors, elastic-charts and infrastructure

Comments

@flash1293
Copy link
Contributor

At the moment Lens formula can only deal with numerical values. A common use case is to calculate the elapsed time between two dates (like last_value(end_date) - last_value(start_date)).

This would be a nice addition to todays feature set, but it brings a bunch of technical complexity to formula we could avoid so far. We would have to track the data type when traversing the AST - for example it doesn't make sense to subtract a date from a number and vice versa. Also, tinymath functions would either need to be extended to handle dates as well or we would have to introduce additional functions for date math specifically, e.g seconds_between(last_value(end_date), last_value(start_date)).

Right now there's a workaround for some cases by using runtime fields instead, but this doesn't work for dates originating from different documents.

To clarify:

  • What are the exact use cases to cover?
  • Which functions do we need for date math?
  • Should it be possible to define a date-only formula? Right now it's limited to numerical output
@flash1293 flash1293 added enhancement New value added to drive a business result Team:Visualizations Visualization editors, elastic-charts and infrastructure Feature:Lens labels Sep 22, 2021
@elasticmachine
Copy link
Contributor

Pinging @elastic/kibana-vis-editors (Team:VisEditors)

@flash1293
Copy link
Contributor Author

cc @ghudgins - maybe you have some things in mind for this?

@dej611
Copy link
Contributor

dej611 commented Sep 22, 2021

for example it doesn't make sense to subtract a date from a number and vice versa.

Probably it is still useful to do some math with Dates and some number constant.

@ghudgins
Copy link
Contributor

Adding additional cases:

  • Exposing the time range size in ms as a variable in the formula
  • interval_ms - exposing the size of the current interval when using a date histogram

@flash1293
Copy link
Contributor Author

Another possible variable - now() which returns the current time stamp in ms

@flash1293 flash1293 self-assigned this Oct 18, 2022
dej611 added a commit that referenced this issue Jun 6, 2023
## Summary

Fixes #151827,  Fixes #117548

This PR contains constant function features in Lens formula:
* direct exposure of the `time_range()` - as suggested in #117548)

<img width="297" alt="Screenshot 2023-05-29 at 12 05 43"
src="https://github.com/elastic/kibana/assets/924948/8dbd63a6-c5ec-4177-a4a2-cc0770a495bc">

* `interval()` (as intended in #151827) 

<img width="359" alt="Screenshot 2023-05-29 at 12 04 18"
src="https://github.com/elastic/kibana/assets/924948/8c3a4637-bd23-4b4d-8a5f-36853ccbfee3">

* and `now()` (as suggested in
[here](#112851 (comment)))
functions in the formula input

<img width="364" alt="Screenshot 2023-05-25 at 14 30 14"
src="https://github.com/elastic/kibana/assets/924948/2137717b-44a0-4531-bcc2-1fbda40368ba">

A visual explanation of what each constant represent:
<img width="677" alt="Screenshot 2023-05-29 at 11 56 16"
src="https://github.com/elastic/kibana/assets/924948/b7b7e92c-b061-4cce-a77e-e7baeab323a8">


Documentation:

<img width="546" alt="Screenshot 2023-05-31 at 12 59 40"
src="https://github.com/elastic/kibana/assets/924948/1fea7e88-6e4f-4958-949f-6b71a770cd44">



Some example usage
<img width="683" alt="Screenshot 2023-05-25 at 13 33 26"
src="https://github.com/elastic/kibana/assets/924948/e03442ab-0062-4999-8a38-c00a66b3b13b">

TSVB => Lens with `params._interval` support:

![tsvb_to_lens_with_interval](https://github.com/elastic/kibana/assets/924948/6ea86bd1-8e71-4928-b2e2-7ad27537b33d)

**Notes**: 

* context values work like static values, with the same limits,
therefore it is not possible to build a date histogram with a context
value alone (i.e. a formula with only `interval()` or `now()`). It works
ok without the `Date Histogram` dimension.
* The `interval()` function will report an error if used without a
configured `Date Histogram` dimension:

<img width="671" alt="Screenshot 2023-05-29 at 12 14 13"
src="https://github.com/elastic/kibana/assets/924948/ca67f102-35bb-4e4d-ab16-15d1be4cadcc">

* The `interval()` function does not take into account different bucket
interval size (i.e. DST changes, leap years, etc...), rather return the
same value to all the buckets. This is the same behaviour as in TSVB,
but in Lens it can be a problem due to the usage of `calendar_interval`.

* I had to duplicate a couple of function from the helpers to avoid
issues with tests. I've tried a different organization of the helpers
(between pure vs impure fns) but that took longer than expected, so I
would defer this task later in another PR.

<details>
  <summary>General approach with `constant(...)` removed</summary>
* a more general approach using `constants(value="...")`
<img width="301" alt="Screenshot 2023-05-29 at 12 07 23"
src="https://github.com/elastic/kibana/assets/924948/0a800aa4-8db5-4055-996d-344d37adfc01">
</details>


A cloud deployment has been created to test both approaches here. Let me
know which one do you prefer cc @elastic/kibana-visualizations

### Checklist

Delete any items that are not applicable to this PR.

- [ ] Any text added follows [EUI's writing
guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses
sentence case text and includes [i18n
support](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md)
- [ ]
[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)
was added for features that require explanation or tutorials
- [ ] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios
- [ ] Any UI touched in this PR is usable by keyboard only (learn more
about [keyboard accessibility](https://webaim.org/techniques/keyboard/))
- [ ] Any UI touched in this PR does not create any new axe failures
(run axe in browser:
[FF](https://addons.mozilla.org/en-US/firefox/addon/axe-devtools/),
[Chrome](https://chrome.google.com/webstore/detail/axe-web-accessibility-tes/lhdoppojpmngadmnindnejefpokejbdd?hl=en-US))
- [ ] If a plugin configuration key changed, check if it needs to be
allowlisted in the cloud and added to the [docker
list](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)
- [ ] This renders correctly on smaller devices using a responsive
layout. (You can test this [in your
browser](https://www.browserstack.com/guide/responsive-testing-on-local-server))
- [ ] This was checked for [cross-browser
compatibility](https://www.elastic.co/support/matrix#matrix_browsers)


### Risk Matrix

Delete this section if it is not applicable to this PR.

Before closing this PR, invite QA, stakeholders, and other developers to
identify risks that should be tested prior to the change/feature
release.

When forming the risk matrix, consider some of the following examples
and how they may potentially impact the change:

| Risk | Probability | Severity | Mitigation/Notes |

|---------------------------|-------------|----------|-------------------------|
| Multiple Spaces&mdash;unexpected behavior in non-default Kibana Space.
| Low | High | Integration tests will verify that all features are still
supported in non-default Kibana Space and when user switches between
spaces. |
| Multiple nodes&mdash;Elasticsearch polling might have race conditions
when multiple Kibana nodes are polling for the same tasks. | High | Low
| Tasks are idempotent, so executing them multiple times will not result
in logical error, but will degrade performance. To test for this case we
add plenty of unit tests around this logic and document manual testing
procedure. |
| Code should gracefully handle cases when feature X or plugin Y are
disabled. | Medium | High | Unit tests will verify that any feature flag
or plugin combination still results in our service operational. |
| [See more potential risk
examples](https://github.com/elastic/kibana/blob/main/RISK_MATRIX.mdx) |


### For maintainers

- [ ] This was checked for breaking API changes and was [labeled
appropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)

---------

Co-authored-by: kibanamachine <[email protected]>
Co-authored-by: Stratoula Kalafateli <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New value added to drive a business result Feature:Lens Team:Visualizations Visualization editors, elastic-charts and infrastructure
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants