From c9ae2766fd566358a1ac3a38a7dc6f7b22a3449f Mon Sep 17 00:00:00 2001
From: Martin Klozik <martinx.klozik@intel.com>
Date: Wed, 23 Aug 2017 14:31:31 +0100
Subject: opnfvresultdb: Update data reported to result DB

Data reported to result DB were updated to comply
with recent API version and requirements.

JIRA: VSPERF-488

Change-Id: I6ab02ea54d99099e88b0d5ab6e9745ee8297f544
Signed-off-by: Martin Klozik <martinx.klozik@intel.com>
Signed-off-by: Mars Toktonaliev <mars.toktonaliev@nokia.com>
Signed-off-by: Morgan Richomme <morgan.richomme@orange.com>
Reviewed-by: Al Morton <acmorton@att.com>
Reviewed-by: Christian Trautman <ctrautma@redhat.com>
Reviewed-by: Sridhar Rao <sridhar.rao@spirent.com>
Reviewed-by: Trevor Cooper <trevor.cooper@intel.com>
---
 conf/10_custom.conf                    |  2 +-
 core/results/results_constants.py      |  2 ++
 testcases/testcase.py                  | 10 +++++++++-
 tools/opnfvdashboard/opnfvdashboard.py | 13 +++++++++++++
 vsperf                                 | 15 ++++++++++++++-
 5 files changed, 39 insertions(+), 3 deletions(-)

diff --git a/conf/10_custom.conf b/conf/10_custom.conf
index 1d62c0ce..9622fd71 100644
--- a/conf/10_custom.conf
+++ b/conf/10_custom.conf
@@ -118,7 +118,7 @@ TRAFFICGEN_MOONGEN_LINE_SPEED_GBPS = '10'
 
 #TEST_PARAMS = {'TRAFFICGEN_PKT_SIZES':(64,)}
 OPNFV_INSTALLER = "Fuel"
-OPNFV_URL = "http://testresults.opnfv.org/testapi"
+OPNFV_URL = "http://testresults.opnfv.org/test/api/v1"
 PACKAGE_LIST = "src/package-list.mk"
 
 
diff --git a/core/results/results_constants.py b/core/results/results_constants.py
index f6fbaad2..ef2df847 100644
--- a/core/results/results_constants.py
+++ b/core/results/results_constants.py
@@ -66,6 +66,8 @@ class ResultsConstants(object):
     OPTIMAL_LEARNING_RATE_FPS = 'optimal_learning_rate_fps'
 
     TEST_RUN_TIME = "test_execution_time"
+    TEST_START_TIME = "start_time"
+    TEST_STOP_TIME = "stop_time"
 
     @staticmethod
     def get_traffic_constants():
diff --git a/testcases/testcase.py b/testcases/testcase.py
index a213bbe8..01b3a975 100644
--- a/testcases/testcase.py
+++ b/testcases/testcase.py
@@ -24,6 +24,7 @@ import re
 import time
 import subprocess
 
+from datetime import datetime as dt
 from conf import settings as S
 from conf import get_test_param, merge_spec
 import core.component_factory as component_factory
@@ -58,6 +59,7 @@ class TestCase(object):
         cfg = copy.deepcopy(test_cfg)
 
         self._testcase_start_time = time.time()
+        self._testcase_stop_time = self._testcase_start_time
         self._hugepages_mounted = False
         self._traffic_ctl = None
         self._vnf_ctl = None
@@ -355,8 +357,9 @@ class TestCase(object):
             # tear down test execution environment and log results
             self.run_finalize()
 
+        self._testcase_stop_time = time.time()
         self._testcase_run_time = time.strftime("%H:%M:%S",
-                                                time.gmtime(time.time() -
+                                                time.gmtime(self._testcase_stop_time -
                                                             self._testcase_start_time))
         logging.info("Testcase execution time: " + self._testcase_run_time)
         # report test results
@@ -394,6 +397,11 @@ class TestCase(object):
             item[ResultsConstants.VSWITCH] = S.getValue('VSWITCH')
             item[ResultsConstants.TRAFFIC_TYPE] = self._traffic['l3']['proto']
             item[ResultsConstants.TEST_RUN_TIME] = self._testcase_run_time
+            # convert timestamps to human readable format
+            item[ResultsConstants.TEST_START_TIME] = dt.fromtimestamp(
+                self._testcase_start_time).strftime('%Y-%m-%d %H:%M:%S')
+            item[ResultsConstants.TEST_STOP_TIME] = dt.fromtimestamp(
+                self._testcase_stop_time).strftime('%Y-%m-%d %H:%M:%S')
             if self._traffic['multistream']:
                 item[ResultsConstants.SCAL_STREAM_COUNT] = self._traffic['multistream']
                 item[ResultsConstants.SCAL_STREAM_TYPE] = self._traffic['stream_type']
diff --git a/tools/opnfvdashboard/opnfvdashboard.py b/tools/opnfvdashboard/opnfvdashboard.py
index 3f465c04..c24b9f8c 100644
--- a/tools/opnfvdashboard/opnfvdashboard.py
+++ b/tools/opnfvdashboard/opnfvdashboard.py
@@ -45,12 +45,20 @@ def _push_results(reader, int_data):
     version = ""
     allowed_pkt = ["64", "128", "512", "1024", "1518"]
     details = {"64": '', "128": '', "512": '', "1024": '', "1518": ''}
+    test_start = None
+    test_stop = None
 
     for row_reader in reader:
         if allowed_pkt.count(row_reader['packet_size']) == 0:
             logging.error("The framesize is not supported in opnfv dashboard")
             continue
 
+        # test execution time includes all frame sizes, so start & stop time
+        # is the same (repeated) for every framesize in CSV file
+        if test_start is None:
+            test_start = row_reader['start_time']
+            test_stop = row_reader['stop_time']
+
         casename = _generate_test_name(row_reader['id'], int_data)
         if "back2back" in row_reader['id']:
             details[row_reader['packet_size']] = row_reader['b2b_frames']
@@ -73,10 +81,15 @@ def _push_results(reader, int_data):
 
     # Build body
     body = {"project_name": "vsperf",
+            "scenario": "vsperf",
+            "start_date": test_start,
+            "stop_date": test_stop,
             "case_name": casename,
             "pod_name": int_data['pod'],
             "installer": int_data['installer'],
             "version": version,
+            "build_tag": int_data['build_tag'],
+            "criteria": int_data['criteria'],
             "details": details}
 
     my_data = requests.post(url, json=body)
diff --git a/vsperf b/vsperf
index bb0e199b..f4bc63b8 100755
--- a/vsperf
+++ b/vsperf
@@ -306,7 +306,18 @@ def get_vswitch_names(rst_files):
     # fallback to the default value
     return ['vSwitch']
 
+def get_build_tag():
+    """ Function will return a Jenkins job ID environment variable.
+    """
+
+    try:
+        build_tag = os.environ['BUILD_TAG']
+
+    except KeyError:
+        _LOGGER.warning('Cannot detect Jenkins job ID')
+        build_tag = "none"
 
+    return build_tag
 
 def generate_final_report():
     """ Function will check if partial test results are available
@@ -692,12 +703,14 @@ def main():
 
         if args['opnfvpod']:
             pod_name = args['opnfvpod']
-            installer_name = settings.getValue('OPNFV_INSTALLER')
+            installer_name = str(settings.getValue('OPNFV_INSTALLER')).lower()
             opnfv_url = settings.getValue('OPNFV_URL')
             pkg_list = settings.getValue('PACKAGE_LIST')
 
             int_data = {'vanilla': False,
                         'pod': pod_name,
+                        'criteria': "PASS",
+                        'build_tag': get_build_tag(),
                         'installer': installer_name,
                         'pkg_list': pkg_list,
                         'db_url': opnfv_url}
-- 
cgit