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

Add support for percentiles aggregation #306

Merged

Conversation

milicns
Copy link
Contributor

@milicns milicns commented Aug 18, 2023

Part of #118

* to be performed.
*/
final def percentilesAggregation[A: Numeric](name: String, field: Field[_, A]): PercentilesAggregation =
Percentiles(name = name, field = field.toString, Chunk.empty, missing = None)
Copy link
Collaborator

Choose a reason for hiding this comment

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

All parameters should be named.

Comment on lines 235 to 236
) extends PercentilesAggregation { self =>
def missing(value: Double): PercentilesAggregation =
Copy link
Collaborator

Choose a reason for hiding this comment

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

Put an empty line between.

with WithAgg {

/**
* Sets the `percents` field for the [[zio.elasticsearch.aggregation.PercentilesAggregation]].
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
* Sets the `percents` field for the [[zio.elasticsearch.aggregation.PercentilesAggregation]].
* Sets the `percents` parameter for the [[zio.elasticsearch.aggregation.PercentilesAggregation]].

* Sets the `percents` field for the [[zio.elasticsearch.aggregation.PercentilesAggregation]].
*
* @param percents
* a array of percentiles for calculation for [[zio.elasticsearch.aggregation.PercentilesAggregation]]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
* a array of percentiles for calculation for [[zio.elasticsearch.aggregation.PercentilesAggregation]]
* a array of percentiles to be calculated for [[zio.elasticsearch.aggregation.PercentilesAggregation]]

*This is only suggestion. I am not sure, if meaning stays the same.

Comment on lines 242 to 243
def percents(percents: Double*): PercentilesAggregation =
self.copy(percents = Chunk.fromIterable(percents))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Does it make sense to restrict percents parameter, so it must have at least one value? Something like this percents(percent: Double, percents: Double*)?

Copy link
Collaborator

Choose a reason for hiding this comment

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

And, maybe we could do something like this:

    def percents(percent: Double, percents: Double*): PercentilesAggregation =
      self.copy(percents = percents ++ (percent +: percents))


private[elasticsearch] def toJson: Json = {
val percentsField = if (percents.nonEmpty) Some("percents" -> Arr(percents.map(_.toJson))) else None
val optionalFields = percentsField ++ missing.map("missing" -> _.toJson)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe we could omit this optionalFields field, and write it directly in return?

implicit val decoder: JsonDecoder[PercentilesAggregationResponse] =
DeriveJsonDecoder.gen[PercentilesAggregationResponse]
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

Look at lines below (140+). You must adjust that too. Please, test this aggregation as sub aggregation of some other aggregation (you can add one it-test also).

Copy link
Collaborator

Choose a reason for hiding this comment

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

And below at 171+.

@@ -221,6 +221,30 @@ object ElasticAggregationSpec extends ZIOSpecDefault {
)
)
},
test("percentiles") {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Add aggregationTsWithAllParams test (in creating json too).

Comment on lines 232 to 246
assert(aggregation)(
equalTo(Percentiles(name = "aggregation", field = "testField", Chunk.empty, missing = None))
) &&
assert(aggregationTs)(
equalTo(Percentiles(name = "aggregation", field = "intField", Chunk.empty, missing = None))
) &&
assert(aggregationTsRaw)(
equalTo(Percentiles(name = "aggregation", field = "intField.raw", Chunk.empty, missing = None))
) &&
assert(aggregationWithMissing)(
equalTo(Percentiles(name = "aggregation", field = "intField", Chunk.empty, missing = Some(20.0)))
) &&
assert(aggregationWithPercents)(
equalTo(Percentiles(name = "aggregation", field = "intField", Chunk(65, 90, 99), missing = None))
)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Make all params named.

@milicns milicns requested a review from dbulaja98 August 22, 2023 09:07
```

You can find more information about `Percentiles` aggregation [here](https://www.elastic.co/guide/en/elasticsearch/reference/7.17/search-aggregations-metrics-percentile-aggregation.html).

Copy link
Member

Choose a reason for hiding this comment

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

Unnecessary new line.

Copy link
Collaborator

Choose a reason for hiding this comment

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

We have it everywhere.

private[elasticsearch] final case class Percentiles(
name: String,
field: String,
percents: Chunk[Double],
Copy link
Member

Choose a reason for hiding this comment

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

Let's put missing before percents. I prefer alphabetically sorted fields and methods.

def missing(value: Double): PercentilesAggregation =
self.copy(missing = Some(value))

def withAgg(agg: SingleElasticAggregation): MultipleAggregations =
Copy link
Member

Choose a reason for hiding this comment

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

Please put withAgg below percents.

def withAgg(agg: SingleElasticAggregation): MultipleAggregations =
multipleAggregations.aggregations(self, agg)

def percents(percent: Double, percents: Double*): PercentilesAggregation =
Copy link
Member

Choose a reason for hiding this comment

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

Do we have any specific reason why we named this percents if the aggregation is percentiles?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In elastic api the parameter is named percents. But i can change the name to percentiles if it seems more intuitive.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed to leave this named percents

@dbulaja98 dbulaja98 merged commit 0ad2161 into lambdaworks:main Aug 23, 2023
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants