module Commands::List

Public Class Methods

included(thor) click to toggle source
# File lib/commands/list.rb, line 3
def self.included(thor)
    thor.class_eval do
        option :all, :type => :boolean, :default => false
        option :order, :type => :string, :default => "updated_at"
        desc "ls", "List time series"
        def ls(search='')
            if options[:all]
                logs = Log.all
            else
                logs = Log.where(active: true)
            end
            logs = logs.includes(:series).includes(:tags)
            logs = logs.order(options[:order].to_sym => :desc)
            logs = logs.tagged(search) if !search.blank?

            creep_stats = Statistics.new
            table = [['#', 'name', '', 'tags', 'series', 'time', 'start', 'stop', 'estimate', 'creep %']] # header
            logs.each do |log|

                # tags
                tags = log.tags.map do |t|
                    t.tag
                end
                tags = tags.join ' '

                # active text
                active = "ACTIVE" if log.active == "t"

                # total counter
                total_time = (log.total_time/3600).round(2) # to hours
                total_time = "#{total_time} hours"

                # first start time
                start = time_display(log.series.first.start) if log.series.first

                # last stop time
                stop = time_display(log.series.last.end) if log.series.last

                # estimation and creep
                if log.estimation
                    creep = log.creep # creep %
                    creep_stats << creep
                    creep = creep.round(2) # round perecentage
                    estimation = (log.estimation/3600).round(2) # estimation to hours
                end

                # table
                table << [log.id,
                    log.name,
                    active || '',
                    tags,
                    log.series.count || '',
                    total_time || '',
                    start || '',
                    stop || '',
                    estimation || '',
                    creep || '']
            end

            if logs.count == 0
                output do
                    say "No logs found", :red
                end
                exit
            else
                output do
                    print_table table

                    output_spacer

                    say [logs.count.to_s, "logs out of", Log.count.to_s].join(' '), :cyan
                    say [creep_stats.count, "estimations"].join(' '), :cyan
                    say [creep_stats.mean.round(2), "percent mean creep"].join(' '), :cyan
                end
            end
        end
    end
end

Public Instance Methods

ls(search='') click to toggle source
# File lib/commands/list.rb, line 8
def ls(search='')
    if options[:all]
        logs = Log.all
    else
        logs = Log.where(active: true)
    end
    logs = logs.includes(:series).includes(:tags)
    logs = logs.order(options[:order].to_sym => :desc)
    logs = logs.tagged(search) if !search.blank?

    creep_stats = Statistics.new
    table = [['#', 'name', '', 'tags', 'series', 'time', 'start', 'stop', 'estimate', 'creep %']] # header
    logs.each do |log|

        # tags
        tags = log.tags.map do |t|
            t.tag
        end
        tags = tags.join ' '

        # active text
        active = "ACTIVE" if log.active == "t"

        # total counter
        total_time = (log.total_time/3600).round(2) # to hours
        total_time = "#{total_time} hours"

        # first start time
        start = time_display(log.series.first.start) if log.series.first

        # last stop time
        stop = time_display(log.series.last.end) if log.series.last

        # estimation and creep
        if log.estimation
            creep = log.creep # creep %
            creep_stats << creep
            creep = creep.round(2) # round perecentage
            estimation = (log.estimation/3600).round(2) # estimation to hours
        end

        # table
        table << [log.id,
            log.name,
            active || '',
            tags,
            log.series.count || '',
            total_time || '',
            start || '',
            stop || '',
            estimation || '',
            creep || '']
    end

    if logs.count == 0
        output do
            say "No logs found", :red
        end
        exit
    else
        output do
            print_table table

            output_spacer

            say [logs.count.to_s, "logs out of", Log.count.to_s].join(' '), :cyan
            say [creep_stats.count, "estimations"].join(' '), :cyan
            say [creep_stats.mean.round(2), "percent mean creep"].join(' '), :cyan
        end
    end
end