Updated demo scripts: the demo now outputs per-protocol information.
[affinity.git] / scripts / stats.py
index 87a78f125be1d283dc38a59a4d2d0bb1bac2227a..cb1c17dae0e3e0c88d17363932793f8455a65820 100644 (file)
@@ -1,7 +1,6 @@
 #!/usr/bin/python
 
-import httplib2
-import json
+import analytics
 
 '''
 Class for keeping track of host statistics
@@ -17,21 +16,17 @@ class Stats:
         if stat_type == Stats.TYPE_HOST:
             self.src = kwargs['src']
             self.dst = kwargs['dst']
-            self.url_prefix = "http://localhost:8080/affinity/nb/v2/analytics/default/hoststats/" 
         elif stat_type == Stats.TYPE_AL:
             self.al = kwargs['al']
-            self.url_prefix = "http://localhost:8080/affinity/nb/v2/analytics/default/affinitylinkstats/"
         elif stat_type == Stats.TYPE_SUBNET:
             self.subnet = kwargs['subnet']
-            self.url_prefix = "http://localhost:8080/affinity/nb/v2/analytics/default/subnetstats/"
         else:
             print "incorrect stat type", stat_type
 
         self.stats = {}
+        self.protocol_stats = {}
         self.rate_ewma = None
         self.large_flow_threshold = 5 * 10**6 # in bytes
-        self.http = httplib2.Http(".cache")
-        self.http.add_credentials('admin', 'admin')
 
     def __str__(self):
         if (self.stat_type == Stats.TYPE_HOST):
@@ -46,18 +41,19 @@ class Stats:
     # Refresh statistics
     def refresh(self):
         if (self.stat_type == Stats.TYPE_HOST):
-            resp, content = self.http.request(self.url_prefix + self.src + "/" + self.dst, "GET")
+            self.stats = analytics.stats_hosts(self.src, self.dst, False)
+            self.protocol_stats = analytics.all_stats_hosts(self.src, self.dst, False)
         elif (self.stat_type == Stats.TYPE_AL):
-            resp, content = self.http.request(self.url_prefix + self.al, "GET")
+            self.stats = analytics.stats_link(self.al, False)
+            self.protocol_stats = analytics.all_stats_link(self.al, False)
         elif (self.stat_type == Stats.TYPE_SUBNET):
-            resp, content = self.http.request(self.url_prefix + "null/null/" + self.subnet, "GET")
+            self.stats = analytics.stats_subnet("null/null", self.subnet, False)
+            self.protocol_stats = analytics.all_stats_subnet("null/null", self.subnet, False)
         try:
-            self.stats = json.loads(content)
             is_fast = self.handle_rate_ewma()
             is_big = self.check_large_flow()
             return [is_fast, is_big]
-        except Exception as e:
-            print "error: ", e
+        except:
             return [False, False]
 
     def set_large_flow_threshold(self, s):
@@ -66,10 +62,8 @@ class Stats:
     # Return all hosts that transferred a particular percentage of data into this entity.  Right now only supports subnets.
     def get_large_incoming_hosts(self):
         if (self.stat_type == Stats.TYPE_SUBNET):
-            resp, content = self.http.request(self.url_prefix + "incoming/" + self.subnet, "GET")
-            data = json.loads(content)
+            data = analytics.incoming_hosts(self.subnet, False)
             if (data == {}): return []
-            data = data['stats']
             ips = []
             total_bytes_in = self.get_bytes()
             n = len(data)
@@ -79,7 +73,6 @@ class Stats:
                 if (bytes_from_ip >= total_bytes_in / float(n)):
                     ips.append(ip)
             return ips
-
         else:
             print "Stat type not supported for incoming hosts"
         return []
@@ -108,14 +101,27 @@ class Stats:
         return False
 
     # Bytes
-    def get_bytes(self):
+    def get_bytes(self, protocol=None):
         try:
-            bytes = long(self.stats["byteCount"])
+            if (protocol == None):
+                bytes = long(self.stats["byteCount"])
+            else:
+                bytes = long(self.protocol_stats[protocol]["byteCount"])
         except Exception as e:
-            print "exception: ", e
             bytes = 0
         return bytes
 
+    # Packets
+    def get_packets(self, protocol=None):
+        try:
+            if (protocol == None):
+                packets = long(self.stats["packetCount"])
+            else:
+                packets = long(self.protocol_stats[protocol]["byteCount"])
+        except Exception as e:
+            packets = 0
+        return packets
+
     # Bit Rate
     def get_bit_rate(self):
         try: