blob: 2f949987856ee147b4daa898518da7ab005e3c13 [file] [log] [blame]
Matteo Scandoloe0e4de42016-06-08 16:15:30 -07001/*
Brian O'Connor8fb63ec2017-08-03 22:46:35 -07002 * Copyright 2015 Open Networking Foundation
Matteo Scandoloe0e4de42016-06-08 16:15:30 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17angular.module('mCord')
18 .directive('relatedStats', function () {
19 return {
20 restrict: 'E',
21 templateUrl: 'app/components/related-stats/related-stats.tpl.html',
22 scope: {
23 model: '=',
24 type: '@'
25 },
26 bindToController: true,
27 controllerAs: 'vm',
28 controller: function($scope, $filter, _){
29 $scope.$watch(() => this.model, enode => {
30 if(enode){
31 loadStats();
32 }
33 });
34
35 const loadStats = () => {
36 this.model.getStats()
37 .then((stats) => {
38 this.selectedStats = 'download_data';
Matteo Scandolo3b82a592016-06-21 12:10:35 +020039 if(angular.isDefined(stats.ProfileArray)){
40 this.stats = formatStats(stats.ProfileArray);
41 }
Matteo Scandoloe0e4de42016-06-08 16:15:30 -070042 });
43 };
44
45 const formatStats = stats => {
46 let series = _.reduce(stats, (list, s) => {
47 return list.concat([s.Profile]);
48 }, []);
49 let labels = _.reduce(stats[0].StatsArray, (list, s) => {
50 return list.concat([$filter('date')(new Date(s.Time * 1000), 'shortTime')]);
51 }, []);
52 const download_data = _.reduce(stats, (list, stat) => {
53 let bitrate = _.reduce(stat.StatsArray, (data, s) => {
54 return data.concat([s.DlBitrate]);
55 }, []);
56 list.push(bitrate);
57 return list;
58 }, []);
59 const upload_data = _.reduce(stats, (list, stat) => {
60 let bitrate = _.reduce(stat.StatsArray, (data, s) => {
61 return data.concat([s.UlBitrate]);
62 }, []);
63 list.push(bitrate);
64 return list;
65 }, []);
66 return {
67 labels: labels,
68 series: series,
69 data: download_data,
70 upload_data: upload_data,
71 download_data: download_data
72 };
73 };
74
75 this.setChart = type => {
76 if(!this.stats){
77 return;
78 }
79 this.selectedStats = type;
80 this.stats.data = this.stats[type];
81 }
82 }
83 };
84 });