Class: OsCtl::Cli::Tree

Inherits:
Object
  • Object
show all
Includes:
CGroupParams, Lib::Utils::Humanize
Defined in:
lib/osctl/cli/tree.rb

Constant Summary collapse

DECORATIONS =
{
  ascii: {
    branch: '|-- ',
    leaf: '`-- ',
    continuation: '|   '
  },
  unicode: {
    branch: '├── ',
    leaf: '└── ',
    continuation: ''
  }
}.freeze

Constants included from CGroupParams

CGroupParams::CGPARAM_DEFAULT_FIELDS, CGroupParams::CGPARAM_FIELDS, CGroupParams::CGPARAM_FILTERS, CGroupParams::CGPARAM_STATS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CGroupParams

#cg_add_raw_cgroup_params, #cg_add_stats, #cg_init_subsystems, #cg_list_raw_cgroup_params, #do_cgparam_apply, #do_cgparam_list, #do_cgparam_replace, #do_cgparam_set, #do_cgparam_unset, #do_set_cpu_limit, #do_set_memory, #do_unset_cpu_limit, #do_unset_memory, #parse_cgparams, #parse_subsystem

Constructor Details

#initialize(pool, parsable: false, color: true, containers: false) ⇒ Tree

Returns a new instance of Tree.



29
30
31
32
33
34
35
# File 'lib/osctl/cli/tree.rb', line 29

def initialize(pool, parsable: false, color: true, containers: false)
  @pool = pool
  @parsable = parsable
  @containers = containers

  Rainbow.enabled = color
end

Instance Attribute Details

#clientObject (readonly, protected)

Returns the value of attribute client.



101
102
103
# File 'lib/osctl/cli/tree.rb', line 101

def client
  @client
end

#ctsObject (readonly, protected)

Returns the value of attribute cts.



101
102
103
# File 'lib/osctl/cli/tree.rb', line 101

def cts
  @cts
end

#groupsObject (readonly, protected)

Returns the value of attribute groups.



101
102
103
# File 'lib/osctl/cli/tree.rb', line 101

def groups
  @groups
end

#parsableObject (readonly, protected)

Returns the value of attribute parsable.



101
102
103
# File 'lib/osctl/cli/tree.rb', line 101

def parsable
  @parsable
end

#poolObject (readonly, protected)

Returns the value of attribute pool.



101
102
103
# File 'lib/osctl/cli/tree.rb', line 101

def pool
  @pool
end

Class Method Details



23
24
25
26
27
# File 'lib/osctl/cli/tree.rb', line 23

def self.print(*, **)
  tree = new(*, **)
  tree.render
  tree.print
end

Instance Method Details

#decorationsObject (protected)



169
170
171
172
173
174
175
176
# File 'lib/osctl/cli/tree.rb', line 169

def decorations
  if ENV['LANG']
    DECORATIONS[/UTF-8/i =~ ENV['LANG'] ? :unicode : :ascii]

  else
    DECORATIONS[:ascii]
  end
end

#fetchObject (protected)



103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/osctl/cli/tree.rb', line 103

def fetch
  @client = OsCtl::Client.new
  @client.open

  cg_init_subsystems(@client)

  @groups = @client.cmd_data!(:group_list, pool:).sort! do |a, b|
    a[:name] <=> b[:name]
  end

  @cts = @client.cmd_data!(:ct_list, pool:) if @containers
end

#preprocessObject (protected)



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/osctl/cli/tree.rb', line 116

def preprocess
  groups.map! do |grp|
    # Read cgroup params
    cg_add_stats(
      grp,
      grp[:full_path],
      %i[memory cpu_us],
      parsable
    )

    # Supply parameters for renderer
    if grp[:name] == '/'
      grp[:parts] = ['/']
      grp[:shortname] = '/'
      grp[:parent] = nil

    else
      parts = grp[:name].split('/')
      grp[:parts] = parts
      grp[:shortname] = parts.last
      grp[:parent] = parts[0..-2].join('/')
      grp[:parent] = '/' if grp[:parent].empty?
    end

    # If containers aren't included, we're done
    next grp unless cts

    # Skip the group if it has no direct nor indirect containers
    next unless cts.detect { |ct| ct[:group].start_with?(grp[:name]) }

    # Include group's containers
    group_cts = cts.select { |ct| ct[:group] == grp[:name] }.sort! do |a, b|
      a[:id] <=> b[:id]
    end.map! do |ct|
      cg_add_stats(
        ct,
        ct[:group_path],
        %i[memory cpu_us],
        parsable
      )

      ct[:parts] = ct[:group].split('/') << ct[:id]
      ct[:shortname] = ct[:id]
      ct[:parent] = ct[:group]
      ct
    end

    [grp, group_cts]
  end
  groups.compact!
  groups.flatten!
end


87
88
89
90
91
92
93
94
95
96
97
# File 'lib/osctl/cli/tree.rb', line 87

def print
  OsCtl::Lib::Cli::OutputFormatter.print(
    groups,
    cols: [
      { label: cts ? 'GROUP/CONTAINER' : 'GROUP', name: :branch },
      :memory,
      :cpu_us
    ],
    layout: :columns, color: Rainbow.enabled
  )
end

#renderObject



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
# File 'lib/osctl/cli/tree.rb', line 37

def render
  fetch
  preprocess

  decor = decorations
  i = 0

  groups.map! do |grp|
    # Look ahead to see if the group has any more siblings on the same level
    has_sibling = groups[i + 1..].detect { |g| g[:parent] == grp[:parent] }

    dir_indent = if has_sibling
                   decor[:branch]
                 else
                   decor[:leaf]
                 end

    res = ''

    # For every level, we need to find out whether the groups have any siblings
    # on that level
    t = ''
    res << grp[:parts][0..-3].inject('') do |acc, v|
      t = File.join(t, v)

      has_sibling = groups[i + 1..].detect { |g| g[:parent] == t }

      acc << if has_sibling
               decor[:continuation]
             else
               '    '
             end
    end

    res << dir_indent if grp[:parent]

    res << if grp.has_key?(:name)
             Rainbow(grp[:shortname]).blue.bright

           else
             Rainbow(grp[:shortname]).red.bright
           end

    grp[:branch] = res

    i += 1
    grp
  end
end