only white spaces
[plewww.git] / planetlab / includes / plc_objects.php
index e34bf12..0fd9a1f 100644 (file)
@@ -23,25 +23,25 @@ function timeDiff ($timestamp,$detailed=false,$n = 0) {
   $lengths = array(1, 60, 3600, 86400, 604800, 2630880, 31570560, 315705600);
 
 # Go from decades backwards to seconds
-  $i = sizeof($lengths) - 1;                            # Size of the lengths / periods in case you change them
-  $time = "";                                                                                          # The string we will hold our times in
-  while($i >= $n) {
+  $i = sizeof($lengths) - 1;                           # Size of the lengths / periods in case you change them
+  $time = "";                                          # The string we will hold our times in
+  while($i > $n) {
 # if the difference is greater than the length we are checking... continue
-    if ($diff > $lengths[$i-1]) {                              
+    if ($diff > $lengths[$i-1]) {
 # 65 / 60 = 1. That means one minute.  130 / 60 = 2. Two minutes.. etc
-      $val = floor($diff / $lengths[$i-1]);            
+      $val = floor($diff / $lengths[$i-1]);
 # The value, then the name associated, then add 's' if plural
-      $time .= $val ." ". $periods[$i-1].($val > 1 ? 's ' : ' ');      
-# subtract the values we just used from the overall diff so we can 
+      $time .= $val ." ". $periods[$i-1].($val > 1 ? 's ' : ' ');
+# subtract the values we just used from the overall diff so we can
 # find the rest of the information
-      $diff -= ($val * $lengths[$i-1]);                
-# if detailed is turn off (default) only show the first set found, 
+      $diff -= ($val * $lengths[$i-1]);
+# if detailed is turn off (default) only show the first set found,
 # else show all information
-      if(!$detailed) { $i = 0; }               
+      if(!$detailed) { $i = 0; }
     }
     $i--;
   }
-       
+
 # Basic error checking.
   if ($time == "") {
     return "error: bad time";
@@ -64,12 +64,12 @@ class PlcObject {
 class Person {
   var $roles;
   var $person_id;
-  var $first_name; 
+  var $first_name;
   var $last_name;
   var $email;
   var $enabled;
 
-  function Person($person) {
+  function __construct($person) {
     $this->roles = $person['role_ids'];
     $this->person_id = $person['person_id'];
     $this->first_name = $person['first_name'];
@@ -136,19 +136,19 @@ class Person {
 class PCU {
   var $data;
 
-  function PCU($pcu) {
+  function __construct($pcu) {
     $this->data = $pcu;
   }
 
   function deletePCUlink($node) {
-    return "<a href='/db/sites/index.php?id=" . $node->site_id . 
-      "&delete_node_from_pcu=" . $node->node_id . 
+    return "<a href='/db/sites/index.php?id=" . $node->site_id .
+      "&delete_node_from_pcu=" . $node->node_id .
       "&pcu_id=" . $this->data['pcu_id'] . "'>&nbsp;Remove from PCU</a>";
   }
   function pcu_name() {
     if ( $this->data['hostname'] != NULL and $this->data['hostname'] != "" ):
       return $this->data['hostname'];
-    else: 
+    else:
       if ( $this->data['ip'] != NULL and $this->data['ip'] != "" ):
        return $this->data['ip'];
       else:
@@ -169,14 +169,14 @@ class PCU {
 class Address {
   var $data;
 
-  function Address($address) {
+  function __construct($address) {
     $this->data = $address;
   }
 
   function link($str) {
     return "<a href='/db/addresses/index.php?id=" . $this->data['address_id'] . "'>" . $str . "</a>";
   }
-       
+
   function label() {
     $label = "";
     $comma= sizeof( $this->data['address_types'] );
@@ -204,32 +204,35 @@ class Node extends PlcObject {
   var $pcu_ids;
   var $data;
 
-  function Node($node) {
+  function __construct($node) {
     global $plc, $api, $adm;
     $this->data = $node;
+    $this->node_type = $node['node_type'];
     $this->model = $node['model'];
     $this->node_id = $node['node_id'];
     $this->hostname = $node['hostname'];
     $this->boot_state = $node['boot_state'];
+    $this->run_level = $node['run_level'];
     $this->date_created = $node['date_created'];
     $this->last_updated = $node['last_updated'];
     $this->last_contact = $node['last_contact'];
     $this->site_id = $node['site_id'];
     $this->pcu_ids = $node['pcu_ids'];
-    $this->nn = $api->GetNodeNetworks($node['nodenetwork_ids']);
+    $this->nn = $api->GetInterfaces($node['interface_ids']);
     foreach ($this->nn as $nnet)
       {
-       if ( $nnet['is_primary'] == true )
-         {
-           $this->ip = $nnet['ip'];
-           $this->netmask = $nnet['netmask'];
-           $this->network = $nnet['network'];
-           $this->gateway= $nnet['gateway'];
-           $this->broadcast = $nnet['broadcast'];
-           $this->dns1 = $nnet['dns1'];
-           $this->dns2 = $nnet['dns2'];
-           $this->method = $nnet['method'];
-         }
+      if ( $nnet['is_primary'] == true )
+        {
+          $this->ip = $nnet['ip'];
+          $this->netmask = $nnet['netmask'];
+          $this->network = $nnet['network'];
+          $this->gateway= $nnet['gateway'];
+          $this->broadcast = $nnet['broadcast'];
+          $this->dns1 = $nnet['dns1'];
+          $this->dns2 = $nnet['dns2'];
+          $this->method = $nnet['method'];
+          $this->interface_id = $nnet['interface_id'];
+        }
       }
   }
 
@@ -271,6 +274,7 @@ class Node extends PlcObject {
   function lastContact() {
     return $this->timeaway($this->last_contact);
   }
+
   function timeaway($val) {
     if ( $val != NULL ) {
       $ret = timeDiff(intval($val));
@@ -278,14 +282,43 @@ class Node extends PlcObject {
       $ret = "Never";
     }
     return $ret;
-    
   }
+
+  // code needs to be accessible from outside an object too
+  // b/c of the performance overhead of creating as many objects as nodes
+  static function status_label_class__ ($boot_state, $run_level, $last_contact, $peer_id) {
+    $label= $run_level ? $run_level : ( $boot_state . '*' ) ;
+    if (Node::stale_($last_contact,$peer_id)) $label .= '...';
+    $class=($label=="boot") ? 'node-ok' : 'node-ko';
+    return array($label,$class);
+  }
+  static function status_label_class_ ($node) {
+    return Node::status_label_class__ ($node['boot_state'],$node['run_level'],$node['last_contact'], $node['peer_id']);
+  }
+  function status_label_class () {
+    return Node::status_label_class__ ($this->boot_state,$this->run_level,$this->last_contact, $this->peer_id);
+  }
+  static function status_footnote () {
+    return "state; * if node doesn't have an observed state; ... if status is stale (" . Node::stale_text() . ")";
+  }
+
+  // ditto
+  static function stale_ ($last_contact, $peer_id) {
+    // remote nodes don't have a last_contact
+    if ( $peer_id) return false;
+    $STALE_LENGTH = 2*60*60;   /* TODO: set by some policy */
+    $now = time();
+    return ( $last_contact + $STALE_LENGTH < $now );
+  }
+  function stale() { return Node::stale_ ($this->last_contact,$this->peer_id); }
+  static function stale_text() { return "2 hours"; }
+
 }
 
 class Slice {
   var $data;
 
-  function Slice($val) {
+  function __construct($val) {
     $this->data = $val;
   }
 
@@ -316,7 +349,7 @@ class Site extends PlcObject {
   var $site_id;
   var $data;
 
-  function Site($site_id) {
+  function __construct($site_id) {
     global $plc, $api, $adm;
     $site_info= $adm->GetSites( array( intval($site_id) ) );
     $this->data = $site_info[0];
@@ -339,29 +372,11 @@ class Site extends PlcObject {
     $adm->GetPCUs( $this->pcu_ids );
     $adm->GetNodes( $this->node_ids, array( "node_id", "hostname", "boot_state",
                                            "date_created", "last_updated", "last_contact", "site_id", "pcu_ids" ) );
-    $adm->GetPersons( $this->person_ids, array( "role_ids", "person_id", "first_name", 
+    $adm->GetPersons( $this->person_ids, array( "role_ids", "person_id", "first_name",
                                                "last_name", "email", "enabled" ) );
     $adm->GetSlices( $this->slice_ids, array( "name", "slice_id", "instantiation", "created", "expires" ) );
     return $adm->commit();
   }
 }
 
-/* class Blue extends PlcObject
- {
- var $val;
- function Blue($arg)
- {
- $this->val = $arg;
- }
- }
-
- $cl = PlcObject::constructList('Blue', array('this', 'is', 'a', 'test'));
- echo sizeof($cl) . "\n";
- foreach ($cl as $obj)
- {
- echo get_class( $obj) . "\n";
- echo $obj->val . "\n";
- }*/
-
-
 ?>