elstat/priv/frontend/src/components/Service.js

85 lines
1.9 KiB
JavaScript
Raw Normal View History

2018-07-16 04:04:43 +00:00
import React from 'react'
import PropTypes from 'prop-types'
2018-07-22 18:52:54 +00:00
import classnames from 'classnames'
2018-06-12 21:56:50 +00:00
2018-07-16 04:04:43 +00:00
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
2018-07-14 01:57:02 +00:00
2018-07-16 04:04:43 +00:00
import './Service.css'
import Graph from './Graph.js'
import config from '../config.json'
2018-08-09 00:06:35 +00:00
import { truncateToTwoPlaces } from '../util'
2018-06-12 21:56:50 +00:00
2018-08-08 23:43:53 +00:00
const { slow_threshold: SLOW_THRESHOLD = 1500 } = config
const icons = {
alive: 'check-circle',
slow: 'exclamation-circle',
dead: 'times-circle',
}
const titles = {
alive: 'This service is healthy.',
slow: 'This service is slow, but responding.',
dead: 'This service is unresponsive.',
}
2018-08-08 23:43:53 +00:00
export function getServiceState(status, latency, threshold = SLOW_THRESHOLD) {
if (status && latency > threshold) {
return 'slow'
}
return status ? 'alive' : 'dead'
}
2018-08-09 00:06:35 +00:00
export default function Service({
graph,
name,
status,
latency,
description,
uptime,
}) {
const state = getServiceState(status, latency)
const title = titles[state]
const icon = icons[state]
2018-07-22 18:52:54 +00:00
2018-08-08 23:43:53 +00:00
const className = classnames('service', `service-${state}`)
2018-08-09 00:06:35 +00:00
const uptimePercentage = truncateToTwoPlaces(uptime)
return (
2018-07-22 18:52:54 +00:00
<div className={className}>
<header>
<div className="emoji" title={title}>
2018-08-08 23:43:53 +00:00
<FontAwesomeIcon title={title} icon={icon} />
</div>
<h2 className="title">
2018-08-08 23:43:53 +00:00
{name}{' '}
{latency ? (
2018-08-09 00:06:35 +00:00
<span className="information">
2018-08-08 23:43:53 +00:00
{Math.round(latency)}
2018-08-09 00:06:35 +00:00
ms ({uptimePercentage}
%)
</span>
) : null}
</h2>
</header>
2018-08-08 23:43:53 +00:00
<p className="description">{description}</p>
{graph ? <Graph data={graph} /> : null}
</div>
)
}
2018-06-12 21:56:50 +00:00
2018-07-14 02:01:13 +00:00
Service.defaultProps = {
graph: null,
latency: null,
2018-07-16 04:04:43 +00:00
}
2018-07-14 02:01:13 +00:00
Service.propTypes = {
2018-08-08 23:43:53 +00:00
graph: PropTypes.arrayOf(PropTypes.arrayOf(PropTypes.number)),
2018-07-14 02:01:13 +00:00
name: PropTypes.string.isRequired,
status: PropTypes.bool.isRequired,
latency: PropTypes.number,
description: PropTypes.string.isRequired,
2018-07-16 04:04:43 +00:00
}