Class: Kuroko2::Api::StatsController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/kuroko2/api/stats_controller.rb

Constant Summary collapse

COUNT_INSTANCES_SQL =
<<-SQL
  SELECT COUNT(error_at IS NULL OR NULL)     AS `kuroko2.job_instances.working`,
         COUNT(error_at IS NOT NULL OR NULL) AS `kuroko2.job_instances.error`
  FROM #{Kuroko2::JobInstance.table_name}
  WHERE finished_at IS NULL
    AND canceled_at IS NULL
SQL

Instance Method Summary collapse

Instance Method Details

#instanceObject



12
13
14
# File 'app/controllers/kuroko2/api/stats_controller.rb', line 12

def instance
  render json: ActiveRecord::Base.connection.select_one(COUNT_INSTANCES_SQL).to_hash
end

#waiting_executionObject



16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/controllers/kuroko2/api/stats_controller.rb', line 16

def waiting_execution
  waiting_executions_count = Kuroko2::Execution.select('queue, COUNT(1) AS `count`').
    where('started_at IS NULL AND created_at < ?', 3.minutes.ago).group(:queue)

  render json: Kuroko2::Worker.pluck(:queue).uniq.inject({}) { |result, queue|
    result.merge(
      "kuroko2.executions.waiting.#{queue}" => waiting_executions_count.find { |count|
        count.queue == queue
      }.try!(:[], "count") || 0
    )
  }
end