5 function timeDiff ($timestamp,$detailed=false,$n = 0) {
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 //)
13 // echo "Val: greater $timestamp : $now<br>\n";
15 // echo "Val: less than $timestamp : $now<br>\n";
19 $diff = ($action == 'away' ? $timestamp - $now : $now - $timestamp);
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);
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
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; }
45 # Basic error checking.
47 return "error: bad time";
54 public static function constructList($cname, $list) {
56 foreach ($list as $item) {
57 $ret_list[] = new $cname ($item);
72 function Person($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;
82 public static function getPIs($persons) {
84 foreach( $persons as $person ) {
85 $role_ids= $person->roles;
87 if ( in_array( '20', $role_ids ) && $person->enabled )
88 $pis[$person->person_id]= $person->email;
93 public static function getTechs($persons) {
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;
103 function getSites() {
104 return $this->data['site_ids'];
106 function isMember($site_id) {
107 return in_array($site_id, $this->data['site_ids']);
111 return in_array( '10', $this->roles);
114 return in_array( '20', $this->roles);
117 return in_array( '30', $this->roles);
120 return in_array( '40', $this->roles);
123 function link($str) {
124 return "<a href='/db/persons/index.php?id=" . $this->person_id . "'>" . $str . "</a>";
129 $person[] = $this->first_name . " " . $this->last_name;
130 $person[] = $this->link($this->email);
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'] . "'> Remove from PCU</a>";
148 function pcu_name() {
149 if ( $this->data['hostname'] != NULL and $this->data['hostname'] != "" ):
150 return $this->data['hostname'];
152 if ( $this->data['ip'] != NULL and $this->data['ip'] != "" ):
153 return $this->data['ip'];
155 return "NO-HOSTNAME-OR-IP";
160 function link($str) {
161 return "<a href='/db/sites/pcu.php?id=" . $this->data['pcu_id'] . "'>" . $str . "</a>";
165 return substr($this->data['hostname'], 0, strpos($this->data['hostname'], '.'));
172 function Address($address) {
173 $this->data = $address;
176 function link($str) {
177 return "<a href='/db/addresses/index.php?id=" . $this->data['address_id'] . "'>" . $str . "</a>";
182 $comma= sizeof( $this->data['address_types'] );
184 foreach( $this->data['address_types'] as $add_type ) {
187 if ( $comma > 0 && $count != $comma )
196 class Node extends PlcObject {
207 function Node($node) {
208 global $plc, $api, $adm;
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)
224 if ( $nnet['is_primary'] == true )
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'];
239 public static function filter($nodes, $nodes_listed) {
241 foreach ($nodes as $node) {
242 if ( ! in_array($node, $nodes_listed) ) {
249 function link($str) {
250 return "<a href='/db/nodes/index.php?id=" . $this->node_id . "'>" . $str . "</a>";
252 function pcuport($pcu) {
254 foreach ( $pcu->data['node_ids'] as $node_id ) {
255 if ( $node_id == $this->node_id ) {
256 return $pcu->data['ports'][$count];
263 function hasPCU($pcu) {
264 $pcu_id = $pcu->data['pcu_id'];
265 return in_array( $pcu_id, $this->pcu_ids );
267 function dateCreated() {
268 $date_created = date("M j, Y", $this->date_created);
269 return $date_created;
271 function lastUpdated() {
272 return $this->timeaway($this->last_updated);
274 function lastContact() {
275 return $this->timeaway($this->last_contact);
278 function timeaway($val) {
279 if ( $val != NULL ) {
280 $ret = timeDiff(intval($val));
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);
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']);
298 function status_label_class () {
299 return Node::status_label_class__ ($this->boot_state,$this->run_level,$this->last_contact, $this->peer_id);
301 static function status_footnote () {
302 return "state; * if node doesn't have an observed state; ... if status is stale (" . Node::stale_text() . ")";
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 */
311 return ( $last_contact + $STALE_LENGTH < $now );
313 function stale() { return Node::stale_ ($this->last_contact,$this->peer_id); }
314 static function stale_text() { return "2 hours"; }
321 function Slice($val) {
325 // <!--sort_slices( $slices ); -->
326 function dateCreated() {
327 $date_created = date("M j, Y", $this->data['created']);
328 return $date_created;
332 if ( $this->data['expires'] != 0 ) {
333 $expires = timeDiff(intval($this->data['expires']));
341 class Site extends PlcObject {
352 function Site($site_id) {
353 global $plc, $api, $adm;
354 $site_info= $adm->GetSites( array( intval($site_id) ) );
355 $this->data = $site_info[0];
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'];
368 function getSiteObjects() {
369 global $plc, $api, $adm;
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();
382 /* class Blue extends PlcObject
391 $cl = PlcObject::constructList('Blue', array('this', 'is', 'a', 'test'));
392 echo sizeof($cl) . "\n";
393 foreach ($cl as $obj)
395 echo get_class( $obj) . "\n";
396 echo $obj->val . "\n";