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

145 lines
3.4 KiB
JavaScript
Raw Normal View History

2018-07-16 04:04:43 +00:00
import React, { Component } from 'react'
2018-06-12 21:56:50 +00:00
2018-07-16 04:04:43 +00:00
import './App.css'
import Service from './Service.js'
import ServicePlaceholder from './ServicePlaceholder.js'
import DegradedNotice from './DegradedNotice.js'
import StreamingClient from '../ws/client'
import { log, objectFromEntries } from '../util.js'
import { domain as DOMAIN } from '../config.json'
2018-06-12 21:56:50 +00:00
export default class App extends Component {
client = null
2018-07-14 02:11:50 +00:00
2018-06-12 21:56:50 +00:00
state = {
loading: true,
error: null,
metrics: null,
}
2018-06-12 21:56:50 +00:00
2018-07-16 04:04:43 +00:00
async componentDidMount () {
await this.loadMetrics()
const endpoint = `${DOMAIN}/api/streaming`
.replace('https', 'wss')
.replace('http', 'ws')
this.client = new StreamingClient(endpoint, this.state.metrics)
this.client.connect()
this.client.on('status', this.handleStatus.bind(this))
this.client.on('latency', this.handleLatency.bind(this))
}
2018-07-16 04:22:06 +00:00
handleStatus (name, [, status]) {
const { status: statuses } = this.state.metrics
log('updating status on:', name)
2018-07-16 04:22:06 +00:00
if (!(name in statuses)) {
log(`failed to locate channel ${name} to update statuses`)
return
}
if (statuses[name].status === status) {
log(`ignoring stale status (${status}) for ${name}`)
2018-07-16 04:04:43 +00:00
return
}
2018-07-16 04:22:06 +00:00
this.setState(({ metrics: old }, _props) => {
const metrics = { ...old }
metrics.status[name].status = status
2018-07-16 04:22:06 +00:00
return { metrics }
})
}
2018-07-16 04:22:06 +00:00
handleLatency (name, data) {
2018-07-16 04:04:43 +00:00
const { metrics } = this.state
2018-07-16 04:22:06 +00:00
log('adding latency entry:', data)
2018-07-17 01:15:51 +00:00
// latency entries come in newest to oldest, so remove the oldest entry
const graph = metrics.graph[name].slice(0, metrics.graph[name].length - 1)
// make new data come in first
const newGraph = [data, ...graph]
2018-07-16 04:22:06 +00:00
this.setState(({ metrics: old }, _props) => {
const metrics = { ...old }
metrics.graph[name] = newGraph
2018-07-16 04:04:43 +00:00
const [, latency] = data
2018-07-16 04:22:06 +00:00
metrics.status[name].latency = latency
2018-07-16 04:22:06 +00:00
return { metrics }
2018-07-16 04:04:43 +00:00
})
2018-06-12 21:56:50 +00:00
}
2018-07-16 04:04:43 +00:00
async loadMetrics () {
log('loading metrics')
try {
2018-07-17 01:06:06 +00:00
var resp = await fetch(`${DOMAIN}/api/status`)
} catch (err) {
2018-07-17 01:06:06 +00:00
this.setState({
error: `Network error: ${err}`,
})
}
if (!resp.ok) {
this.setState({
error: `Failed to fetch stats (${resp.status} ${resp.statusText})`,
})
return
}
2018-07-17 01:06:06 +00:00
const json = await resp.json()
this.setState({ metrics: json, loading: false })
}
2018-07-16 04:04:43 +00:00
render () {
let metrics = null
if (this.state.metrics) {
const allServices = Object.entries(this.state.metrics.status)
const graphs = this.state.metrics.graph
const services = allServices.map(([name, info]) => (
<Service name={name} key={name} graph={graphs[name]} {...info}/>
))
const down = allServices.filter(([, { status }]) => !status)
2018-07-17 01:16:39 +00:00
const notice = down.length > 0
? <DegradedNotice services={objectFromEntries(down)}/>
: null
metrics = (
<div className="services">
{notice}
{services}
</div>
)
}
2018-06-12 21:56:50 +00:00
return (
<div className="dashboard">
<h1>elstatus</h1>
2018-07-14 02:06:38 +00:00
{this.state.error ? (
<div className="error">
Error: {this.state.error}
</div>
) : null}
2018-07-14 02:34:03 +00:00
{this.state.loading && !this.state.error ? (
2018-07-14 01:43:10 +00:00
<React.Fragment>
2018-07-17 01:06:15 +00:00
<ServicePlaceholder/>
<ServicePlaceholder/>
<ServicePlaceholder/>
2018-07-14 01:43:10 +00:00
</React.Fragment>
2018-07-14 02:06:38 +00:00
) : metrics}
2018-06-12 21:56:50 +00:00
</div>
2018-07-16 04:04:43 +00:00
)
2018-06-12 21:56:50 +00:00
}
}