2015-02-12 13:01:05 +03:00
|
|
|
require "date"
|
|
|
|
|
|
|
|
|
|
module Connectors
|
|
|
|
|
class Report < Base
|
|
|
|
|
include Helpers::ShowCommand,
|
|
|
|
|
Helpers::ListCommand
|
|
|
|
|
|
|
|
|
|
def initialize(db)
|
2015-09-21 15:54:33 +03:00
|
|
|
super(db)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def collection_name
|
|
|
|
|
'reports'
|
2015-02-12 13:01:05 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def save_report r
|
|
|
|
|
r.created_at = Time.new
|
2015-08-05 14:05:14 +03:00
|
|
|
collection.insert(r.to_mongo_hash) unless report_exists?(r.id)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def report_exists? id
|
|
|
|
|
!collection.find({"_id" => id}, fields: ["_id" => true]).to_a.empty?
|
2015-02-12 13:01:05 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def reports options={}
|
|
|
|
|
date = {}
|
|
|
|
|
if options.has_key?("date_from") or options.has_key?("date_to")
|
|
|
|
|
if options.has_key?("date_from")
|
|
|
|
|
begin
|
|
|
|
|
d = Date.parse(options["date_from"])
|
|
|
|
|
date["$gte"] = d.to_time
|
|
|
|
|
rescue ArgumentError
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
if options.has_key?("date_to")
|
|
|
|
|
begin
|
|
|
|
|
d = Date.parse(options["date_to"])
|
|
|
|
|
date["$lt"] = d.to_time
|
|
|
|
|
rescue ArgumentError
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
options.delete("date_from")
|
|
|
|
|
options.delete("date_to")
|
|
|
|
|
options["created_at"] = date unless date.empty?
|
|
|
|
|
end
|
2015-09-15 15:41:30 +03:00
|
|
|
sort = {created_at: -1}
|
2015-09-15 15:08:14 +03:00
|
|
|
if options["sort"]
|
|
|
|
|
val = options.delete("sort")
|
2015-09-15 15:41:30 +03:00
|
|
|
sort = {created_at: (val == "desc" ? -1 : 1)}
|
2015-09-15 15:08:14 +03:00
|
|
|
end
|
2015-09-15 15:41:30 +03:00
|
|
|
collection.find(options).sort(sort).to_a.map {|bson| model_from_bson(bson)}
|
2015-02-12 13:01:05 +03:00
|
|
|
end
|
|
|
|
|
|
2015-07-21 17:13:10 +03:00
|
|
|
def set_report_status(jid, status)
|
2015-08-10 15:10:47 +03:00
|
|
|
collection.update({"_id" => jid}, {"$set" => {"status" => status, "updated_at" => Time.new}})
|
2015-08-10 13:47:14 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def set_report_server_data id, chef_node_name, host
|
2015-08-10 15:10:47 +03:00
|
|
|
collection.update({"_id" => id}, {"$set" => {"chef_node_name" => chef_node_name, "host" => host}})
|
2015-07-21 17:13:10 +03:00
|
|
|
end
|
|
|
|
|
|
2015-09-15 14:27:06 +03:00
|
|
|
def create_indexes
|
2015-09-15 14:31:05 +03:00
|
|
|
info = collection.index_information
|
2015-09-15 14:27:06 +03:00
|
|
|
unless info["created_at_1"]
|
2015-09-15 14:31:05 +03:00
|
|
|
collection.create_index({created_at: Mongo::ASCENDING})
|
2015-09-15 14:33:53 +03:00
|
|
|
DevopsLogger.logger.info("Index 'created_at_1' for reports collection has been created")
|
2015-09-15 14:27:06 +03:00
|
|
|
end
|
|
|
|
|
unless info["project_1_deploy_env_1_created_at_1"]
|
2015-09-15 14:31:05 +03:00
|
|
|
collection.create_index({project: Mongo::ASCENDING, deploy_env: Mongo::ASCENDING, created_at: Mongo::ASCENDING})
|
2015-09-15 14:33:53 +03:00
|
|
|
DevopsLogger.logger.info("Index 'project_1_deploy_env_1_created_at_1' for reports collection has been created")
|
2015-09-15 14:27:06 +03:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2015-02-12 13:01:05 +03:00
|
|
|
private
|
|
|
|
|
|
|
|
|
|
def model_from_bson(bson)
|
2015-03-06 12:20:30 +03:00
|
|
|
Devops::Model::Report.new(bson)
|
2015-02-12 13:01:05 +03:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|