c876fb425808aca233c4ea71e2aa4a4bf051417d
[integration/test.git] / csit / scripts / data_generate.py
1 from datetime import datetime
2 import glob
3 import os
4 import time
5 import xml.etree.ElementTree as ET
6
7
8 def generate():
9     BODY = {}
10
11     ts = time.time()
12     formatted_ts = datetime.fromtimestamp(ts).strftime("%Y-%m-%dT%H:%M:%S.%fZ")
13     BODY["@timestamp"] = formatted_ts
14
15     # Plots are obtained from csv files ( in archives directory in $WORKSPACE).
16
17     csv_files = glob.glob("archives/*.csv")
18     BODY["project"] = "opendaylight"
19     BODY["subject"] = "test"
20
21     # If there are no csv files, then it is a functional test.
22     # Parse csv files and fill perfomance parameter values
23
24     if len(csv_files) == 0:
25         BODY["test-type"] = "functional"
26     else:
27         BODY["test-type"] = "performance"
28         BODY["plots"] = {}
29         for f in csv_files:
30             key = (f.split("/")[-1])[:-4]
31             BODY["plots"][key] = {}
32             with open(f) as file:
33                 lines = file.readlines()
34             props = lines[0].strip().split(",")
35             vals = lines[1].strip().split(",")
36             for i in range(len(props)):
37                 BODY["plots"][key][props[i]] = float(vals[i])
38
39     # Fill the required parameters whose values are obtained from environment.
40
41     BODY["jenkins-silo"] = os.environ["SILO"]
42     BODY["test-name"] = os.environ["JOB_NAME"]
43     BODY["test-run"] = int(os.environ["BUILD_NUMBER"])
44
45     # Parsing robot log for stats on start-time, pass/fail tests and duration.
46
47     robot_log = os.environ["WORKSPACE"] + "/output.xml"
48     tree = ET.parse(robot_log)
49     BODY["id"] = "{}-{}".format(os.environ["JOB_NAME"], os.environ["BUILD_NUMBER"])
50     BODY["start-time"] = tree.getroot().attrib["generated"]
51     BODY["pass-tests"] = int(tree.getroot().find("statistics")[0][1].get("pass"))
52     BODY["fail-tests"] = int(tree.getroot().find("statistics")[0][1].get("fail"))
53     endtime = tree.getroot().find("suite").find("status").get("endtime")
54     starttime = tree.getroot().find("suite").find("status").get("starttime")
55     elap_time = datetime.strptime(endtime, "%Y%m%d %H:%M:%S.%f") - datetime.strptime(
56         starttime, "%Y%m%d %H:%M:%S.%f"
57     )
58     BODY["duration"] = str(elap_time)
59
60     return BODY