remove dead code
[plewww.git] / planetlab / includes / plc_objects.php
1 <?php
2
3   // $Id$
4
5 function timeDiff ($timestamp,$detailed=false,$n = 0) {
6   $now = time();
7
8 #If the difference is positive "ago" - negative "away"
9   ($timestamp >= $now) ? $action = 'away' : $action = 'ago';
10   //echo "Away: $action<br>\n";
11   //if ( $timestamp >= $now //)
12   //{
13   //    echo "Val: greater $timestamp : $now<br>\n";
14   //} else{
15   //    echo "Val: less than $timestamp : $now<br>\n";
16   //}
17
18
19   $diff = ($action == 'away' ? $timestamp - $now : $now - $timestamp);
20
21 # Set the periods of time
22   $periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
23   $lengths = array(1, 60, 3600, 86400, 604800, 2630880, 31570560, 315705600);
24
25 # Go from decades backwards to seconds
26   $i = sizeof($lengths) - 1;                            # Size of the lengths / periods in case you change them
27   $time = "";                                           # The string we will hold our times in
28   while($i > $n) {
29 # if the difference is greater than the length we are checking... continue
30     if ($diff > $lengths[$i-1]) {                               
31 # 65 / 60 = 1.  That means one minute.  130 / 60 = 2. Two minutes.. etc
32       $val = floor($diff / $lengths[$i-1]);             
33 # The value, then the name associated, then add 's' if plural
34       $time .= $val ." ". $periods[$i-1].($val > 1 ? 's ' : ' ');       
35 # subtract the values we just used from the overall diff so we can 
36 # find the rest of the information
37       $diff -= ($val * $lengths[$i-1]);         
38 # if detailed is turn off (default) only show the first set found, 
39 # else show all information
40       if(!$detailed) { $i = 0; }                
41     }
42     $i--;
43   }
44         
45 # Basic error checking.
46   if ($time == "") {
47     return "error: bad time";
48   } else {
49     return $time.$action;
50   }
51 }
52
53 class PlcObject {
54   public static function constructList($cname, $list) {
55     $ret_list = array();
56     foreach ($list as $item) {
57       $ret_list[] = new $cname ($item);
58     }
59     return $ret_list;
60   }
61 }
62
63
64 class Person {
65   var $roles;
66   var $person_id;
67   var $first_name; 
68   var $last_name;
69   var $email;
70   var $enabled;
71
72   function __construct($person) {
73     $this->roles = $person['role_ids'];
74     $this->person_id = $person['person_id'];
75     $this->first_name = $person['first_name'];
76     $this->last_name = $person['last_name'];
77     $this->email = $person['email'];
78     $this->enabled = $person['enabled'];
79     $this->data = $person;
80   }
81
82   public static function getPIs($persons) {
83     $pis = array();
84     foreach( $persons as $person ) {
85       $role_ids= $person->roles;
86
87       if ( in_array( '20', $role_ids ) && $person->enabled )
88         $pis[$person->person_id]= $person->email;
89     }
90     return $pis;
91   }
92
93   public static function getTechs($persons) {
94     $techs = array();
95     foreach( $persons as $person ) {
96       $role_ids= $person->roles;
97       if( in_array( '40', $role_ids ) && $person->enabled )
98         $techs[$person->person_id]= $person->email;
99     }
100     return $techs;
101   }
102
103   function getSites() {
104     return $this->data['site_ids'];
105   }
106   function isMember($site_id) {
107     return in_array($site_id, $this->data['site_ids']);
108   }
109
110   function isAdmin() {
111     return in_array( '10', $this->roles);
112   }
113   function isPI() {
114     return in_array( '20', $this->roles);
115   }
116   function isUser() {
117     return in_array( '30', $this->roles);
118   }
119   function isTech() {
120     return in_array( '40', $this->roles);
121   }
122
123   function link($str) {
124     return "<a href='/db/persons/index.php?id=" . $this->person_id . "'>" . $str . "</a>";
125   }
126
127   function display() {
128     $person = array();
129     $person[] = $this->first_name . " " . $this->last_name;
130     $person[] = $this->link($this->email);
131     return $person;
132   }
133 }
134
135
136 class PCU {
137   var $data;
138
139   function __construct($pcu) {
140     $this->data = $pcu;
141   }
142
143   function deletePCUlink($node) {
144     return "<a href='/db/sites/index.php?id=" . $node->site_id . 
145       "&delete_node_from_pcu=" . $node->node_id . 
146       "&pcu_id=" . $this->data['pcu_id'] . "'>&nbsp;Remove from PCU</a>";
147   }
148   function pcu_name() {
149     if ( $this->data['hostname'] != NULL and $this->data['hostname'] != "" ):
150       return $this->data['hostname'];
151     else: 
152       if ( $this->data['ip'] != NULL and $this->data['ip'] != "" ):
153         return $this->data['ip'];
154       else:
155         return "NO-HOSTNAME-OR-IP";
156     endif;
157     endif;
158   }
159
160   function link($str) {
161     return "<a href='/db/sites/pcu.php?id=" . $this->data['pcu_id'] . "'>" . $str . "</a>";
162   }
163
164   function host() {
165     return substr($this->data['hostname'], 0, strpos($this->data['hostname'], '.'));
166   }
167 }
168
169 class Address {
170   var $data;
171
172   function __construct($address) {
173     $this->data = $address;
174   }
175
176   function link($str) {
177     return "<a href='/db/addresses/index.php?id=" . $this->data['address_id'] . "'>" . $str . "</a>";
178   }
179         
180   function label() {
181     $label = "";
182     $comma= sizeof( $this->data['address_types'] );
183     $count= 0;
184     foreach( $this->data['address_types'] as $add_type ) {
185       $label .= $add_type;
186       $count++;
187       if ( $comma > 0 && $count != $comma )
188         $label .= ", ";
189     }
190     return $label;
191   }
192
193 }
194
195
196 class Node extends PlcObject {
197   var $node_id;
198   var $hostname;
199   var $boot_state;
200   var $date_created;
201   var $last_updated;
202   var $last_contact;
203   var $site_id;
204   var $pcu_ids;
205   var $data;
206
207   function __construct($node) {
208     global $plc, $api, $adm;
209     $this->data = $node;
210     $this->node_type = $node['node_type'];
211     $this->model = $node['model'];
212     $this->node_id = $node['node_id'];
213     $this->hostname = $node['hostname'];
214     $this->boot_state = $node['boot_state'];
215     $this->run_level = $node['run_level'];
216     $this->date_created = $node['date_created'];
217     $this->last_updated = $node['last_updated'];
218     $this->last_contact = $node['last_contact'];
219     $this->site_id = $node['site_id'];
220     $this->pcu_ids = $node['pcu_ids'];
221     $this->nn = $api->GetInterfaces($node['interface_ids']);
222     foreach ($this->nn as $nnet)
223       {
224         if ( $nnet['is_primary'] == true )
225           {
226             $this->ip = $nnet['ip'];
227             $this->netmask = $nnet['netmask'];
228             $this->network = $nnet['network'];
229             $this->gateway= $nnet['gateway'];
230             $this->broadcast = $nnet['broadcast'];
231             $this->dns1 = $nnet['dns1'];
232             $this->dns2 = $nnet['dns2'];
233             $this->method = $nnet['method'];
234             $this->interface_id = $nnet['interface_id'];
235           }
236       }
237   }
238
239   public static function filter($nodes, $nodes_listed) {
240     $ret = array();
241     foreach ($nodes as $node) {
242       if ( ! in_array($node, $nodes_listed) ) {
243         $ret[] = $node;
244       }
245     }
246     return $ret;
247   }
248
249   function link($str) {
250     return "<a href='/db/nodes/index.php?id=" . $this->node_id . "'>" . $str . "</a>";
251   }
252   function pcuport($pcu) {
253     $count = 0;
254     foreach ( $pcu->data['node_ids'] as $node_id ) {
255       if ( $node_id == $this->node_id ) {
256         return $pcu->data['ports'][$count];
257       }
258       $count += 1;
259     }
260     return 0;
261   }
262
263   function hasPCU($pcu) {
264     $pcu_id = $pcu->data['pcu_id'];
265     return in_array( $pcu_id, $this->pcu_ids );
266   }
267   function dateCreated() {
268     $date_created = date("M j, Y", $this->date_created);
269     return $date_created;
270   }
271   function lastUpdated() {
272     return $this->timeaway($this->last_updated);
273   }
274   function lastContact() {
275     return $this->timeaway($this->last_contact);
276   }
277
278   function timeaway($val) {
279     if ( $val != NULL ) {
280       $ret = timeDiff(intval($val));
281     } else {
282       $ret = "Never";
283     }
284     return $ret;
285   }
286
287   // code needs to be accessible from outside an object too 
288   // b/c of the performance overhead of creating as many objects as nodes
289   static function status_label_class__ ($boot_state, $run_level, $last_contact, $peer_id) {
290     $label= $run_level ? $run_level : ( $boot_state . '*' ) ;
291     if (Node::stale_($last_contact,$peer_id)) $label .= '...';
292     $class=($label=="boot") ? 'node-ok' : 'node-ko';
293     return array($label,$class);
294   }
295   static function status_label_class_ ($node) {
296     return Node::status_label_class__ ($node['boot_state'],$node['run_level'],$node['last_contact'], $node['peer_id']);
297   }
298   function status_label_class () {
299     return Node::status_label_class__ ($this->boot_state,$this->run_level,$this->last_contact, $this->peer_id);
300   }
301   static function status_footnote () {
302     return "state; * if node doesn't have an observed state; ... if status is stale (" . Node::stale_text() . ")";
303   }
304   
305   // ditto
306   static function stale_ ($last_contact, $peer_id) {
307     // remote nodes don't have a last_contact
308     if ( $peer_id) return false;
309     $STALE_LENGTH = 2*60*60;    /* TODO: set by some policy */
310     $now = time();
311     return ( $last_contact + $STALE_LENGTH < $now );
312   }
313   function stale() { return Node::stale_ ($this->last_contact,$this->peer_id); }
314   static function stale_text() { return "2 hours"; }
315
316 }
317
318 class Slice {
319   var $data;
320
321   function __construct($val) {
322     $this->data = $val;
323   }
324
325   //            <!--sort_slices( $slices ); -->
326   function dateCreated() {
327     $date_created = date("M j, Y", $this->data['created']);
328     return $date_created;
329   }
330
331   function expires() {
332     if ( $this->data['expires'] != 0 ) {
333       $expires = timeDiff(intval($this->data['expires']));
334     } else {
335       $expires = "Never";
336     }
337     return $expires;
338   }
339 }
340
341 class Site extends PlcObject {
342   var $address_ids;
343   var $pcu_ids;
344   var $node_ids;
345   var $person_ids;
346   var $slice_ids;
347   var $enabled;
348   var $peer_id;
349   var $site_id;
350   var $data;
351
352   function __construct($site_id) {
353     global $plc, $api, $adm;
354     $site_info= $adm->GetSites( array( intval($site_id) ) );
355     $this->data = $site_info[0];
356
357     $this->site_id = intval($site_id);
358     $this->site_name = $site_info[0]['name'];
359     $this->address_ids = $site_info[0]['address_ids'];
360     $this->pcu_ids = $site_info[0]['pcu_ids'];
361     $this->node_ids = $site_info[0]['node_ids'];
362     $this->person_ids = $site_info[0]['person_ids'];
363     $this->slice_ids = $site_info[0]['slice_ids'];
364     $this->enabled = $site_info[0]['enabled'];
365     $this->peer_id = $site_info[0]['peer_id'];
366   }
367
368   function getSiteObjects() {
369     global $plc, $api, $adm;
370     $adm->begin();
371     $adm->GetAddresses( $this->address_ids );
372     $adm->GetPCUs( $this->pcu_ids );
373     $adm->GetNodes( $this->node_ids, array( "node_id", "hostname", "boot_state",
374                                             "date_created", "last_updated", "last_contact", "site_id", "pcu_ids" ) );
375     $adm->GetPersons( $this->person_ids, array( "role_ids", "person_id", "first_name", 
376                                                 "last_name", "email", "enabled" ) );
377     $adm->GetSlices( $this->slice_ids, array( "name", "slice_id", "instantiation", "created", "expires" ) );
378     return $adm->commit();
379   }
380 }
381
382 ?>