2021-08-10 19:17:54 +02:00
function loadGraph ( graph , stat , minutes , interval ) {
2020-08-11 19:52:03 +02:00
let today = new Date ( )
let start = new Date ( )
2021-08-10 19:17:54 +02:00
start . setMinutes ( start . getMinutes ( ) - minutes )
2020-08-11 19:52:03 +02:00
fetch (
2021-08-10 19:17:54 +02:00
apiEndpoint + "/user/time_series/" + stat +
"?start=" + start . toISOString ( ) +
"&end=" + today . toISOString ( ) +
"&interval=" + interval
2020-08-11 19:52:03 +02:00
) . then ( resp => {
2021-08-10 19:17:54 +02:00
if ( ! resp . ok ) { return Promise . reject ( "Error: " + resp . status ) ; }
2020-08-11 19:52:03 +02:00
return resp . json ( ) ;
} ) . then ( resp => {
resp . timestamps . forEach ( ( val , idx ) => {
let date = new Date ( val ) ;
2021-08-10 19:17:54 +02:00
let dateStr = ( "00" + ( date . getMonth ( ) + 1 ) ) . slice ( - 2 ) ;
dateStr += "-" + ( "00" + date . getDate ( ) ) . slice ( - 2 ) ;
dateStr += " " + ( "00" + date . getHours ( ) ) . slice ( - 2 ) ;
dateStr += ":" + ( "00" + date . getMinutes ( ) ) . slice ( - 2 ) ;
resp . timestamps [ idx ] = " " + dateStr + " " ; // Poor man's padding
2020-08-11 19:52:03 +02:00
} ) ;
graph . data . labels = resp . timestamps ;
graph . data . datasets [ 0 ] . data = resp . amounts ;
graph . update ( ) ;
document . getElementById ( "time_start" ) . innerText = resp . timestamps [ 0 ] ;
document . getElementById ( "time_end" ) . innerText = resp . timestamps . slice ( - 1 ) [ 0 ] ;
let total = 0
resp . amounts . forEach ( e => { total += e ; } ) ;
if ( stat == "views" ) {
document . getElementById ( "total_views" ) . innerText = formatThousands ( total ) ;
} else if ( stat == "downloads" ) {
document . getElementById ( "total_downloads" ) . innerText = formatThousands ( total ) ;
} else if ( stat == "bandwidth" ) {
document . getElementById ( "total_bandwidth" ) . innerText = formatDataVolume ( total , 3 ) ;
2021-08-10 19:17:54 +02:00
} else if ( stat == "direct_bandwidth" ) {
document . getElementById ( "total_direct_bandwidth" ) . innerText = formatDataVolume ( total , 3 ) ;
2020-08-11 19:52:03 +02:00
}
} ) . catch ( e => {
2021-08-10 19:17:54 +02:00
console . error ( "Error requesting time series: " + e ) ;
} )
}
function loadDirectBW ( ) {
let today = new Date ( )
let start = new Date ( )
start . setDate ( start . getDate ( ) - 30 )
fetch (
apiEndpoint + "/user/time_series/direct_bandwidth" +
"?start=" + start . toISOString ( ) +
"&end=" + today . toISOString ( ) +
"&interval=60"
) . then ( resp => {
if ( ! resp . ok ) { return Promise . reject ( "Error: " + resp . status ) ; }
return resp . json ( ) ;
} ) . then ( resp => {
let total = resp . amounts . reduce ( ( accum , val ) => accum += val , 0 ) ;
2021-09-07 17:09:52 +02:00
document . getElementById ( "direct_bandwidth_progress" ) . style . width = ( total / window . user . subscription . direct _linking _bandwidth ) * 100 + "%"
document . getElementById ( "direct_bandwidth_text" ) . innerText = formatDataVolume ( total , 3 ) + " out of " + formatDataVolume ( window . user . subscription . direct _linking _bandwidth , 3 )
document . getElementById ( "storage_progress" ) . style . width = ( window . user . storage _space _used / window . user . subscription . storage _space ) * 100 + "%"
document . getElementById ( "storage_text" ) . innerText = formatDataVolume ( window . user . storage _space _used , 3 ) + " out of " + formatDataVolume ( window . user . subscription . storage _space , 3 )
2021-08-10 19:17:54 +02:00
} ) . catch ( e => {
console . error ( "Error requesting time series: " + e ) ;
2020-08-11 19:52:03 +02:00
} )
}
let graphViews = drawGraph ( document . getElementById ( "views_chart" ) , "Views" , "number" ) ;
let graphDownloads = drawGraph ( document . getElementById ( "downloads_chart" ) , "Downloads" , "number" ) ;
let graphBandwidth = drawGraph ( document . getElementById ( "bandwidth_chart" ) , "Bandwidth" , "bytes" ) ;
2021-08-10 19:17:54 +02:00
let graphDirectBandwidth = drawGraph ( document . getElementById ( "direct_bandwidth_chart" ) , "Direct Bandwidth" , "bytes" ) ;
2020-08-11 19:52:03 +02:00
let graphTimeout = null ;
function updateGraphs ( minutes , interval , live ) {
if ( graphTimeout !== null ) { clearTimeout ( graphTimeout ) }
if ( live ) {
2021-08-10 19:17:54 +02:00
graphTimeout = setTimeout ( ( ) => { updateGraphs ( minutes , interval , true ) } , 10000 )
2020-08-11 19:52:03 +02:00
}
loadGraph ( graphViews , "views" , minutes , interval ) ;
loadGraph ( graphDownloads , "downloads" , minutes , interval ) ;
loadGraph ( graphBandwidth , "bandwidth" , minutes , interval ) ;
2021-08-10 19:17:54 +02:00
loadGraph ( graphDirectBandwidth , "direct_bandwidth" , minutes , interval ) ;
loadDirectBW ( )
2020-08-11 19:52:03 +02:00
}
// Default
2021-08-10 19:17:54 +02:00
updateGraphs ( 1440 , 1 , true ) ;