diff --git a/app/models/View.js b/app/models/View.js index 79622d0..61cb9ab 100644 --- a/app/models/View.js +++ b/app/models/View.js @@ -3,7 +3,6 @@ const mongoose = require('mongoose'); const ViewSchema = mongoose.Schema({ uid: { type: String, - unique: true, required: true }, diff --git a/app/public/panel/controllers/DashCtrl.js b/app/public/panel/controllers/DashCtrl.js index 8920d73..8656b27 100644 --- a/app/public/panel/controllers/DashCtrl.js +++ b/app/public/panel/controllers/DashCtrl.js @@ -8,20 +8,27 @@ angular.module('DashCtrl', ['chart.js', 'StatSvc']).controller('DashController', let labels = Array.from({length: 7}, (date, i) => (new Date(currentDate - oneDay * (6 - i))).toISOString().substr(5, 5)); + const toHumanReadable = bytes => { + const units = ['B', 'KB', 'MB', 'GB', 'TB']; + let index = 0; + for (; bytes >= 1024 && index < units.length - 1; index++, bytes /= 1024) {} + return bytes.toFixed(3) + ' ' + units[index]; + }; + StatService.getWeek((err, stats) => { - $scope.uploadData = [Array.from({length: 7}, (val, i) => stats[labels[i]] ? stats[labels[i]].uploads : 0)]; - $scope.viewData = [Array.from({length: 7}, (val, i) => stats[labels[i]] ? stats[labels[i]].views : 0)]; + $scope.uploadData = [Array.from({length: 7}, (val, i) => stats[labels[i]] && stats[labels[i]].uploads ? stats[labels[i]].uploads : 0)]; + $scope.viewData = [Array.from({length: 7}, (val, i) => stats[labels[i]] && stats[labels[i]].views ? stats[labels[i]].views : 0)]; $scope.stats = stats; - $scope.statUploads = Object.keys(stats).reduce((acc, key) => acc + stats[key].uploads); - $scope.statUploadSize = Object.keys(stats).reduce((acc, key) => acc + stats[key].size); - $scope.statViews = Object.keys(stats).reduce((acc, key) => acc + stats[key].views); + $scope.statUploads = Object.keys(stats).reduce((acc, key) => acc + (stats[key].uploads ? stats[key].uploads : 0), 0); + $scope.statUploadSize = toHumanReadable(Object.keys(stats).reduce((acc, key) => acc + (stats[key].size ? stats[key].size : 0), 0)); + $scope.statViews = Object.keys(stats).reduce((acc, key) => acc + (stats[key].views ? stats[key].views : 0), 0); }); StatService.getAll((err, stats) => { $scope.statTotalUploads = stats.count; - $scope.statTotalUploadSize = stats.size; + $scope.statTotalUploadSize = toHumanReadable(stats.size); $scope.statTotalViews = stats.views; }); diff --git a/app/routes/api/stats.js b/app/routes/api/stats.js index 9cdfccb..b11d5d4 100644 --- a/app/routes/api/stats.js +++ b/app/routes/api/stats.js @@ -28,14 +28,13 @@ function mergeAggregateStats(obj1, obj2) { } function mergeAggregations(res1, res2) { - const arr = res1; - arr.concat(res2); + const arr = res1.concat(res2); let res = {}; for (let obj of arr) { if (res[obj._id]) - mergeAggregateStats(res[obj._id], obj); + res[obj._id] = mergeAggregateStats(res[obj._id], obj); else res[obj._id] = filterAggregateStats(obj); }