Sha256: 5f0deb1855b9c13bf5f9e63189a48188440ae037c9a0b7de85809ff864c5b8f7

Contents?: true

Size: 1.62 KB

Versions: 3

Compression:

Stored size: 1.62 KB

Contents

require 'thread'

module Percheron
  module Formatters
    module Stack
      class Table

        def initialize(stack)
          @stack = stack
          @queue = Queue.new
        end

        def generate
          Terminal::Table.new(title: title, headings: headings, rows: rows)
        end

        private

          attr_reader :stack, :queue

          def title
            stack.name
          end

          def headings
            [
              'Unit',
              'ID',
              'Running?',
              'IP',
              'Ports',
              'Volumes',
              'Version'
            ]
          end

          def rows
            queue_jobs
            process_queue!
          end

          def queue_jobs
            stack.units.map { |_, unit| queue << row_for(unit) }
          end

          def process_queue!
            resp = []
            4.times.map do
              Thread.new do
                queue.size.times { resp << queue.pop(true) }
              end
            end.map(&:join)
            resp
          end

          def row_for(unit)
            [
              unit.name,
              unit.id,
              startable(unit),
              unit.ip,
              unit.ports.join(', '),
              unit.volumes.join(', '),
              version(unit)
            ]
          end

          def version(unit)
            (unit.built_version == '0.0.0') ? '' : unit.built_version
          end

          def startable(unit)
            if unit.startable?
              unit.running? ? 'yes' : '-'
            else
              'n/a'
            end
          end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
percheron-0.7.10 lib/percheron/formatters/stack/table.rb
percheron-0.7.9 lib/percheron/formatters/stack/table.rb
percheron-0.7.8 lib/percheron/formatters/stack/table.rb