Class: OsCtl::Cli::Tree

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

Constant Summary collapse

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

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_stats, #cg_blkio_stats, #cg_read_stats, #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_memory_stat, #parse_subsystem, #read_cgparam, #read_memory_usage

Methods included from Utils::Humanize

#break_interval, #format_long_duration, #format_percent, #format_short_duration, #humanize_data, #humanize_time_ns, #parse_data

Constructor Details

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

Returns a new instance of Tree



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

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



99
100
101
# File 'lib/osctl/cli/tree.rb', line 99

def client
  @client
end

#ctsObject (readonly, protected)

Returns the value of attribute cts



99
100
101
# File 'lib/osctl/cli/tree.rb', line 99

def cts
  @cts
end

#groupsObject (readonly, protected)

Returns the value of attribute groups



99
100
101
# File 'lib/osctl/cli/tree.rb', line 99

def groups
  @groups
end

#parsableObject (readonly, protected)

Returns the value of attribute parsable



99
100
101
# File 'lib/osctl/cli/tree.rb', line 99

def parsable
  @parsable
end

#poolObject (readonly, protected)

Returns the value of attribute pool



99
100
101
# File 'lib/osctl/cli/tree.rb', line 99

def pool
  @pool
end

Class Method Details



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

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

Instance Method Details

#decorationsObject (protected)



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

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

  else
    DECORATIONS[:ascii]
  end
end

#fetchObject (protected)



101
102
103
104
105
106
107
108
109
110
# File 'lib/osctl/cli/tree.rb', line 101

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

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

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

#preprocessObject (protected)



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

def preprocess
  groups.map! do |grp|
    # Read cgroup params
    cg_add_stats(
      client,
      grp,
      grp[:full_path],
      %i(memory cpu_time),
      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(
        client,
        ct,
        ct[:group_path],
        %i(memory cpu_time),
        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


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

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

#renderObject



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

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..-1].detect { |g| g[:parent] == grp[:parent] }

    if has_sibling
      dir_indent = decor[:branch]
    else
      dir_indent = 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..-1].detect { |g| g[:parent] == t }

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

    res << dir_indent if grp[:parent]

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

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

    grp[:branch] = res

    i += 1
    grp
  end
end