|
| 1 | +from datetime import date |
| 2 | +import pandas as pd |
| 3 | +from app import config |
| 4 | +from app.services.splunk import SplunkService |
| 5 | +import app.api.v1.commons.hasher as hasher |
| 6 | +from datetime import datetime, timezone |
| 7 | + |
| 8 | + |
| 9 | +async def getData(start_datetime: date, end_datetime: date, configpath: str): |
| 10 | + test_types = ["oslat", "cyclictest", "cpu_util", "deployment", "ptp", "reboot", "rfc-2544"] |
| 11 | + cfg = config.get_config() |
| 12 | + try: |
| 13 | + jenkins_url = cfg.get('telco.config.job_url') |
| 14 | + except Exception as e: |
| 15 | + print(f"Error reading telco configuration: {e}") |
| 16 | + test_type_execution_times = { |
| 17 | + "oslat": 3720, |
| 18 | + "cyclictest": 3720, |
| 19 | + "cpu_util": 6600, |
| 20 | + "deployment": 3720, |
| 21 | + "ptp": 4200, |
| 22 | + "reboot": 1980, |
| 23 | + "rfc-2544": 5580, |
| 24 | + } |
| 25 | + query = { |
| 26 | + "earliest_time": "{}T00:00:00".format(start_datetime.strftime('%Y-%m-%d')), |
| 27 | + "latest_time": "{}T23:59:59".format(end_datetime.strftime('%Y-%m-%d')), |
| 28 | + "output_mode": "json" |
| 29 | + } |
| 30 | + searchList = ' OR '.join(['test_type="{}"'.format(test_type) for test_type in test_types]) |
| 31 | + splunk = SplunkService(configpath=configpath) |
| 32 | + response = await splunk.query(query=query, searchList=searchList) |
| 33 | + mapped_list = [] |
| 34 | + |
| 35 | + for each_response in response: |
| 36 | + end_timestamp = int(each_response['timestamp']) |
| 37 | + test_data = each_response['data'] |
| 38 | + hash_digest, encrypted_data = hasher.hash_encrypt_json(each_response) |
| 39 | + execution_time_seconds = test_type_execution_times.get(test_data['test_type'], 0) |
| 40 | + start_timestamp = end_timestamp - execution_time_seconds |
| 41 | + start_time_utc = datetime.fromtimestamp(start_timestamp, tz=timezone.utc) |
| 42 | + end_time_utc = datetime.fromtimestamp(end_timestamp, tz=timezone.utc) |
| 43 | + |
| 44 | + mapped_list.append({ |
| 45 | + "uuid": hash_digest, |
| 46 | + "encryptedData": encrypted_data.decode('utf-8'), |
| 47 | + "ciSystem": "Jenkins", |
| 48 | + "testName": test_data['test_type'], |
| 49 | + "version": test_data['ocp_version'], |
| 50 | + "releaseStream": test_data['ocp_build'], |
| 51 | + "startDate": str(start_time_utc), |
| 52 | + "endDate": str(end_time_utc), |
| 53 | + "buildUrl": jenkins_url + "/" + str(test_data['cluster_artifacts']['ref']['jenkins_build']), |
| 54 | + "jobStatus": "success" |
| 55 | + }) |
| 56 | + |
| 57 | + jobs = pd.json_normalize(mapped_list) |
| 58 | + if len(jobs) == 0: |
| 59 | + return jobs |
| 60 | + |
| 61 | + return jobs |
0 commit comments