diff --git a/app/public/panel/controllers/DashCtrl.js b/app/public/panel/controllers/DashCtrl.js index 5bb5434..433c063 100644 --- a/app/public/panel/controllers/DashCtrl.js +++ b/app/public/panel/controllers/DashCtrl.js @@ -1,13 +1,6 @@ const angular = require('angular'); angular.module('DashCtrl', ['chart.js', 'StatSvc']).controller('DashController', ['$scope', 'StatService', async ($scope, StatService) => { - const colorScheme = ['#2a9fd6']; - // Calculate and format descending dates back one week to use for x-axis labels - const currentDate = new Date(); - const oneDay = 1000 * 60 * 60 * 24; - let labels = Array.from({length: 7}, (date, i) => - (new Date(currentDate - oneDay * (6 - i))).toISOString().substr(5, 5)); - const toHumanReadable = bytes => { const units = ['B', 'KiB', 'MiB', 'GiB', 'TiB']; let index = 0; @@ -16,50 +9,32 @@ angular.module('DashCtrl', ['chart.js', 'StatSvc']).controller('DashController', return bytes.toFixed(3) + ' ' + units[index]; }; - // Get stats for the week - $scope.statWeekUploads = 0; - $scope.statWeekUploadSize = 0; - $scope.statWeekViews = 0; - - const weekStats = await StatService.getWeek(); - console.log(weekStats); - $scope.uploadData = [Array.from({length: 7}, (val, i) => weekStats[labels[i]] && weekStats[labels[i]].uploads ? weekStats[labels[i]].uploads : 0)]; - $scope.viewData = [Array.from({length: 7}, (val, i) => weekStats[labels[i]] && weekStats[labels[i]].views ? weekStats[labels[i]].views : 0)]; - - $scope.statWeekUploads = Object.keys(weekStats).reduce((acc, key) => acc + (weekStats[key].uploads ? weekStats[key].uploads : 0), 0); - $scope.statWeekUploadSize = toHumanReadable(Object.keys(weekStats).reduce((acc, key) => acc + (weekStats[key].size ? weekStats[key].size : 0), 0)); - $scope.statWeekViews = Object.keys(weekStats).reduce((acc, key) => acc + (weekStats[key].views ? weekStats[key].views : 0), 0); + const getWeekUploadStats = async () => { + const stats = await StatService.getUploadsWeek(); + console.log(stats); + $scope.statWeekUploads = stats.length; + $scope.statWeekUploadSize = toHumanReadable(stats.reduce((acc, stat) => acc + stat.size, 0)); + }; - // Get all-time stats - $scope.statTotalUploads = 0; - $scope.statTotalUploadSize = 0; - $scope.statTotalViews = 0; - const allStats = await StatService.getAll(); - console.log(allStats); - $scope.statTotalUploads = allStats.count; - $scope.statTotalUploadSize = toHumanReadable(allStats.size); - $scope.statTotalViews = allStats.views; + const getWeekViewStats = async () => { + const stats = await StatService.getViewsWeek(); + $scope.statWeekViews = stats.length; + }; - $scope.uploadColors = colorScheme; - $scope.uploadLabels = labels; - $scope.uploadSeries = ['Uploads']; - //$scope.uploadData = [[5, 11, 4, 3, 7, 9, 21]]; - $scope.uploadOptions = { - title: { - display: true, - text: 'Historical Uploads' - }, + const getAllUploadStats = async () => { + const stats = await StatService.getUploads(); + console.log(stats); + $scope.statTotalUploads = stats.length; + $scope.statTotalUploadSize = toHumanReadable(stats.reduce((acc, stat) => acc + stat.size, 0)); }; - $scope.viewColors = colorScheme; - $scope.viewLabels = labels; - $scope.viewSeries = ['Views']; - //$scope.viewData = [[5, 11, 4, 3, 7, 9, 21]]; - $scope.viewOptions = { - title: { - display: true, - text: 'Historical Views' - } + const getAllViewStats = async () => { + const stats = await StatService.getViews(); + $scope.statTotalViews = stats.length; }; + + + await Promise.all([getWeekUploadStats(), getWeekViewStats(), getAllUploadStats(), getAllViewStats()]); + $scope.$apply(); }]); \ No newline at end of file diff --git a/app/public/services/StatSvc.js b/app/public/services/StatSvc.js index 516c49b..9696698 100644 --- a/app/public/services/StatSvc.js +++ b/app/public/services/StatSvc.js @@ -1,21 +1,48 @@ const angular = require('angular'); angular.module('StatSvc', []).service('StatService', ['$http', function($http) { - this.getWeek = async () => { + this.getUploads = async () => { const res = await $http({ method: 'GET', - url: '/api/stats/week' + url: '/api/stats/uploads' }); return res.data; }; - this.getAll = async () => { + this.getUploadsWeek = async () => { + let oneWeekAgo = new Date(); + oneWeekAgo.setDate(oneWeekAgo.getDate() - 7); + const res = await $http({ method: 'GET', - url: '/api/stats/all' + url: '/api/stats/uploads', + data: { + after: oneWeekAgo + } }); return res.data; - } + }; + + this.getViews = async () => { + const res = await $http({ + method: 'GET', + url: '/api/stats/views' + }); + + return res.data; + }; + + this.getViewsWeek = async () => { + let oneWeekAgo = new Date(); + oneWeekAgo.setDate(oneWeekAgo.getDate() - 7); + + const res = await $http({ + method: 'GET', + url: '/api/stats/uploads' + }); + + return res.data; + }; }]); diff --git a/public/views/panel/dash.html b/public/views/panel/dash.html index 4006137..bffa7d8 100644 --- a/public/views/panel/dash.html +++ b/public/views/panel/dash.html @@ -1,20 +1,4 @@
-
-
- -
-
- -
-