forked from asmexcaliburwoods/dashboard
-
Notifications
You must be signed in to change notification settings - Fork 1
/
build-duration.js
122 lines (105 loc) · 3.13 KB
/
build-duration.js
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
import { Component } from 'react'
import fetch from 'isomorphic-unfetch'
import styled from 'styled-components'
import { object, string, array, number } from 'yup'
import Widget from '../../widget'
import Link from '../../link'
import Table, { Th, Td } from '../../table'
import LoadingIndicator from '../../loading-indicator'
import { basicAuthHeader } from '../../../lib/auth'
const Kpi = styled.span`
color: ${props => props.theme.palette.primaryColor};
font-weight: 700;
font-size: 20px;
`
const schema = object().shape({
url: string().url().required(),
jobs: array(object({
label: string().required(),
path: string().required(),
branch: string()
})).required(),
interval: number(),
title: string(),
authKey: string()
})
export default class JenkinsBuildDuration extends Component {
static defaultProps = {
interval: 1000 * 60 * 5,
title: 'Build Duration'
}
state = {
loading: true,
error: false
}
componentDidMount () {
schema.validate(this.props)
.then(() => this.fetchInformation())
.catch((err) => {
console.error(`${err.name} @ ${this.constructor.name}`, err.errors)
this.setState({ error: true, loading: false })
})
}
componentWillUnmount () {
clearTimeout(this.timeout)
}
formatTime (ms) {
const s = ms / 1000
if (s > 60) {
const min = Math.floor(s / 60)
let minSec = Math.round(s - (min * 60))
minSec = minSec.toString().length === 1 ? `0${minSec}` : minSec
return <><Kpi>{min}:{minSec}</Kpi> min</>
}
return <><Kpi>{Math.round(s)}</Kpi> sec</>
}
async fetchInformation () {
const { authKey, jobs, url } = this.props
const opts = authKey ? { headers: basicAuthHeader(authKey) } : {}
try {
const builds = await Promise.all(
jobs.map(async job => {
const branch = job.branch ? `job/${job.branch}/` : ''
const res = await fetch(`${url}/job/${job.path}/${branch}lastBuild/api/json`, opts)
const json = await res.json()
return {
name: job.label,
url: json.url,
duration: json.duration
}
})
)
this.setState({ error: false, loading: false, builds })
} catch (error) {
this.setState({ error: true, loading: false })
} finally {
this.timeout = setTimeout(() => this.fetchInformation(), this.props.interval)
}
}
render () {
const { loading, error, builds } = this.state
const { title } = this.props
return (
<Widget title={title} error={error} loading={loading}>
<Table>
<tbody>
{builds && builds.map(build => (
<tr key={`jenkins-${build.name}`}>
<Th>{build.name}</Th>
<Td>
<Link href={build.url} title={build.duration}>
{
build.duration
? this.formatTime(build.duration)
: <LoadingIndicator size='small' />
}
</Link>
</Td>
</tr>
))}
</tbody>
</Table>
</Widget>
)
}
}