2022-03-17 03:54:34 +03:00
/* eslint-disable react/no-unescaped-entities */
// import { BulbOutlined, LaptopOutlined, SaveOutlined } from '@ant-design/icons';
2022-04-10 09:04:52 +03:00
import { Row , Col , Typography , Space , Statistic , Card , Alert , Spin } from 'antd' ;
2023-01-16 10:11:44 +03:00
import React , { ReactElement , ReactNode , useEffect , useState } from 'react' ;
2023-01-16 09:31:36 +03:00
import dynamic from 'next/dynamic' ;
2022-04-20 05:57:27 +03:00
import { fetchData , FETCH_INTERVAL , API_STREAM_HEALTH_METRICS } from '../../utils/apis' ;
2023-01-10 07:57:29 +03:00
import { Chart } from '../../components/admin/Chart' ;
import { StreamHealthOverview } from '../../components/admin/StreamHealthOverview' ;
2022-03-17 03:54:34 +03:00
2023-01-16 10:11:44 +03:00
import { AdminLayout } from '../../components/layouts/AdminLayout' ;
2023-01-16 09:31:36 +03:00
// Lazy loaded components
const ClockCircleOutlined = dynamic ( ( ) = > import ( '@ant-design/icons/ClockCircleOutlined' ) , {
ssr : false ,
} ) ;
const WarningOutlined = dynamic ( ( ) = > import ( '@ant-design/icons/WarningOutlined' ) , {
ssr : false ,
} ) ;
const WifiOutlined = dynamic ( ( ) = > import ( '@ant-design/icons/WifiOutlined' ) , {
ssr : false ,
} ) ;
2022-03-17 03:54:34 +03:00
interface TimedValue {
time : Date ;
value : Number ;
}
interface DescriptionBoxProps {
title : String ;
description : ReactNode ;
}
2022-09-07 10:00:28 +03:00
const DescriptionBox = ( { title , description } : DescriptionBoxProps ) = > (
< div className = "description-box" >
< Typography.Title > { title } < / Typography.Title >
< Typography.Paragraph > { description } < / Typography.Paragraph >
< / div >
) ;
2022-03-17 03:54:34 +03:00
2022-09-07 10:00:28 +03:00
const StreamHealth = ( ) = > {
2022-03-17 03:54:34 +03:00
const [ errors , setErrors ] = useState < TimedValue [ ] > ( [ ] ) ;
const [ qualityVariantChanges , setQualityVariantChanges ] = useState < TimedValue [ ] > ( [ ] ) ;
2022-03-17 08:49:38 +03:00
const [ lowestLatency , setLowestLatency ] = useState < TimedValue [ ] > ( ) ;
const [ highestLatency , setHighestLatency ] = useState < TimedValue [ ] > ( ) ;
const [ medianLatency , setMedianLatency ] = useState < TimedValue [ ] > ( [ ] ) ;
const [ medianSegmentDownloadDurations , setMedianSegmentDownloadDurations ] = useState <
TimedValue [ ]
> ( [ ] ) ;
const [ maximumSegmentDownloadDurations , setMaximumSegmentDownloadDurations ] = useState <
TimedValue [ ]
> ( [ ] ) ;
const [ minimumSegmentDownloadDurations , setMinimumSegmentDownloadDurations ] = useState <
TimedValue [ ]
> ( [ ] ) ;
2022-03-17 03:54:34 +03:00
const [ minimumPlayerBitrate , setMinimumPlayerBitrate ] = useState < TimedValue [ ] > ( [ ] ) ;
2022-03-17 08:49:38 +03:00
const [ medianPlayerBitrate , setMedianPlayerBitrate ] = useState < TimedValue [ ] > ( [ ] ) ;
const [ maximumPlayerBitrate , setMaximumPlayerBitrate ] = useState < TimedValue [ ] > ( [ ] ) ;
2022-03-17 03:54:34 +03:00
const [ availableBitrates , setAvailableBitrates ] = useState < Number [ ] > ( [ ] ) ;
const [ segmentLength , setSegmentLength ] = useState ( 0 ) ;
const getMetrics = async ( ) = > {
try {
const result = await fetchData ( API_STREAM_HEALTH_METRICS ) ;
setErrors ( result . errors ) ;
setQualityVariantChanges ( result . qualityVariantChanges ) ;
2022-03-17 08:49:38 +03:00
setHighestLatency ( result . highestLatency ) ;
setLowestLatency ( result . lowestLatency ) ;
setMedianLatency ( result . medianLatency ) ;
setMedianSegmentDownloadDurations ( result . medianSegmentDownloadDuration ) ;
setMaximumSegmentDownloadDurations ( result . maximumSegmentDownloadDuration ) ;
setMinimumSegmentDownloadDurations ( result . minimumSegmentDownloadDuration ) ;
2022-03-17 03:54:34 +03:00
setMinimumPlayerBitrate ( result . minPlayerBitrate ) ;
2022-03-17 08:49:38 +03:00
setMedianPlayerBitrate ( result . medianPlayerBitrate ) ;
setMaximumPlayerBitrate ( result . maxPlayerBitrate ) ;
2022-03-17 03:54:34 +03:00
setAvailableBitrates ( result . availableBitrates ) ;
setSegmentLength ( result . segmentLength - 0.3 ) ;
} catch ( error ) {
console . error ( error ) ;
}
} ;
useEffect ( ( ) = > {
let getStatusIntervalId = null ;
getMetrics ( ) ;
getStatusIntervalId = setInterval ( getMetrics , FETCH_INTERVAL ) ; // runs every 1 min.
// returned function will be called on component unmount
return ( ) = > {
clearInterval ( getStatusIntervalId ) ;
} ;
} , [ ] ) ;
const noData = (
< div >
< Typography.Title > Stream Performance < / Typography.Title >
2022-04-10 09:04:52 +03:00
< Alert
type = "info"
message = "
2022-03-17 03:54:34 +03:00
Data has not yet been collected . Once a stream has begun and viewers are watching this page
2022-04-10 09:04:52 +03:00
will be available . "
/ >
< Spin size = "large" >
< div style = { { marginTop : '50px' , height : '100px' } } / >
< / Spin >
2022-03-17 03:54:34 +03:00
< / div >
) ;
if ( ! errors ? . length ) {
return noData ;
}
2022-03-17 08:49:38 +03:00
if ( ! medianLatency ? . length ) {
2022-03-17 03:54:34 +03:00
return noData ;
}
2022-03-17 08:49:38 +03:00
if ( ! medianSegmentDownloadDurations ? . length ) {
2022-03-17 03:54:34 +03:00
return noData ;
}
const errorChart = [
{
name : 'Errors' ,
color : '#B63FFF' ,
data : errors ,
2023-02-03 01:34:44 +03:00
pointStyle : 'crossRot' ,
pointRadius : 7 ,
2022-03-17 03:54:34 +03:00
} ,
{
name : 'Quality changes' ,
color : '#2087E2' ,
data : qualityVariantChanges ,
} ,
] ;
const latencyChart = [
{
2022-03-17 08:49:38 +03:00
name : 'Median stream latency' ,
color : '#00FFFF' ,
data : medianLatency ,
} ,
{
name : 'Lowest stream latency' ,
color : '#02FD0D' ,
data : lowestLatency ,
} ,
{
name : 'Highest stream latency' ,
2022-03-17 03:54:34 +03:00
color : '#B63FFF' ,
2022-03-17 08:49:38 +03:00
data : highestLatency ,
2022-03-17 03:54:34 +03:00
} ,
] ;
const segmentDownloadDurationChart = [
2022-03-17 08:49:38 +03:00
{
name : 'Max download duration' ,
2022-03-17 03:54:34 +03:00
color : '#B63FFF' ,
options : { radius : 2 } ,
2022-03-17 08:49:38 +03:00
data : maximumSegmentDownloadDurations ,
} ,
2022-03-25 02:44:34 +03:00
{
name : 'Median download duration' ,
color : '#00FFFF' ,
options : { radius : 2 } ,
data : medianSegmentDownloadDurations ,
} ,
2022-03-17 08:49:38 +03:00
{
name : 'Min download duration' ,
color : '#02FD0D' ,
options : { radius : 2 } ,
data : minimumSegmentDownloadDurations ,
2022-03-17 03:54:34 +03:00
} ,
{
name : ` Approximate limit ` ,
color : '#003FFF' ,
2022-03-17 08:49:38 +03:00
data : medianSegmentDownloadDurations.map ( item = > ( {
2022-03-17 03:54:34 +03:00
time : item.time ,
value : segmentLength ,
} ) ) ,
2023-02-03 01:34:44 +03:00
pointStyle : 'dash' as const ,
2022-03-17 03:54:34 +03:00
options : { radius : 0 } ,
} ,
] ;
const bitrateChart = [
{
2022-04-25 02:45:35 +03:00
name : 'Lowest player speed' ,
2022-03-17 03:54:34 +03:00
color : '#B63FFF' ,
data : minimumPlayerBitrate ,
options : { radius : 2 } ,
} ,
2022-03-17 08:49:38 +03:00
{
2022-04-25 02:45:35 +03:00
name : 'Median player speed' ,
2022-03-17 08:49:38 +03:00
color : '#00FFFF' ,
data : medianPlayerBitrate ,
options : { radius : 2 } ,
} ,
{
2022-04-25 02:45:35 +03:00
name : 'Maximum player speed' ,
2022-03-17 08:49:38 +03:00
color : '#02FD0D' ,
data : maximumPlayerBitrate ,
options : { radius : 2 } ,
} ,
2022-03-17 03:54:34 +03:00
] ;
availableBitrates . forEach ( bitrate = > {
bitrateChart . push ( {
2022-03-25 02:44:34 +03:00
name : ` Available bitrate ` ,
2022-03-17 03:54:34 +03:00
color : '#003FFF' ,
data : minimumPlayerBitrate.map ( item = > ( {
time : item.time ,
2022-03-25 02:44:34 +03:00
value : bitrate ,
2022-03-17 03:54:34 +03:00
} ) ) ,
options : { radius : 0 } ,
} ) ;
} ) ;
const currentSpeed = bitrateChart [ 0 ] ? . data [ bitrateChart [ 0 ] . data . length - 1 ] ? . value ;
const currentDownloadSeconds =
2022-03-17 08:49:38 +03:00
medianSegmentDownloadDurations [ medianSegmentDownloadDurations . length - 1 ] ? . value ;
2022-12-20 10:19:21 +03:00
const lowestVariant = availableBitrates . reduce ( ( bitrate1 , bitrate2 ) = >
bitrate1 . valueOf ( ) < bitrate2 . valueOf ( ) ? bitrate1 : bitrate2 ,
) ;
2022-03-17 08:49:38 +03:00
const latencyMedian = medianLatency [ medianLatency . length - 1 ] ? . value || 0 ;
const latencyMax = highestLatency [ highestLatency . length - 1 ] ? . value || 0 ;
const latencyMin = lowestLatency [ lowestLatency . length - 1 ] ? . value || 0 ;
const latencyStat = ( Number ( latencyMax ) + Number ( latencyMin ) + Number ( latencyMedian ) ) / 3 ;
2022-03-17 03:54:34 +03:00
let recentErrorCount = 0 ;
const errorValueCount = errorChart [ 0 ] ? . data . length || 0 ;
if ( errorValueCount > 5 ) {
const values = errorChart [ 0 ] . data . slice ( - 5 ) ;
recentErrorCount = values . reduce ( ( acc , curr ) = > acc + Number ( curr . value ) , 0 ) ;
} else {
recentErrorCount = errorChart [ 0 ] . data . reduce ( ( acc , curr ) = > acc + Number ( curr . value ) , 0 ) ;
}
const showStats = currentSpeed > 0 || currentDownloadSeconds > 0 || recentErrorCount > 0 ;
let bitrateError = null ;
let speedError = null ;
if ( currentSpeed !== 0 && currentSpeed < lowestVariant ) {
2022-03-25 02:44:34 +03:00
bitrateError = ` One of your viewers is playing your stream at ${ currentSpeed } kbps, slower than ${ lowestVariant } kbps, the lowest quality you made available. Consider adding a lower quality with a lower bitrate if the errors over time warrant this. ` ;
2022-03-17 03:54:34 +03:00
}
if ( currentDownloadSeconds > segmentLength ) {
speedError =
2022-03-25 02:44:34 +03:00
'Your viewers may be consuming your video slower than required. This may be due to slow networks or your latency configuration. You need to decrease the amount of time viewers are taking to consume your video. Consider adding a lower quality with a lower bitrate or experiment with increasing the latency buffer setting.' ;
2022-03-17 03:54:34 +03:00
}
2022-12-16 09:00:15 +03:00
const errorStatColor = recentErrorCount > 0 ? '#B63FFF' : 'unset' ;
2022-03-17 03:54:34 +03:00
const statStyle = {
display : 'flex' ,
alignItems : 'center' ,
justifyContent : 'center' ,
height : '80px' ,
} ;
return (
< >
< Typography.Title > Stream Performance < / Typography.Title >
< Typography.Paragraph >
This tool hopes to help you identify and troubleshoot problems you may be experiencing with
your stream . It aims to aggregate experiences across your viewers , meaning one viewer with
an exceptionally bad experience may throw off numbers for the whole , especially with a low
number of viewers .
< / Typography.Paragraph >
< Typography.Paragraph >
The data is only collected by those using the Owncast web interface and is unable to gain
insight into external players people may be using such as VLC , MPV , QuickTime , etc .
< / Typography.Paragraph >
< Space direction = "vertical" size = "middle" >
2022-04-10 09:04:52 +03:00
< Row justify = "space-around" >
< Col style = { { width : '100%' } } >
< Card type = "inner" >
< StreamHealthOverview showTroubleshootButton = { false } / >
< / Card >
< / Col >
< / Row >
2022-03-17 03:54:34 +03:00
< Row
gutter = { [ 16 , 16 ] }
justify = "space-around"
style = { { display : showStats ? 'flex' : 'none' } }
>
< Col >
< Card type = "inner" >
< div style = { statStyle } >
< Statistic
2022-03-17 08:49:38 +03:00
title = "Viewer Playback Speed"
2022-03-17 03:54:34 +03:00
value = { ` ${ currentSpeed } ` }
prefix = { < WifiOutlined style = { { marginRight : '5px' } } / > }
precision = { 0 }
suffix = "kbps"
/ >
< / div >
< / Card >
< / Col >
< Col >
< Card type = "inner" >
< div style = { statStyle } >
< Statistic
2022-03-17 08:49:38 +03:00
title = "Viewer Latency"
2022-03-17 03:54:34 +03:00
value = { ` ${ latencyStat } ` }
prefix = { < ClockCircleOutlined style = { { marginRight : '5px' } } / > }
precision = { 0 }
suffix = "seconds"
/ >
< / div >
< / Card >
< / Col >
< Col >
< Card type = "inner" >
< div style = { statStyle } >
< Statistic
title = "Recent Playback Errors"
value = { ` ${ recentErrorCount || 0 } ` }
valueStyle = { { color : errorStatColor } }
prefix = { < WarningOutlined style = { { marginRight : '5px' } } / > }
suffix = ""
2022-04-10 09:04:52 +03:00
/ >
2022-03-17 03:54:34 +03:00
< / div >
< / Card >
< / Col >
< / Row >
< Card >
< DescriptionBox
title = "Video Segment Download"
description = {
< >
< Typography.Paragraph >
Once a video segment takes too long to download a viewer will experience
buffering . If you see slow downloads you should offer a lower quality for your
viewers , or find other ways , possibly an external storage provider , a CDN or a
faster network , to improve your stream quality . Increasing your latency buffer can
also help for some viewers .
< / Typography.Paragraph >
< Typography.Paragraph >
In short , once the pink line consistently gets near the blue line , your stream is
likely experiencing problems for viewers .
< / Typography.Paragraph >
< / >
}
/ >
{ speedError && (
< Alert message = "Slow downloads" description = { speedError } type = "error" showIcon / >
) }
< Chart
title = "Seconds"
dataCollections = { segmentDownloadDurationChart }
color = "#FF7700"
2023-02-03 01:34:44 +03:00
unit = "seconds"
2022-03-17 03:54:34 +03:00
yLogarithmic
/ >
< / Card >
< Card >
< DescriptionBox
title = "Player Network Speed"
description = {
< >
< Typography.Paragraph >
2022-03-17 08:49:38 +03:00
The playback bitrate of your viewers . Once somebody ' s bitrate drops below the
lowest video variant bitrate they will experience buffering . If you see viewers
with slow connections trying to play your video you should consider offering an
additional , lower quality .
2022-03-17 03:54:34 +03:00
< / Typography.Paragraph >
< Typography.Paragraph >
In short , once the pink line gets near the lowest blue line , your stream is likely
experiencing problems for at least one of your viewers .
< / Typography.Paragraph >
< / >
}
/ >
{ bitrateError && (
< Alert
message = "Low bandwidth viewers"
description = { bitrateError }
type = "error"
showIcon
/ >
) }
< Chart
title = "Lowest Player Bitrate"
dataCollections = { bitrateChart }
color = "#FF7700"
unit = "kbps"
yLogarithmic
/ >
< / Card >
< Card >
< DescriptionBox
title = "Errors and Quality Changes"
description = {
< >
< Typography.Paragraph >
Recent number of errors , including buffering , and quality changes from across all
your viewers . Errors can occur for many reasons , including browser issues ,
plugins , wifi problems , and they don ' t all represent fatal issues or something you
have control over .
< / Typography.Paragraph >
A quality change is not necessarily a negative thing , but if it ' s excessive and
coinciding with errors you should consider adding another quality variant .
< Typography.Paragraph / >
< / >
}
/ >
< Chart title = "#" dataCollections = { errorChart } color = "#FF7700" unit = "" / >
< / Card >
< Card >
< DescriptionBox
2022-03-17 08:49:38 +03:00
title = "Viewer Latency"
2022-04-25 02:45:35 +03:00
description = "An approximate number of seconds that your viewers are behind your live video. The largest cause of latency spikes is buffering. High latency itself is not a problem, and optimizing for low latency can result in buffering, resulting in even higher latency."
2022-03-17 03:54:34 +03:00
/ >
2023-02-03 01:34:44 +03:00
< Chart title = "Seconds" dataCollections = { latencyChart } color = "#FF7700" unit = "seconds" / >
2022-03-17 03:54:34 +03:00
< / Card >
< / Space >
< / >
) ;
2022-09-07 10:00:28 +03:00
} ;
2023-01-16 10:11:44 +03:00
StreamHealth . getLayout = function getLayout ( page : ReactElement ) {
return < AdminLayout page = { page } / > ;
} ;
2022-09-07 10:00:28 +03:00
export default StreamHealth ;