humus-old/client/ui/throughput.js

118 lines
2.5 KiB
JavaScript
Raw Normal View History

2019-04-12 12:10:40 -05:00
// 3rd party.
2019-04-13 17:51:49 -05:00
import classnames from 'classnames';
2019-04-12 12:10:40 -05:00
import React, {useEffect, useState} from 'react';
// 2nd party.
import {compose} from '@avocado/core';
import {Vector} from '@avocado/math';
2019-04-12 12:10:40 -05:00
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 {
2019-04-13 17:51:49 -05:00
color: rgb(0, 255, 0);
content: 'kb/s ';
}
2019-04-13 17:51:49 -05:00
.throughput .mbps:before {
color: rgb(255, 180, 0);
content: 'mb/s ';
}
2019-04-12 12:10:40 -05:00
.throughput .input:before {
content: '\\a0in: ';
2019-04-12 12:10:40 -05:00
}
.throughput .output:before {
content: 'out: ';
2019-04-12 12:10:40 -05:00
}
.throughput p {
margin: 0.25em;
}
`),
);
2019-04-13 17:51:49 -05:00
function formatKbps(bps) {
2019-04-12 12:10:40 -05:00
const kbps = bps / 1000;
return Math.floor(kbps * 100) / 100;
}
2019-04-13 17:51:49 -05:00
function formatMbps(bps) {
const mbps = bps / 1000000;
return Math.floor(mbps * 100) / 100;
}
function format(bps) {
const unit = preferredUnit(bps);
switch (unit) {
case 'kbps':
return formatKbps(bps);
case 'mbps':
return formatMbps(bps);
}
}
function preferredUnit(bps) {
if (bps > 1000000) {
return 'mbps';
}
else {
return 'kbps';
}
}
const ThroughputComponent = ({Parser}) => {
2019-04-12 12:10:40 -05:00
const [throughput, setThroughput] = useState([0, 0]);
2019-04-13 17:51:49 -05:00
const [inUnit, setInUnit] = useState('kbps');
const [outUnit, setOutUnit] = useState('kbps');
2019-04-12 12:10:40 -05:00
useEffect(() => {
const {Decoder, Encoder} = Parser;
const throughputSampleTime = 0.5;
// Throughput ticker.
const throughputHandle = setInterval(() => {
const throughput = Vector.scale([
Decoder.throughput || 0,
Encoder.throughput || 0,
2019-04-13 17:51:49 -05:00
], 8 * (1 / throughputSampleTime));
2019-04-12 12:10:40 -05:00
setThroughput(throughput);
2019-04-13 17:51:49 -05:00
setInUnit(preferredUnit(throughput[0]));
setOutUnit(preferredUnit(throughput[1]));
Decoder.throughput = 0;
Encoder.throughput = 0;
}, throughputSampleTime * 1000);
2019-04-12 12:10:40 -05:00
return () => {
clearInterval(throughputHandle);
2019-04-12 12:10:40 -05:00
};
}, []);
return <div className="throughput unselectable">
2019-04-13 17:51:49 -05:00
<p className={classnames(
'unit',
inUnit,
)}>
<span className="input">
2019-04-13 17:51:49 -05:00
{format(throughput[0])}
</span>
</p>
2019-04-13 17:51:49 -05:00
<p className={classnames(
'unit',
outUnit,
)}>
<span className="output">
2019-04-13 17:51:49 -05:00
{format(throughput[1])}
</span>
</p>
2019-04-12 12:10:40 -05:00
</div>;
}
export default decorate(ThroughputComponent);