UNPKG

5.17 kBJavaScriptView Raw
1const cli = require('heroku-cli-util')
2const io = require('socket.io-client')
3const ansiEscapes = require('ansi-escapes')
4const api = require('./heroku-api')
5const TestRunStates = require('./test-run-states')
6const wait = require('co-wait')
7const TestRunStatesUtil = require('./test-run-states-util')
8
9const SIMI = 'https://simi-production.herokuapp.com'
10
11const { PENDING, CREATING, BUILDING, RUNNING, DEBUGGING, ERRORED, FAILED, SUCCEEDED, CANCELLED } = TestRunStates
12
13// used to pad the status column so that the progress bars align
14const maxStateLength = Math.max.apply(null, Object.keys(TestRunStates).map((k) => TestRunStates[k]))
15
16const STATUS_ICONS = {
17 [PENDING]: '⋯',
18 [CREATING]: '⋯',
19 [BUILDING]: '⋯',
20 [RUNNING]: '⋯',
21 [DEBUGGING]: '⋯',
22 [ERRORED]: '!',
23 [FAILED]: '✗',
24 [SUCCEEDED]: '✓',
25 [CANCELLED]: '!'
26}
27
28const STATUS_COLORS = {
29 [PENDING]: 'yellow',
30 [CREATING]: 'yellow',
31 [BUILDING]: 'yellow',
32 [RUNNING]: 'yellow',
33 [DEBUGGING]: 'yellow',
34 [ERRORED]: 'red',
35 [FAILED]: 'red',
36 [SUCCEEDED]: 'green',
37 [CANCELLED]: 'yellow'
38}
39
40function statusIcon ({ status }) {
41 return cli.color[STATUS_COLORS[status] || 'yellow'](STATUS_ICONS[status] || '-')
42}
43
44function printLine (testRun) {
45 return `${statusIcon(testRun)} #${testRun.number} ${testRun.commit_branch}:${testRun.commit_sha.slice(0, 7)} ${testRun.status}`
46}
47
48function limit (testRuns, count) {
49 return testRuns.slice(0, count)
50}
51
52function sort (testRuns) {
53 return testRuns.sort((a, b) => a.number < b.number ? 1 : -1)
54}
55
56function redraw (testRuns, watch, count = 15) {
57 const arranged = limit(sort(testRuns), count)
58
59 if (watch) {
60 process.stdout.write(ansiEscapes.eraseDown)
61 }
62
63 const rows = arranged.map((testRun) => columns(testRun, testRuns))
64
65 // this is a massive hack but I basically create a table that does not print so I can calculate its width if it were printed
66 let width = 0
67 function printLine (line) {
68 width = line.length
69 }
70
71 cli.table(rows, {
72 printLine: printLine,
73 printHeader: false
74 })
75
76 const printRows = arranged.map((testRun) => columns(testRun, testRuns).concat([progressBar(testRun, testRuns, width)]))
77
78 cli.table(printRows, {
79 printLine: console.log,
80 printHeader: false
81 })
82
83 if (watch) {
84 process.stdout.write(ansiEscapes.cursorUp(arranged.length))
85 }
86}
87
88function handleTestRunEvent (newTestRun, testRuns) {
89 const previousTestRun = testRuns.find(({ id }) => id === newTestRun.id)
90 if (previousTestRun) {
91 const previousTestRunIndex = testRuns.indexOf(previousTestRun)
92 testRuns.splice(previousTestRunIndex, 1)
93 }
94
95 testRuns.push(newTestRun)
96
97 return testRuns
98}
99
100function * render (pipeline, { heroku, watch }) {
101 cli.styledHeader(
102 `${watch ? 'Watching' : 'Showing'} latest test runs for the ${pipeline.name} pipeline`
103 )
104
105 let testRuns = yield api.testRuns(heroku, pipeline.id)
106
107 if (watch) {
108 process.stdout.write(ansiEscapes.cursorHide)
109 }
110
111 redraw(testRuns, watch)
112
113 if (!watch) {
114 return
115 }
116
117 const socket = io(SIMI, { transports: ['websocket'], upgrade: false })
118
119 socket.on('connect', () => {
120 socket.emit('joinRoom', {
121 room: `pipelines/${pipeline.id}/test-runs`,
122 token: heroku.options.token
123 })
124 })
125
126 socket.on('create', ({ resource, data }) => {
127 if (resource === 'test-run') {
128 testRuns = handleTestRunEvent(data, testRuns)
129 redraw(testRuns, watch)
130 }
131 })
132
133 socket.on('update', ({ resource, data }) => {
134 if (resource === 'test-run') {
135 testRuns = handleTestRunEvent(data, testRuns)
136 redraw(testRuns, watch)
137 }
138 })
139
140 // refresh the table every second for progress bar updates
141 while (true) {
142 yield wait(1000)
143 redraw(testRuns, watch)
144 }
145}
146
147function timeDiff (updatedAt, createdAt) {
148 return (updatedAt.getTime() - createdAt.getTime()) / 1000
149}
150
151function averageTime (testRuns) {
152 return testRuns.map((testRun) => timeDiff(new Date(testRun.updated_at), new Date(testRun.created_at))).reduce((a, b) => a + b, 0) / testRuns.length
153}
154
155function progressBar (testRun, allTestRuns, tableWidth) {
156 let numBarDefault = 100
157 let numBars
158 if (process.stderr.isTTY) {
159 numBars = Math.min(process.stderr.getWindowSize()[0] - tableWidth, numBarDefault)
160 } else {
161 numBars = numBarDefault
162 }
163
164 // only include the last X runs which have finished
165 const numRuns = 10
166 const terminalRuns = allTestRuns.filter(TestRunStatesUtil.isTerminal).slice(0, numRuns)
167
168 if (TestRunStatesUtil.isTerminal(testRun) || terminalRuns.length === 0) {
169 return ''
170 }
171
172 const avg = averageTime(terminalRuns)
173 const testRunElapsed = timeDiff(new Date(), new Date(testRun.created_at))
174 const percentageComplete = Math.min(Math.floor((testRunElapsed / avg) * numBars), numBars)
175 return `[${'='.repeat(percentageComplete)}${' '.repeat(numBars - percentageComplete)}]`
176}
177
178function padStatus (testStatus) {
179 return testStatus + ' '.repeat(Math.max(0, maxStateLength - testStatus.length))
180}
181
182function columns (testRun, allTestRuns) {
183 return [statusIcon(testRun), testRun.number, testRun.commit_branch, testRun.commit_sha.slice(0, 7), padStatus(testRun.status)]
184}
185
186module.exports = {
187 render,
188 printLine
189}