class Query
Query¶ ↑
The Query contains a run function which is called by its children (Nodes, Ways, Relations, etc.) with specific arguments which tells the database connection which collection to query.
The #run function will always return buckets as build by AnalysisWindow#build_buckets
Attributes
analysis_window[R]
buckets[R]
selector[RW]
Public Class Methods
new(args)
click to toggle source
# File models/Query.rb, line 15 def initialize(args) @analysis_window = args[:analysis_window] @selector = {} post_initialize(args) end
Public Instance Methods
post_initialize(args)
click to toggle source
Updates the bounding box, time frame, and constraints for the query to Mongo.
The bounding_box geographic constriaints are currently unimplemented because the database doesn't contain any points outside of the bounding box.
# File models/Query.rb, line 27 def post_initialize(args) if analysis_window.bounding_box.active selector[:geometry] = { '$within' => analysis_window.bounding_box.mongo_format } end #This should be over-written farther down, but it's here for safety if analysis_window.time_frame.active selector[:created_at] = { '$gte' => analysis_window.time_frame.start_date, '$lt' => analysis_window.time_frame.end_date } end #If the query was called with new constraints, then they should get added here unless args[:constraints].nil? selector.update(args[:constraints]) end end
run(args = {})
click to toggle source
The main run function which is called as super from children.
Accesses the database through the Singleton DatabaseConnection reference and queries with the selector that was built.
# File models/Query.rb, line 54 def run(args = {}) # puts "Got to super run function with args #{args}" @buckets = analysis_window.build_buckets( unit = args[:unit], step = args[:step] ) unless args[:constraints].nil? selector.update(args[:constraints]) end buckets.each do |bucket| update_created_at( bucket[:start_date], bucket[:end_date] ) results = DatabaseConnection.database[args[:collection]].find( selector ) results.each do |obj| bucket[:objects] << args[:type].new(obj.from_mongo) end end buckets end