Class: OsCtl::Cli::Ps::Filter

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

Constant Summary collapse

OPERANDS =
%w[=~ !~ != >= <= = > <].freeze
NUMERIC =
%i[
  pid
  ctpid
  ruid
  rgid
  euid
  egid
  ctruid
  ctrgid
  cteuid
  ctegid
].freeze
BYTES =
%i[
  vmsize
  rss
].freeze
TIMES =
%i[
  start
  time
].freeze
STRINGS =
%i[
  pool
  ctid
  state
  command
  name
].freeze
ALL_PARAMS =
NUMERIC + BYTES + TIMES + STRINGS

Instance Method Summary collapse

Constructor Details

#initialize(str_rule) ⇒ Filter

Returns a new instance of Filter.

Parameters:

  • str_rule (String)

    condition written as string



43
44
45
# File 'lib/osctl/cli/ps/filter.rb', line 43

def initialize(str_rule)
  @parameter, @op, @value = parse_rule(str_rule)
end

Instance Method Details

#get_param(process, param) ⇒ Object (protected)



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/osctl/cli/ps/filter.rb', line 106

def get_param(process, param)
  case param
  when :command
    v = process.cmdline
    v.empty? ? process.name : v

  when :start
    process.start_time.to_i

  when :time
    process.user_time + process.sys_time

  else
    process.send(param)
  end
end

#match?(process) ⇒ Boolean

Parameters:

  • process (OsCtl::Lib::OsProcess)

Returns:

  • (Boolean)


49
50
51
52
53
54
55
56
57
58
59
# File 'lib/osctl/cli/ps/filter.rb', line 49

def match?(process)
  param = get_param(process, @parameter)

  if @op == :=~
    @value.match?(param)
  elsif @op == :!~
    !@value.match?(param)
  else
    param.send(@op, @value)
  end
end

#parse_rule(str_rule) ⇒ Object (protected)



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
97
98
99
100
101
102
103
104
# File 'lib/osctl/cli/ps/filter.rb', line 63

def parse_rule(str_rule)
  ret_param, ret_op, ret_value = nil

  OPERANDS.detect do |op|
    i = str_rule.index(op)
    next if i.nil?

    ret_param = str_rule[0..(i - 1)].to_sym

    unless ALL_PARAMS.include?(ret_param)
      raise ArgumentError, "invalid parameter #{ret_param.to_s.inspect}"
    end

    is_string = STRINGS.include?(ret_param)
    is_num = NUMERIC.include?(ret_param)
    is_data = !is_string && !is_num && BYTES.include?(ret_param)
    is_time = !is_string && !is_num && !is_data && TIMES.include?(ret_param)
    ret_op = op == '=' ? :== : op.to_sym

    if !is_string && %i[=~ !~].include?(ret_op)
      raise ArgumentError, "invalid parameter filter #{str_rule.inspect}: #{ret_op} cannot be used on numbers"
    end

    value = str_rule[(i + op.size)..]
    ret_value =
      if is_num || is_time
        value.to_i
      elsif is_data
        parse_data(value)
      elsif %i[=~ !~].include?(ret_op)
        Regexp.new(value)
      else
        value
      end
  end

  if ret_op.nil?
    raise ArgumentError, "invalid parameter filter #{str_rule.inspect}: unknown operand"
  end

  [ret_param, ret_op, ret_value]
end