Sha256: 5f5485b27f2fd1d9c2d2aff521ef2fbe8c61acab3638cd718884dfb29cf29476

Contents?: true

Size: 1.57 KB

Versions: 10

Compression:

Stored size: 1.57 KB

Contents

require "cf/cli/app/base"

module CF::App
  class Stats < Base
    desc "Display application instance status"
    group :apps, :info
    input :app, :desc => "Application to get the stats for",
          :argument => true, :from_given => by_name(:app)
    def stats
      app = input[:app]

      stats =
        with_progress("Getting stats for #{c(app.name, :name)}") do |s|
          begin
            app.stats
          rescue CFoundry::StatsError
            s.fail do
              err "Application #{b(app.name)} is not running."
              return
            end
          end
        end

      line unless quiet?

      table(
        %w{instance cpu memory disk},
        stats.sort_by { |idx, _| idx.to_i }.collect { |idx, info|
          idx = c("\##{idx}", :instance)

          if info[:state] == "DOWN"
            [idx, c("down", :bad)]
          else
            stats = info[:stats]
            usage = stats[:usage]

            if usage
              [ idx,
                "#{percentage(usage[:cpu])}",
                "#{usage(usage[:mem], stats[:mem_quota])}",
                "#{usage(usage[:disk], stats[:disk_quota])}"
              ]
            else
              [idx, c("n/a", :neutral)]
            end
          end
        })
    end

    def percentage(num, low = 50, mid = 70)
      color =
        if num <= low
          :good
        elsif num <= mid
          :warning
        else
          :bad
        end

      c(format("%.1f\%", num), color)
    end

    def usage(used, limit)
      "#{b(human_size(used))} of #{b(human_size(limit, 0))}"
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
cf-5.2.1.rc4 lib/cf/cli/app/stats.rb
cf-5.2.1.rc3 lib/cf/cli/app/stats.rb
cf-5.2.1.rc2 lib/cf/cli/app/stats.rb
cf-5.2.0 lib/cf/cli/app/stats.rb
cf-5.1.0.rc1 lib/cf/cli/app/stats.rb
cf-5.0.0.rc4 lib/cf/cli/app/stats.rb
cf-5.0.0.rc3 lib/cf/cli/app/stats.rb
cf-5.0.0.rc1 lib/cf/cli/app/stats.rb
cf-4.2.10.rc2 lib/cf/cli/app/stats.rb
cf-4.2.10.rc1 lib/cf/cli/app/stats.rb