Class: Timeline

Inherits:
Object
  • Object
show all
Defined in:
service/showback.rb

Overview

Class for compiling all history records from all records sources into one readable timeline

Defined Under Namespace

Classes: FinalRecord, InitRecord

Constant Summary collapse

SOURCES =

Should be generated automaticaly in the future

[ # Should be generated automaticaly in the future
  Records, SnapshotRecords, TrafficRecords, DiskRecords
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(vm, stime, etime, group_by_day = false) ⇒ Timeline

Initializes timeline - collects record sources(yet staticly) and stores entry vars



13
14
15
16
17
# File 'service/showback.rb', line 13

def initialize vm, stime, etime, group_by_day = false
  @vm, @stime, @etime, @group_by_day = vm, stime, etime, group_by_day
  @sources = SOURCES
  @compiled = false
end

Instance Attribute Details

#compiledObject (readonly)

Returns the value of attribute compiled.



6
7
8
# File 'service/showback.rb', line 6

def compiled
  @compiled
end

#etimeObject (readonly)

Returns the value of attribute etime.



6
7
8
# File 'service/showback.rb', line 6

def etime
  @etime
end

#group_by_dayObject (readonly)

Returns the value of attribute group_by_day.



6
7
8
# File 'service/showback.rb', line 6

def group_by_day
  @group_by_day
end

#sourcesObject (readonly)

Returns the value of attribute sources.



6
7
8
# File 'service/showback.rb', line 6

def sources
  @sources
end

#stateObject (readonly)

Returns the value of attribute state.



6
7
8
# File 'service/showback.rb', line 6

def state
  @state
end

#stimeObject (readonly)

Returns the value of attribute stime.



6
7
8
# File 'service/showback.rb', line 6

def stime
  @stime
end

#timelineObject (readonly)

Returns the value of attribute timeline.



6
7
8
# File 'service/showback.rb', line 6

def timeline
  @timeline
end

#vmObject (readonly)

Returns the value of attribute vm.



6
7
8
# File 'service/showback.rb', line 6

def vm
  @vm
end

Instance Method Details

#compileObject

Compiles timeline:

obtains events from sources and filters useless events
sorts events
filters again


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
# File 'service/showback.rb', line 23

def compile
  records = @sources.inject([]) do | r, source |
    r.concat source.tl_filter( # Filtering useless events(e.g. unfinished TrafficRecord)
      source.new(@vm.id).find(@stime, @etime).all # Seeking for all events happend between stime and etime
    )
  end

  records.map! do | rec |
    # Some of Records need to be processed extra to become sortable and usable
    # for example SnapshotRecords are holding both creation and deletion time,
    # so each SnapshotRecord will be splitted into SnapshotCreateRecord and SnapshotDeleteRecord
    rec.sortable
  end
  records.flatten!

  @timeline = records.sort_by { |rec| rec.sorter } # Sorting records by time sorter(.sorter method returns timestamp to be used as sorter)
  @timeline.select! { |rec| rec.sorter.between?(@stime, @etime) } # Filtering records again

  init # Generating initial state
  init_rec = InitRecord.new @stime, @state
  @timeline.unshift init_rec
  @timeline << FinalRecord.new(@etime)

  @compiled = true
  self
end

#initObject

Generates initial state

Examples:

VM was turned off a minute before stime, which means init state must be 'off' and versa

VM Snapshot was created a minute before stime, which means init state must have 'snaps: 1'



53
54
55
56
57
# File 'service/showback.rb', line 53

def init
  @state = @sources.inject({}) do | r, source |
    r.merge source.new(@vm.id).init_state(@stime)
  end
end