Unverified Commit 08e4da86 authored by Francesc Guasch's avatar Francesc Guasch Committed by GitHub
Browse files

refactor(frontend): turn list machines into a hash (#1282)

- Removed order by other fields
- Also tweaked ping backed so it is more accurate

issue #1276
parent e5e2a0f3
......@@ -11,6 +11,7 @@ no warnings "experimental::signatures";
use feature qw(signatures);
my $DEBUG=0;
my $T0 = time;
has clients => (
is => 'ro'
......@@ -224,17 +225,31 @@ sub _different_list($list1, $list2) {
}
sub _different_hash($h1,$h2) {
return 1 if keys %$h1 != keys %$h2;
my $different = 0;
for my $key (keys %$h1) {
next if $key =~ /^_/;
next if !defined $h1->{$key} && !defined $h2->{$key};
if (!exists $h2->{$key}
|| !defined $h1->{$key} && defined $h2->{$key}
|| defined $h1->{$key} && !defined $h2->{$key}
|| _different($h1->{$key}, $h2->{$key})) {
return 1;
unlock_hash(%$h1);
$h1->{_timestamp} = time - $T0;
lock_hash(%$h1);
$different = 1;
}
}
return 0;
if (!exists $h1->{_timestamp}) {
unlock_hash(%$h1);
if (exists $h2->{_timestamp}) {
$h1->{_timestamp} = $h2->{_timestamp}
} else {
$h1->{_timestamp} = time - $T0;
}
lock_hash(%$h1);
}
return $different;
}
sub _different($var1, $var2) {
return 1 if !defined $var1 && defined $var2;
......
......@@ -10,6 +10,20 @@ ravadaApp.directive("solShowMachine", swMach)
.controller("new_node", newNodeCtrl)
;
ravadaApp.filter('orderObjectBy', function() {
return function(items, field, reverse) {
var filtered = [];
angular.forEach(items, function(item) {
filtered.push(item);
});
filtered.sort(function (a, b) {
return (a[field] > b[field] ? 1 : -1);
});
if(reverse) filtered.reverse();
return filtered;
};
});
function swMach() {
return {
restrict: "E",
......@@ -165,20 +179,33 @@ ravadaApp.directive("solShowMachine", swMach)
var data = JSON.parse(event.data);
$scope.$apply(function () {
$scope.list_machines = [];
var mach;
if (Object.keys($scope.list_machines).length != data.length) {
$scope.list_machines = {};
}
for (var i=0, iLength = data.length; i<iLength; i++){
mach = data[i];
if (!mach.id_base){
if (!mach.id_base
&& (typeof $scope.list_machines[mach.id] == 'undefined'
|| $scope.list_machines[mach.id]._timestamp != mach._timestamp)
){
$scope.list_machines[mach.id] = mach;
$scope.list_machines[mach.id].childs = [];
$scope.list_machines[mach.id].childs = {};
}
}
$scope.n_clones = 0;
for (var i=0, iLength = data.length; i<iLength; i++){
mach = data[i];
if (mach.id_base){
$scope.list_machines[mach.id_base].childs.push(mach);
var childs;
if (mach.id_base) {
childs = $scope.list_machines[mach.id_base].childs;
}
if (mach.id_base
&& ( typeof childs[mach.id] == 'undefined'
|| childs[mach.id]._timestamp != mach._timestamp
)
){
childs[mach.id] = mach;
$scope.n_clones++;
}
}
......@@ -188,10 +215,7 @@ ravadaApp.directive("solShowMachine", swMach)
$scope.hide_clones = true;
}
}
for (var i = $scope.list_machines.length-1; i >= 0; i--){
if (!$scope.list_machines[i]){
$scope.list_machines.splice(i,1);
}
for (var i in $scope.list_machines){
mach = $scope.list_machines[i];
if (!mach.id_base && typeof $scope.show_clones[mach.id] == 'undefined'
&& typeof mach.childs != 'undefined'
......@@ -238,6 +262,7 @@ ravadaApp.directive("solShowMachine", swMach)
}
};
$scope.list_machines = {};
$scope.orderParam = ['name'];
$scope.auto_hide_clones = true;
$scope.orderMachineList = function(type1,type2){
......@@ -251,7 +276,7 @@ ravadaApp.directive("solShowMachine", swMach)
$scope.showClones = function(value){
$scope.auto_hide_clones = false;
$scope.hide_clones = !value;
for (var i = $scope.list_machines.length-1; i >= 0; i--){
for (var i in $scope.list_machines){
mach = $scope.list_machines[i];
if (!mach.id_base) {
$scope.show_clones[mach.id] = value;
......@@ -305,12 +330,15 @@ ravadaApp.directive("solShowMachine", swMach)
$scope.cancel_modal=function(){
$scope.modalOpened=false;
}
$scope.toggle_show_clones =function(id) {
$scope.show_clones[id] = !$scope.show_clones[id];
}
//On load code
$scope.modalOpened=false;
$scope.rename= {new_name: 'new_name'};
$scope.show_rename = false;
$scope.new_name_duplicated=false;
$scope.show_clones = {};
$scope.show_clones = { '0': false };
};
function usersPageC($scope, $http, $interval, request) {
......
......@@ -101,7 +101,7 @@
<div class="col-md-4" align="right">
% if ($_user->can_create_machine){
<h2>
<button class="btn btn-warning" ng-click="list_machines=[];subscribe_all('<%= url_for('ws_subscribe')->to_abs %>');"><i class="fa fa-sync-alt" aria-hidden="true"></i></button>
<button class="btn btn-warning" ng-click="list_machines={};subscribe_all('<%= url_for('ws_subscribe')->to_abs %>');"><i class="fa fa-sync-alt" aria-hidden="true"></i></button>
<a type="button" class="btn btn-success" href="/new_machine.html">
<b><%=l 'New Machine' %></b>
<i ng-show="download_working"
......@@ -120,14 +120,7 @@
<table class="table admin-cont-body" border="0" ng-show="list_machines" ng-cloak="1">
<thead>
<tr>
<th ><div class="lgMachName machine-button" style="display:inline;float:down"
ng-click="orderMachineList('name','')">
<i class="fas fa-exchange fa-rotate-90"
ng-hide="orderParam[0] === 'name' || orderParam[0] === '-name'"></i>
<i class="fas fa-arrow-down" ng-cloak
ng-hide="orderParam[0] !== 'name'"></i>
<i class="fas fa-arrow-up" ng-cloak
ng-hide="orderParam[0] !== '-name'"></i>
<th ><div class="lgMachName machine-button" style="display:inline;float:down">
<%=l 'Machine Name' %></div>
<div style="float:right" ng-show="n_clones">
<a ng-show="hide_clones" ng-click="showClones(true)"
......@@ -147,27 +140,23 @@
% if ($autostart) {
<th class="lgMachToggle"><%=l 'Autostart'%></th>
% }
<th class="lgMachToggle machine-button"
ng-click="orderMachineList('is_active','is_paused')">
<th class="lgMachToggle machine-button">
<div style="float:right">
<i class="fas fa-exchange-alt fa-rotate-90" ng-hide="orderParam[0] === 'is_active' || orderParam[0] === '-is_active'"></i>
<i class="fas fa-arrow-down" ng-cloak ng-hide="orderParam[0] !== 'is_active'"></i>
<i class="fas fa-arrow-up" ng-cloak ng-hide="orderParam[0] !== '-is_active'"></i>
<%=l 'Status' %></div></th>
<th class="lgMachActions"><%=l 'Actions' %></th>
<th class="lgMachNode"><%=l 'Node' %></th>
</tr>
</thead>
<tbody>
<tr ng-repeat-start="machine in list_machines | orderBy : orderParam" class="bg-light">
<tr ng-repeat-start="machine in list_machines| orderObjectBy:'name'" class="bg-light">
<td class="lgMachName">
<a align="right" href="/machine/manage/{{machine.id}}.html"
ng-class="{disabled: !machine.can_manage}"
title ="<%=l 'Manage machine' %>"><b
ng-cloak>{{machine.name}}</b></a> {{machine.comment}}
>{{machine.name}}</b></a> {{machine.comment}}
<button ng-show="machine.has_clones" type="button"
class="badge badge-light text-blue"
ng-click="show_clones[machine.id] = !show_clones[machine.id]"
ng-click="toggle_show_clones(machine.id)"
title="<%=l 'Show/Hide clones' %>">
<b ng-show="show_clones[machine.id]" >-</b>
<b ng-show="!show_clones[machine.id]">+</b>
......@@ -293,7 +282,7 @@
</td>
<td class="lgMachNode"></td>
</tr>
<tr ng-show="show_clones[machine.id]" ng-repeat="child in machine.childs | orderBy : orderParam">
<tr ng-show="show_clones[machine.id]" ng-repeat="child in machine.childs | orderObjectBy:'name'">
<td class="lgMachName">
&nbsp;<i title="[cloned]" class="fa fa-fw fa-long-arrow-right"
style="color:white"></i>
......@@ -302,7 +291,7 @@
<a align="right" href="/machine/manage/{{child.id}}.html"
ng-class="{disabled: !child.can_manage}"
title ="Manage machine"><i
ng-cloak>&nbsp;{{child.name}}</i></a> {{child.comment}}
ng-cloak>&nbsp;{{child.name}}</i></a>{{child.comment}}
</td>
<td class="lgMachToggle">
% if ($_user->can_create_base ) {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment