Class: OsCtl::Cli::Ps::Columns

Inherits:
Object
  • Object
show all
Includes:
Lib::Utils::Humanize
Defined in:
lib/osctl/cli/ps/columns.rb

Constant Summary collapse

COLS =
%i[
  pool
  ctid
  pid
  ctpid
  ruid
  rgid
  euid
  egid
  ctruid
  ctrgid
  cteuid
  ctegid
  vmsize
  rss
  state
  start
  time
  command
  name
].freeze
DEFAULT_ONE_CT =
%i[
  pid
  ctpid
  cteuid
  vmsize
  rss
  state
  start
  time
  command
].freeze
DEFAULT_ONE_POOL =
%i[ctid] + DEFAULT_ONE_CT
DEFAULT_MULTIPLE_POOLS =
%i[pool] + DEFAULT_ONE_POOL
ALIGN_RIGHT =
%i[
  pid
  ruid
  rgid
  euid
  egid
  ctruid
  ctrgid
  cteuid
  ctegid
  vmsize
  rss
  time
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(os_proc, precise) ⇒ Columns

Returns a new instance of Columns.

Parameters:

  • os_proc (OsCtl::Lib::OsProcess)
  • precise (Boolean)


90
91
92
93
# File 'lib/osctl/cli/ps/columns.rb', line 90

def initialize(os_proc, precise)
  @os_proc = os_proc
  @precise = precise
end

Instance Attribute Details

#os_procObject (readonly, protected)

Returns the value of attribute os_proc.



181
182
183
# File 'lib/osctl/cli/ps/columns.rb', line 181

def os_proc
  @os_proc
end

Class Method Details

.generate(process_list, cols, precise) ⇒ Array<Hash>

Parameters:

  • process_list (OsCtl::Lib::ProcessList)
  • cols (Array<Symbol>)
  • precise (Boolean)

Returns:

  • (Array<Hash>)


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/osctl/cli/ps/columns.rb', line 62

def self.generate(process_list, cols, precise)
  spec = cols.map do |c|
    {
      name: c,
      label: c.to_s.upcase,
      align: ALIGN_RIGHT.include?(c) ? :right : :left
    }
  end

  data = []

  process_list.each do |os_proc|
    row = new(os_proc, precise)

    begin
      data << cols.to_h { |c| [c, row.send(c)] }
    rescue OsCtl::Lib::Exceptions::OsProcessNotFound
      next
    end
  end

  [spec, data]
end

Instance Method Details

#commandObject



165
166
167
168
169
170
171
172
173
# File 'lib/osctl/cli/ps/columns.rb', line 165

def command
  cmdline = os_proc.cmdline

  if cmdline.strip.empty?
    "[#{os_proc.name}]"
  else
    cmdline
  end
end

#ctegidObject



141
142
143
# File 'lib/osctl/cli/ps/columns.rb', line 141

def ctegid
  mapped_id_or_fallback(:egid)
end

#cteuidObject



137
138
139
# File 'lib/osctl/cli/ps/columns.rb', line 137

def cteuid
  mapped_id_or_fallback(:euid)
end

#ctidObject



100
101
102
103
# File 'lib/osctl/cli/ps/columns.rb', line 100

def ctid
  pool, id = os_proc.ct_id
  pool.nil? ? '[host]' : id
end

#ctpidObject



109
110
111
# File 'lib/osctl/cli/ps/columns.rb', line 109

def ctpid
  os_proc.ct_pid
end

#ctrgidObject



133
134
135
# File 'lib/osctl/cli/ps/columns.rb', line 133

def ctrgid
  mapped_id_or_fallback(:rgid)
end

#ctruidObject



129
130
131
# File 'lib/osctl/cli/ps/columns.rb', line 129

def ctruid
  mapped_id_or_fallback(:ruid)
end

#egidObject



125
126
127
# File 'lib/osctl/cli/ps/columns.rb', line 125

def egid
  os_proc.egid
end

#euidObject



121
122
123
# File 'lib/osctl/cli/ps/columns.rb', line 121

def euid
  os_proc.euid
end

#format_time(v) ⇒ Object (protected)



195
196
197
198
199
200
201
202
203
# File 'lib/osctl/cli/ps/columns.rb', line 195

def format_time(v)
  now = Time.now

  if now - v > 24 * 60 * 60
    v.strftime('%b%d')
  else
    v.strftime('%H:%M')
  end
end

#mapped_id_or_fallback(id) ⇒ Object (protected)



205
206
207
208
209
# File 'lib/osctl/cli/ps/columns.rb', line 205

def mapped_id_or_fallback(id)
  os_proc.send(:"ct_#{id}")
rescue OsCtl::Lib::Exceptions::IdMappingError
  os_proc.send(id) * -1
end

#nameObject



175
176
177
# File 'lib/osctl/cli/ps/columns.rb', line 175

def name
  os_proc.name
end

#pidObject



105
106
107
# File 'lib/osctl/cli/ps/columns.rb', line 105

def pid
  os_proc.pid
end

#poolObject



95
96
97
98
# File 'lib/osctl/cli/ps/columns.rb', line 95

def pool
  pool, = os_proc.ct_id
  pool.nil? ? '-' : pool
end

#precise?Boolean (protected)

Returns:

  • (Boolean)


211
212
213
# File 'lib/osctl/cli/ps/columns.rb', line 211

def precise?
  @precise
end

#present_data(v) ⇒ Object (protected)



183
184
185
# File 'lib/osctl/cli/ps/columns.rb', line 183

def present_data(v)
  OsCtl::Lib::Cli::Presentable.new(v, formatted: precise? ? nil : humanize_data(v))
end

#present_duration(v) ⇒ Object (protected)



191
192
193
# File 'lib/osctl/cli/ps/columns.rb', line 191

def present_duration(v)
  OsCtl::Lib::Cli::Presentable.new(v, formatted: precise? ? nil : format_short_duration(v))
end

#present_time(v) ⇒ Object (protected)



187
188
189
# File 'lib/osctl/cli/ps/columns.rb', line 187

def present_time(v)
  OsCtl::Lib::Cli::Presentable.new(v, formatted: precise? ? nil : format_time(v))
end

#rgidObject



117
118
119
# File 'lib/osctl/cli/ps/columns.rb', line 117

def rgid
  os_proc.rgid
end

#rssObject



149
150
151
# File 'lib/osctl/cli/ps/columns.rb', line 149

def rss
  present_data(os_proc.rss)
end

#ruidObject



113
114
115
# File 'lib/osctl/cli/ps/columns.rb', line 113

def ruid
  os_proc.ruid
end

#startObject



157
158
159
# File 'lib/osctl/cli/ps/columns.rb', line 157

def start
  present_time(os_proc.start_time)
end

#stateObject



153
154
155
# File 'lib/osctl/cli/ps/columns.rb', line 153

def state
  os_proc.state
end

#timeObject



161
162
163
# File 'lib/osctl/cli/ps/columns.rb', line 161

def time
  present_duration(os_proc.user_time + os_proc.sys_time)
end

#vmsizeObject



145
146
147
# File 'lib/osctl/cli/ps/columns.rb', line 145

def vmsize
  present_data(os_proc.vmsize)
end