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

[APM] Make aggregations optional in ES response type definition #43451

Merged
merged 5 commits into from
Aug 20, 2019

Conversation

dgieselaar
Copy link
Member

Closes #43436.

Our type definition for ElasticSearch aggregations suggests that the aggregations key will always be defined if we request aggregations. However, that is not the case, as there is an edge case where if no matching indices were found, the aggregations key will be undefined.

@dgieselaar dgieselaar added the release_note:skip Skip the PR/issue when compiling release notes label Aug 16, 2019
@dgieselaar dgieselaar requested a review from a team as a code owner August 16, 2019 13:57

return {
title: chartBase.title,
key: chartBase.key,
yUnit: chartBase.yUnit,
totalHits: hits.total,
series: Object.keys(chartBase.series).map((seriesKey, i) => {
const agg = aggregations[seriesKey];
const agg = aggregations ? aggregations[seriesKey] : { value: null };
Copy link
Member

Choose a reason for hiding this comment

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

Can you do this instead?

Suggested change
const agg = aggregations ? aggregations[seriesKey] : { value: null };
const overallValue = idx(aggregations, _=> _[seriesKey].value);

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmm, it's not the same though. It creates a union type of { value: number | null} | undefined which leads to errors further down the line. We would have to change line 68 to:

overallValue: agg ? agg.value : null,

still worth it?

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh, sorry, misread your suggestion!

Copy link
Member Author

Choose a reason for hiding this comment

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

Much better 👍

Copy link
Member

@sorenlouv sorenlouv Aug 16, 2019

Choose a reason for hiding this comment

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

I think you need to adjust ByteFormatter in formatters to also accept undefined. asBytes and asDynamicBytes already accepts number | undefined | null so makes sense for all of the byte formatting functions to behave consistently.

Copy link
Member

Choose a reason for hiding this comment

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

... aka so doing

overallValue: idx(aggregations, _=> _[seriesKey].value)

should still be possible.

0,
MAX_KPIS
)
: [];
Copy link
Member

@sorenlouv sorenlouv Aug 16, 2019

Choose a reason for hiding this comment

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

Can you avoid the ternary like this?

const visibleKpis = sortByOrder(
  formatBucket(resp.aggregations || []), 
  'percentage', 
  'desc'
).slice(0, MAX_KPIS)

Copy link
Member Author

Choose a reason for hiding this comment

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

resp.aggregations is not an array, but a plain object. If I pass in a plain object, TypeScript will complain about a type mismatch.

Copy link
Member

Choose a reason for hiding this comment

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

Ahh, yeah. You could also change formatBucket like:

  const formatBucket = (
    aggs:
      | typeof resp['aggregations']
      | Required<typeof resp>['aggregations']['by_date']['buckets'][0]
  ) => {
    if (!aggs) {
      return [];
    }

But if you prefer the ternary that's also totally fine.

@elasticmachine
Copy link
Contributor

💔 Build Failed

@elasticmachine
Copy link
Contributor

💔 Build Failed

@dgieselaar
Copy link
Member Author

Interestingly enough some of the API tests that run requests without matching indices already expected a 400 (which is actually a 500). All expect a 200 now.

@elasticmachine
Copy link
Contributor

💔 Build Failed

}
return cb(val);
};
};
Copy link
Member

Choose a reason for hiding this comment

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

I like how you've extracted the logic instead of having it in every formatter 👍
Can we do something similar with the time formatters?

btw. Looks like a similar null check in asDynamicBytes can now be removed.

Copy link
Member Author

Choose a reason for hiding this comment

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

Fwiw, this is now fixed

@elasticmachine
Copy link
Contributor

💔 Build Failed

Closes elastic#43436.

Our type definition for ElasticSearch aggregations suggests that the aggregations key will always be defined if we request aggregations. However, that is not the case, as there is an edge case where if no matching indices were found, the aggregations key will be undefined.
@elasticmachine
Copy link
Contributor

💚 Build Succeeded

@dgieselaar dgieselaar merged commit 53063d5 into elastic:master Aug 20, 2019
@dgieselaar dgieselaar deleted the aggregations-optional branch August 20, 2019 06:32
dgieselaar added a commit to dgieselaar/kibana that referenced this pull request Aug 20, 2019
…tic#43451)

* [APM] Make aggregations optional in ES response type definition

Closes elastic#43436.

Our type definition for ElasticSearch aggregations suggests that the aggregations key will always be defined if we request aggregations. However, that is not the case, as there is an edge case where if no matching indices were found, the aggregations key will be undefined.

* Review feedback

* Support undefined values in number formatters

* Expect 200s for all API requests even without matching indices

* Bail early for asDynamicBytes as well
dgieselaar added a commit that referenced this pull request Aug 20, 2019
* [APM] Make aggregations optional in ES response type definition

Closes #43436.

Our type definition for ElasticSearch aggregations suggests that the aggregations key will always be defined if we request aggregations. However, that is not the case, as there is an edge case where if no matching indices were found, the aggregations key will be undefined.

* Review feedback

* Support undefined values in number formatters

* Expect 200s for all API requests even without matching indices

* Bail early for asDynamicBytes as well
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release_note:skip Skip the PR/issue when compiling release notes v7.4.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[APM] Make aggregations optional in ES response type def
3 participants