Class: Auth::System::Merger

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

Instance Method Summary collapse

Instance Method Details

#add_query_result(query_result, query_id, target_query_id, location_applicability_hash, min_addition, max_addition) ⇒ Object

@param query_result : the array of the query results. @param query_id : the query_id of the current query. @param target_query_id : an earlier query that you are targeting. @param location_applicability_hash : the hash which contains which location is applicable to which location eg : =>

indices_of_applicable_location_ids_from_the_result



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
185
186
187
188
189
190
191
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
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
# File 'app/models/auth/system/merger.rb', line 79

def add_query_result(query_result,query_id,target_query_id,location_applicability_hash,min_addition,max_addition)

  query_result = query_result.to_a

  h = query_result_to_hash(query_result)
  range_start = h.keys[0] - min_addition
  range_end = h.keys[1] - max_addition

  query_id = query_id.to_sym
  target_query_id = target_query_id.to_sym
  location_applicability_hash.deep_symbolize_keys!

  ## what is the range of applicable minutes?
  ## first minute - minimum
  ## last minute - maximum

  minutes_to_delete_for_query_id = []

  ## at the end we have only 
  ## okay its time to abort this, and finish the test object, guidelines, b2b, chat, ui frontend api, and video and image synchronization.
  ## can i find a simplified solution for this ?
  ## 
=begin
## STRUCTURE OF COMBINATIONS TO INSERT : 
{
  minute : {
    query_id : {
      location : {
        combinations : []
      }
    }
  }
}
=end


  combinations_to_insert = {}

=begin
{
  running_combinations : 
  {
      location_id : 
      [
        combination_one : closes_at_minute
        combination_two : closes_at_minute
      ],
      location_id_2 :
      [
        combination_three : closes_at_minute
        combination_four : closes_at_minute
      ]
  },
  combinations_to_close : {
    minute : {location_id => [combinations_ids..]}
  }

  ## how to handle 
  ## if a combination opens up, then we have to add it to this.
  ## and if we encounter the minute where the combination closes, then we have to remove it at that time.
  ## how to know we are iterating a combination ?
  ## if that combination is encountered.
  ## in the merger hash, at that minute, then it is open.
  ## what will it have
  ## for eg :
  ## [1,2,3,4], and it will have a point where that combination closes.
  ## so that minute will also be defined.
  ## so how will the merger has look at a particular location at a particular minute ? 
  ## it just has a hash of combinations.
  ## and where it ends.
  ## thats; all.
  ## so let us give it a key, like 
}
=end

    open_combinations_hash = {
      :running_combinations => {}
    }


    ## how does it work
    ## take the result
    ## take each minute
    ## merger hash get minutes applicable
    ## if minute has query id
    ## check if it has any of the locations, otherwise delete it.
    ## if it has, then check if applicable, otherwise move on
    ## rebuild the query result first
    ## get it by minute.
    ## we actually just want the minutes
    ## and the locations.
    ## nothing else.
    ## that can be easily done


    self.merger_hash.each_key do |minute|
      
      ## everytime you do a new minute, clear the open_combinations hsh.
      open_combinations_hash = {:running_combinations => {}}
      if merger_hash[minute].key? target_query_id
        merger_hash[minute][target_query_id].each_key do |location_id|
          if location_applicability_hash.key? location_id
            ## add open combinations.
            if combinations_hash = merger_hash[minute][target_query_id][location_id][:combinations]

              open_combinations_hash[:running_combinations][location_id] = {} unless open_combinations_hash[:running_combinations][location_id]

              combinations_hash.each_key do |combination|
                open_combinations_hash[:running_combinations][location_id][combination] = combinations_hash[combination]
              
              end

            end

            ## close the open combinations that no longer apply.
            ## where do we close the combinations ?
            ## exactly ?
            ## when are combinations no longer applicable ?
            ## 

            ## now comes the part where we actually write forward.
            ## these values, 
            location_applicability_hash[location_id].each do |index_in_result|
              ## here we need only the first and last minute.
              ## here we need the first minute within range.
              ## and also the last minute.
              start_search = minute.to_s.to_i + min_addition
              end_search = minute.to_s.to_i + max_addition
              start_minute = nil
              end_minute = nil
              

              location_id_of_result_location = query_result[index_in_result]["_id"]

              
              query_result[index_in_result]["minutes"].each do |result_minute|

                puts "start search is: #{start_search}"
                puts "end search is  : #{end_search}"
                puts "root minute is :#{minute}"
                puts "start minute is: #{start_minute}"
                puts "end minute is: #{end_minute}"
                puts "Result minute is:"
                puts result_minute["minute"]
                puts "------------------------------"
                ## why does 1 -> 218?

                if ((result_minute["minute"] >= start_search) && (result_minute["minute"] <= end_search))
                  if start_minute.nil?
                    start_minute = result_minute["minute"]
                  else
                    end_minute = result_minute["minute"]
                  end
                else
                  ## TODO : test this.
                  break
                end
              end
              
              puts "open combination hash is:"
              puts JSON.pretty_generate(open_combinations_hash)

              ## if we only have a start minute, then the end minute equals to it.
              end_minute = start_minute unless end_minute

              if start_minute && end_minute
                puts "start minute: #{start_minute} and end_minute: #{end_minute}:"

                open_combinations_hash[:running_combinations][location_id].each_key do |combi|

                  combinations_to_insert = combinations_to_insert.deep_merge({
                    start_minute.to_s.to_sym =>
                    {
                      query_id.to_sym => {
                         location_id_of_result_location.to_s.to_sym => {
                          :combinations => {
                            (combi.to_s + "_" + location_id_of_result_location.to_s + "_" + start_minute.to_s).to_sym => end_minute
                          }
                         }
                      }
                    }
                  })

                  puts "after merging it becomes:"
                  puts JSON.pretty_generate(combinations_to_insert)

                end
              end
            end
          end 
        end
      else
        #puts "target query id is not a key of this minute."
      end
    end
  
  #puts "the combinations to insert are:"
  #puts JSON.pretty_generate(combinations_to_insert)
  self.merger_hash = self.merger_hash.deep_merge(combinations_to_insert)

  

end

#populate_merger_hash_for_the_first_time(query_result, query_id) ⇒ Object

the query result for a given query. the query_id.



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
47
48
49
50
51
52
53
54
55
# File 'app/models/auth/system/merger.rb', line 22

def populate_merger_hash_for_the_first_time(query_result,query_id)

  query_result.each do |location|
      
    location_id = location["_id"].to_s
    
    minutes = location["minutes"]
    
    minutes = minutes.map{|c| c = Auth.configuration.minute_class.constantize.new(c)}
    
    minutes.each do |minute|
      
      min = minute.minute.to_s
      

      #puts "came to minute."
      self.merger_hash.deep_merge!({
        min.to_sym => {
          query_id.to_sym => {
            location_id.to_sym => {
              :combinations => {
                (location_id.to_s + "_" + min).to_sym => min 
              }
            }
          }
        }
      })

      
    end

  end

end

#query_result_to_hash(query_result) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/models/auth/system/merger.rb', line 57

def query_result_to_hash(query_result)
  h = {}
  query_result.each_key do |location|
    location["minutes"].map{|c| c = c["minute"]}.each do |minute|
      build = {minute.to_sym => {}}
      location["applicable_to_locations"].each do |_id|
        build[minute_to_sym][_id] = true
      end
      h.deep_merge!(build)
    end
  end
  h
end