icytv/src/dashboard.js

58 lines
1.5 KiB
JavaScript

import $ from 'jquery'
// https://stackoverflow.com/a/18650828
function formatBytes (a, b) {
if (a === 0) return '0 Bytes'
let c = 1024
let d = b || 2
let e = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
let f = Math.floor(Math.log(a) / Math.log(c))
return parseFloat((a / Math.pow(c, f)).toFixed(d)) + ' ' + e[f]
}
function recursiveStats (table, subtable) {
for (let key in table) {
let val = table[key]
if (typeof val === 'object') {
recursiveStats(val, key)
} else {
if (key === 'time') {
let date = new Date(null)
date.setSeconds(Math.floor(parseInt(val) / 1000)) // specify value for SECONDS here
val = date.toISOString().substr(11, 8)
} else if (key.indexOf('bytes') !== -1) {
val = formatBytes(val, 3)
}
$('#stat_' + (subtable ? subtable + '_' : '') + key).text(val)
}
}
}
function dashboard (k) {
$.get('/dashboard/data', function (res) {
if (res.error) {
window.location.href = '/'
return
}
let fullURL = window.location.origin + '/watch/' + res.name
$('#myStream').attr('src', fullURL)
$('#stream_url').text(fullURL).attr('href', fullURL)
$('#stream_live').text(res.live ? 'Yes' : 'No')
})
$('#show_key').click(function (e) {
e.preventDefault()
$('#show_key').html(k)
})
setInterval(function () {
$.get('/dashboard/stats', function (res) {
if (res.error) return
recursiveStats(res, '')
})
}, 5000)
}
export default dashboard