speedtest: first itteration of the speedtest widget is done
authorReidar Cederqvist <reidar.cederqvist@gmail.com>
Wed, 7 Oct 2015 15:36:19 +0000 (17:36 +0200)
committerMartin Schröder <mkschreder.uk@gmail.com>
Thu, 8 Oct 2015 07:45:21 +0000 (09:45 +0200)
plugins/juci-broadcom-dsl/src/widgets/dsl-base-device-picker.js
plugins/juci-diagnostics/access.json
plugins/juci-utils/src/widgets/diagnostics-widget-speedtest.html
plugins/juci-utils/src/widgets/diagnostics-widget-speedtest.js
plugins/juci-utils/src/widgets/utils-add-testserver-picker.html [new file with mode: 0644]
plugins/juci-utils/src/widgets/utils-add-testserver-picker.js [new file with mode: 0644]

index 4e20562..43cbaa5 100644 (file)
@@ -36,15 +36,15 @@ JUCI.app
 .controller("dslBaseDevicePicker", function($scope, $modalInstance, $wireless, items, gettext){
        $scope.items = items; 
        $scope.data = {}; 
-  $scope.ok = function () {
+       $scope.ok = function () {
                if(!$scope.data.selected) {
                        alert($tr(gettext("You need to select a device!"))); 
                        return; 
                }
                $modalInstance.close($scope.data.selected);
-  };
+       };
 
-  $scope.cancel = function () {
-    $modalInstance.dismiss('cancel');
-  };
+       $scope.cancel = function () {
+               $modalInstance.dismiss('cancel');
+       };
 })
index 070d0db..439e06c 100644 (file)
@@ -3,6 +3,9 @@
                "description": "JUCI Diagnostics module",
                "read": {
                        "ubus": {
+                               "juci.utils.speedtest": [
+                                       "run"
+                               ],
                                "/juci/diagnostics": [
                                        "ping", 
                                        "traceroute"
index 0fff80b..5b0a720 100644 (file)
@@ -2,13 +2,14 @@
        <juci-expandable title="TP test">
                <juci-config-lines>
                        <juci-config-line title="{{'Test direction'|translate}}">
-                               <juci-select ng-model="server" ng-items="testType"/>
+                               <juci-select ng-model="data.test_type" ng-items="testType"/>
                        </juci-config-line>
                        <juci-config-line title="{{'Package size'|translate}}">
-                               <input type="number" min="10000" max="100000" step="1000" class="form-control" ng-model="packagesize" />
+                               <input type="number" step="1000" class="form-control" ng-model="data.packagesize" />
+                               <span class="label label-info">{{min + "-" + max}}</span>
                        </juci-config-line>
                        <juci-config-line title="{{'address and prot' | translate}}">
-                               <juci-select ng-model="server.value" ng-items="allTestServers"/>
+                               <juci-select ng-model="data.server" ng-items="allTestServers"/>
                        </juci-config-line>
                        <juci-config-line>
                                <button class="btn btn-default" ng-click="onRemoveAddress()">
                                {{'Run test' | translate}}</button>
                        </juci-config-line>
                </juci-config-lines>
+               <div class="alert alert-success" ng-show="data.state == 'result'">
+                       {{'Trace results'|translate}}: <br/>
+                       <pre>{{data.result}}</pre>
+               </div>
+               <div class="alert alert-success" ng-show="data.state == 'running'">
+                       <h4>
+                               {{" Running test"|translate}}
+                               <i class="fa fa-spinner fa-pulse fa-2x"></i>
+                       </h4>
+               </div>
+           <div class="alert alert-danger" ng-show="data.state == 'error'">
+                       {{'Error'|translate}}: <br/>
+                       <pre>{{data.result|translate}}</pre>
+               </div>
        </juci-expandable>
 </div>
 
-
-
index ce351a5..0c89c85 100644 (file)
@@ -1,4 +1,4 @@
-//! Author: Martin K. Schröder <mkschreder.uk@gmail.com>
+//! Author: Reidar Cederqvist <reidar.cederqvist@gmail.com>
 JUCI.app
 .directive("diagnosticsWidget90Speedtest", function($compile, $parse){
        return {
@@ -6,22 +6,108 @@ JUCI.app
                controller: "diagnosticsWidget90Speedtest", 
         };  
 })
-.controller("diagnosticsWidget90Speedtest", function($scope, $rpc, $events, $uci){
-       $scope.packagesize = 50000;
-       $scope.testType = [{value:"up_down", label: "up and down"}, {value:"up", label: "up"}, {value:"down", label:"down"} ];
-       $scope.addresses = [{label:"tptest.bredband.net/1640"}, {label:"tptest.elion.ee/1550"}];
-       $uci.$sync("speedtest").done(function(){
-               $scope.testServers = $uci.speedtest["@testserver"];
+.controller("diagnosticsWidget90Speedtest", function($scope, $rpc, $events, $uci, utilsAddTestserverPicker){
+       $scope.data = {
+               packagesize: 50000,
+               test_type: "up_down",
+               result: "",
+               state: ""
+       }; 
+       $scope.min = 10000; 
+       $scope.max = 100000; 
+       $scope.$watch('data.packagesize', function(new_value){
+               if(new_value < $scope.min)$scope.data.packagesize = $scope.min;
+               if(new_value > $scope.max)$scope.data.packagesize = $scope.max;
+               console.log("new_value = " + new_value);
+       });
+       function getServers(){
                $scope.allTestServers = $scope.testServers.map(function(x){
                        return {
                                label: x.server.value + "/" + x.port.value, 
-                               value: x[".name"]
+                               value: x
                        }
                });
-               $scope.server = $scope.allTestServers[0];
+       }
+       $scope.testType = [
+               {value:"up_down", label: "up and down"}, 
+               {value:"up", label: "up"}, 
+               {value:"down", label:"down"} ];
+       $uci.$sync("speedtest").done(function(){
+               $scope.testServers = $uci.speedtest["@testserver"];
+               getServers();
+               if($scope.testServers.length)
+                       $scope.data.server = $scope.testServers[0]; 
                $scope.$apply();
        });
        $scope.runTest = function(){
-               window.alert($scope.server.value);
+               if(!$scope.testServers.length){
+                       window.alert("Server and port is mandatory");
+                       return;
+               }
+               if($scope.data.state == "running"){
+                       window.alert("Only one test can be run at a time");
+                       return;
+               }
+               $rpc.juci.utils.speedtest.run({
+                       "testmode": $scope.data.test_type,
+                       "port": $scope.data.server.port.value,
+                       "packagesize": $scope.data.packagesize,
+                       "address": $scope.data.server.server.value
+               }).done(function(response){
+                       if(response && response.message=="success"){
+                               console.log("success running tptest");
+                               $scope.data.state="running";
+                       }else{
+                               console.log("error running tptest");
+                               $scope.data.state="";
+                       }
+                       $scope.$apply();
+               });
+       }
+       $scope.onRemoveAddress = function(){
+               $scope.data.server.$delete().done(function(){
+                       $uci.$save().done(function(){
+                               getServers();
+                               if($scope.testServers.length)
+                                       $scope.data.server = $scope.testServers[0]; 
+                               $scope.$apply();
+                       });
+               });
        }
+       $scope.onAddAddress = function(){
+               utilsAddTestserverPicker.show().done(function(data){
+                       if(!data)return;
+                       $uci.speedtest.create({
+                               ".type": "testserver",
+                               "server": data.address,
+                               "port": data.port
+                       }).done(function(){
+                               $uci.$save().done(function(){
+                                       getServers();
+                                       if($scope.testServers.length==1)
+                                               $scope.data.server = $scope.testServers[0];
+                                       $scope.$apply();
+                               });
+                       });
+                       console.log("address = " + data.address + "port = " + data.port);
+               });
+       }
+       $events.subscribe("juci.utils.speedtest", function(res){
+               console.log(res);
+               switch(res.data.status) {
+               case 0:
+                       $scope.data.result="Upstream: " + res.data.upstream + "\nDownstream: " + res.data.downstream;
+                       $scope.data.state="result";
+                       break;
+               case -1:
+                       $scope.data.result="Wrong TP-test address and/or port";
+                       $scope.data.state="error";
+                       break;
+               case -2:
+                       $scope.data.result="Wrong TP-test port but correct address";
+                       $scope.data.state="error";
+                       break;
+               }
+               $scope.$apply();
+       });
 }); 
diff --git a/plugins/juci-utils/src/widgets/utils-add-testserver-picker.html b/plugins/juci-utils/src/widgets/utils-add-testserver-picker.html
new file mode 100644 (file)
index 0000000..ac62182
--- /dev/null
@@ -0,0 +1,19 @@
+<div>
+       <div class="modal-header">
+               <h3 class="modal-title" translate>Add test server</h3>
+       </div>
+       <div class="modal-body">
+               <juci-config-lines>
+                       <juci-config-line title="{{'Server'|translate}}">
+                               <input type="text" ng-model="data.address"/>
+                       </juci-config-line>
+                       <juci-config-line title="{{'Port'|translate}}">
+                               <input type="number" min="0" max="65535" ng-model="data.port"/>
+                       </juci-config-line>
+               </juci-config-lines>
+       </div>
+       <div class="modal-footer">
+               <button class="btn btn-primary" ng-click="ok()">OK</button>
+               <button class="btn btn-default" ng-click="cancel()">Cancel</button>
+       </div>
+</div>
diff --git a/plugins/juci-utils/src/widgets/utils-add-testserver-picker.js b/plugins/juci-utils/src/widgets/utils-add-testserver-picker.js
new file mode 100644 (file)
index 0000000..47e9d24
--- /dev/null
@@ -0,0 +1,38 @@
+//! Author: Reidar Cederqvist <reidar.cederqvist@gmail.com>
+
+JUCI.app
+.factory("utilsAddTestserverPicker", function($modal, $network){
+       return {
+               show: function(){
+                       var def = $.Deferred(); 
+                       var modalInstance = $modal.open({
+                               animation: true,
+                               templateUrl: 'widgets/utils-add-testserver-picker.html',
+                               controller: 'utilsAddTestserverPicker'
+                       });
+
+                       modalInstance.result.then(function (data) {
+                               setTimeout(function(){ // do this because the callback is called during $apply() cycle
+                                       def.resolve(data); 
+                               }, 0); 
+                       }, function () {
+                                       
+                       });
+                       return def.promise(); 
+               }
+       }; 
+})
+.controller("utilsAddTestserverPicker", function($scope, $modalInstance, $tr, gettext){
+       $scope.data = {}; 
+       $scope.ok = function () {
+               if((!$scope.data.address) || (!$scope.data.port)) {
+                       alert($tr(gettext("Address and port needed"))); 
+                       return; 
+               }
+               $modalInstance.close($scope.data);
+       };
+
+       $scope.cancel = function () {
+               $modalInstance.dismiss('cancel');
+       };
+})