using as many as one php object per node was killing the UI
[plewww.git] / planetlab / nodes / nodes.php
1 <?php
2
3 // $Id$
4
5 // Require login
6 require_once 'plc_login.php';
7
8 // Get session and API handles
9 require_once 'plc_session.php';
10 global $plc, $api;
11
12 // Print header
13 require_once 'plc_drupal.php';
14 include 'plc_header.php';
15
16 // Common functions
17 require_once 'plc_functions.php';
18 require_once 'plc_objects.php';
19 require_once 'plc_peers.php';
20 require_once 'plc_visibletags.php';
21 require_once 'linetabs.php';
22 require_once 'table.php';
23 require_once 'nifty.php';
24
25 ini_set("memory_limit","64M");
26
27 // -------------------- 
28 // recognized URL arguments
29 $peerscope=$_GET['peerscope'];
30 $pattern=$_GET['pattern'];
31 $site_id=intval($_GET['site_id']);
32 $slice_id=intval($_GET['slice_id']);
33 $person_id=intval($_GET['person_id']);
34
35 // --- decoration
36 $title="Nodes";
37 $tabs=array();
38 $tabs []= tab_nodes();
39 if (count (plc_my_site_ids()) == 1) {
40     $tabs []= tab_nodes_mysite();
41 } else {
42     $tabs []= tab_nodes_all_mysite();
43 }
44 $tabs []= tab_nodes_local();
45
46 // -------------------- 
47 $node_filter=array();
48
49 //////////////////
50 // performs sanity check and summarize the result in a single column
51 function node_status ($node) {
52
53   $messages=array();
54   if ($node['node_type'] != 'regular') 
55     $messages []= $node['node_type'];
56
57   // checks on local nodes only
58   if ( ( ! $node['peer_id']) ) {
59     // has it got interfaces 
60     if (count($node['interface_ids']) == 0) 
61       $messages []= "No interface";
62   }
63   return plc_vertical_table($messages,'plc-warning');
64 }
65
66 // fetch nodes 
67 $node_fixed_columns=array('hostname','node_type','site_id','node_id','boot_state','run_level','last_contact',
68                           'interface_ids','peer_id', 'slice_ids');
69 $visibletags = new VisibleTags ($api, 'node');
70 $visiblecolumns = $visibletags->column_names();
71 $node_columns=array_merge($node_fixed_columns,$visiblecolumns);
72
73 // server-side filtering - set pattern in $_GET for filtering on hostname
74 if ($pattern) {
75   $node_filter['hostname']=$pattern;
76   $title .= " matching " . $pattern;
77  } else {
78   $node_filter['hostname']="*";
79  }
80
81 // server-side selection on peerscope
82 $peerscope=new PeerScope($api,$_GET['peerscope']);
83 $node_filter=array_merge($node_filter,$peerscope->filter());
84 $title .= ' - ' . $peerscope->label();
85
86 if ($site_id) {
87   $sites=$api->GetSites(array($site_id));
88   $site=$sites[0];
89   $name=$site['name'];
90   $login_base=$site['login_base'];
91   $title .= t_site($site);
92   $tabs []= tab_site($site);
93   $node_filter['site_id']=array($site_id);
94 }
95
96 if ($slice_id) {
97   $slices=$api->GetSlices(array($slice_id),array('node_ids','name'));
98   $slice=$slices[0];
99   $title .= t_slice($slice);
100   $tabs []= tab_slice($slice);
101   $node_filter['node_id'] = $slice['node_ids'];
102  }
103
104 // person_id is set : this is mostly oriented towards people managing several sites
105 if ($person_id) {
106   // avoid doing a useless call to GetPersons if the person_id is already known though $plc,
107   // as this is mostly done for the 'all my sites nodes' link
108   if ($person_id == plc_my_person_id()) { 
109     $person=plc_my_person();
110     $site_ids = plc_my_site_ids();
111   } else {
112     // fetch the person's site_ids
113     $persons = $api->GetPersons(array('person_id'=>$person_id),array('person_id','email','site_ids'));
114     $person=$persons[0];
115     $site_ids=$person['site_ids'];
116   }
117   $title .= t_person($person);
118   $node_filter['site_id']=$site_ids;
119  }
120
121 // go
122 $nodes=$api->GetNodes($node_filter,$node_columns);
123
124 // build site_ids - interface_ids
125 $site_ids=array();
126 $interface_ids=array();
127 if ($nodes) foreach ($nodes as $node) {
128   $site_ids []= $node['site_id'];
129   $interface_ids = array_merge ($interface_ids,$node['interface_ids']);
130 }
131
132 // fetch related interfaces
133 $interface_columns=array('ip','node_id','interface_id');
134 $interface_filter=array('is_primary'=>TRUE,'interface_id'=>$interface_ids);
135 $interfaces=$api->GetInterfaces($interface_filter,$interface_columns);
136
137 $interface_hash=array();
138 foreach ($interfaces as $interface) $interface_hash[$interface['node_id']]=$interface;
139
140 // fetch related sites
141 $site_columns=array('site_id','login_base');
142 $site_filter=array('site_id'=>$site_ids);
143 $sites=$api->GetSites($site_filter,$site_columns);
144
145 $site_hash=array();
146 foreach ($sites as $site) $site_hash[$site['site_id']]=$site;
147
148 // --------------------
149 drupal_set_title($title);
150
151 plekit_linetabs($tabs);
152
153 if ( ! $nodes ) {
154   drupal_set_message ('No node found');
155   return;
156  }
157   
158 $nifty=new PlekitNifty ('','objects-list','big');
159 $nifty->start();
160 $headers = array (); $offset=0;
161 $notes=array();
162
163 // fixed columns
164 if (plc_is_admin()) { 
165   $short="I"; $long="node_id"; $type='int'; 
166         $headers[$short]=array('type'=>$type,'title'=>$long); $notes []= "$short = $long";
167   $offset=1; 
168  }
169 $short="P"; $long="Peer"; $type='string'; 
170         $headers[$short]=array('type'=>$type,'title'=>$long); $notes []= "$short = $long";
171 $short="D"; $long="toplevel domain name"; $type='string'; 
172         $headers[$short]=array('type'=>$type,'title'=>$long); $notes []= "$short = $long";
173 $headers["Site"]="string";
174 $headers["Hostname"]="string";
175 $short="IP"; $long="IP Address"; $type='sortIPAddress'; 
176         $headers[$short]=array('type'=>$type,'title'=>$long); $notes []= "$short = $long";
177 $short="ST"; $long=Node::status_footnote(); $type='string'; 
178         $headers[$short]=array('type'=>$type,'title'=>$long); $notes []= "$short = $long";
179 $short="SL"; $long="Number of slivers"; $type='int'; 
180         $headers[$short]=array('type'=>$type,'title'=>$long); $notes []= "$short = $long";
181
182 $headers=array_merge($headers,$visibletags->headers());
183 $notes=array_merge($notes,$visibletags->notes());
184 $short="?"; $long="extra status info"; $type='string'; 
185         $headers[$short]=array('type'=>$type,'title'=>$long); $notes []= "$short = $long";
186
187 # initial sort on hostnames
188 $table=new PlekitTable ("nodes",$headers,3+$offset);
189 $table->start();
190
191 $peers = new Peers ($api);
192 // write rows
193 foreach ($nodes as $node) {
194   //$node_obj = new Node ($node);
195   $hostname=$node['hostname'];
196   $node_id=$node['node_id'];
197   $site_id=$node['site_id'];
198   $site=$site_hash[$site_id];
199   $login_base = $site['login_base'];
200   $ip=$interface_hash[$node['node_id']]['ip'];
201   $interface_id=$interface_hash[$node['node_id']]['interface_id'];
202   $peer_id=$node['peer_id'];
203   
204   $table->row_start();
205   if (plc_is_admin()) $table->cell(l_node_t($node_id,$node_id));
206   $peers->cell ($table,$peer_id);
207   $table->cell (topdomain($hostname));
208   $table->cell (l_site_t($site_id,$login_base));
209   $table->cell (l_node_t($node_id,$hostname));
210   $table->cell (l_interface_t($interface_id,$ip),array('only-if'=> !$peer_id));
211   list($label,$class) = Node::status_label_class_($node);
212   $table->cell ($label,array('class'=>$class));
213   $table->cell (count($node['slice_ids']));
214   foreach ($visiblecolumns as $tagname) $table->cell($node[$tagname]);
215   $table->cell (node_status($node));
216   $table->row_end();
217   
218 }
219
220 $table->end(array('notes'=>$notes));
221 $nifty->end();
222
223 //plekit_linetabs ($tabs,"bottom");
224
225 // Print footer
226 include 'plc_footer.php';
227
228 ?>