Class: FeldtRuby::MongoDBLogger

Inherits:
Logger show all
Defined in:
lib/feldtruby/mongodb_logger.rb

Overview

This is an EventLogger that logs to a MongoDB database. It caches the last two events per event type for quicker access to them.

Constant Summary collapse

OurParams =
{
  :host => "localhost",
  :port => 27017,
}
DefaultParams =
FeldtRuby::Logger::DefaultParams.clone.update(OurParams)

Constants inherited from Logger

Logger::UnixEpoch

Instance Attribute Summary collapse

Attributes inherited from Logger

#start_time

Instance Method Summary collapse

Methods inherited from Logger

#add_io, #add_output_file, #elapsed_time, #io_puts, #log, #log_counter, #log_data, #log_value, #print_frequency=, #print_message_if_needed, #verbose=

Constructor Details

#initialize(io = STDOUT, params = DefaultParams) ⇒ MongoDBLogger

I think this is needed since we have redefined DefaultParams but should investigate…



77
78
79
# File 'lib/feldtruby/mongodb_logger.rb', line 77

def initialize(io = STDOUT, params = DefaultParams)
  super
end

Instance Attribute Details

#all_logsObject (readonly)

Reader methods for the main db and its all_logs collection. The latter is the main “directory” for listing all available logs.



119
120
121
# File 'lib/feldtruby/mongodb_logger.rb', line 119

def all_logs
  @all_logs
end

#dbObject (readonly)

Reader methods for the db and mongo client and db name.



86
87
88
# File 'lib/feldtruby/mongodb_logger.rb', line 86

def db
  @db
end

#db_nameObject (readonly)

Reader methods for the db and mongo client and db name.



86
87
88
# File 'lib/feldtruby/mongodb_logger.rb', line 86

def db_name
  @db_name
end

#main_dbObject (readonly)

Reader methods for the main db and its all_logs collection. The latter is the main “directory” for listing all available logs.



119
120
121
# File 'lib/feldtruby/mongodb_logger.rb', line 119

def main_db
  @main_db
end

#mongo_clientObject (readonly)

Reader methods for the db and mongo client and db name.



86
87
88
# File 'lib/feldtruby/mongodb_logger.rb', line 86

def mongo_client
  @mongo_client
end

Instance Method Details

#collection_for_type(t) ⇒ Object



166
167
168
169
# File 'lib/feldtruby/mongodb_logger.rb', line 166

def collection_for_type(t)
  ts = t || "___default___"
  @collections[ts] ||= @db[ts.to_s]
end

#current_value(eventType, metric = "v") ⇒ Object

Return the current (latest) value for a given eventType and metric. Return nil if no value has been set.



191
192
193
# File 'lib/feldtruby/mongodb_logger.rb', line 191

def current_value eventType, metric = "v"
  @cache_last[eventType][metric]
end

#events(eventType = nil) ⇒ Object

Return all the events for a given eventType.



196
197
198
199
# File 'lib/feldtruby/mongodb_logger.rb', line 196

def events(eventType = nil)
  c = collection_for_type eventType
  c.find.to_a
end

#events_between(start, stop, eventType = nil, includePreEvent = false) ⇒ Object

Return all events, for a given eventType, between the start and stop times. If includePreEvent is true we include the event that comes directly before the start time.



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
# File 'lib/feldtruby/mongodb_logger.rb', line 204

def events_between start, stop, eventType = nil, includePreEvent = false

  all_events = events(eventType)

  es = all_events.select do |e|

    t = e.time

    t >= start && t <= stop

  end

  if includePreEvent

    index_to_first_selected_event = all_events.index(es.first)

    if index_to_first_selected_event && index_to_first_selected_event > 0
      # There is a pre-event so add it
      es.unshift all_events[index_to_first_selected_event - 1]
    end

  end

  es

end

#last_event(eventType) ⇒ Object



171
172
173
# File 'lib/feldtruby/mongodb_logger.rb', line 171

def last_event eventType
  @cache_last[eventType]
end

#last_value(eventType) ⇒ Object



179
180
181
# File 'lib/feldtruby/mongodb_logger.rb', line 179

def last_value eventType
  current_value eventType
end

#log_event(eventType, event, message = nil) ⇒ Object

Log the event and print the message, if any.



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/feldtruby/mongodb_logger.rb', line 146

def log_event eventType, event, message = nil

  @counts[eventType] += 1

  if event

    event["t"] = Time.now.utc

    save_event(event, eventType)

    if message
      print_message_if_needed message, eventType, (eventType == "___default___")
    end

  end

  event

end

#num_events(eventType) ⇒ Object

Number of events of eventType we have seen so far.



141
142
143
# File 'lib/feldtruby/mongodb_logger.rb', line 141

def num_events eventType
  @counts[eventType]
end

#prev_event(eventType) ⇒ Object



175
176
177
# File 'lib/feldtruby/mongodb_logger.rb', line 175

def prev_event eventType
  @cache_2ndlast[eventType]
end

#previous_value(eventType, metric = "v") ⇒ Object

Return the current (latest) value for a given eventType and metric. Return nil if no value has been set.



185
186
187
# File 'lib/feldtruby/mongodb_logger.rb', line 185

def previous_value eventType, metric = "v"
  @cache_2ndlast[eventType][metric]
end

#save_event(event, type) ⇒ Object

Events are saved in the mongodb db, using one collection per event type. The type itself is not saved in the event since it is implicit in the collection in which the event is saved.



124
125
126
127
128
129
130
# File 'lib/feldtruby/mongodb_logger.rb', line 124

def save_event event, type

  collection_for_type(type).insert event

  update_cache event, type

end

#setup_data_storeObject

Set up the internal data store.



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
# File 'lib/feldtruby/mongodb_logger.rb', line 89

def setup_data_store

  super

  # To handle the main "directory" of logger dbs in the mongodb.
  @all_dbs = FeldtRuby::AllMongoDBLoggers.new @params[:host], @params[:port]

  @mongo_client = Mongo::MongoClient.new @params[:host], @params[:port]

  @db_name = unique_mongodb_name()

  # Always creates a new db based on unique timestamp and object_id. The
  # latter is used to ensure each db has a unique name.
  @db = @mongo_client.db(@db_name)

  @all_dbs.add_logger self

  # Will map each event type to a collection in the db were we save
  # events for that type.
  @collections = Hash.new

  # Caches for the last and second_last events per type so we need
  # not lookup in db for most common requests.
  @cache_last = Hash.new
  @cache_2ndlast = Hash.new

end

#unique_mongodb_nameObject



81
82
83
# File 'lib/feldtruby/mongodb_logger.rb', line 81

def unique_mongodb_name
  "MongoDBLogger_" + @start_time.utc.strftime("%Y%m%d_%H%M%S") + "_#{object_id}"
end

#update_cache(event, type) ⇒ Object



132
133
134
135
136
137
138
# File 'lib/feldtruby/mongodb_logger.rb', line 132

def update_cache event, type

  @cache_2ndlast[type] = @cache_last[type]

  @cache_last[type] = event

end

#values_for(eventType) ⇒ Object

Shortcut method to get the value saved for a certain eventType.



238
239
240
# File 'lib/feldtruby/mongodb_logger.rb', line 238

def values_for eventType
  values_for_event_and_metric eventType
end

#values_for_event_and_metric(eventType, metric = "v") ⇒ Object

Get an array of values for the metric named metric in events of type eventType.



233
234
235
# File 'lib/feldtruby/mongodb_logger.rb', line 233

def values_for_event_and_metric eventType, metric = "v"
  events(eventType).map {|e| e.data[metric]}
end