Class: Wackamole::SearchFilter

Inherits:
Object
  • Object
show all
Defined in:
lib/wackamole/models/search_filter.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSearchFilter


Ctor



10
11
12
# File 'lib/wackamole/models/search_filter.rb', line 10

def initialize
  reset!
end

Instance Attribute Details

#browser_typeObject

Returns the value of attribute browser_type.



6
7
8
# File 'lib/wackamole/models/search_filter.rb', line 6

def browser_type
  @browser_type
end

#feature_idObject

Returns the value of attribute feature_id.



6
7
8
# File 'lib/wackamole/models/search_filter.rb', line 6

def feature_id
  @feature_id
end

#hourObject

Returns the value of attribute hour.



6
7
8
# File 'lib/wackamole/models/search_filter.rb', line 6

def hour
  @hour
end

#search_termsObject

Returns the value of attribute search_terms.



6
7
8
# File 'lib/wackamole/models/search_filter.rb', line 6

def search_terms
  @search_terms
end

#time_frameObject

Returns the value of attribute time_frame.



6
7
8
# File 'lib/wackamole/models/search_filter.rb', line 6

def time_frame
  @time_frame
end

#typeObject

Returns the value of attribute type.



6
7
8
# File 'lib/wackamole/models/search_filter.rb', line 6

def type
  @type
end

Class Method Details

.browser_typesObject


Available browsers



27
28
29
# File 'lib/wackamole/models/search_filter.rb', line 27

def self.browser_types
  @browsers ||= [ 'All', 'Unknown', 'Firefox', 'Safari', 'MSIE 8.0', 'MSIE 7.0', 'MSIE 6.0', 'Opera', 'Chrome' ] 
end

.hourliesObject


Available hours



52
53
54
# File 'lib/wackamole/models/search_filter.rb', line 52

def self.hourlies
  @hourlies ||= ['all'] + (1..23).to_a
end

.mole_typesObject


Collection of mole types



58
59
60
# File 'lib/wackamole/models/search_filter.rb', line 58

def self.mole_types
  @types ||= %w[All Feature Perf Fault]
end

.time_conds(local_now, days, current_hour = 0) ⇒ Object


compute query time factors



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
# File 'lib/wackamole/models/search_filter.rb', line 136

def self.time_conds( local_now, days, current_hour=0 )
  conds = {}

  if current_hour == 0 and days == 0
    from_utc = Time.local( local_now.year, local_now.month, local_now.day, 0, 0, 1 ).utc
    to_utc   = Time.local( local_now.year, local_now.month, local_now.day, 23, 59, 59 ).utc

    from_date_id = from_utc.to_date_id.to_s
    to_date_id   = to_utc.to_date_id.to_s
    
    if from_date_id != to_date_id
      from_time_id = from_utc.to_time_id.to_s
      to_time_id   = to_utc.to_time_id.to_s        
      conds['$where'] = "((this.did == '#{from_date_id}' && this.tid >= '#{from_time_id}') || ( this.did == '#{to_date_id}' && this.tid <= '#{to_time_id}') )"
    else        
      conds[:did] = to_date_id
    end
  else
    date         = Chronic.parse( "#{days == 0 ? "now" : "#{days} days ago"}" )
    time         = Time.gm( date.year, date.month, date.day, current_hour, 0, 1 )
puts "!!!!!TIME", time.inspect        
    conds[:did]  = { '$gte' => time.to_date_id.to_s }         
    conds[:tid]  = /^#{"%02d"%time.hour}.+/ unless current_hour == 0
  end
  conds
end

.time_framesObject


Available time frames



33
34
35
# File 'lib/wackamole/models/search_filter.rb', line 33

def self.time_frames
  @time_frames ||= ['today', '2 days', '1 week', '2 weeks', '1 month', '3 months', '6 months', '1 year' ]
end

.time_frames_in_daysObject



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/wackamole/models/search_filter.rb', line 37

def self.time_frames_in_days
  @time_frame_in_days ||= {
    'today'    => 0,
    '2 days'   => 1,
    '1 week'   => 7,
    '2 weeks'  => 14, 
    '1 month'  => 30,
    '3 months' => 90,
    '6 months' => 180,
    '1 year'   => 360 
  }
end

Instance Method Details

#context_for(f) ⇒ Object


Retrieves feature context



79
80
81
82
# File 'lib/wackamole/models/search_filter.rb', line 79

def context_for( f )
  return "#{f['ctl']}##{f['act']}" if f['ctl']
  f['ctx']
end

#featuresObject


Find all features



70
71
72
73
74
75
# File 'lib/wackamole/models/search_filter.rb', line 70

def features
  rows = Feature.features_cltn.find().to_a
  features = rows.map { |f| [context_for(f), f['_id'].to_s] }   
  features.sort! { |a,b| a.first <=> b.first }
  features.insert( 0, ["All", -1] )
end

#from_date_idObject


Fetch filter start date id



86
87
88
89
# File 'lib/wackamole/models/search_filter.rb', line 86

def from_date_id
  start = Chronic.parse( time_frame + ( time_frame == SearchFilter.time_frames.first ? "" : " ago" ) )
  start.to_date_id
end

#from_options(options) ⇒ Object


Populate filter from request params



127
128
129
130
131
132
# File 'lib/wackamole/models/search_filter.rb', line 127

def from_options( options )
  return unless options
  options.each_pair do |k,v|
    self.send( "#{k}=", v )
  end
end

#mole_type(type) ⇒ Object


Set filter type



64
65
66
# File 'lib/wackamole/models/search_filter.rb', line 64

def mole_type( type )
  self.type = to_filter_type( type )
end

#reset!Object


Reset filter to defaults



16
17
18
19
20
21
22
23
# File 'lib/wackamole/models/search_filter.rb', line 16

def reset!
  @feature_id   = -1
  @time_frame   = SearchFilter.time_frames.first
  @browser_type = SearchFilter.browser_types.first
  @type         = SearchFilter.mole_types.first
  @hour         = SearchFilter.hourlies.first
  @search_terms = ""
end

#time_idsObject


fetch time series from time frame



93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/wackamole/models/search_filter.rb', line 93

def time_ids
  now          = Time.now
  start        = Chronic.parse( time_frame + ( time_frame == SearchFilter.time_frames.first ? "" : " ago" ) )
  corpus_end   = DateTime.new( now.year, now.month, now.day )
  corpus_start = DateTime.new( start.year, start.month, start.day )

  calc_date   = corpus_start
  time_series = []  
  while calc_date <= corpus_end do   
    time_series << calc_date.to_date_id
    calc_date += 1
  end     
  time_series
end

#time_seriesObject


fetch time series from time frame



110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/wackamole/models/search_filter.rb', line 110

def time_series    
  now          = Time.now
  start        = Chronic.parse( time_frame + ( time_frame == SearchFilter.time_frames.first ? "" : " ago" ) )
  corpus_end   = DateTime.new( now.year, now.month, now.day )
  corpus_start = DateTime.new( start.year, start.month, start.day )

  calc_date   = corpus_start
  time_series = []  
  while calc_date <= corpus_end do   
    time_series << calc_date.strftime( "%Y-%m-%d" )
    calc_date += 1
  end     
  time_series
end

#to_condsObject


Spews filter conditions



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
# File 'lib/wackamole/models/search_filter.rb', line 165

def to_conds
  current_hour = self.hour
  current_hour = 0 if self.hour == SearchFilter.hourlies.first
  conds = SearchFilter.time_conds( Time.now, SearchFilter.time_frames_in_days[self.time_frame], current_hour )

  # filter mole_types
  if type != 'All'
    conds[:typ] = map_mole_type( type )
  end

  if browser_type != 'All'
    conds["bro.name"] = browser_type
  end

  # filter mole_features
  unless feature_id.to_s == "-1"
    conds[:fid] = BSON::ObjectID.from_string( feature_id )
  end
                            
  if search_terms and !search_terms.empty?
    tokens = search_terms.split( ":" ).collect{ |c| c.strip }
    key    = tokens.shift
    if key
      if key == "user"
        users = Wackamole::User.users_cltn.find( { :una => Regexp.new( tokens.first ) }, :fields => ['_id'] )
        conds[field_map( 'user_id' )] = { '$in' => users.collect{ |u| u['_id'] } }
      elsif tokens.size == 2
        conds["#{field_map(key)}.#{tokens.first}"] = Regexp.new( tokens.last )
      elsif tokens.size == 1
        conds[field_map(key)] = Regexp.new( tokens.first )
      else
        raise "Unable to evaluate search terms"
      end
    end
  end
  conds
end