reports filters

This commit is contained in:
amartynov 2014-12-02 16:13:48 +03:00
parent ad92c1fd67
commit 0887a8c8fc
2 changed files with 28 additions and 3 deletions

View File

@ -1,4 +1,5 @@
require "mongo"
require "date"
require "db/exceptions/record_not_found"
require "db/exceptions/invalid_record"
@ -367,8 +368,28 @@ class MongoConnector
@reports.insert(r.to_mongo_hash)
end
def reports
@reports.find().to_a.map{|e| Report.new(e)}
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
@reports.find(options).to_a.map{|e| Report.new(e)}
end
def report id

View File

@ -10,7 +10,11 @@ class ReportRoutes < Sinatra::Base
enable :inline_templates
get "/all" do
json DevopsService.mongo.reports.map{|r| r.to_hash}
options = {}
["project", "deploy_env", "type", "created_by", "date_from", "date_to"].each do |k|
options[k] = params[k] unless params[k].nil?
end
json DevopsService.mongo.reports(options).map{|r| r.to_hash}
=begin
res = {}
uri = URI.parse(request.url)