-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
Add scripted_metric in Visualize #8808
Add scripted_metric in Visualize #8808
Conversation
8623df0
to
0f9f536
Compare
return 'Scripted Metric'; | ||
}, | ||
getFormat: function () { | ||
return fieldFormats.getDefaultInstance('number') || fieldFormats.getDefaultInstance('percent'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think that defaulting to "percent" is necessary here. Just use the default instance of "number"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok I will correct that
name: 'reduce_script', | ||
type: 'string', | ||
editor: textHtml | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We'll need to add the ability to choose the scripting language to use here, which means it will need to reach out to the API to discover which scripting languages are available.
Fetching the languages is currently only done in the field editor, so check that out for some inspiration.
We also don't want to fetch the list of languages until we need it, so how about you add a controller to the new "lang"
param like we do in the terms agg and do the fetching in there.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently, because of ES, lang can't be passed globally for the 4 scripts.
If someone needs to use another language, a JSON object must be passed instead of a string for each script.
For example:
{ "inline": "....", "lang": "painless" }
Do you want me to add lang selection for each script?
If so, I think that a selectbox must be also added to switch between file/inline/stored and a new field to hold script params map. It is turning into a heavy development ^^
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that a global language selector is sufficient. Each param can specify a write()
method that can produce the { "inline": "", "lang": "" }
version of the param:
{
name: 'reduce_script',
type: 'string',
write(aggConfig, output) {
output.params.reduce_script = {
lang: aggConfig.params.lang,
inline: aggConfig.params.reduce_script
}
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is looking good, let me know if you need help with the requested changes
0f9f536
to
c907106
Compare
Can one of the admins verify this patch? |
All requested changes applied ;) |
Hope you don't mind, but I moved things around a little bit Cosium#1 |
jenkins, test this |
@reda-alaoui Can you update the description of this PR with details about the nature of the change? Keeping a reference to previous PRs/issues is great, but people should be able to figure out what this PR is all about without clicking into other PRs. That'll be greatly helpful for both reviewers and for when we link to this PR from release notes. Thanks! |
@epixa, I updated the description |
@@ -133,6 +133,7 @@ uiModules | |||
.append(param.editor) | |||
.get(0); | |||
} | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Corrected ^^
@reda-alaoui That is great, thanks! |
seems i need to enclose everything inside the fields in quotes ? for example in init field i need to put quotes around: if i do it without i get a compilation error. i think it would be nice to auto add those quotes, what do you think ? |
@ppisljar, I just did a test and I don't have your issue. But I noticed that blank script field were used to build a script object missing its inline attribute: "init_script": {
"lang": "painless"
} I corrected this in my latest commit 674e7d0. |
@ppisljar, can you provide your compilation error? |
|
@ppisljar, this is because the doc is wrong. Use "params": {
"init_script": "params._agg['result'] = 0d",
"map_script": "params._agg['result'] += doc['foo'].value",
"combine_script": "return params._agg['result']",
"reduce_script": "def result = 0d; for(int i=0; i<params._aggs.length; i++){result+=params._aggs[i];} return result;"
} |
Do we know when will this patch be available with official release ? Is it possible to use aggregations like avg , percentage sum etc while doing calculations on a scripted field ? |
@reda-alaoui I just tried checking out this PR on top of master and I get this error, whenever I click on for instance "Split Rows" in vis builder. Has anyone encountered this?
|
@tbragin can't reproduce your problem, could you provide exact steps ? thanks |
@Bargs metric and table visualizations use number formatter by default for this reason you get '' for all your values when you return 'foo'. #7302 depends on the field type to use the right formatter but in this case you could return anything from the script.
@reda-alaoui this is also missing tests. At least unit tests should be there but this specific change could also benefit from some functional tests. Let me know if you need help with any of those. Its OK with me to write those later when we'll have more clarity on which way we want to take this but I just wanted you to know. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with @ppisljar, this needs some tests to verify the new agg type
@Bargs can you give your opinion on this ? |
I think either option is reasonable at this time. We don't know how many users will want to return non-numeric values, so it may not be worth the additional complexity of exposing the field formatters. If we restrict the agg to numbers only we should add some help text explaining that, and show an error if we detect that the agg is returning a non-number. Down the road if a lot of users want non-numbers we can expose the field formatters as an option at that time. |
OK lets restrict this to only numeric types. If non numeric type is returned from script an error should be shown. What do you think @reda-alaoui ? |
@reda-alaoui A couple of questions and comments:
First, the example:
Problem 1. Sorting by metric resulting from the scripted metric aggregation does not work in the vis builder, and this is the default behavior when creating any sort of terms aggregations and sub-aggregations. Example with sub-aggregation on a line chart Example with Terms aggregation in table Problem 2. I saw some really strange behavior, where the visualization that previously worked somehow gets confused and switches rows and columns. There seems to be no way to recover. Have you seen that? |
+1 for this feature |
@reda-alaoui do you plan to take this further ? if not, would you be ok with me taking this pr further ? |
@ppisljar , I am sorry because I currently don't have time to finish this PR. |
Would like to know when this will be officially released? Is there any timeline? |
there is no timeline, but we do plan to take this further. |
Hi @reda-alaoui I'm inclined to close this, as this seems to be a little stalled for a while now. Thanks for your efforts on this, it would certainly be a nice improvement. From our end, there's no immediate plan to add this, so don't hesitate to reopen this PR if you would like to continue to work on this. We'll ping you should plans changed around this on our end too. Thanks again, |
I'm sad to see this pr closed :( |
Very sad as well! |
Fixes #2646
This PR adds the ability to use Scripted Metric Aggregations in Kibana visualizations.
Each scripted metric aggregation is composed of 5 fields:
lang
init_script
map_script
combine_script
reduce_script
lang
defines the language of the 4 scripts and is by default bound topainless
.Inline scripts are expected inside textareas.
Inspired by the changes made by @pineur in #5558.