Class: Auth::Workflow::Location
- Inherits:
-
Object
- Object
- Auth::Workflow::Location
- Includes:
- Mongoid::Document
- Defined in:
- app/models/auth/workflow/location.rb
Class Method Summary collapse
- .add_approx_duration_field(aggregation_clause, speed) ⇒ Object
- .add_duration_applicable_filter(aggregation_clause) ⇒ Object
- .add_entity_transport_filter(aggregation_clause, query_array) ⇒ Object
- .add_geo_filter(aggregation_clause, query_array, coordinates, max_distance, ids_or_categories = "ids") ⇒ Object
- .add_location_category_filter(aggregation_clause, query_array) ⇒ Object
- .add_location_id_filter(aggregation_clause, query_array) ⇒ Object
-
.add_location_minute_category_filter(aggregation_clause, query_array, ids_or_categories = "ids") ⇒ Object
match the catgories as usual, without the duration clause.
- .add_max_duration_minus_approx_duration_field(aggregation_clause) ⇒ Object
-
.bench ⇒ Object
its basically superfast.
-
.filter_by_location_ids_or_categories?(query_array) ⇒ Boolean
will check if the location ids or categories are to be returned.
- .find_entities_non_transport(query_array) ⇒ Object
- .find_entities_transport(query_array) ⇒ Object
- .find_entities_within_circle(query_array, coordinates, max_distance) ⇒ Object
- .regroup_from_categories(aggregation_clause) ⇒ Object
- .regroup_from_entities(aggregation_clause) ⇒ Object
- .regroup_from_minutes(aggregation_clause) ⇒ Object
- .travel_to_point_with_entities(query_array, coordinates, max_distance, travel_speed) ⇒ Object
- .unwind_categories(aggregation_clause) ⇒ Object
- .unwind_entities(aggregation_clause) ⇒ Object
- .unwind_minutes(aggregation_clause) ⇒ Object
Instance Method Summary collapse
-
#geom ⇒ Object
it has to be committed like : => “Point”, :coordinates => [lng,lat].
Class Method Details
.add_approx_duration_field(aggregation_clause, speed) ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'app/models/auth/workflow/location.rb', line 153 def self.add_approx_duration_field(aggregation_clause,speed) aggregation_clause << { "$addFields" => { "approx_duration" => { "$divide" => [ "$dist_calculated", speed ] } } } aggregation_clause end |
.add_duration_applicable_filter(aggregation_clause) ⇒ Object
320 321 322 323 324 325 326 327 328 329 |
# File 'app/models/auth/workflow/location.rb', line 320 def self.add_duration_applicable_filter(aggregation_clause) aggregation_clause << { "$match" => { "duration_applicable" => { "$gte" => 0 } } } aggregation_clause end |
.add_entity_transport_filter(aggregation_clause, query_array) ⇒ Object
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 |
# File 'app/models/auth/workflow/location.rb', line 351 def self.add_entity_transport_filter(aggregation_clause,query_array) aggregation_clause << { "$match" => { "$or" => [ ] } } query_array.each do |arr| aggregation_clause.last["$match"]["$or"] << { "$and" => arr["categories"].map {|category| query_clause = { "minutes.categories.category" => category["category"], "minutes.categories.entities.duration" => { "$lte" => arr["duration"] } } if category["transport_capacity"] query_clause["minutes.categories.entities.transport_capacity"] = { "$gte" => category["transport_capacity"] } query_clause["minutes.categories.entities.departs_from_location_id"] = arr["location_id"].to_s query_clause["minutes.categories.entities.arrives_at_location_categories"] = { "$in" => category["arrives_at_location_categories"] } #else #query_clause["minutes.categories.capacity"] = { # "$gte" => category["capacity"] #} end category = query_clause } } end aggregation_clause end |
.add_geo_filter(aggregation_clause, query_array, coordinates, max_distance, ids_or_categories = "ids") ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'app/models/auth/workflow/location.rb', line 114 def self.add_geo_filter(aggregation_clause,query_array,coordinates,max_distance,ids_or_categories="ids") aggregation_clause << { "$geoNear" => { "near" => { "type" => "Point", "coordinates" => [coordinates[:lng],coordinates[:lat]] }, "maxDistance" => max_distance, "spherical" => true, "distanceField" => "dist_calculated", "includeLocs" => "dist_location", "query" => { } } } if ids_or_categories if ids_or_categories == "ids" aggregation_clause.last["$geoNear"]["query"] = { "_id" => { "$in" => query_array.map{|c| c = c["location_id"]} } } else aggregation_clause.last["$geoNear"]["query"] = { "location_categories" => { "$in" => query_array.map{|c| c = c["location_categories"]}.flatten } } end end aggregation_clause end |
.add_location_category_filter(aggregation_clause, query_array) ⇒ Object
184 185 186 187 188 189 190 191 192 193 |
# File 'app/models/auth/workflow/location.rb', line 184 def self.add_location_category_filter(aggregation_clause,query_array) aggregation_clause << { "$match" => { "location_categories" => { "$in" => query_array.map{|c| c = c["location_categories"]}.flatten } } } aggregation_clause end |
.add_location_id_filter(aggregation_clause, query_array) ⇒ Object
173 174 175 176 177 178 179 180 181 182 |
# File 'app/models/auth/workflow/location.rb', line 173 def self.add_location_id_filter(aggregation_clause,query_array) aggregation_clause << { "$match" => { "_id" => { "$in" => query_array.map{|c| c = c["location_id"]} } } } aggregation_clause end |
.add_location_minute_category_filter(aggregation_clause, query_array, ids_or_categories = "ids") ⇒ Object
match the catgories as usual, without the duration clause
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 |
# File 'app/models/auth/workflow/location.rb', line 205 def self.add_location_minute_category_filter(aggregation_clause,query_array,ids_or_categories="ids") aggregation_clause << { "$match" => { "$or" => [ ] } } ## we have to add the duration here. ## query_array.each do |arr| aggregation_clause.last["$match"]["$or"] << { "$and" => [ { "minutes.minute" => { "$gte" => arr["start_time_range_beginning"], "$lte" => arr["start_time_range_end"] } }, { "minutes.categories" => { "$all" => arr["categories"].map{|c| qc = {} qc["category"] = c["category"] if arr["capacity"] qc["capacity"] = { "$gte" => arr["capacity"] } end if arr["duration"] qc["max_free_duration"] = { "$gte" => arr["duration"] } end c = { "$elemMatch" => qc } } } } ] } if ids_or_categories == "ids" aggregation_clause.last["$match"]["$or"].last["$and"] << { "_id" => arr["location_id"] } end if ids_or_categories == "categories" aggregation_clause.last["$match"]["$or"].last["$and"] << { "location_categories" => { "$in" => arr["location_categories"] } } end if arr["consumables"] aggregation_clause.last["$match"]["$or"].last["$and"] << { "minutes.consumables" => { "$all" => [] } } arr["consumables"].each do |consumable| aggregation_clause.last["$match"]["$or"].last["$and"]["minutes.consumables"]["$all"] << { "$elemMatch" => { "product_id" => consumable.product_id, "quantity" => { "$gte" => consumable.quantity } } } end end end aggregation_clause end |
.add_max_duration_minus_approx_duration_field(aggregation_clause) ⇒ Object
305 306 307 308 309 310 311 312 313 314 315 316 317 318 |
# File 'app/models/auth/workflow/location.rb', line 305 def self.add_max_duration_minus_approx_duration_field(aggregation_clause) aggregation_clause << { "$addFields" => { "duration_applicable" => { "$subtract" => [ "$minutes.categories.max_free_duration", "$approx_duration" ] } } } aggregation_clause end |
.bench ⇒ Object
its basically superfast. so what we need to do is
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'app/models/auth/workflow/location.rb', line 32 def self.bench a = [] 10000000.times do |n| a << n end s = [] 10000.times do |k| s << k end Benchmark.bm do |x| x.report "Array#bsearch" do s.size.times do |l| a.bsearch{|y| y >= l } end end end end |
.filter_by_location_ids_or_categories?(query_array) ⇒ Boolean
will check if the location ids or categories are to be returned. if neither ids nor categories are specified, then will return nothing.
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'app/models/auth/workflow/location.rb', line 96 def self.filter_by_location_ids_or_categories?(query_array) ids = query_array.map{|c| c = c["location_id"]} categories = query_array.map{|c| c = c["location_categories"]} ids.compact! categories.compact! raise "both location ids and categories provided" if (ids.size > 0 && categories.size > 0) return "ids" if ids.size > 0 return "categories" if categories.size > 0 return nil end |
.find_entities_non_transport(query_array) ⇒ Object
499 500 501 502 503 504 505 506 507 508 509 |
# File 'app/models/auth/workflow/location.rb', line 499 def self.find_entities_non_transport(query_array) aggregation_clause = [] ids_or_categories = filter_by_location_ids_or_categories?(query_array) aggregation_clause = add_location_id_filter(aggregation_clause,query_array) if (ids_or_categories == "ids") aggregation_clause = add_location_category_filter(aggregation_clause,query_array) if (ids_or_categories == "categories") aggregation_clause = unwind_minutes(aggregation_clause) aggregation_clause = add_location_minute_category_filter(aggregation_clause,query_array,ids_or_categories) aggregation_clause = regroup_from_minutes(aggregation_clause) puts JSON.pretty_generate(aggregation_clause) Auth.configuration.location_class.constantize.collection.aggregate(aggregation_clause) end |
.find_entities_transport(query_array) ⇒ Object
511 512 513 514 515 516 517 518 519 520 521 522 523 524 |
# File 'app/models/auth/workflow/location.rb', line 511 def self.find_entities_transport(query_array) puts "came to transport filter." aggregation_clause = [] ids_or_categories = filter_by_location_ids_or_categories?(query_array) aggregation_clause = add_location_id_filter(aggregation_clause,query_array) aggregation_clause = unwind_minutes(aggregation_clause) aggregation_clause = add_location_minute_category_filter(aggregation_clause,query_array) aggregation_clause = unwind_categories(aggregation_clause) aggregation_clause = unwind_entities(aggregation_clause) aggregation_clause = add_entity_transport_filter(aggregation_clause,query_array) aggregation_clause = regroup_from_entities(aggregation_clause) puts JSON.pretty_generate(aggregation_clause) Auth.configuration.location_class.constantize.collection.aggregate(aggregation_clause) end |
.find_entities_within_circle(query_array, coordinates, max_distance) ⇒ Object
526 527 528 529 530 531 532 533 534 535 536 |
# File 'app/models/auth/workflow/location.rb', line 526 def self.find_entities_within_circle(query_array,coordinates,max_distance) aggregation_clause = [] ids_or_categories = filter_by_location_ids_or_categories?(query_array) aggregation_clause = add_geo_filter(aggregation_clause,query_array,coordinates,max_distance,ids_or_categories) aggregation_clause = unwind_minutes(aggregation_clause) aggregation_clause = add_location_minute_category_filter(aggregation_clause,query_array,ids_or_categories) aggregation_clause = regroup_from_minutes(aggregation_clause) puts JSON.pretty_generate(aggregation_clause) Auth.configuration.location_class.constantize.collection.aggregate(aggregation_clause) end |
.regroup_from_categories(aggregation_clause) ⇒ Object
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 |
# File 'app/models/auth/workflow/location.rb', line 406 def self.regroup_from_categories(aggregation_clause) aggregation_clause << { "$group" => { "_id" => "$minutes._id", "categories" => { "$push" => "$minutes.categories" }, "location" => { "$push" => "$_id" } } } ## the incoming documents are grouped by minute ## now all we have to do is group them by the first element in the location. aggregation_clause << { "$group" => { "_id" => { "$arrayElemAt" => ["$location",0] }, "minutes" => { "$push" => "$$ROOT" } } } aggregation_clause end |
.regroup_from_entities(aggregation_clause) ⇒ Object
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 |
# File 'app/models/auth/workflow/location.rb', line 437 def self.regroup_from_entities(aggregation_clause) ## this is not going to work. ## first group by the categories ## okay then what to group by ? ## aggregation_clause << { "$group" => { "_id" => "$minutes.categories._id", "category" => { "$first" => "$minutes.categories.category" }, "entities" => { "$push" => "$minutes.categories.entities" }, "minute" => { "$first" => "$minutes._id" }, "minute_actual" => { "$first" => "$minutes.minute" }, "location" => { "$first" => "$_id" } } } aggregation_clause << { "$group" => { "_id" => "$minute", "location" => { "$first" => "$location" }, "minute" => { "$first" => "$minute_actual" }, "categories" => { "$push" => "$$ROOT" } } } aggregation_clause << { "$group" => { "_id" => "$location", "minutes" => { "$push" => "$$ROOT" } } } aggregation_clause end |
.regroup_from_minutes(aggregation_clause) ⇒ Object
338 339 340 341 342 343 344 345 346 347 348 349 |
# File 'app/models/auth/workflow/location.rb', line 338 def self.regroup_from_minutes(aggregation_clause) aggregation_clause << { "$group" => { "_id" => "$_id", "minutes" => { "$push" => "$minutes" } } } aggregation_clause end |
.travel_to_point_with_entities(query_array, coordinates, max_distance, travel_speed) ⇒ Object
538 539 540 541 542 543 544 545 546 547 548 549 550 551 |
# File 'app/models/auth/workflow/location.rb', line 538 def self.travel_to_point_with_entities(query_array,coordinates,max_distance,travel_speed) aggregation_clause = [] ids_or_categories = filter_by_location_ids_or_categories?(query_array) aggregation_clause = add_geo_filter(aggregation_clause,query_array,coordinates,max_distance,ids_or_categories) aggregation_clause = add_approx_duration_field(aggregation_clause,travel_speed) aggregation_clause = unwind_minutes(aggregation_clause) aggregation_clause = add_location_minute_category_filter(aggregation_clause,query_array,ids_or_categories) aggregation_clause = unwind_categories(aggregation_clause) aggregation_clause = add_max_duration_minus_approx_duration_field(aggregation_clause) aggregation_clause = add_duration_applicable_filter(aggregation_clause) aggregation_clause = regroup_from_categories(aggregation_clause) puts JSON.pretty_generate(aggregation_clause) Auth.configuration.location_class.constantize.collection.aggregate(aggregation_clause) end |
.unwind_categories(aggregation_clause) ⇒ Object
298 299 300 301 302 303 |
# File 'app/models/auth/workflow/location.rb', line 298 def self.unwind_categories(aggregation_clause) aggregation_clause << { "$unwind" => "$minutes.categories" } aggregation_clause end |
.unwind_entities(aggregation_clause) ⇒ Object
331 332 333 334 335 336 |
# File 'app/models/auth/workflow/location.rb', line 331 def self.unwind_entities(aggregation_clause) aggregation_clause << { "$unwind" => "$minutes.categories.entities" } aggregation_clause end |
Instance Method Details
#geom ⇒ Object
it has to be committed like : => “Point”, :coordinates => [lng,lat]
22 |
# File 'app/models/auth/workflow/location.rb', line 22 field :geom, type: Array |