Class: VpsAdmin::CLI::Commands::NetworkTop

Inherits:
HaveAPI::CLI::Command
  • Object
show all
Includes:
Curses
Defined in:
lib/vpsadmin/cli/commands/network_top.rb

Constant Summary collapse

REFRESH_RATE =
10
FILTERS =
%i[limit user environment location node vps network_interface].freeze

Instance Method Summary collapse

Instance Method Details

#exec(_args) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/vpsadmin/cli/commands/network_top.rb', line 34

def exec(_args)
  if @global_opts[:list_output]
    exclude = %i[id network_interface updated_at delta]

    @api.network_interface_monitor.actions[:index].params.each_key do |name|
      next if exclude.include?(name)

      puts name
    end

    exit
  end

  set_global_opts
  init_screen
  start_color
  crmode
  stdscr.keypad = true
  curs_set(0) # hide cursor
  use_default_colors

  init_pair(1, COLOR_BLACK, COLOR_WHITE)
  last = nil

  loop do
    now = Time.now

    if @refresh || last.nil? || (now - last) >= REFRESH_RATE
      @refresh = false
      render(now, true)
      last = Time.now
      self.timeout = REFRESH_RATE * 1000

    else
      render(last, false)
      self.timeout = (REFRESH_RATE - (now - last)) * 1000
    end

    case getch
    when 'q'
      break

    when Key::LEFT
      clear
      sort_next(-1)

    when Key::RIGHT
      clear
      sort_next(+1)

    when Key::UP, Key::DOWN
      clear
      sort_inverse

    when Key::RESIZE
      clear
    end
  end
rescue Interrupt
  # break loop and exit
ensure
  close_screen
end

#options(opts) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/vpsadmin/cli/commands/network_top.rb', line 14

def options(opts)
  @opts = {
    unit: :bits
  }

  opts.on('--unit UNIT', %w[bytes bits], 'Select data unit (bytes or bits)') do |v|
    @opts[:unit] = v.to_sym
  end

  opts.on('--limit LIMIT', Integer, 'Number of IP addresses to monitor') do |v|
    @opts[:limit] = v
  end

  (FILTERS - %i[limit]).each do |f|
    opts.on("--#{f.to_s.gsub('_', '-')} ID", Integer, "Filter network interfaces by #{f}") do |v|
      @opts[f] = v
    end
  end
end