1
0
mirror of https://github.com/Foltik/Shimapan synced 2024-11-15 09:08:06 -05:00
shimapan/app/public/panel/controllers/DashCtrl.js

65 lines
2.6 KiB
JavaScript
Raw Normal View History

2018-12-31 18:37:34 -05:00
const angular = require('angular');
2018-09-15 18:26:01 -04:00
angular.module('DashCtrl', ['chart.js', 'StatSvc']).controller('DashController', ['$scope', 'StatService', async ($scope, StatService) => {
2018-08-15 11:23:30 -04:00
const colorScheme = ['#2a9fd6'];
2018-09-15 18:26:01 -04:00
// Calculate and format descending dates back one week to use for x-axis labels
2018-09-15 14:00:20 -04:00
const currentDate = new Date();
const oneDay = 1000 * 60 * 60 * 24;
2018-09-15 18:26:01 -04:00
let labels = Array.from({length: 7}, (date, i) =>
(new Date(currentDate - oneDay * (6 - i))).toISOString().substr(5, 5));
2018-09-15 22:25:49 -04:00
const toHumanReadable = bytes => {
2018-12-31 18:37:34 -05:00
const units = ['B', 'KiB', 'MiB', 'GiB', 'TiB'];
2018-09-15 22:25:49 -04:00
let index = 0;
2018-12-31 18:37:34 -05:00
for(; bytes >= 1024 && index < units.length - 1; index++)
bytes /= 1024;
2018-09-15 22:25:49 -04:00
return bytes.toFixed(3) + ' ' + units[index];
};
2018-12-31 18:37:34 -05:00
// Get stats for the week
$scope.statWeekUploads = 0;
$scope.statWeekUploadSize = 0;
$scope.statWeekViews = 0;
2018-09-15 18:26:01 -04:00
2018-12-31 18:37:34 -05:00
const weekStats = await StatService.getWeek();
console.log(weekStats);
2018-09-15 18:26:01 -04:00
2018-12-31 18:37:34 -05:00
$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)];
2018-09-15 18:26:01 -04:00
2018-12-31 18:37:34 -05:00
$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);
// 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;
2018-08-15 11:23:30 -04:00
$scope.uploadColors = colorScheme;
2018-09-15 14:00:20 -04:00
$scope.uploadLabels = labels;
$scope.uploadSeries = ['Uploads'];
2018-09-15 18:26:01 -04:00
//$scope.uploadData = [[5, 11, 4, 3, 7, 9, 21]];
$scope.uploadOptions = {
2018-08-15 11:23:30 -04:00
title: {
display: true,
text: 'Historical Uploads'
},
};
2018-08-15 11:23:30 -04:00
$scope.viewColors = colorScheme;
2018-09-15 14:00:20 -04:00
$scope.viewLabels = labels;
2018-08-15 11:23:30 -04:00
$scope.viewSeries = ['Views'];
2018-09-15 18:26:01 -04:00
//$scope.viewData = [[5, 11, 4, 3, 7, 9, 21]];
2018-08-15 11:23:30 -04:00
$scope.viewOptions = {
title: {
display: true,
text: 'Historical Views'
}
};
2018-09-15 18:26:01 -04:00
}]);