blob: 08bc8137e4ba6a7045952acbebbd38eb305df822 [file] [log] [blame]
Matteo Scandoloe0e4de42016-06-08 16:15:30 -07001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
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 Scandolob2946032016-06-10 11:24:58 -070039 this.stats = formatStats(stats.ProfileArray);
Matteo Scandoloe0e4de42016-06-08 16:15:30 -070040 });
41 };
42
43 const formatStats = stats => {
44 let series = _.reduce(stats, (list, s) => {
45 return list.concat([s.Profile]);
46 }, []);
47 let labels = _.reduce(stats[0].StatsArray, (list, s) => {
48 return list.concat([$filter('date')(new Date(s.Time * 1000), 'shortTime')]);
49 }, []);
50 const download_data = _.reduce(stats, (list, stat) => {
51 let bitrate = _.reduce(stat.StatsArray, (data, s) => {
52 return data.concat([s.DlBitrate]);
53 }, []);
54 list.push(bitrate);
55 return list;
56 }, []);
57 const upload_data = _.reduce(stats, (list, stat) => {
58 let bitrate = _.reduce(stat.StatsArray, (data, s) => {
59 return data.concat([s.UlBitrate]);
60 }, []);
61 list.push(bitrate);
62 return list;
63 }, []);
64 return {
65 labels: labels,
66 series: series,
67 data: download_data,
68 upload_data: upload_data,
69 download_data: download_data
70 };
71 };
72
73 this.setChart = type => {
74 if(!this.stats){
75 return;
76 }
77 this.selectedStats = type;
78 this.stats.data = this.stats[type];
79 }
80 }
81 };
82 });