Sha256: 915ce34a7c1f4ecdb480b3f25cb1deaaf8c75035e3078eae5dfe111e470dac8f

Contents?: true

Size: 1.63 KB

Versions: 4

Compression:

Stored size: 1.63 KB

Contents

# frozen_string_literal: true

module Gl
  class Global < Thor
    desc 'registry', 'display statistics about the registry'
    def registry
      spinner = TTY::Spinner.new('[:spinner] Fetching projects ...')

      results = {}

      spinner.auto_spin
      projects = Gitlab.projects.auto_paginate
      spinner.stop("Found #{projects.count}")

      bar = TTY::ProgressBar.new(
        'processing projects [:bar] :current/:total processed (:percent, :eta remaining)',
        total: projects.size
      )

      projects.each do |project|
        bar.advance(1)
        registries = begin
          Gitlab.registry_repositories(project.id).auto_paginate
                     rescue Gitlab::Error::Forbidden
                       next
        end

        next if registries.empty?

        infos = registries.map do |registry|
          tags = Gitlab.registry_repository_tags(project.id, registry.id).auto_paginate

          next if tags.empty?

          tags = tags.map do |tag|
            Gitlab.registry_repository_tag(project.id, registry.id, tag.name)
          rescue Gitlab::Error::NotFound
            next
          end

          tags
        end

        sizes = infos.flatten.compact.map(&:total_size)

        project_size = sizes.compact.inject(0) { |sum, x| sum + x }
        results[project.path_with_namespace] = (project_size / 1024.0 / 1024.0).round(2)
      end

      bar.finish
      table = TTY::Table.new(['Project', 'Size in MB'], results.sort_by { |_key, value| -value })
      table << ['Total usage', results.values.inject(0) { |sum, x| sum + x }.round(2)]
      puts table.render(:ascii, alignments: %i[left right])
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
gl-0.4.1 lib/gl/cli/global.rb
gl-0.4.0 lib/gl/cli/global.rb
gl-0.3.1 lib/gl/cli/global.rb
gl-0.3.0 lib/gl/cli/global.rb