Class: Bosh::Director::Api::EventManager

Inherits:
Object
  • Object
show all
Defined in:
lib/bosh/director/api/event_manager.rb

Instance Method Summary collapse

Constructor Details

#initialize(record_events) ⇒ EventManager

Returns a new instance of EventManager.



4
5
6
# File 'lib/bosh/director/api/event_manager.rb', line 4

def initialize(record_events)
  @record_events = record_events
end

Instance Method Details

#create_event(options) ⇒ Object



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
# File 'lib/bosh/director/api/event_manager.rb', line 25

def create_event(options)
  unless @record_events
    return Models::Event.new
  end

  parent_id   = options.fetch(:parent_id, nil)
  user        = options[:user]
  action      = options[:action]
  object_type = options[:object_type]
  object_name = options.fetch(:object_name, nil)
  task        = options.fetch(:task, nil)
  error       = options.fetch(:error, nil)
  deployment  = options.fetch(:deployment, nil)
  instance    = options.fetch(:instance, nil)
  context     = options.fetch(:context, {})

  Models::Event.create(
      parent_id:   parent_id,
      timestamp:   Time.now,
      user:        user,
      action:      action,
      object_type: object_type,
      object_name: object_name,
      error:       error ? error.to_s : nil,
      task:        task,
      deployment:  deployment,
      instance:    instance,
      context:     context)
end

#event_to_hash(event) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/bosh/director/api/event_manager.rb', line 8

def event_to_hash(event)
  {
      'id' => event.id.to_s,
      'parent_id' => event.parent_id.to_s,
      'timestamp' => event.timestamp.to_i,
      'user' => event.user,
      'action' => event.action,
      'object_type' => event.object_type,
      'object_name' => event.object_name,
      'error' => event.error,
      'task' => event.task,
      'deployment' => event.deployment,
      'instance' => event.instance,
      'context' => event.context
  }.reject { |k, v| v.nil? || v == '' }
end

#remove_old_events(max_events = 10000) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/bosh/director/api/event_manager.rb', line 55

def remove_old_events (max_events = 10000)
  if Bosh::Director::Models::Event.count > max_events
    last_id = Bosh::Director::Models::Event.
        order { Sequel.desc(:id) }.limit(1, max_events).first.id
    last_parent_id = Bosh::Director::Models::Event.
        order { Sequel.desc(:id) }.limit(max_events).min(:parent_id)
    start_id_to_remove = (last_parent_id.nil? || (last_parent_id > last_id)) ? last_id+1: last_parent_id

    Bosh::Director::Models::Event.filter("id < ?", start_id_to_remove).delete if start_id_to_remove != 0
  end
end