Skip to content

[TSVB/Markdown] Introduce formatted date field label#75555

Merged
stratoula merged 9 commits intoelastic:masterfrom
sulemanof:fix/markdown_date_field_label
Sep 8, 2020
Merged

[TSVB/Markdown] Introduce formatted date field label#75555
stratoula merged 9 commits intoelastic:masterfrom
sulemanof:fix/markdown_date_field_label

Conversation

@sulemanof
Copy link
Copy Markdown
Contributor

@sulemanof sulemanof commented Aug 20, 2020

Summary

Fixes #75341

This PR introduces a formatted date field label.
It is built on key_as_string bucket value, which is only inherent for fields with type date (as far as I know).

image

Release note

TSVB Mardown now handles the case when a field has key_as_string value.
Common case is the value is a date string (e.x. 2020-08-21T20:36:58.000Z) or a boolean stringified value ("true"/"false").
Such a value will be first converted into a moment object and formatted with dateFormat from Kibana UI settings.
If the key_as_string value is not recognized by a known format in Moments.js, a formatted value from elasticsearch will be returned.

Checklist

Delete any items that are not applicable to this PR.

For maintainers

if (row.labelFormatted) {
set(
variables,
`${_.snakeCase(row.label)}.formatted`,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking of it more like label.formatted but maybe this is my personal opinion.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Specifying it as:

set(variables, `${_.snakeCase(row.label)}.label.formatted`, value)

will override the previous one since in that case the label would become as object type variable and should have next shape:

set(variables, _.snakeCase(row.label), { 
  raw: row.label,
  foramtted: moment(row.labelFormatted).format(dateFormat)
})

That means existing visualizations could be affected. So I chose this way as a safe one

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes you are right, I am fine with it ❤️

return buckets.map((bucket) => {
bucket.id = `${series.id}:${bucket.key}`;
bucket.label = formatKey(bucket.key, series);
bucket.labelFormatted = bucket.key_as_string || '';
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have made some tests with various types and it seems that you are right, key_as_string is used for dates. @timroes do you have more input on this?

@stratoula
Copy link
Copy Markdown
Contributor

@sulemanof thank you for that. I agree dates should be human-readable, I will create an issue for this 🍰

@stratoula
Copy link
Copy Markdown
Contributor

And here is the issue #75634

Copy link
Copy Markdown
Contributor

@stratoula stratoula left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assuming that key_as_string is used only for dates, I am fine with it! I have tested it on Safari, works perfectly and haven't detected any regressions. 🥇 Code LGTM

@sulemanof sulemanof requested a review from markov00 August 24, 2020 10:43
Copy link
Copy Markdown
Contributor

@alexwizp alexwizp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM Thank you!

@sulemanof sulemanof added Feature:TSVB TSVB (Time Series Visual Builder) Team:Visualizations Team label for Lens, elastic-charts, Graph, legacy editors (TSVB, Visualize, Timelion) t// release_note:skip Skip the PR/issue when compiling release notes v7.10.0 v8.0.0 labels Aug 24, 2020
@sulemanof sulemanof marked this pull request as ready for review August 24, 2020 10:50
@sulemanof sulemanof requested a review from a team August 24, 2020 10:50
@elasticmachine
Copy link
Copy Markdown
Contributor

Pinging @elastic/kibana-app (Team:KibanaApp)

Copy link
Copy Markdown
Contributor

@markov00 markov00 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sulemanof @stratoula I think we already have this functionality in TSVB
you can access the formatted version of each bucket with something like:

{{# 1595980800000.data.formatted }}
{{[0]}}
{{/ 1595980800000.data.formatted }}

where [0] is the first element of the bucket, in this case the used label/timestamp.
You can also use this technique to in the each method

Screenshot 2020-08-24 at 16 00 03

@sulemanof
Copy link
Copy Markdown
Contributor Author

sulemanof commented Aug 24, 2020

@sulemanof @stratoula I think we already have this functionality in TSVB
you can access the formatted version of each bucket with something like:

{{# 1595980800000.data.formatted }}
{{[0]}}
{{/ 1595980800000.data.formatted }}

where [0] is the first element of the bucket, in this case the used label/timestamp.
You can also use this technique to in the each method

Screenshot 2020-08-24 at 16 00 03

The initial request is to have a formatted version of a bucket key, while the data is split by date field and have labels as epoch millis.
In such a case, your data will have an array of all existing time buckets, but the label represent one of top specified.
You're not exactly sure which exact bucket (by index from the data array) is your split one.
For more details, please look at initial issue.

UPD:
Here is an example of desired/first-bucket formatted label:

image

Notice the agg config is:

image

@markov00 markov00 self-requested a review August 25, 2020 12:44
Copy link
Copy Markdown
Contributor

@markov00 markov00 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @sulemanof for the explanation on how to reproduce it, I've checked and this works under the standard use case.
The problem is that the key_as_string, use the format provided in the aggregation as a parameter (as described here), if not provided (as in TSVB) it use the first date format specified in the field mapping.
This means that if the user has a [date format] mappings (https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-date-format.html) that doesn't conform with what moment can digest, this field will be transformed into the following Invalid date string.

Screenshot 2020-08-25 at 15 16 43

You can verify that with this simple example:
PUT my-index-000001
{
  "mappings": {
    "properties": {
      "timestamp": {
        "type":   "date"
      },
      "date": {
        "type":   "date",
         "format": "QQ-yyyy-M-D"
      },
      "value": {
        "type": "long"
      }
    }
  }
}
POST my-index-000001/_doc
{
  "timestamp": "2020-08-20T13:02:45.111Z",
  "date": "01-2020-8-5",
  "value": 100
}

GET my-index-000001/_search
{
  "aggs": {
    "dh": {
      "terms": {
        "field": "date"
      }
    }
  },
  "size": 0
}

We shouldn't probably directly rely on that key_as_string field or at least don't convert it right away, but check the field type used by the terms aggregation and then apply a valid format.
If we know that the key field is a date, we can apply the formatting to the key instead of key_as_string, but we should always check the key field type before doing any conversion

@sulemanof
Copy link
Copy Markdown
Contributor Author

The PR was updated to format basic label with moment, instead of key_as_string from elasticsearch.
It also handles a case when a label is a boolean value:

image

@sulemanof sulemanof added release_note:enhancement and removed release_note:skip Skip the PR/issue when compiling release notes labels Sep 3, 2020
@sulemanof
Copy link
Copy Markdown
Contributor Author

@elasticmachine merge upstream

@kibanamachine
Copy link
Copy Markdown
Contributor

💚 Build Succeeded

Build metrics

async chunks size

id value diff baseline
visTypeTimeseries 1.8MB +311.0B 1.8MB

History

To update your PR or re-run it, just comment with:
@elasticmachine merge upstream

@timroes timroes requested a review from markov00 September 8, 2020 13:50
@timroes timroes dismissed markov00’s stale review September 8, 2020 13:50

Out of office for some time and all requested changes have been addressed

@stratoula stratoula merged commit caa4e0c into elastic:master Sep 8, 2020
@sulemanof sulemanof deleted the fix/markdown_date_field_label branch September 8, 2020 14:44
sulemanof added a commit that referenced this pull request Sep 8, 2020
* Introduce formatted date field label

* Apply changes

* Use default format if can't parse, add comments

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
@MakoWish
Copy link
Copy Markdown

MakoWish commented Jul 21, 2022

We just upgraded from 8.2.0 to 8.3.2, and it seems this PR was either removed or broken with the update. We are right back to where we were before this PR. There is no longer an option to display human-readable dates or boolean values again. My visualization that worked perfectly after this merge now gives an error:

Error processing your markdown
{{formatted}} is an unknown variable

Looking at the options for displaying the markdown, we are exactly back to how I described in Issue 75341

Eric

@flash1293
Copy link
Copy Markdown
Contributor

@MakoWish I can confirm there has been a regression - I opened a new issue to track it here: #136929

We will take care of it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Feature:TSVB TSVB (Time Series Visual Builder) release_note:enhancement Team:Visualizations Team label for Lens, elastic-charts, Graph, legacy editors (TSVB, Visualize, Timelion) t// v7.10.0 v8.0.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TSVB Markdown Top Date Value Shows Array of Dates

8 participants