Browse Source

add interval statistics to export

add multiple current interval statistics tables export support
add export support for the default interval
Jens Keim 5 years ago
parent
commit
db1ac62882
2 changed files with 77 additions and 20 deletions
  1. 40 2
      code/Core/Statistics.py
  2. 37 18
      code/Core/StatsDatabase.py

+ 40 - 2
code/Core/Statistics.py

@@ -92,7 +92,7 @@ class Statistics:
         elif not isinstance(intervals, list):
             intervals = [intervals]
 
-        current_interval = intervals[0]
+        current_intervals = intervals[:]
 
         # Inform user about recalculation of statistics and its reason
         if flag_recalculate_stats:
@@ -154,7 +154,7 @@ class Statistics:
                 self.pcap_proc.collect_statistics(intervals)
                 self.pcap_proc.write_new_interval_statistics(self.path_db, intervals)
 
-        self.stats_db.set_current_interval_statistics_table(current_interval)
+        self.stats_db.set_current_interval_statistics_tables(current_intervals)
 
         # Load statistics from database
         self.file_info = self.stats_db.get_file_info()
@@ -205,6 +205,28 @@ class Statistics:
                 ("Avg. bandwidth in", self.file_info['avgBandwidthIn'], "kbit/s"),
                 ("Avg. bandwidth out", self.file_info['avgBandwidthOut'], "kbit/s")]
 
+    def get_interval_statistics(self, table_name: str=""):
+        """
+        Returns a list of tuples, each containing interval statistics.
+
+        :param table_name: the name of the interval statistics table
+        :return: a list of tuples, each consisting of (description, values, unit).
+        """
+        return [("First packet timestamp(seconds)",
+                 [int(item[0]) for item in self.stats_db.process_interval_statistics_query(
+                     "SELECT starttimestamp from %s", table_name)], ""),
+                ("Last packet timestamp(seconds)",
+                 [int(item[0]) for item in self.stats_db.process_interval_statistics_query(
+                     "SELECT lastpkttimestamp from %s", table_name)], ""),
+                ("Avg. packet rate(packets/sec)", [item[0] for item in self.stats_db.process_interval_statistics_query(
+                    "SELECT pktrate from %s", table_name)], ""),
+                ("packets count(packets)", [item[0] for item in self.stats_db.process_interval_statistics_query(
+                    "SELECT pktscount from %s", table_name)], ""),
+                ("Avg. kbyte rate(kbytes/sec)", [item[0] for item in self.stats_db.process_interval_statistics_query(
+                    "SELECT kbyterate from %s", table_name)], ""),
+                ("kbyte count(kbytes)", [item[0] for item in self.stats_db.process_interval_statistics_query(
+                    "SELECT kbytes from %s", table_name)], "")]
+
     @staticmethod
     def write_list(desc_val_unit_list, func, line_ending="\n"):
         """
@@ -531,6 +553,16 @@ class Statistics:
             target.write(title + " \n")
             target.write("====================== \n")
 
+        def _write_sub_header(title: str):
+            """
+            Writes the section header into the open file.
+
+            :param title: The section title
+            """
+            target.write("---------------------- \n")
+            target.write(title + " \n")
+            target.write("---------------------- \n")
+
         target = open(self.pcap_filepath + ".stat", 'w')
         target.truncate()
 
@@ -540,6 +572,12 @@ class Statistics:
         _write_header("General statistics")
         Statistics.write_list(self.get_general_file_statistics(), target.write)
 
+        _write_header("Interval statistics")
+        tables = self.stats_db.get_all_current_interval_statistics_tables()
+        for table in tables:
+            _write_sub_header(table)
+            Statistics.write_list(self.get_interval_statistics(table), target.write)
+
         _write_header("Tests statistics")
         Statistics.write_list(self.get_tests_statistics(), target.write)
 

+ 37 - 18
code/Core/StatsDatabase.py

@@ -41,7 +41,7 @@ class StatsDatabase:
         self.existing_db = os.path.exists(db_path)
         self.database = sqlite3.connect(db_path)
         self.cursor = self.database.cursor()
-        self.current_interval_statistics_table = ""
+        self.current_interval_statistics_tables = []
 
         # If DB not existing, create a new DB scheme
         if self.existing_db:
@@ -159,21 +159,37 @@ class StatsDatabase:
         """
         :return: the current interval statistics table used for internal calculations
         """
-        return self.current_interval_statistics_table
+        if len(self.current_interval_statistics_tables) > 0:
+            return self.current_interval_statistics_tables[0]
+        else:
+            return ""
 
-    def set_current_interval_statistics_table(self, current_interval: float=0.0):
+    def get_all_current_interval_statistics_tables(self):
         """
-        Sets the current interval statistics table, which should be used for internal calculations.
-        :param current_interval: the current interval, which should be used for internal calculations, in seconds
+        :return: the list of all current interval statistics tables
         """
-        if current_interval == 0.0:
-            table_name = self.process_db_query("SELECT name FROM interval_tables WHERE is_default=1")
-            print(table_name)
-            print("No user specified interval found. Using default interval: " +
-                  str(float(table_name[len("interval_statistics_"):])/1000000) + "s")
-        else:
-            self.current_interval_statistics_table = "interval_statistics_" + str(int(current_interval*1000000))
-            print("User specified interval(s) found. Using first interval length given: " + str(current_interval) + "s")
+        if len(self.current_interval_statistics_tables) == 0:
+            return [self.process_db_query("SELECT name FROM interval_tables WHERE is_default=1")]
+        return self.current_interval_statistics_tables
+
+    def set_current_interval_statistics_tables(self, current_intervals: list):
+        """
+        Sets the current interval statistics table, which should be used for internal calculations.
+        :param current_intervals: a list of current intervals in seconds, first of which should be used for internal
+                                  calculations
+        """
+        for current_interval in current_intervals:
+            if current_interval == 0.0:
+                table_name = self.process_db_query("SELECT name FROM interval_tables WHERE is_default=1")
+                print(table_name)
+                print("No user specified interval found. Using default interval: " +
+                      str(float(table_name[len("interval_statistics_"):])/1000000) + "s")
+            else:
+                self.current_interval_statistics_tables.append("interval_statistics_" +
+                                                               str(int(current_interval*1000000)))
+                if current_interval == current_intervals[0]:
+                    print("User specified interval(s) found. Using first interval length given for internal "
+                          "calculations: " + str(current_interval) + "s")
 
     def named_query_parameterized(self, keyword: str, param_op_val: list):
         """
@@ -386,14 +402,17 @@ class StatsDatabase:
 
         return result
 
-    def process_interval_statistics_query(self, query_string_in: str):
+    def process_interval_statistics_query(self, query_string_in: str, table_param: str=""):
         """
 
-        :param query_string_in:
-        :return:
+        :param query_string_in: a query to be executed over the current internal interval statistics table
+        :param table_param: a name of a specific interval statistics table
+        :return: the result of the query
         """
-        if self.current_interval_statistics_table != "":
-            table_name = self.current_interval_statistics_table
+        if table_param != "":
+            table_name = table_param
+        elif self.get_current_interval_statistics_table() != "":
+            table_name = self.get_current_interval_statistics_table()
         else:
             table_name = self.process_db_query("SELECT name FROM interval_tables WHERE is_default=1")
         return self.process_user_defined_query(query_string_in % table_name)