Class: Bosh::Cli::EventLogRenderer

Inherits:
TaskLogRenderer show all
Defined in:
lib/cli/event_log_renderer.rb

Defined Under Namespace

Classes: InvalidEvent, Task

Instance Attribute Summary collapse

Attributes inherited from TaskLogRenderer

#time_adjustment

Instance Method Summary collapse

Methods inherited from TaskLogRenderer

create_for_log_type

Constructor Details

#initializeEventLogRenderer

Returns a new instance of EventLogRenderer.



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

def initialize
  @lock = Monitor.new
  @events_count = 0
  @seen_stages = Set.new
  @out = Bosh::Cli::Config.output || $stdout
  @out.sync = true
  @buffer = StringIO.new
  @progress_bars = {}
  @pos = 0
  @time_adjustment = 0
end

Instance Attribute Details

#current_stageObject (readonly)

Returns the value of attribute current_stage.



22
23
24
# File 'lib/cli/event_log_renderer.rb', line 22

def current_stage
  @current_stage
end

#events_countObject (readonly)

Returns the value of attribute events_count.



23
24
25
# File 'lib/cli/event_log_renderer.rb', line 23

def events_count
  @events_count
end

#finished_atObject (readonly)

Returns the value of attribute finished_at.



24
25
26
# File 'lib/cli/event_log_renderer.rb', line 24

def finished_at
  @finished_at
end

#started_atObject (readonly)

Returns the value of attribute started_at.



24
25
26
# File 'lib/cli/event_log_renderer.rb', line 24

def started_at
  @started_at
end

Instance Method Details

#add_error(event) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/cli/event_log_renderer.rb', line 116

def add_error(event)
  error = event["error"] || {}
  code = error["code"]
  message = error["message"]

  error = "Error"
  error += " #{code}" if code
  error += ": #{message}" if message

  # TODO: add KB article link and maybe cck reference?
  @buffer.puts("\n" + error.red)
end

#add_event(event_line) ⇒ Object



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
75
76
77
78
79
80
81
82
# File 'lib/cli/event_log_renderer.rb', line 44

def add_event(event_line)
  event = parse_event(event_line)

  @lock.synchronize do
    # Handling the special "error" event
    if event["error"]
      done_with_stage if @current_stage
      add_error(event)
      return
    end

    # One way to handle old stages is to prevent them
    # from appearing on screen altogether. That means
    # that we can always render the current stage only
    # and that simplifies housekeeping around progress
    # bars and messages. However we could always support
    # resuming the older stages rendering if we feel
    # that it's valuable.

    tags = event["tags"].is_a?(Array) ? event["tags"] : []
    stage_header = event["stage"]

    if tags.size > 0
      stage_header += " " + tags.sort.join(", ").green
    end

    unless @seen_stages.include?(stage_header)
      done_with_stage if @current_stage
      begin_stage(event, stage_header)
    end

    if @current_stage == stage_header
      append_event(event)
    end
  end

rescue InvalidEvent => e
  # Swallow for the moment
end

#add_output(output) ⇒ Object



38
39
40
41
42
# File 'lib/cli/event_log_renderer.rb', line 38

def add_output(output)
  output.to_s.split("\n").each do |line|
    add_event(line)
  end
end

#begin_stage(event, header) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/cli/event_log_renderer.rb', line 84

def begin_stage(event, header)
  @current_stage = header
  @seen_stages << @current_stage

  @stage_start_time = Time.at(event["time"]) rescue Time.now
  @local_start_time = adjusted_time(@stage_start_time)

  @tasks = {}
  @done_tasks = []

  @eta = nil
  @stage_has_error = false # Error flag
  # Tracks max_in_flight best guess
  @tasks_batch_size = 0
  @batches_count = 0

  # Running average of task completion time
  @running_avg = 0

  append_stage_header
end

#durationObject



155
156
157
158
# File 'lib/cli/event_log_renderer.rb', line 155

def duration
  return unless duration_known?
  @finished_at - @started_at
end

#duration_known?Boolean

Returns:

  • (Boolean)


151
152
153
# File 'lib/cli/event_log_renderer.rb', line 151

def duration_known?
  @started_at && @finished_at
end

#finish(state) ⇒ Object



141
142
143
144
145
146
147
148
149
# File 'lib/cli/event_log_renderer.rb', line 141

def finish(state)
  return if @events_count == 0

  @lock.synchronize do
    @done = true
    done_with_stage(state)
    render
  end
end

#refreshObject



129
130
131
132
133
134
135
136
137
138
139
# File 'lib/cli/event_log_renderer.rb', line 129

def refresh
  # This is primarily used to refresh timer
  # without advancing rendering buffer
  @lock.synchronize do
    if @in_progress
      progress_bar.label = time_with_eta(Time.now - @local_start_time, @eta)
      progress_bar.refresh
    end
    render
  end
end

#renderObject



106
107
108
109
110
111
112
113
114
# File 'lib/cli/event_log_renderer.rb', line 106

def render
  @lock.synchronize do
    @buffer.seek(@pos)
    output = @buffer.read
    @out.print output
    @pos = @buffer.tell
    output
  end
end