humus-old/client/ui/throughput.js

80 lines
1.8 KiB
JavaScript

// 3rd party.
import React, {useEffect, useState} from 'react';
// 2nd party.
import {compose} from '@avocado/core';
import {Vector} from '@avocado/math';
import contempo from 'contempo';
// 1st party.
import {WorldTime} from '../../common/world-time';
const decorate = compose(
contempo(`
.throughput {
background-color: rgba(0, 0, 0, .3);
color: white;
font-size: 0.6em;
position: absolute;
top: 0.5em;
left: 0.5em;
line-height: 1em;
width: 11em;
padding: 0.5em;
font-family: monospace;
}
.throughput .kbps:before {
color: rgb(180, 180, 180);
content: 'kb/s ';
}
.throughput .input:before {
content: '\\a0in: ';
}
.throughput .output:before {
content: 'out: ';
}
.throughput p {
margin: 0.25em;
}
`),
);
const formatKbps = (bytes) => {
const bps = bytes * 8;
const kbps = bps / 1000;
return Math.floor(kbps * 100) / 100;
}
const ThroughputComponent = ({Parser}) => {
const [throughput, setThroughput] = useState([0, 0]);
useEffect(() => {
const {Decoder, Encoder} = Parser;
const throughputSampleTime = 0.5;
// Throughput ticker.
const throughputHandle = setInterval(() => {
const throughput = Vector.scale([
Decoder.throughput || 0,
Encoder.throughput || 0,
], 1 / throughputSampleTime);
setThroughput(throughput);
Decoder.throughput = 0;
Encoder.throughput = 0;
}, throughputSampleTime * 1000);
return () => {
clearInterval(throughputHandle);
};
}, []);
return <div className="throughput unselectable">
<p className="unit kbps">
<span className="input">
{formatKbps(throughput[0])}
</span>
</p>
<p className="unit kbps">
<span className="output">
{formatKbps(throughput[1])}
</span>
</p>
</div>;
}
export default decorate(ThroughputComponent);