0b9cf8320d38c1012010c1a960bc841d1ab95b86
[juci.git] / plugins / juci-diagnostics / src / widgets / diagnostics-widget-speedtest.js
1 //! Author: Reidar Cederqvist <reidar.cederqvist@gmail.com>
2
3 JUCI.app
4 .directive("diagnosticsWidget90Speedtest", function(){
5         return {
6                 scope: true,
7                 replace: true,
8                 templateUrl: "/widgets/diagnostics-widget-speedtest.html",
9                 controller: "diagnosticsWidget90Speedtest"
10         };
11 })
12 .controller("diagnosticsWidget90Speedtest", function($scope, $rpc, $events, $uci, utilsAddTestserverPicker, $tr, gettext){
13         $scope.data = {
14                 test_type: "up_down",
15                 result: [],
16                 state: "",
17                 auto: true
18         };
19
20         function refresh(){
21                 var def = $.Deferred();
22                 $rpc.$call("juci.diagnostics", "tptest_running").done(function(res){
23                         if(!res){
24                                 def.reject();
25                                 return;
26                         }
27                         if(res.running){
28                                 $scope.data.state = "running";
29                         }
30                         else if($scope.data.state !== "error"){
31                                 $scope.data.state = "";
32                         }
33                         def.resolve();
34                 }).fail(function(e){def.reject(e);});
35                 return def.promise();
36         }
37
38         function getServers(){
39                 $scope.allTestServers = $scope.testServers.map(function(x){
40                         return {
41                                 label: x.server.value + ":" + x.port.value,
42                                 value: x.server.value
43                         }
44                 });
45                 if($scope.allTestServers.length)
46                         $scope.data.server = $scope.allTestServers[0].value;
47                 else
48                         $scope.data.server = null;
49         }
50
51         JUCI.interval.repeat("diagnostics-tptest-running", 10000, function(next){
52                 refresh().done(function(){
53                         $scope.$apply();
54                 }).always(function(){
55                         next();
56                 });
57         });
58
59         $scope.removeResult = function(res){
60                 if(!res) return;
61                 $scope.data.result = $scope.data.result.filter(function(r){
62                         return r !== res;
63                 });
64         }
65
66         $rpc.$call("juci.diagnostics", "tptest_packet_size").done(function(data){
67                 $scope.data.downsize = parseInt(data.packetsize_down);
68                 $scope.data.upsize = parseInt(data.packetsize_up);
69                 $scope.$apply();
70         }).fail(function(e){ console.log(e); });
71
72         $scope.testType = [
73                 {value:"down", label: $tr(gettext("Download")) },
74                 {value:"up", label: $tr(gettext("Upload")) },
75                 {value:"up_down", label: $tr(gettext("Download/Upload")) }
76         ];
77
78         $uci.$sync("speedtest").done(function(){
79                 $scope.testServers = $uci.speedtest["@testserver"];
80                 getServers();
81                 $scope.$apply();
82         });
83
84         $scope.runTest = function(){
85                 if($scope.data.state === "running") return;
86                 if($scope.data.test_type.match(/up/) && !$scope.data.upsize){ alert("Upstream packet size missing"); return; }
87                 if($scope.data.test_type.match(/down/) && !$scope.data.downsize){ alert("Downstream packet size missing"); return; }
88                 if(!$scope.testServers.length){
89                         window.alert($tr(gettext("Server and port is mandatory")));
90                         return;
91                 }
92                 var server = $scope.testServers.find(function(x){ return $scope.data.server == x.server.value;});
93                 var port = server.port.value;
94                 var address = server.server.value;
95                 $scope.data.state="running";
96                 var speedtestArgs = {
97                         "auto": $scope.data.auto,
98                         "testmode": $scope.data.test_type,
99                         "port": port,
100                         "host": address,
101                 };
102
103                 if($scope.data.test_type.match(/up/) && !$scope.data.auto){
104                         speedtestArgs.packetsize_up = $scope.data.upsize * 1000000;
105                 }
106                 if($scope.data.test_type.match(/down/) && !$scope.data.auto){
107                         speedtestArgs.packetsize_down = $scope.data.downsize * 1000000;
108                 }
109
110                 $rpc.$call("juci.diagnostics", "tptest_start", speedtestArgs).done(function(response){
111                         if(response && response.message=="success"){
112                                 $scope.data.state="running";
113                         }else{
114                                 $scope.data.state="";
115                         }
116                         $scope.$apply();
117                 }).fail(function(e){
118                         console.log(e);
119                         $scope.data.state = "";
120                         $scope.$apply();
121                 });
122         };
123
124         $scope.onRemoveAddress = function(){
125                 var server = $scope.testServers.find(function(x){
126                         return $scope.data.server == x.server.value
127                 });
128                 if(!server){
129                         alert($tr(gettext("error deleting server")));
130                         return;
131                 }
132                 server.$delete().done(function(){
133                         getServers();
134                         $scope.$apply();
135                 });
136         };
137
138         $scope.onAddAddress = function(){
139                 utilsAddTestserverPicker.show().done(function(data){
140                         if(!data)return;
141                         $uci.speedtest.$create({
142                                 ".type": "testserver",
143                                 "server": data.address,
144                                 "port": data.port
145                         }).done(function(){
146                                 getServers();
147                                 $scope.$apply();
148                         });
149                 });
150         }
151         $events.subscribe("diagnostics.speedtest", function(res){
152                 if(res.data && res.data.status != undefined){
153                         switch(res.data.status) {
154                         case 0:
155                                 var upstream = parseInt(res.data.upstream);
156                                 if(upstream == "NaN") {
157                                         upstream = "none"
158                                 }else{
159                                         upstream = upstream / 1000 / 1000;
160                                 }
161                                 var downstream = parseInt(res.data.downstream);
162                                 if(downstream == "NAN"){
163                                         downstream = "none"
164                                 }else{
165                                         downstream = downstream / 1000 / 1000;
166                                 }
167                                 if(res.data.upstream != "none" && res.data.downstream != "none"){
168                                         $scope.data.result.push($tr(gettext("Downstream:"))+" " + downstream.toFixed(2) + " " + $tr(gettext("Mbit/s\nUpstream:"))+" " + upstream.toFixed(2) +" "+$tr(gettext("Mbit/s")));
169                                 }else if(res.data.upstream != "none"){
170                                         $scope.data.result.push($tr(gettext("Upstream:"))+" " + upstream.toFixed(2) + " "+$tr(gettext("Mbit/s")));
171                                 }else if(res.data.downstream != "none"){
172                                         $scope.data.result.push($tr(gettext("Downstream:"))+" " + downstream.toFixed(2) + " "+$tr(gettext("Mbit/s")));
173                                 }else {
174                                         $scope.data.result.push($tr(gettext("No speeds found")));
175                                 }
176                                 $scope.data.state="result";
177                                 break;
178                         case -1:
179                                 $scope.data.result.push($tr(gettext("Wrong TP-test address and/or port")));
180                                 $scope.data.state="error";
181                                 break;
182                         }
183                         refresh().always(function(){
184                                 $scope.$apply();
185                         });
186                 }
187         });
188 });