Skip to content

Add used_by_project_ids and fetch_ci_components_used to usage model

Rajendra Kadam requested to merge 466575-usage-model-components-usage into master

What does this MR do and why?

This MR is a pre-work for !164731

Refer to issue #466575

This MR adds a couple of methods in Usage model, to do the following:

  1. used_by_project_ids: Fetch the list of projects that used any CI/CD catalog components in the last 30 days. Now it does not read all the projects in the database, it is given a list of projects to check from. It returns the ActiveRecord object of each project that were found using components in the last 30 days. Query plan.
  2. fetch_ci_components_used: Fetch the list of components used by a given single project, and among the components if there are duplicate usages, it fetches the latest usage based on used_date column. We already have the used_date column indexed in a composite index. Query plan for the part before mapping on the response.

MR acceptance checklist

Please evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

Screenshots or screen recordings

NA

How to set up and validate locally

  1. Create some catalog resource records using the below command in rails console:

bundle exec rake "gitlab:seed:ci_catalog_resources[Flightjs, 12, true]"

  1. Create some dummy usage records for a given project with a given component as follows:
project = Project.last
component = Ci::Catalog::Resources::Component.first
catalog_resource = component.catalog_resource
Ci::Catalog::Resources::Components::Usage.create!(
  component: component,
  catalog_resource: catalog_resource,
  project: project,
  used_by_project_id: project.id,
  used_date: Date.today - 2.days)
  1. Repeat step 2 for as many project/resource/component combinations you would like and create usage records for all.
  2. Run the Ci::Catalog::Resources::Components::Usage.used_by_project_ids(Project.all) to get the list of projects where the components were used in the last 30 days.
  3. Run the Ci::Catalog::Resources::Components::Usage.fetch_ci_components_used(Project.first) to get the list of components that were used in the given project along with component name, its version and last used date.

Related to #466575

Edited by Rajendra Kadam

Merge request reports

Loading