Skip to content

Expose instance statistics measurements via GraphQL

Adam Hegyi requested to merge 241673-expose-instance-statistics-via-graphql into master

What does this MR do?

This MR exposes the instance statistics measurements (weekly object counts) via GraphQL. Note: this resource is only available for admins.

GraphQL queries

Before start, make sure you seed the DB to have some dummy data:

FILTER=instance_statistics bundle exec rake db:seed_fu

Log in as Admin and go to http://localhost:3000/-/graphql-explorer

Query for the latest counts:

{
  projects_count: instanceStatisticsMeasurements(identifier: PROJECTS, first: 1) {
     nodes {
      count
      recordedAt
    }
  }
  groups_count: instanceStatisticsMeasurements(identifier: GROUPS, first: 1) {
     nodes {
      count
      recordedAt
    }
  }
  users_count: instanceStatisticsMeasurements(identifier: USERS, first: 1) {
     nodes {
      count
      recordedAt
    }
  }
  issues_count: instanceStatisticsMeasurements(identifier: ISSUES, first: 1) {
     nodes {
      count
      recordedAt
    }
  }  
  merge_requests_count: instanceStatisticsMeasurements(identifier: MERGE_REQUESTS, first: 1) {
     nodes {
      count
      recordedAt
    }
  }  
  pipelines_count: instanceStatisticsMeasurements(identifier: PIPELINES, first: 1) {
     nodes {
      count
      recordedAt
    }
  }  
}

Note: the nodes array could be null or [] when we don't have any measurements (counts) in the DB. In this case we should probably show "No data available" message.

Query to load historical data:

We plan to refresh the counts weekly. Data for the last 10 weeks for a particular "object":

{
  latest_projects_counts: instanceStatisticsMeasurements(identifier: PROJECTS, first: 10) {
     nodes {
      count
      recordedAt
    }
  }
}

DB Query

The identifier and recorded_at columns are backed by an index. Since the index is unique and we always query with identifier a tie breaker column for the sorting is not needed.

SELECT "analytics_instance_statistics_measurements".* FROM "analytics_instance_statistics_measurements" WHERE "analytics_instance_statistics_measurements"."identifier" = 1 ORDER BY "analytics_instance_statistics_measurements"."recorded_at" DESC LIMIT 1;
 Limit  (cost=0.14..0.36 rows=1 width=26) (actual time=0.024..0.025 rows=1 loops=1)
   ->  Index Scan Backward using index_on_instance_statistics_recorded_at_and_identifier on analytics_instance_statistics_measurements  (cost=0.14..2.31 rows=10 width=26) (actual time=0.022..0.023 rows=1 loops=1)
         Index Cond: (identifier = 1)
 Planning Time: 0.176 ms
 Execution Time: 0.056 ms
(5 rows)

Does this MR meet the acceptance criteria?

Conformity

Availability and Testing

Security

If this MR contains changes to processing or storing of credentials or tokens, authorization and authentication methods and other items described in the security review guidelines:

  • 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

Closes #241673 (closed)

Edited by Adam Hegyi

Merge request reports