forked from cloudfoundry/bosh-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reporter.go
207 lines (167 loc) · 4.95 KB
/
reporter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
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
83
84
85
86
87
88
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package task
import (
"encoding/json"
"fmt"
"strings"
"sync"
boshui "github.com/cloudfoundry/bosh-cli/ui"
)
type ReporterImpl struct {
ui boshui.UI
isForEvents bool
events map[int][]*Event
eventMarkers []eventMarker
lastGlobalEvent *Event
outputRest string
sync.Mutex
}
type eventMarker struct {
TaskID int
Type int
}
const (
taskStarted = iota
taskOutput = iota
taskEnded = iota
)
func NewReporter(ui boshui.UI, isForEvents bool) *ReporterImpl {
return &ReporterImpl{ui: ui, isForEvents: isForEvents, events: map[int][]*Event{}, eventMarkers: []eventMarker{}}
}
func (r *ReporterImpl) TaskStarted(id int) {
r.Lock()
defer r.Unlock()
if len(r.eventMarkers) > 0 {
r.ui.EndLinef("")
}
r.eventMarkers = append(r.eventMarkers, eventMarker{TaskID: id, Type: taskStarted})
r.events[id] = []*Event{}
r.lastGlobalEvent = &Event{TaskID: id}
r.ui.BeginLinef("Task %d", id)
}
func (r *ReporterImpl) TaskFinished(id int, state string) {
r.Lock()
defer r.Unlock()
if len(r.events[id]) > 0 {
start := r.events[id][0].TimeAsStr()
end := r.lastEventForTask(id).TimeAsStr()
duration := r.events[id][0].DurationAsStr(*r.lastEventForTask(id))
r.ui.BeginLinef("\n\nTask %d Started %s\nTask %d Finished %s\nTask %d Duration %s", id, start, id, end, id, duration)
}
if r.noOutputSinceTaskStarted(id) {
r.ui.EndLinef(". %s", strings.Title(state))
} else {
r.ui.BeginLinef("\nTask %d %s\n", id, state)
}
r.eventMarkers = append(r.eventMarkers, eventMarker{TaskID: id, Type: taskEnded})
}
func (r *ReporterImpl) TaskOutputChunk(id int, chunk []byte) {
r.Lock()
defer r.Unlock()
if r.noOutputSinceTaskStarted(id) {
r.ui.BeginLinef("\n")
if !r.isForEvents {
r.ui.BeginLinef("\n")
}
}
if r.isForEvents {
r.outputRest += string(chunk)
for {
idx := strings.Index(r.outputRest, "\n")
if idx == -1 {
break
}
if len(r.outputRest[0:idx]) > 0 {
r.showEvent(id, r.outputRest[0:idx])
}
r.outputRest = r.outputRest[idx+1:]
}
} else {
r.showChunk(chunk)
}
r.eventMarkers = append(r.eventMarkers, eventMarker{TaskID: id, Type: taskOutput})
}
func (r *ReporterImpl) showEvent(id int, str string) {
event := Event{TaskID: id}
err := json.Unmarshal([]byte(str), &event)
if err != nil {
panic(fmt.Sprintf("unmarshal chunk '%s'", str))
}
for _, ev := range r.events[id] {
if ev.IsSame(event) {
event.StartEvent = ev
break
}
}
if r.lastGlobalEvent != nil && r.lastGlobalEvent.IsSame(event) {
switch {
case event.State == EventStateStarted:
// does not make sense
case event.State == EventStateFinished:
r.ui.PrintBlock(fmt.Sprintf(" (%s)", event.DurationSinceStartAsStr()))
case event.State == EventStateFailed:
r.ui.PrintBlock(fmt.Sprintf(" (%s)", event.DurationSinceStartAsStr()))
r.ui.PrintErrorBlock(fmt.Sprintf(
"\n %s L Error: %s", strings.Repeat(" ", len(string(id))), event.Data.Error))
}
} else {
if r.lastGlobalEvent != nil && !r.lastGlobalEvent.IsSameTaskID(event) && event.IsWorthKeeping() {
if event.Type == EventTypeDeprecation || event.Error != nil {
// Some spacing around deprecations and errors
r.ui.PrintBlock("\n")
}
}
prefix := fmt.Sprintf("\nTask %d | %s | ", id, event.TimeAsHoursStr())
desc := event.Stage
if len(event.Tags) > 0 {
desc += " " + strings.Join(event.Tags, ", ")
}
switch {
case event.Type == EventTypeDeprecation:
r.ui.PrintBlock(prefix)
r.ui.PrintErrorBlock(fmt.Sprintf("Deprecation: %s", event.Message))
case event.Type == EventTypeWarning:
r.ui.PrintBlock(prefix)
r.ui.PrintErrorBlock(fmt.Sprintf("Warning: %s", event.Message))
case event.State == EventStateStarted:
r.ui.PrintBlock(prefix)
r.ui.PrintBlock(fmt.Sprintf("%s: %s", desc, event.Task))
case event.State == EventStateFinished:
r.ui.PrintBlock(prefix)
r.ui.PrintBlock(fmt.Sprintf("%s: %s (%s)",
desc, event.Task, event.DurationSinceStartAsStr()))
case event.State == EventStateFailed:
r.ui.PrintBlock(prefix)
r.ui.PrintBlock(fmt.Sprintf("%s: %s (%s)",
desc, event.Task, event.DurationSinceStartAsStr()))
r.ui.PrintErrorBlock(fmt.Sprintf(
"\n %s L Error: %s", strings.Repeat(" ", len(string(id))), event.Data.Error))
case event.Error != nil:
r.ui.PrintBlock(prefix)
r.ui.PrintErrorBlock(fmt.Sprintf("Error: %s", event.Error.Message))
default:
// Skip event
}
}
if event.IsWorthKeeping() {
r.events[id] = append(r.events[id], &event)
r.lastGlobalEvent = &event
}
}
func (r *ReporterImpl) showChunk(bytes []byte) {
r.ui.PrintBlock(string(bytes))
}
func (r *ReporterImpl) lastEventForTask(id int) *Event {
eventCount := len(r.events[id])
if eventCount > 0 {
return r.events[id][eventCount-1]
}
return nil
}
func (r *ReporterImpl) noOutputSinceTaskStarted(id int) bool {
markerCount := len(r.eventMarkers)
if markerCount == 0 {
return true
}
lastMarker := r.eventMarkers[markerCount-1]
return lastMarker.TaskID == id && lastMarker.Type == taskStarted
}