Skip to content

Add support to generate metrics queries in instrumentation classes

What does this MR do?

Add support to generate metrics queries in instrumentation classes

  • Add to_sql method to DatabaseMetric
  • Move query generation logic in a separate class Gitlab::Usage::Metrics::Query
  • Refactor usage_data_queries.rb to use Gitlab::Usage::Metrics::Query
  • This will be later used for generating metrics queries for instrumentation classes

Usage/Test

[122] pry(main)> Gitlab::Usage::Metrics::Instrumentations::CountIssuesMetric.new(time_frame: '28d') 
=> #<Gitlab::Usage::Metrics::Instrumentations::CountIssuesMetric:0x00007fc8cb477f70 @options={}, @time_frame="28d">
[123] pry(main)> Gitlab::Usage::Metrics::Instrumentations::CountIssuesMetric.new(time_frame: '28d').to_sql
=> "SELECT COUNT(\"issues\".\"id\") FROM \"issues\" WHERE \"issues\".\"created_at\" BETWEEN '2021-04-24 14:22:13.996937' AND '2021-05-22 14:22:13.997029'"
[124] pry(main)> Gitlab::Usage::Metrics::Instrumentations::CountUsersCreatingIssuesMetric.new(time_frame: '28d').to_sql
=> "SELECT COUNT(DISTINCT \"issues\".\"author_id\") FROM \"issues\" WHERE \"issues\".\"created_at\" BETWEEN '2021-04-24 14:22:30.095072' AND '2021-05-22 14:22:30.095156'"
[125] pry(main)> Gitlab::Usage::Metrics::Instrumentations::CountUsersCreatingIssuesMetric.new(time_frame: 'all').to_sql
=> "SELECT COUNT(DISTINCT \"issues\".\"author_id\") FROM \"issues\""

Screenshots (strongly suggested)

Does this MR meet the acceptance criteria?

Conformity

Availability and Testing

Security

Does this MR contain changes to processing or storing of credentials or tokens, authorization and authentication methods or other items described in the security review guidelines? If not, then delete this Security section.

  • Label as security and @ mention @gitlab-com/gl-security/appsec
  • The MR includes necessary changes to maintain consistency between UI, API, email, or other methods
  • Security reports checked/validated by a reviewer from the AppSec team

Related to #326425 (closed)

Edited by Alina Mihaila

Merge request reports