OpenBMC test metrics

Tool to generate the test statistics from robot framework generated
output.xml file.

Adding support for the following fields:
 - driver
 - env
 - proc
 - platform_type
 - test_func_area

Resolves openbmc/openbmc-test-automation#217

Change-Id: I1eb911f041c55713182d38aba6343b7ac0540836
Signed-off-by: George Keishing <gkeishin@in.ibm.com>
diff --git a/tools/oem/ibm/gen_csv_results.py b/tools/oem/ibm/gen_csv_results.py
index f3174f4..3e2f657 100755
--- a/tools/oem/ibm/gen_csv_results.py
+++ b/tools/oem/ibm/gen_csv_results.py
@@ -15,10 +15,12 @@
 import robot.errors
 from robot.api import ExecutionResult
 from robot.result.visitor import ResultVisitor
+from xml.etree import ElementTree
+import re
 
 
-def parse_output_xml(i_source, i_dest):
-    result = ExecutionResult(i_source)
+def parse_output_xml(xml_file_path, csv_dir_path):
+    result = ExecutionResult(xml_file_path)
     result.configure(stat_config={'suite_stat_level': 2,
                                   'tag_stat_combine': 'tagANDanother'})
 
@@ -45,26 +47,45 @@
 
     # Write the result statistics attributes to CSV file
     l_csvlist = []
+
+    # Default Test data
     l_subsys = 'OPENBMC'
     l_test_type = 'FTC'
     l_pse_rel = 'OBMC910'
+    l_env = 'HW'
+    l_proc = 'P9'
+    l_platform_type = ""
+    l_func_area = ""
+
+    # System data from XML meta data
+    l_system_info = get_system_details(xml_file_path)
+    l_driver = l_system_info[0]
+    if l_system_info[1]:
+        l_platform_type = l_system_info[1]
+    else:
+        print "System model is not set"
+        sys.exit()
 
     # Default header
     l_header = ['test_start', 'test_end', 'subsys', 'test_type',
-                'test_result', 'test_name', 'pse_rel']
+                'test_result', 'test_name', 'pse_rel', 'driver',
+                'env', 'proc', 'platform_type', 'test_func_area']
 
     l_csvlist.append(l_header)
 
     # Generate CSV file onto the path with current time stamp
-    l_base_dir = i_dest
+    l_base_dir = csv_dir_path
     l_timestamp = datetime.utcnow().strftime("%Y-%m-%d-%H-%M-%S")
-    l_csvfile = l_base_dir + l_timestamp + ".csv"
+    # Example: 2017-02-20-08-47-22_Witherspoon.csv
+    l_csvfile = l_base_dir + l_timestamp + "_" + l_platform_type + ".csv"
 
     print "Writing data into csv file:", l_csvfile
 
     for testcase in collectDataObj.testData:
-        # Test category : Test case type
-        l_test_name = str(testcase.parent) + ":" + str(testcase)
+        # Functional Area: Suite Name
+        # Test Name: Test Case Name
+        l_func_area = str(testcase.parent).split(' ', 1)[1]
+        l_test_name = str(testcase)
 
         # Test Result pass=0 fail=1
         if testcase.status == 'PASS':
@@ -72,10 +93,15 @@
         else:
             l_test_result = 1
 
+        # Format datetime from robot output.xml to "%Y-%m-%d-%H-%M-%S"
+        l_stime = xml_to_csv_time(testcase.starttime)
+        l_etime = xml_to_csv_time(testcase.endtime)
         # Data Sequence: test_start,test_end,subsys,test_type,
-        #                test_result,test_name,pse_rel
-        l_data = [testcase.starttime, testcase.endtime, l_subsys,
-                  l_test_type, l_test_result, l_test_name, l_pse_rel]
+        #                test_result,test_name,pse_rel,driver,
+        #                env,proc,platform_tyep,test_func_area,
+        l_data = [l_stime, l_etime, l_subsys, l_test_type, l_test_result,
+                  l_test_name, l_pse_rel, l_driver, l_env, l_proc,
+                  l_platform_type, l_func_area]
         l_csvlist.append(l_data)
 
     # Open the file and write to the CSV file
@@ -85,6 +111,54 @@
     l_file.close()
 
 
+def xml_to_csv_time(xml_datetime):
+    r"""
+    Convert the time from %Y%m%d %H:%M:%S.%f format to %Y-%m-%d-%H-%M-%S format
+    and return it.
+
+    Description of arguments:
+        datetime  The date in the following format: %Y%m%d %H:%M:%S.%f
+                  (This is the format typically found in an XML file.)
+
+    The date returned will be in the following format: %Y-%m-%d-%H-%M-%S
+    """
+    # 20170206 05:05:19.342
+    l_str = datetime.strptime(xml_datetime, "%Y%m%d %H:%M:%S.%f")
+    # 2017-02-06-05-05-19
+    l_str = l_str.strftime("%Y-%m-%d-%H-%M-%S")
+    return str(l_str)
+
+
+def get_system_details(xml_file_path):
+    r"""
+    Get the system data from output.xml generated by robot and return it.
+    The list returned will be in the following order: [driver,platfrom]
+
+    Description of arguments:
+        xml_file_path  The relative or absolute path to the output.xml file.
+    """
+    bmc_version = ""
+    bmc_platform = ""
+    with open(xml_file_path, 'rt') as output:
+        tree = ElementTree.parse(output)
+
+    for node in tree.iter('msg'):
+        # /etc/os-release output is logged in the XML as msg
+        # Example: ${output} = VERSION_ID="v1.99.2-71-gbc49f79-dirty"
+        if '${output} = VERSION_ID=' in node.text:
+            # Get BMC version (e.g. v1.99.1-96-g2a46570-dirty)
+            bmc_version = str(node.text.split("VERSION_ID=")[1])[1:-1]
+
+        # Platform is logged in the XML as msg.
+        # Example: ${bmc_model} = Witherspoon BMC
+        if '${bmc_model} = ' in node.text:
+            bmc_platform = node.text.split(" = ")[1]
+
+    print "BMC Version:", bmc_version
+    print "BMC Platform:", bmc_platform
+    return [str(bmc_version).strip("-dirty"), str(bmc_platform).strip(" BMC")]
+
+
 def usage():
     name = 'gen_csv_results.py'
     print 'Usage: '