Skip to content

Add ability to sort vulnerabilities by state

What does this MR do?

Related to #249245 (closed)

This change adds ability to sort vulnerabilities by state in GraphQL.

Queries

SELECT "vulnerabilities".*
FROM "vulnerabilities"
WHERE "vulnerabilities"."project_id" = 278964
ORDER BY ARRAY_POSITION(
    ARRAY [1, 4, 3, 2]::smallint [],
    "vulnerabilities"."state"
  ) ASC,
  "vulnerabilities"."id" DESC
LIMIT 100;
SELECT "vulnerabilities".*
FROM "vulnerabilities"
WHERE "vulnerabilities"."project_id" = 278964
ORDER BY ARRAY_POSITION(
    ARRAY [1, 4, 3, 2]::smallint [],
    "vulnerabilities"."state"
  ) DESC,
  "vulnerabilities"."id" DESC
LIMIT 100;
CREATE INDEX index_vulnerabilities_on_state_case_id ON vulnerabilities USING btree (array_position(ARRAY[(1)::smallint, (4)::smallint, (3)::smallint, (2)::smallint], state), id DESC);

The query has been executed. Duration: 18.512 s

CREATE INDEX index_vulnerabilities_on_state_case_id_desc ON vulnerabilities USING btree (array_position(ARRAY[(1)::smallint, (4)::smallint, (3)::smallint, (2)::smallint], state) DESC, id DESC);

The query has been executed. Duration: 4.828 s

Before adding indexes:

ASC: https://explain.depesz.com/s/8R5R (~53ms)

DESC: https://explain.depesz.com/s/DxWA (~58ms)

After adding indexes:

ASC: https://explain.depesz.com/s/ehiT (~7.5ms)

DESC: https://explain.depesz.com/s/Sf0f (~2ms)

Migration

⋊> ~/D/g/gitlab on 249245-add-sorting-vulnerabilities-by-state ⨯ env VERBOSE=true bundle exec rake db:migrate:up VERSION=20200928164807                                                                                 03:12:48
== 20200928164807 AddIndexOnVulnerabilitiesStateCase: migrating ===============
-- transaction_open?()
   -> 0.0000s
-- index_exists?(:vulnerabilities, "ARRAY_POSITION(ARRAY[1, 4, 3, 2]::smallint[], state), id DESC", {:name=>"index_vulnerabilities_on_state_case_id", :algorithm=>:concurrently})
   -> 0.0056s
-- execute("SET statement_timeout TO 0")
   -> 0.0001s
-- add_index(:vulnerabilities, "ARRAY_POSITION(ARRAY[1, 4, 3, 2]::smallint[], state), id DESC", {:name=>"index_vulnerabilities_on_state_case_id", :algorithm=>:concurrently})
   -> 0.0792s
-- execute("RESET ALL")
   -> 0.0002s
-- transaction_open?()
   -> 0.0000s
-- index_exists?(:vulnerabilities, "ARRAY_POSITION(ARRAY[1, 4, 3, 2]::smallint[], state) DESC, id DESC", {:name=>"index_vulnerabilities_on_state_case_id_desc", :algorithm=>:concurrently})
   -> 0.0037s
-- add_index(:vulnerabilities, "ARRAY_POSITION(ARRAY[1, 4, 3, 2]::smallint[], state) DESC, id DESC", {:name=>"index_vulnerabilities_on_state_case_id_desc", :algorithm=>:concurrently})
   -> 0.0663s
== 20200928164807 AddIndexOnVulnerabilitiesStateCase: migrated (0.1558s) ======
⋊> ~/D/g/gitlab on 249245-add-sorting-vulnerabilities-by-state  env VERBOSE=true bundle exec rake db:migrate:down VERSION=20200928164807                                                                                03:12:24
== 20200928164807 AddIndexOnVulnerabilitiesStateCase: reverting ===============
-- transaction_open?()
   -> 0.0000s
-- indexes(:vulnerabilities)
   -> 0.0058s
-- execute("SET statement_timeout TO 0")
   -> 0.0002s
-- remove_index(:vulnerabilities, {:algorithm=>:concurrently, :name=>"index_vulnerabilities_on_state_case_id_desc"})
   -> 0.0037s
-- execute("RESET ALL")
   -> 0.0001s
-- transaction_open?()
   -> 0.0000s
-- indexes(:vulnerabilities)
   -> 0.0032s
-- remove_index(:vulnerabilities, {:algorithm=>:concurrently, :name=>"index_vulnerabilities_on_state_case_id"})
   -> 0.0016s
== 20200928164807 AddIndexOnVulnerabilitiesStateCase: reverted (0.0153s) ======

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
Edited by Alan (Maciej) Paruszewski

Merge request reports