From 292bfa660615de1d3a4e3c6ed0b8a28c84963f88 Mon Sep 17 00:00:00 2001
From: Ciro Scognamiglio <ciro.scognamiglio@cslash.net>
Date: Fri, 25 Oct 2013 19:20:49 +0200
Subject: [PATCH] slicestat plugin: stats on the sliceview, top 10 nodes

---
 plugins/slicestat/__init__.py              |  2 +-
 plugins/slicestat/static/css/slicestat.css |  2 +-
 plugins/slicestat/static/js/slicestat.js   | 24 +++++++----
 plugins/slicestat/templates/slicestat.html |  2 +-
 portal/sliceview.py                        | 50 +++++++++++++++++++++-
 5 files changed, 67 insertions(+), 13 deletions(-)

diff --git a/plugins/slicestat/__init__.py b/plugins/slicestat/__init__.py
index 7186b761..d601663d 100644
--- a/plugins/slicestat/__init__.py
+++ b/plugins/slicestat/__init__.py
@@ -23,7 +23,7 @@ class Slicestat(Plugin):
         return reqs
 
     def json_settings_list (self):
-        return ['plugin_uuid', 'domid', 'query_uuid']
+        return ['plugin_uuid', 'domid', 'query_uuid', 'slicename', 'o']
 
     def export_json_settings (self):
         return True
diff --git a/plugins/slicestat/static/css/slicestat.css b/plugins/slicestat/static/css/slicestat.css
index 9962c83b..49ee844b 100644
--- a/plugins/slicestat/static/css/slicestat.css
+++ b/plugins/slicestat/static/css/slicestat.css
@@ -1,4 +1,4 @@
-iframe#slicestat_resource {
+iframe.slicestat-iframe {
     width:100%;
     height:400px;
     border:0;
diff --git a/plugins/slicestat/static/js/slicestat.js b/plugins/slicestat/static/js/slicestat.js
index a68704e9..5c1920a6 100644
--- a/plugins/slicestat/static/js/slicestat.js
+++ b/plugins/slicestat/static/js/slicestat.js
@@ -26,7 +26,7 @@
             // Call the parent constructor, see FAQ when forgotten
             this._super(options, element);			 
 			
-
+			
             /* Member variables */
 
             /* Plugin events */
@@ -37,10 +37,17 @@
             // What happens when we don't define some events ?
             // Some can be less efficient
             this.listen_query(options.query_uuid);
-
+			
             /* GUI setup and event binding */
             // call function
-
+			if (typeof options.slicename != 'undefined') {
+        		options.slicename = options.slicename.replace("ple.","").replace(".","_");
+        	}
+        	if (typeof options.o == 'undefined') {
+        		//options.o = 'cpu';
+        	}
+        	
+        	this.elmt().on('show', options, this.on_show);
         },
 
         /* PLUGIN EVENTS */
@@ -64,7 +71,11 @@
             // this.id, this.el, this.cl, this.elts
             // same output as a jquery selector with some guarantees
         },
-
+		
+		on_show: function(ev) 
+		{
+			$('iframe#' + ev.target.id + '-iframe').attr('src','http://plestats.planet-lab.eu/nodes.php?slice='+ev.data.slicename+'&order='+ev.data.o);
+		},
         /* TEMPLATES */
 
         // see in the html template
@@ -88,10 +99,7 @@
         /* RECORD HANDLERS */
         on_new_record: function(record)
         {
-            console.log(record);
-            
-            $('iframe#slicestat_resource').attr('src','http://plestats.planet-lab.eu/node.php?node='+record.hostname);
-            
+        	$('iframe#' + this.elmt().attr('id') + '-iframe').attr('src','http://plestats.planet-lab.eu/node.php?node='+record.hostname);            
         },
 
         /* INTERNAL FUNCTIONS */
diff --git a/plugins/slicestat/templates/slicestat.html b/plugins/slicestat/templates/slicestat.html
index c97cd9e0..15a87553 100644
--- a/plugins/slicestat/templates/slicestat.html
+++ b/plugins/slicestat/templates/slicestat.html
@@ -1,3 +1,3 @@
 <div id={{ domid }}>
-<iframe id="slicestat_resource" src=""></iframe> 
+<iframe class="slicestat-iframe" id="{{ domid }}-iframe" src=""></iframe> 
 </div>
diff --git a/portal/sliceview.py b/portal/sliceview.py
index efceec4a..8767e8b2 100644
--- a/portal/sliceview.py
+++ b/portal/sliceview.py
@@ -20,6 +20,7 @@ from plugins.query_editor            import QueryEditor
 from plugins.active_filters          import ActiveFilters
 from plugins.quickfilter             import QuickFilter
 from plugins.messages                import Messages
+from plugins.slicestat               import Slicestat
 
 from myslice.config                  import Config
 
@@ -173,6 +174,50 @@ class SliceView (LoginRequiredAutoLogoutView):
                 },
             )
 
+        resources_stats_cpu = Slicestat(
+            title = "CPU Usage",
+            domid = 'resources-stats-cpu',
+            page  = page,
+            stats = 'slice',
+            key   = 'hrn',
+            query = 'none',
+            slicename = slicename,
+            o = 'cpu'
+        )
+        
+        resources_stats_mem = Slicestat(
+            title = "Memory Usage",
+            domid = 'resources-stats-mem',
+            page  = page,
+            stats = 'slice',
+            key   = 'hrn',
+            query = 'none',
+            slicename = slicename,
+            o = 'mem'
+        )
+        
+        resources_stats_asb = Slicestat(
+            title = "Traffic Sent",
+            domid = 'resources-stats-asb',
+            page  = page,
+            stats = 'slice',
+            key   = 'hrn',
+            query = 'none',
+            slicename = slicename,
+            o = 'asb'
+        )
+        
+        resources_stats_arb = Slicestat(
+            title = "Traffic Received",
+            domid = 'resources-stats-arb',
+            page  = page,
+            stats = 'slice',
+            key   = 'hrn',
+            query = 'none',
+            slicename = slicename,
+            o = 'arb'
+        )
+        
         # with the new 'Filter' stuff on top, no need for anything but the hazelnut
         resources_as_list_area = resources_as_list 
 
@@ -181,12 +226,11 @@ class SliceView (LoginRequiredAutoLogoutView):
                                 togglable=True,
                                 title="Resources",
                                 outline_complete=True,
-                                sons=[ resources_as_map, resources_as_list_area, ],
+                                sons=[ resources_as_map, resources_as_list_area, resources_stats_cpu, resources_stats_mem, resources_stats_asb, resources_stats_arb ],
                                 active_domid = 'resources-map',
                                 )
         main_stack.insert (resources_area)
 
-
         # --------------------------------------------------------------------------
         # USERS
     
@@ -217,6 +261,8 @@ class SliceView (LoginRequiredAutoLogoutView):
                     'bAutoWidth'     : True,
                 },
             ))
+            
+        
 # DEMO    
         # --------------------------------------------------------------------------
         # MEASUREMENTS
-- 
2.47.0