Class: Bosh::Cli::TaskTracking::EventLogRenderer

Inherits:
TaskLogRenderer show all
Extended by:
Forwardable
Defined in:
lib/cli/task_tracking/event_log_renderer.rb

Defined Under Namespace

Classes: InvalidEvent, Task

Constant Summary

Constants inherited from TaskLogRenderer

TaskLogRenderer::EVENT_LOG_STAGES_WITHOUT_PROGRESS_BAR

Instance Attribute Summary collapse

Attributes inherited from TaskLogRenderer

#duration, #time_adjustment

Instance Method Summary collapse

Methods inherited from TaskLogRenderer

create_for_log_type, #duration_known?

Constructor Details

#initialize(options = {}) ⇒ EventLogRenderer

Returns a new instance of EventLogRenderer.



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/cli/task_tracking/event_log_renderer.rb', line 11

def initialize(options={})
  @total_duration = TotalDuration.new
  @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
  @stages_without_progress_bar = options[:stages_without_progress_bar] || []
end

Instance Attribute Details

#current_stageObject (readonly)

Returns the value of attribute current_stage.



8
9
10
# File 'lib/cli/task_tracking/event_log_renderer.rb', line 8

def current_stage
  @current_stage
end

#events_countObject (readonly)

Returns the value of attribute events_count.



9
10
11
# File 'lib/cli/task_tracking/event_log_renderer.rb', line 9

def events_count
  @events_count
end

Instance Method Details

#add_error(event) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
# File 'lib/cli/task_tracking/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

  @buffer.puts("\n" + error.make_red)
end

#add_event(event_line) ⇒ Object



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
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/task_tracking/event_log_renderer.rb', line 29

def add_event(event_line)
  event = parse_event(event_line)

  @lock.synchronize do
    if event['type'] == 'deprecation'
      @buffer.puts("Deprecation: #{event['message']}".make_red)
      return
    end

    # Handling the special "error" event
    if event['error']
      done_with_stage if @current_stage
      add_error(event)
      return
    end

    if can_handle_event_without_progress_bar?(event)
      if @current_stage
        done_with_stage
        @current_stage = nil
        @buffer.print "\n"
      end
      handle_event_without_progress_bar(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(', ').make_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



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

def add_output(output)
  output.to_s.split("\n").each { |line| add_event(line) }
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/task_tracking/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

#finish(state) ⇒ Object



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

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

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

#refreshObject



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

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/task_tracking/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