diff --git a/svelte/src/admin_panel/Home.svelte b/svelte/src/admin_panel/Home.svelte index 71c3d15..de3ab03 100644 --- a/svelte/src/admin_panel/Home.svelte +++ b/svelte/src/admin_panel/Home.svelte @@ -39,11 +39,12 @@ const loadGraph = (minutes, interval, live) => { dateStr += ":" + ("00" + date.getMinutes()).slice(-2); resp.views.timestamps[idx] = " " + dateStr + " "; // Poor man's padding }); - graphViews.chart().data.labels = resp.views.timestamps; - graphViews.chart().data.datasets[0].data = resp.views.amounts; - graphBandwidth.chart().data.labels = resp.views.timestamps; - graphBandwidth.chart().data.datasets[0].data = resp.bandwidth.amounts - graphBandwidth.chart().data.datasets[1].data = resp.bandwidth_paid.amounts + graphViews.data().labels = resp.views.timestamps; + graphViews.data().datasets[0].data = resp.views.amounts; + graphViews.data().datasets[1].data = resp.downloads.amounts; + graphBandwidth.data().labels = resp.views.timestamps; + graphBandwidth.data().datasets[0].data = resp.bandwidth.amounts + graphBandwidth.data().datasets[1].data = resp.bandwidth_paid.amounts graphViews.update() graphBandwidth.update() @@ -107,7 +108,7 @@ function getStats(order) { let statsInterval = null onMount(() => { // Prepare chart datasets - graphBandwidth.chart().data.datasets = [ + graphBandwidth.data().datasets = [ { label: "Bandwidth (free)", borderWidth: 2, @@ -123,7 +124,7 @@ onMount(() => { backgroundColor: "#"+window.style.dangerColor, }, ]; - graphViews.chart().data.datasets = [ + graphViews.data().datasets = [ { label: "Views", borderWidth: 2, @@ -131,6 +132,13 @@ onMount(() => { borderColor: "#"+window.style.highlightColor, backgroundColor: "#"+window.style.highlightColor, }, + { + label: "Downloads", + borderWidth: 2, + pointRadius: 0, + borderColor: "#"+window.style.dangerColor, + backgroundColor: "#"+window.style.dangerColor, + }, ]; loadGraph(10080, 10, false); @@ -163,8 +171,8 @@ onDestroy(() => { - - + +
Total usage from {start_time} to {end_time}
{formatDataVolume(total_bandwidth, 3)} bandwidth, diff --git a/svelte/src/file_viewer/DetailsWindow.svelte b/svelte/src/file_viewer/DetailsWindow.svelte index 78548f6..ac2547c 100644 --- a/svelte/src/file_viewer/DetailsWindow.svelte +++ b/svelte/src/file_viewer/DetailsWindow.svelte @@ -1,7 +1,8 @@
@@ -126,10 +151,42 @@ let update_charts = () => { {/if} -

Downloads

- -

Views

- +

Views and downloads

+ +
+ + + + + + +
+ +

Charts rendered by the amazing Chart.js. @@ -155,3 +212,11 @@ let update_charts = () => { SHIFT + s = Download all the files in the list as a zip archive

+ + diff --git a/svelte/src/user_home/Home.svelte b/svelte/src/user_home/Home.svelte index bc0fd7b..5b74ac3 100644 --- a/svelte/src/user_home/Home.svelte +++ b/svelte/src/user_home/Home.svelte @@ -6,10 +6,8 @@ import StorageProgressBar from "./StorageProgressBar.svelte"; import HotlinkProgressBar from "./HotlinkProgressBar.svelte"; import Euro from "../util/Euro.svelte" -let graph_view = null -let graph_download = null +let graph_views_downloads = null let graph_bandwidth = null -let graph_transfer_paid = null let time_start = "" let time_end = "" let total_views = 0 @@ -17,49 +15,73 @@ let total_downloads = 0 let total_bandwidth = 0 let total_transfer_paid = 0 -let load_graph = (graph, stat, minutes, interval) => { - let today = new Date() +let load_graphs = async (minutes, interval) => { + let end = new Date() let start = new Date() start.setMinutes(start.getMinutes() - minutes) - fetch( + try { + let views = get_graph_data("views", start, end, interval); + let downloads = get_graph_data("downloads", start, end, interval); + let bandwidth = get_graph_data("bandwidth", start, end, interval); + let transfer_paid = get_graph_data("transfer_paid", start, end, interval); + views = await views + downloads = await downloads + bandwidth = await bandwidth + transfer_paid = await transfer_paid + + graph_views_downloads.data().labels = views.timestamps; + graph_views_downloads.data().datasets[0].data = views.amounts + graph_views_downloads.data().datasets[1].data = downloads.amounts + graph_bandwidth.data().labels = bandwidth.timestamps; + graph_bandwidth.data().datasets[0].data = bandwidth.amounts + graph_bandwidth.data().datasets[1].data = transfer_paid.amounts + + graph_views_downloads.update() + graph_bandwidth.update() + + time_start = views.timestamps[0]; + time_end = views.timestamps.slice(-1)[0]; + } catch (err) { + console.error("Failed to update graphs", err) + return + } +} + +let get_graph_data = async (stat, start, end, interval) => { + let resp = await fetch( window.api_endpoint + "/user/time_series/" + stat + "?start=" + start.toISOString() + - "&end=" + today.toISOString() + + "&end=" + end.toISOString() + "&interval=" + interval - ).then(resp => { - if (!resp.ok) { return Promise.reject("Error: " + resp.status); } - return resp.json(); - }).then(resp => { - resp.timestamps.forEach((val, idx) => { - let date = new Date(val); - 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 - }); - graph.chart().data.labels = resp.timestamps; - graph.chart().data.datasets[0].data = resp.amounts; - graph.chart().update(); + ) + resp = await resp.json() - time_start = resp.timestamps[0]; - time_end = resp.timestamps.slice(-1)[0]; + // Convert the timestamps to a human-friendly format + resp.timestamps.forEach((val, idx) => { + let date = new Date(val); + let str = ("00" + (date.getMonth() + 1)).slice(-2); + str += "-" + ("00" + date.getDate()).slice(-2); + str += " " + ("00" + date.getHours()).slice(-2); + str += ":" + ("00" + date.getMinutes()).slice(-2); + resp.timestamps[idx] = " " + str + " "; // Poor man's padding + }); - let total = resp.amounts.reduce((acc, cur) => { return acc + cur }, 0) + // Add up the total amount and save it in the correct place + let total = resp.amounts.reduce((acc, cur) => { return acc + cur }, 0) - if (stat == "views") { - total_views = total; - } else if (stat == "downloads") { - total_downloads = total; - } else if (stat == "bandwidth") { - total_bandwidth = total; - } else if (stat == "transfer_paid") { - total_transfer_paid = total; - } - }).catch(e => { - console.error("Error requesting time series: " + e); - }) + if (stat == "views") { + total_views = total; + } else if (stat == "downloads") { + total_downloads = total; + graph_views_downloads.update() + } else if (stat == "bandwidth") { + total_bandwidth = total; + } else if (stat == "transfer_paid") { + total_transfer_paid = total; + } + + return resp } let graph_timeout = null @@ -72,10 +94,7 @@ let update_graphs = (minutes, interval, live) => { graph_timespan = minutes - load_graph(graph_view, "views", minutes, interval) - load_graph(graph_download, "downloads", minutes, interval) - load_graph(graph_bandwidth, "bandwidth", minutes, interval) - load_graph(graph_transfer_paid, "transfer_paid", minutes, interval) + load_graphs(minutes, interval) load_direct_bw() } @@ -113,6 +132,39 @@ onMount(() => { transfer_cap = -1 } + graph_views_downloads.data().datasets = [ + { + label: "Views", + borderWidth: 2, + pointRadius: 0, + borderColor: "#"+window.style.highlightColor, + backgroundColor: "#"+window.style.highlightColor, + }, + { + label: "Downloads", + borderWidth: 2, + pointRadius: 0, + borderColor: "#"+window.style.dangerColor, + backgroundColor: "#"+window.style.dangerColor, + }, + ]; + graph_bandwidth.data().datasets = [ + { + label: "Bandwidth (total)", + borderWidth: 2, + pointRadius: 0, + borderColor: "#"+window.style.highlightColor, + backgroundColor: "#"+window.style.highlightColor, + }, + { + label: "Bandwidth (premium)", + borderWidth: 2, + pointRadius: 0, + borderColor: "#"+window.style.dangerColor, + backgroundColor: "#"+window.style.dangerColor, + }, + ]; + update_graphs(1440, 1, true); }) onDestroy(() => { @@ -235,43 +287,31 @@ onDestroy(() => { {formatDataVolume(total_transfer_paid, 3)} paid transfers
-

Paid transfers

+

Premium transfers and total bandwidth usage

- A paid transfer is when a file is downloaded using the data cap on - your subscription plan. These can be files you downloaded from other - people, or other people downloading your files if you have bandwidth - sharing enabled. Bandwidth sharing can be changed on + A premium transfer is when a file is downloaded using the data cap + on your subscription plan. These can be files you downloaded from + other people, or other people downloading your files if you have + bandwidth sharing enabled. Bandwidth sharing can be changed on the subscription page.

-
- -
-

Views

- A view is counted when someone visits the download page of one - of your files. Views are unique per user per file. + Total bandwidth usage is the combined bandwidth usage of all the + files on your account. This includes paid transfers.

- +
-

Downloads

+

Views and downloads

- Downloads are counted when a user clicks the download button - on one of your files. It does not matter whether the - download is completed or not, only the start of the download - is counted. + A view is counted when someone visits the download page of one of + your files. Views are unique per user per file. +

+

+ Downloads are counted when a user clicks the download button on one + of your files. It does not matter whether the download is completed + or not, only the start of the download is counted.

- -
-

Bandwidth

-

- This is how much bandwidth your files are using in total. - Bandwidth is used when a file is tranferred from a - pixeldrain server to a user who is downloading the file. - When a 5 MB file is downloaded 8 times it has used 40 MB of - bandwidth. -

-
- + diff --git a/svelte/src/util/Chart.svelte b/svelte/src/util/Chart.svelte index 4586ef1..46f5206 100644 --- a/svelte/src/util/Chart.svelte +++ b/svelte/src/util/Chart.svelte @@ -5,12 +5,14 @@ import { Chart, PointElement, LineElement, LinearScale, CategoryScale, LineContr let chart_element let chart_object -export let label = "label" -export let dataType = "" +export let data_type = "" export const chart = () => { return chart_object } +export const data = () => { + return chart_object.data +} export const update = () => { return chart_object.update() } @@ -32,15 +34,8 @@ onMount(() => { { type: 'line', data: { - datasets: [ - { - label: label, - borderWidth: 0, - pointRadius: 0, - fill: 'origin', - backgroundColor: "#"+window.style.highlightColor, - } - ] + labels: [], + datasets: [], }, options: { legend: { display: false }, @@ -57,7 +52,7 @@ onMount(() => { position: "left", ticks: { callback: function (value, index, values) { - if (dataType == "bytes") { + if (data_type == "bytes") { return formatDataVolume(value, 3); } return formatNumber(value, 3); @@ -88,7 +83,6 @@ onMount(() => { } ); }) -