62 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			Ruby
		
	
	
	
	
	
			
		
		
	
	
			62 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			Ruby
		
	
	
	
	
	
| require_relative "request_handler"
 | |
| 
 | |
| module Devops
 | |
|   module API2_0
 | |
|     module Handler
 | |
|       class Report < RequestHandler
 | |
| 
 | |
|         def options
 | |
|           params = @request.params
 | |
|           options = {}
 | |
|           ["project", "deploy_env", "type", "created_by", "date_from", "date_to", "sort", "status", "chef_node_name", "max_number"].each do |k|
 | |
|             options[k] = params[k] unless params[k].nil?
 | |
|           end
 | |
|           #attributes_keys = params.keys.select{|k| k =~ /attributes\.*/}
 | |
|           #attributes_keys.each do |ak|
 | |
|           #  options[ak] = params[ak]
 | |
|           #end
 | |
|           options
 | |
|         end
 | |
| 
 | |
|         def all
 | |
|           Devops::Db.connector.reports(options)
 | |
|         end
 | |
| 
 | |
|         def all_latest
 | |
|           Devops::Db.connector.latest_reports(options())
 | |
|         end
 | |
| 
 | |
|         def attributes name
 | |
|           Devops::Db.connector.reports_attributes_values(name)
 | |
|         end
 | |
| 
 | |
|         def report id
 | |
|           r = Devops::Db.connector.report(id)
 | |
|           file = r.file
 | |
|           raise RecordNotFound.new("Report '#{id}' does not exist") unless File.exists? file
 | |
|           return Rack::Utils.escape_html(File.read(file).encode('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '')), completed?(id)
 | |
|         rescue RecordNotFound => e
 | |
|           if status(id) == Worker::STATUS::IN_QUEUE
 | |
|             return "Task '#{id}' has been queued", false
 | |
|           else
 | |
|             raise e
 | |
|           end
 | |
|         end
 | |
| 
 | |
|         def status id
 | |
|           Sidekiq.redis do |connection|
 | |
|             connection.hget("devops", id)
 | |
|           end
 | |
|         end
 | |
| 
 | |
|         def completed? id
 | |
|           r = self.status(id)
 | |
|           r == "completed" or r == "failed"
 | |
|         end
 | |
| 
 | |
|       end
 | |
|     end
 | |
|   end
 | |
| end
 | |
| 
 | 
