Skip to content

Introduce database metric batch average operation

What does this MR do and why?

This merge request introduces batch average operation to be used in DatabaseMetric instrumentation classes of Service Ping. This new method will be used initially to create two metrics for counting the average of deployment approvals of protected environments in #361811 (closed), and will also be documented in a later merge request to make this available for usage in other instrumentations where applicable.

Please note: lots of inspiration came from !35922 (merged), so if anything is missing, please let me know and I'd happy to discuss/solve it to get this shipped and unblock #361811 (closed).

Resolves #364815 (closed).

Sample Queries

Below is an example for how this might be used (from my local GDK setup), and the resulting queries:

[3] pry(main)> Ci::Pipeline.count # => 29
[4] pry(main)> Ci::Pipeline.pluck(:duration) # => [2, 2, 1, 1, 2, 1, 2, 1, 5, 3, 1, 2, 1, 4, 4, 3, 2, 1, 1, 3, 4, 6, 3, 4, 2, 2, 1, 5, 1]
[5] pry(main)> Gitlab::Database::BatchCount.batch_average(Ci::Pipeline, :duration) # => 0.24137931034482759e1
(1.1ms)  SELECT MIN("ci_pipelines"."id") FROM "ci_pipelines"
(0.4ms)  SELECT MAX("ci_pipelines"."id") FROM "ci_pipelines"
(0.4ms)  SELECT AVG("ci_pipelines"."duration") FROM "ci_pipelines" WHERE "ci_pipelines"."id" >= 45 AND "ci_pipelines"."id" < 93 

MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by Ahmed Hemdan

Merge request reports

Loading