[Update] Manage history order + State `machineDefault/getState`"

[Todo] Test
This commit is contained in:
xdrm-brackets 2017-02-19 18:07:04 +01:00
parent 4e017aeb2f
commit 934545aae4
8 changed files with 633 additions and 595 deletions

View File

@ -6,6 +6,7 @@
use \error\core\Error;
use \error\core\Err;
use \database\core\Repo;
use \api\core\Request;
class machineDefault{
@ -315,6 +316,110 @@
/* RETURN MACHINE STATE
*
* @id_machine<int> UID of the machine
*
* @return state<string> Machine state
*
*/
public function getState($params){
extract($params);
/* [1] Get machine info
=========================================================*/
/* (1) Write request */
$machine_req = new Request('machineDefault/getById', ['id_machine' => $id_machine]);
/* (2) Execute request */
$machine_res = $machine_req->dispatch();
/* (3) Manage error */
if( $machine_res->error->get() != Err::Success )
return [ 'error' => $machine_res->error ];
/* [2] Get action id=>name
=========================================================*/
$action = [];
/* (1) Write request */
$action_req = new Repo('action/getAll', []);
/* (2) Manage error */
if( $action_req->error->get() != Err::Success )
return [ 'error' => $action_req->error ];
/* (3) Create association array */
foreach($action_req->answer() as $a)
$action[ strtolower($a['name']) ] = $a['id_action'];
/* [3] Get history for the machine
=========================================================*/
/* (1) Write request */
$history_req = new Repo('history/getByIdMachine', [$id_machine]);
/* (2) Manage error */
if( $history_req->error->get() != Err::Success )
return [ 'error' => $history_req->error ];
/* (3) Extract history */
$history = $history_req->answer();
/* [4] Process state
=========================================================*/
/* (1) LOCKED (last = lock)
---------------------------------------------------------*/
if( count($history) > 0 && $history[0]['id_action'] == $action['lock'] )
return [ 'state' => 'locked' ];
/* (2) STOPPED (last = unlock | stop)
---------------------------------------------------------*/
if( count($history) > 0 && in_array($history[0]['id_action'], [$action['stop'], $action['unlock']]) )
return [ 'state' => 'stopped' ];
/* (3) SIGNALED (start|stop ..... signal)
---------------------------------------------------------*/
if( count($history) > 0 && $history[0]['id_action'] == $action['signal'] )
return [ 'state' => 'signaled' ];
for( $c = 1 ; $c < count($history) ; $c++ ){
/* (1) If (start|stop), continue to search */
if( in_array($history[$c]['id_action'] , [$action['start'], $action['stop']]) )
continue;
/* (2) If (signal) found, therefore it is signaled */
else if( $history[$c]['id_action'] == $action['signal'] )
return [ 'state' => 'signaled' ];
/* (4) STARTED (last state)
---------------------------------------------------------*/
else
return [ 'state' => 'started' ];
}
/* (5) DETACHED (no state)
---------------------------------------------------------*/
return [ 'state' => 'detached' ];
}

View File

@ -132,7 +132,7 @@
->join('id_action', $actions)
->select('id_history')
->select('timestamp')
->orderby('timestamp', Rows::ORDER_ASC);
->orderby('timestamp', Rows::ORDER_DESC);
return $history->fetch();
}

View File

@ -299,6 +299,7 @@
=========================================================*/
$user = Table::get('user')
->whereIdWarehouse($id_warehouse)
->orderby('username', Rows::ORDER_ASC)
->select('*');
return $user->fetch();

View File

@ -40,8 +40,16 @@
return $answer->get('machines');
}));
$twig->addFunction(new \Twig_Function('f_getstate', function(){
return 'detached';
$twig->addFunction(new \Twig_Function('f_getstate', function($id_machine){
/* (1) Write / Execute request */
$req = new Request('machineDefault/getState', ['id_machine' => $id_machine]);
$res = $req->dispatch();
/* (2) Manage error */
if( $res->error->get() != Err::Success )
return 'detached';
return $res->get('state');
}));
$twig->addFunction(new \Twig_Function('f_clusters', function($id_machine){

View File

@ -348,6 +348,17 @@
"output": {
"status": { "description": "Status de la suppression.", "type": "boolean" }
}
},
"POST::getState": {
"description": "Retourne l'état d'une machine.",
"permissions": ["warehouse", "admin"],
"parameters": {
"id_machine": { "description": "UID de la machine", "type": "id" }
},
"output": {
"state": { "description": "Etat de la machine", "type": "text" }
}
}
},

View File

@ -6,9 +6,9 @@
"search",
"getAll",
"getByUser",
"getByMachine",
"getByCluster"
"getByIdUser",
"getByIdMachine",
"getByIdAction"
],

View File

@ -123,8 +123,8 @@
background-color: #ddd;
&[data-state='stop']{ background-color: #ddd; }
&[data-state='start']{ background-color: #22E07B; }
&[data-state='stopped']{ background-color: #ddd; }
&[data-state='started']{ background-color: #22E07B; }
&[data-state='signaled']{ background-color: #3897D6; }
&[data-state='locked']{ background-color: #EA460A; }
&[data-state='detached']{ background-color: #AB1EE2; }

File diff suppressed because it is too large Load Diff