jdrago999/distribot

View on GitHub
examples/status

Summary

Maintainability
Test Coverage
#!/usr/bin/env ruby

require 'distribot'
require 'byebug'

redis = Distribot.redis
loop do
  puts "\n"
  redis
    .smembers('distribot.flows.active')
    .map{|id| Distribot::Flow.find(id) }
    .sort_by(&:created_at)
    .reverse
    .each do |flow|
      # Cover the odd case of fetching a flow that hasn't had its first transition yet:
      latest_transition = flow.transitions.last or next

      latest_phase = flow.phase(latest_transition.to) || Distribot::Phase.new(name: "UNKNOWN")
      next if latest_phase.is_final
      time_in_phase = Time.now.to_f - latest_transition.timestamp
      total_age = Time.now.to_f - flow.created_at

      if latest_phase.is_final
        puts "Flow #{flow.id} is finished."
      else
        handler_counts = latest_phase.handlers.sort_by(&:name).map do |handler|
          remaining_tasks = redis.get("distribot.flow.#{flow.id}.#{latest_phase.name}.#{handler}.finished").to_i
          total_tasks = redis.get("distribot.flow.#{flow.id}.#{latest_phase.name}.#{handler}.finished.total").to_i
          percent_finished = ((total_tasks - remaining_tasks) * 1.0) / (total_tasks * 1.0) * 100
          finished_tasks = total_tasks - remaining_tasks
          "#{handler}:\t#{['=' * (finished_tasks > 0 ? finished_tasks : 0), '-' * (remaining_tasks > 0 ? remaining_tasks : 0)].join('')} #{finished_tasks}/#{total_tasks} (#{percent_finished.round(2)}%)"
        end
        puts "Flow #{flow.id} (age:#{sprintf('%05.02f',total_age)} sec)\tphase:#{latest_phase.name}\t(#{time_in_phase.round(2)} seconds\t#{handler_counts.join("\t")}"
      end
  end
  puts "-------------------------------------------------------------------------------------------------------------------------------------------------------"
  sleep 0.5
end