Class: Auth::Work::Minute

Inherits:
Object
  • Object
show all
Includes:
Mongoid::Document
Defined in:
app/models/auth/work/minute.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.get_affected_minutes(cycle_start_time, cycle_end_time, cycle_workers_assigned, cycle_entities_assigned) ⇒ Object

returns all minutes which have affected cycles , only containing the affected cycles. does not consider the cycle chains.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'app/models/auth/work/minute.rb', line 50

def self.get_affected_minutes(cycle_start_time,cycle_end_time,cycle_workers_assigned,cycle_entities_assigned)
	response = Auth::Work::Minute.collection.aggregate([
		{
			"$match" => {
				"cycles" => {
					"$elemMatch" => {
						"$and" => [
							{
								"$or" => [
									{
										"start_time" => {
											"$gte" => cycle_start_time,
											"$lte" => cycle_end_time 
										}
									},
									{
										"end_time" => {
											"$gte" => cycle_start_time,
											"$lte" => cycle_end_time
										}
									},
									{
										"$and" => [
											{
												"start_time" => {
													"$lte" => cycle_start_time
												}
											},
											{
												"end_time" => {
													"$gte" => cycle_end_time
												}
											}
										]
									}
								]
							},
							{
								"$or" => [
									{
										"workers_available" => {
											"$in" => cycle_workers_assigned
										}
									},
									{
										"entities_available" => {
											"$in" => cycle_entities_assigned
										}
									}
								]
							}
						]
					}
				}
			}
		},
		{
			"$unwind" =>
			{
				"path" => "$cycles",
				"includeArrayIndex" => "cycle_index"
			}
		},
		{
			"$addFields" => {
				"cycles.cycle_index" => "$cycle_index"
			}
		},
		{
			"$match" => {
				"$and" => [
					{
						"$or" => [
							{
								"cycles.start_time" => {
									"$gte" => cycle_start_time,
									"$lte" => cycle_end_time 
								}
							},
							{
								"cycles.end_time" => {
									"$gte" => cycle_start_time,
									"$lte" => cycle_end_time
								}
							},
							{
								"$and" => [
									{
										"cycles.start_time" => {
											"$lte" => cycle_start_time
										}
									},
									{
										"cycles.end_time" => {
											"$gte" => cycle_end_time
										}
									}
								]
							}
						]
					},
					{
						"$or" => [
							{
								"cycles.workers_available" => {
									"$in" => cycle_workers_assigned
								}
							},
							{
								"cycles.entities_available" => {
									"$in" => cycle_entities_assigned
								}
							}
						]
					}
				]
			}
		},
		{
			"$group" => {
				"_id" => "$_id",
				"cycles" => {
					"$push" => "$cycles"
				}
			}
		}
	])

	array_of_minute_objects = []
	response.each do |res|
		#puts JSON.pretty_generate(res)
		array_of_minute_objects << Auth::Work::Minute.new(res)
	end
	array_of_minute_objects
end

.update_all_affected_cycles(minutes, cycle_workers_assigned, cycle_entities_assigned) ⇒ Object



186
187
188
189
# File 'app/models/auth/work/minute.rb', line 186

def self.update_all_affected_cycles(minutes,cycle_workers_assigned,cycle_entities_assigned)
	update_cycle_chains(minutes)
	update_cycles(minutes,cycle_workers_assigned,cycle_entities_assigned)
end

.update_cycle_chains(minutes) ⇒ Object

first knock of the cycle chains. then the actual cycles.



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'app/models/auth/work/minute.rb', line 226

def self.update_cycle_chains(minutes)
	cycles_to_pull = []
	minutes.each do |minute|
		minute.cycles.each do |cycle|
			cycles_to_pull << cycle.cycle_chain
		end
	end

	cycles_to_pull.flatten.map{|c| c = BSON::ObjectId(c)}.each_slice(100) do |ids_to_pull|
		pull_hash = {}
		pull_hash["cycles"] = {
			"_id" => {
				"$in" => ids_to_pull
			}
		}
		response = Auth::Work::Minute.collection.update_many({},{"$pull" => pull_hash})
	end

	cycles_to_pull.flatten	
end

.update_cycles(minutes, cycle_workers_assigned, cycle_entities_assigned) ⇒ Object

@param minutes :



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'app/models/auth/work/minute.rb', line 192

def self.update_cycles(minutes,cycle_workers_assigned,cycle_entities_assigned)

	minutes = minutes.map {|minute|
		
		pull_hash = {}
		
		minute.cycles.each do |cycle|
			pull_hash["cycles.#{cycle.cycle_index}.workers_available"] = {
				"$in" => cycle_workers_assigned
			}				 
		end

		minute = Auth::Work::Minute
			.where({
				"_id" => BSON::ObjectId(minute.id.to_s)
			})
			.find_one_and_update(
				{
					"$pull" => pull_hash
				},
				{
					:return_document => :after
				}
			)	
		
		minute
	}

	minutes

end

Instance Method Details

#entity_typesObject

this includes workers and entities, and actually anything that could be needed for the cycle.



12
# File 'app/models/auth/work/minute.rb', line 12

field :entity_types, type: Hash, default: {}

#find_applicable_minute(cycle_requirements_array) ⇒ Object

match where [cycle is a and cycle is primary] OR [cycle is b and cycle is primary, cycle is c and cycle is primary] present. then unwind the cycles so now we know that all these cycles even if they don’t be primary, still belong to a minute with a primary now match only the useful cycles group by minutes add a field that combines the size of the avialable cycles + the distance from now sort by that. @param cycle_requirements_array : , worker_requirements :{“type” : number, entity_requirements: : number} @param transport_information : : , coordinates : lets get the edit out of the way first.



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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'app/models/auth/work/minute.rb', line 263

def find_applicable_minute(cycle_requirements_array)
	## go to elasticsearch for this ?
	## ?
	aggregations = [
		{
			"cycles" => {
				"$elemMatch" => {
					"$or" => 
					[

					]
				}
			}	
		},
		{
			"$unwind" => "$cycles"
		},
		{
			## keep all cycles, dont enforce the belongs_to_minute at this level.
			## just cycle_type and workers and entities
			## since we want to include the 30 min rolling slots as well.
			## we are only keeping the cycles which have the required type.
			## and we can match again, 
			"$match" => {
				"cycles.cycle_type" => {
					"$in" => "all_the_cycles"
				}
			} 
		}
	]



	cycle_requirements_array.each do |req|
		aggregations[0]["cycles"]["$elemMatch"]["$or"] << {
			"cycle_type" => req["cycle_type"], 
			"workers_available.#{req['workers_count'] - 1}" => {
				"$exists" => true
			},
			"entities_available.#{req['entities_count'] - 1}" => {
				"$exists" => true
			},
			"belongs_to_minute" => true
		}
	end

	## we are going to get minutes that have at the minimum one of the cycles as belonging to the given minute.

	## now we are going to unwind the cycles, 
	## keep only those which satisfy these conditions.

	## we take a look at other cycles which will be affected only if that cycle, is not already booked, otherwise it doesnt make any difference at all.

end

#update_entity_typesObject

next step is to pull correctly. so this done.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/models/auth/work/minute.rb', line 16

def update_entity_types
	#puts "came to update entity types"
	applicable_schedules = Auth::Work::Schedule.collection.aggregate([
		{
			"$match" => {
				"$and" => [
					{
						"start_time" => {
							"$lte" => self.time
						}
					},
					{
						"end_time" => {
							"$gte" => self.time
						}
					}
				]
			}
		}
	])

	applicable_schedules.each do |schedule|
		sched = Auth::Work::Schedule.new(schedule)
		object = sched.for_object_class.constantize.find(sched.for_object_id)
		object.cycle_types.keys.each do |k|
			self.entity_types[k]+=1 if self.entity_types[k]
			self.entity_types[k]= 1 unless self.entity_types[k]
		end
	end

end