From 8b38cb08a0c9ae9611a8cee469003843b59f5907 Mon Sep 17 00:00:00 2001
From: Steven Cahail <scahail@codeaurora.org>
Date: Fri, 23 Oct 2015 17:44:58 -0600
Subject: [PATCH] ipc_logging: Refactor LogPage_v1.sortAndLink()

LogPage_v1.sortAndLink() has excessive cyclomatic complexity. Reduce the
complexity by refactoring sortAndLink() into several smaller functions.

This commit is to be squashed into the commit containing all other
style fixes.

Change-Id: I52ac6027e5f9d45af7d44d4b24beff7a28baea05
---
 .../parsers/ipc_logging.py                    | 101 +++++++++++-------
 1 file changed, 64 insertions(+), 37 deletions(-)

diff --git a/linux-ramdump-parser-v2/parsers/ipc_logging.py b/linux-ramdump-parser-v2/parsers/ipc_logging.py
index cfacc11..d9c7b73 100644
--- a/linux-ramdump-parser-v2/parsers/ipc_logging.py
+++ b/linux-ramdump-parser-v2/parsers/ipc_logging.py
@@ -909,6 +909,67 @@ class LogPage_v1(LogPage):
         self.context.header_size = V1_PAGE_HDR_SIZE
         self.page_header_size = self.context.header_size
 
+    def find_min_end_position(self, lstPages):
+        """
+        Find the last page written to. Since the pages are filled in order by
+        page ID, the first non-full page is the last page written to.
+
+        :param lstPages: The list of pages
+
+        :return: The index of the last page written to
+        """
+        min_end_time = None
+        min_end_position = 0
+
+        for n in range(len(lstPages)):
+            cur_end_time = lstPages[n].get_end_time()
+            if cur_end_time == 0:
+                continue
+
+            if not min_end_time:
+                min_end_time = cur_end_time
+                min_end_position = n
+                continue
+
+            if cur_end_time > 0 and cur_end_time < min_end_time:
+                min_end_time = cur_end_time
+                min_end_position = n
+
+        return min_end_position
+
+    def find_min_position(self, lstPages, min_position):
+        """
+        Find the first chronological page (the page with the lowest non-zero
+        end time.
+
+        :param lstPages: The list of pages
+        :param min_position: The index of the first non-full page
+
+        :return: The index of the first chronological page
+        """
+        min_start_time = None
+        min_start_position = 0
+
+        if lstPages[min_position].read_offset == 0:
+            for n in range(len(lstPages)):
+                cur_start_time = lstPages[n].get_start_time()
+                if cur_start_time == 0:
+                    continue
+
+                if not min_start_time:
+                    min_start_time = cur_start_time
+                    min_start_position = n
+                    continue
+
+                if cur_start_time > 0 and cur_start_time < min_start_time:
+                    min_start_time = cur_start_time
+                    min_start_position = n
+
+            if lstPages[min_start_position].read_offset != 0:
+                min_position = min_start_position
+
+        return min_position
+
     def sortAndLink(self, lstPages, bSort):
         """
         Given a list of pages in ascending page number order,
@@ -928,44 +989,10 @@ class LogPage_v1(LogPage):
         # Since the pages are filled in order by page-id, the first
         # non-full page is the last page written to.
         if bSort:
-            # Rotate to lowest non-zero end time
-            min_end_time = None
-            min_start_time = None
-            min_end_position = 0
-            min_start_position = 0
-            for n in range(len(lstPages)):
-                cur_end_time = lstPages[n].get_end_time()
-                if cur_end_time == 0:
-                    continue
-
-                if not min_end_time:
-                    min_end_time = cur_end_time
-                    min_end_position = n
-                    continue
-
-                if cur_end_time > 0 and cur_end_time < min_end_time:
-                    min_end_time = cur_end_time
-                    min_end_position = n
-
-            min_position = min_end_position
-            if lstPages[min_end_position].read_offset == 0:
-                for n in range(len(lstPages)):
-                    cur_start_time = lstPages[n].get_start_time()
-                    if cur_start_time == 0:
-                        continue
-
-                    if not min_start_time:
-                        min_start_time = cur_start_time
-                        min_start_position = n
-                        continue
-
-                    if cur_start_time > 0 and cur_start_time < min_start_time:
-                        min_start_time = cur_start_time
-                        min_start_position = n
-
-                if lstPages[min_start_position].read_offset != 0:
-                    min_position = min_start_position
+            min_end_position = self.find_min_end_position(lstPages)
+            min_position = self.find_min_position(lstPages, min_end_position)
 
+            # Rotate to lowest non-zero end time
             lstPages.rotate(-min_position)
             lstPages = list(lstPages)
 
-- 
GitLab