Class: JDC::App::Stats

Inherits:
Base show all
Defined in:
lib/jdc/cli/app/stats.rb

Instance Method Summary collapse

Methods inherited from Base

#app_status, #human_mb, #human_size, #megabytes, #memory_choices, #state_color

Methods included from LoginRequirements

#precondition

Methods inherited from CLI

#add_exception_name_to_msg, #build_client, #check_key, #check_logged_in, #check_organization_and_space, #check_target, client, #client, client=, #client_target, #color_enabled?, #debug?, #default_action, #ensure_config_dir, #err, #execute, #fail, #fail_unknown, #force?, #formatted_exception_output, #help, #help_header, #invalidate_client, #log_error, #name_list, #normalize_targets_info, #one_of, #quiet?, #remove_target_info, #sane_target_url, #save_target_info, #save_targets, #save_token_if_it_changes, #set_target, #table, #target_info, #targets_info, #user_colors, #verbose?, #wrap_errors

Methods included from Spacing

#indented, #justify, #line, #lines, #quiet?, #spaced, #start_line, #tabular, #text_width, #trim_escapes

Methods included from Interactive

#ask, #handler, #input_state, #list_choices, #prompt, #show_default

Instance Method Details

#percentage(num, low = 50, mid = 70) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/jdc/cli/app/stats.rb', line 50

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

#statsObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/jdc/cli/app/stats.rb', line 9

def stats
  app = input[:app]

  stats =
    with_progress("Getting stats for #{c(app.name, :name)}") do |s|
      begin
        app.stats
      rescue JFoundry::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

#usage(used, limit) ⇒ Object



63
64
65
# File 'lib/jdc/cli/app/stats.rb', line 63

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