// build stats for settings page 'use strict'; /* globals apiRequest */ function primaryStats() { let apiVideoEndpoint = '/api/stats/video/'; let responseData = apiRequest(apiVideoEndpoint, 'GET'); let primaryBox = document.getElementById('primaryBox'); clearLoading(primaryBox); let totalTile = buildTotalVideoTile(responseData); primaryBox.appendChild(totalTile); let videosTypeTile = buildVideosTypeTile(responseData); primaryBox.appendChild(videosTypeTile); let shortsTypeTile = buildShortsTypeTile(responseData); primaryBox.appendChild(shortsTypeTile); let streamsTypeTile = buildStreamsTypeTile(responseData); primaryBox.appendChild(streamsTypeTile); } function secondaryStats() { let apiChannelEndpoint = '/api/stats/channel/'; let channelResponseData = apiRequest(apiChannelEndpoint, 'GET'); let secondaryBox = document.getElementById('secondaryBox'); clearLoading(secondaryBox); let channelTile = buildChannelTile(channelResponseData); secondaryBox.appendChild(channelTile); let apiPlaylistEndpoint = '/api/stats/playlist/'; let playlistResponseData = apiRequest(apiPlaylistEndpoint, 'GET'); let playlistTile = buildPlaylistTile(playlistResponseData); secondaryBox.appendChild(playlistTile); let apiDownloadEndpoint = '/api/stats/download/'; let downloadResponseData = apiRequest(apiDownloadEndpoint, 'GET'); let downloadTile = buildDownloadTile(downloadResponseData); secondaryBox.appendChild(downloadTile); } function buildTotalVideoTile(responseData) { const totalCount = responseData.doc_count || 0; const totalSize = humanFileSize(responseData.media_size || 0); const content = { Items: `${totalCount}`, 'Media Size': `${totalSize}`, }; const tile = buildTile('All: '); const table = buildTileContenTable(content, 2); tile.appendChild(table); return tile; } function buildVideosTypeTile(responseData) { const videosCount = responseData.type_videos.doc_count || 0; const videosSize = humanFileSize(responseData.type_videos.media_size || 0); const content = { Items: `${videosCount}`, 'Media Size': `${videosSize}`, }; const tile = buildTile('Regular Videos: '); const table = buildTileContenTable(content, 2); tile.appendChild(table); return tile; } function buildShortsTypeTile(responseData) { const shortsCount = responseData.type_shorts.doc_count || 0; const shortsSize = humanFileSize(responseData.type_shorts.media_size || 0); const content = { Items: `${shortsCount}`, 'Media Size': `${shortsSize}`, }; const tile = buildTile('Shorts: '); const table = buildTileContenTable(content, 2); tile.appendChild(table); return tile; } function buildStreamsTypeTile(responseData) { const streamsCount = responseData.type_streams.doc_count || 0; const streamsSize = humanFileSize(responseData.type_streams.media_size || 0); const content = { Items: `${streamsCount}`, 'Media Size': `${streamsSize}`, }; const tile = buildTile('Streams: '); const table = buildTileContenTable(content, 2); tile.appendChild(table); return tile; } function buildChannelTile(responseData) { let tile = buildTile('Channels: '); const total = responseData.doc_count || 0; const subscribed = responseData.subscribed_true || 0; const active = responseData.active_true || 0; const content = { Subscribed: subscribed, Active: active, Total: total, }; const table = buildTileContenTable(content, 3); tile.appendChild(table); return tile; } function buildPlaylistTile(responseData) { let tile = buildTile('Playlists: '); const total = responseData.doc_count || 0; const subscribed = responseData.subscribed_true || 0; const active = responseData.active_true || 0; const content = { Subscribed: subscribed, Active: active, Total: total, }; const table = buildTileContenTable(content, 2); tile.appendChild(table); return tile; } function buildDownloadTile(responseData) { const pendingTotal = responseData.pending || 0; let tile = buildTile(`Downloads Pending: ${pendingTotal}`); const pendingVideos = responseData.pending_videos || 0; const pendingShorts = responseData.pending_shorts || 0; const pendingStreams = responseData.pending_streams || 0; const content = { Videos: pendingVideos, Shorts: pendingShorts, Streams: pendingStreams, }; const table = buildTileContenTable(content, 3); tile.appendChild(table); return tile; } function watchStats() { let apiEndpoint = '/api/stats/watch/'; let responseData = apiRequest(apiEndpoint, 'GET'); let watchBox = document.getElementById('watchBox'); clearLoading(watchBox); const { total, watched, unwatched } = responseData; let firstCard = buildWatchTile('total', total); watchBox.appendChild(firstCard); let secondCard = buildWatchTile('watched', watched); watchBox.appendChild(secondCard); let thirdCard = buildWatchTile('unwatched', unwatched); watchBox.appendChild(thirdCard); } function buildWatchTile(title, watchDetail) { const items = watchDetail?.items ?? 0; const duration = watchDetail?.duration ?? 0; const duration_str = watchDetail?.duration_str ?? '0s'; const hasProgess = !!watchDetail?.progress; const progress = (Number(watchDetail?.progress) * 100).toFixed(2) ?? '0'; let titleCapizalized = capitalizeFirstLetter(title); if (hasProgess) { titleCapizalized = `${progress}% ` + titleCapizalized; } let tile = buildTile(titleCapizalized); const content = { Videos: items, Seconds: duration, Playback: duration_str, }; const table = buildTileContenTable(content, 3); tile.appendChild(table); return tile; } function downloadHist() { let apiEndpoint = '/api/stats/downloadhist/'; let responseData = apiRequest(apiEndpoint, 'GET'); let histBox = document.getElementById('downHistBox'); clearLoading(histBox); if (responseData.length === 0) { let tile = buildTile('No recent downloads'); histBox.appendChild(tile); return; } for (let i = 0; i < responseData.length; i++) { const dailyStat = responseData[i]; let tile = buildDailyStat(dailyStat); histBox.appendChild(tile); } } function buildDailyStat(dailyStat) { let tile = buildTile(dailyStat.date); let message = document.createElement('p'); const isExactlyOne = dailyStat.count === 1; let text = 'Videos'; if (isExactlyOne) { text = 'Video'; } message.innerText = `+${dailyStat.count} ${text} ${humanFileSize(dailyStat.media_size)}`; tile.appendChild(message); return tile; } function buildChannelRow(id, name, value) { let tableRow = document.createElement('tr'); tableRow.innerHTML = `