| 
									
										
										
										
											2015-02-12 13:01:05 +03:00
										 |  |  | require "date" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module Connectors | 
					
						
							|  |  |  |   class Report < Base | 
					
						
							|  |  |  |     include Helpers::ShowCommand, | 
					
						
							|  |  |  |       Helpers::ListCommand | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def initialize(db) | 
					
						
							|  |  |  |       self.collection = db.collection('reports') | 
					
						
							|  |  |  |     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 | 
					
						
							|  |  |  |       list(options) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-21 17:13:10 +03:00
										 |  |  |     def set_report_status(jid, status) | 
					
						
							|  |  |  |       # TODO: merge from novartis | 
					
						
							|  |  |  |     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 |