Class: Squab::Events

Inherits:
DB
  • Object
show all
Defined in:
lib/squab/events.rb

Instance Attribute Summary

Attributes inherited from DB

#events, #threadsafe

Instance Method Summary collapse

Methods inherited from DB

#bootstrap

Constructor Details

#initialize(conn_string, opts = {}) ⇒ Events

Returns a new instance of Events.



7
8
9
10
# File 'lib/squab/events.rb', line 7

def initialize(conn_string, opts={})
  @return_json = opts.include?(:json) ? opts[:json] : true
  super(conn_string)
end

Instance Method Details

#add_event(event) ⇒ Object



250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/squab/events.rb', line 250

def add_event(event)
  if event.date.nil?
    now = Time.now.to_i
    event.date = now
  end
  @events.insert(:date => event.date,
                 :value => event.value,
                 :url => event.url,
                 :source => event.source,
                 :uid => event.uid,
                 :deleted => false)
  event.to_json
end

#all(limit = 1000) ⇒ Object



12
13
14
15
16
17
18
19
20
21
# File 'lib/squab/events.rb', line 12

def all(limit=1000)
  all_events = nil
  if limit == 0
    all_events = @events.all.sort_by { |event| event[:id] }
    all_events.reverse!
  else
    all_events = @events.limit(limit.to_i).reverse_order(:id)
  end
  format_rows(all_events)
end

#between(from, to) ⇒ Object



245
246
247
248
# File 'lib/squab/events.rb', line 245

def between(from, to)
  new_events = @events.where{date >= "#{from}"}.where{date <= "#{to}"}.reverse_order(:id)
  format_rows(new_events)
end

#between_ids(start_id, end_id) ⇒ Object



186
187
188
189
# File 'lib/squab/events.rb', line 186

def between_ids(start_id, end_id)
  events = @events.where(:id => start_id..end_id)
  format_rows(events)
end

#by_id(id) ⇒ Object



169
170
171
172
# File 'lib/squab/events.rb', line 169

def by_id(id)
  single_event = @events.where(:id => id)
  format_rows(single_event)
end

#by_source(source, limit = 1000) ⇒ Object



164
165
166
167
# File 'lib/squab/events.rb', line 164

def by_source(source, limit=1000)
  source_events = @events.where(:source => source).reverse_order(:id).limit(limit)
  format_rows(source_events)
end

#by_user(uid, limit = 1000) ⇒ Object



159
160
161
162
# File 'lib/squab/events.rb', line 159

def by_user(uid, limit=1000)
  user_events = @events.where(:uid => uid).reverse_order(:id).limit(limit)
  format_rows(user_events)
end

#delete(event_id) ⇒ Object



178
179
# File 'lib/squab/events.rb', line 178

def delete(event_id)
end

#event_slice(search_params) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
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
# File 'lib/squab/events.rb', line 38

def event_slice(search_params)
  # Grab all events, or just a slice of events if there's
  # a time specified
  # TODO: Build this off the sequel data source so it's not
  #       a giant if/else
  with_no_json do
    if search_params.include?(:from) || search_params.include?(:to)
      from = search_params.delete(:from)
      to = search_params.delete(:to)
      if to.nil?
        newer_than(from)
      elsif from.nil?
        between(0, to)
      else
        between(from, to)
      end
    elsif search_params.include?(:fromId) || search_params.include?(:toId)
      from_id = search_params.delete(:fromId)
      to_id = search_params.delete(:toId)
      if to_id.nil?
        starting_at(from_id)
      elsif from_id.nil?
        between_ids(1, to_id)
      else
        between_ids(from_id, to_id)
      end
    else
      # If all the params nil or empty or 0 then return a limited set
      if search_params.all? {|_, v| v.to_i == 0}
        all(1000)
      else
        # We have some search params, so return everything
        all(0)
      end
    end
  end
end

#format_rows(rows) ⇒ Object



216
217
218
219
220
221
222
# File 'lib/squab/events.rb', line 216

def format_rows(rows)
  if @return_json
    rows_to_json(rows)
  else
    rows
  end
end

#keys_to_sym(old_hash) ⇒ Object



264
265
266
267
268
269
270
# File 'lib/squab/events.rb', line 264

def keys_to_sym(old_hash)
  new_hash = {}
  old_hash.each do |k,v|
    new_hash[k.to_sym] = old_hash[k]
  end
  new_hash
end

#list_distinct(column) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/squab/events.rb', line 191

def list_distinct(column)
  column = column.to_sym
  things = @events.select(column).distinct
  thing_list = things.map{|t| t[column]}
  # don't return nil, that's just silly
  thing_list.delete(nil)
  if @return_json
    "#{thing_list.to_json}\n"
  else
    thing_list
  end
end

#newer_than(timestamp) ⇒ Object



240
241
242
243
# File 'lib/squab/events.rb', line 240

def newer_than(timestamp)
  new_events = @events.where{date >= "#{timestamp.to_s}"}.reverse_order(:id)
  format_rows(new_events)
end

#recentObject



174
175
176
# File 'lib/squab/events.rb', line 174

def recent()
  all(50)
end

#rows_to_json(rows) ⇒ Object



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/squab/events.rb', line 224

def rows_to_json(rows)
  ret_events = []
  unless rows.nil?
    rows.each do |row|
      cur_event = Squab::Event.new(row[:value],
                                   row[:url],
                                   row[:uid],
                                   row[:source],
                                   row[:date],
                                   row[:id])
      ret_events.push(cur_event)
    end
  end
  "#{ret_events.to_json}\n"
end

#search(pattern, fields = ['value']) ⇒ Object

This now brokers between the new and old style search



129
130
131
132
133
134
135
136
137
# File 'lib/squab/events.rb', line 129

def search(pattern, fields=['value'])
  if pattern.kind_of?(Hash)
    format_rows(search_fields(pattern))
  elsif pattern.kind_of?(String)
    format_rows(search_text(pattern, fields))
  else
    format_rows(nil)
  end
end

#search_fields(search_params) ⇒ Object



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
# File 'lib/squab/events.rb', line 76

def search_fields(search_params)
  search_params = keys_to_sym(search_params)
  ret_limit = search_params.delete(:limit)
  all_events = event_slice(search_params)

  # Filter out nils and empty strings up front
  # This allows the catch-all default-all to still work
  valid_search_params = {}
  search_params.each do |k,v|
    next if v.nil?
    next if v.empty?
    valid_search_params[k] = v
  end

  # Short circuit here and give back all events if there's no search
  # parameters
  if valid_search_params.empty?
    if ret_limit
      return all_events.limit(ret_limit)
    else
      return all_events
    end
  end

  ret_events = []

  all_events.each do |event|
    matched = false
    valid_search_params.each do |k, v|
      # Silently ignore bad search fields
      if event[k]
        if event[k].match(/#{v}/i)
          matched = true
        else
          matched = false
          # Stop looking, this is an AND search
          break
        end
      end
    end
    if matched
      ret_events.push(event)
    end
  end

  if ret_limit
    ret_events[0..(ret_limit.to_i - 1)]
  else
    ret_events
  end
end

#search_text(pattern, fields) ⇒ Object

Old style search, one pattern, multiple fields



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/squab/events.rb', line 140

def search_text(pattern, fields)
  all_events = with_no_json do
    all(0)
  end
  ret_events = []

  all_events.each do |event|
    event.each do |k, v|
      if fields.member?(k.to_s) or fields.member?('all')
        if v =~ /#{pattern}/i
          ret_events.push(event)
          break
        end
      end
    end
  end
  ret_events
end

#source_listObject



208
209
210
# File 'lib/squab/events.rb', line 208

def source_list()
  list_distinct(:source)
end

#starting_at(event_id) ⇒ Object



181
182
183
184
# File 'lib/squab/events.rb', line 181

def starting_at(event_id)
  new_events =  @events.where{id >= "#{event_id}"}.reverse_order(:id)
  format_rows(new_events)
end

#url_listObject



204
205
206
# File 'lib/squab/events.rb', line 204

def url_list()
  list_distinct(:url)
end

#user_listObject



212
213
214
# File 'lib/squab/events.rb', line 212

def user_list()
  list_distinct(:uid)
end

#with_no_json(&block) ⇒ Object

Wrap calls with this when you don’t want JSON to return ever. TODO: Redo the entire internal-returning-json-thing



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/squab/events.rb', line 25

def with_no_json(&block)
  # Whatever the situation is on json, store it, and set it to false for
  # the period of this block call
  returner_type = @return_json
  @return_json = false

  ret_val = block.call

  # Put it back to whatever it was
  @return_json = returner_type
  ret_val
end