summaryrefslogtreecommitdiff
path: root/lib/gitlab/metrics_dashboard/sorter.rb
blob: 59b21d207e108219d09d2e621006384765671041 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# frozen_string_literal: true

module Gitlab
  module MetricsDashboard
    class Sorter
      class << self
        def transform!(dashboard, _project)
          sort_groups!(dashboard)
          sort_panels!(dashboard)
        end

        private

        # Sorts the groups in the dashboard by the :priority key
        def sort_groups!(dashboard)
          dashboard[:panel_groups] = dashboard[:panel_groups].sort_by { |group| -group[:priority].to_i }
        end

        # Sorts the panels in the dashboard by the :weight key
        def sort_panels!(dashboard)
          dashboard[:panel_groups].each do |group|
            group[:panels] = group[:panels].sort_by { |panel| -panel[:weight].to_i }
          end
        end
      end
    end
  end
end