|
| 1 | +from ibutsu_server.constants import BARCHART_MAX_BUILDS |
| 2 | +from ibutsu_server.constants import JJV_RUN_LIMIT |
| 3 | +from ibutsu_server.db.models import Result |
| 4 | +from ibutsu_server.db.models import Run |
| 5 | +from ibutsu_server.filters import string_to_column |
| 6 | +from ibutsu_server.widgets.jenkins_job_view import get_jenkins_job_view |
| 7 | + |
| 8 | + |
| 9 | +def get_importance_component( |
| 10 | + env="prod", group_field="component", job_name="", builds=5, components="", project=None |
| 11 | +): |
| 12 | + # taken from get_jenkins_line_chart in jenkins_job_analysis.py |
| 13 | + run_limit = int((JJV_RUN_LIMIT / BARCHART_MAX_BUILDS) * builds) |
| 14 | + jobs = get_jenkins_job_view( |
| 15 | + filter_=f"job_name={job_name}", page_size=builds, project=project, run_limit=run_limit |
| 16 | + ).get("jobs") |
| 17 | + |
| 18 | + # A list of job build numbers to filter our runs by |
| 19 | + job_ids = [] |
| 20 | + for job in jobs: |
| 21 | + job_ids.append(job["build_number"]) |
| 22 | + |
| 23 | + # query for RUN ids |
| 24 | + # metadata has to have a string to column to work |
| 25 | + # because it is a sqlalchemy property otherwise (AFAIK) |
| 26 | + bnumdat = string_to_column("metadata.jenkins.build_number", Run) |
| 27 | + run_data = ( |
| 28 | + Run.query.filter(bnumdat.in_(job_ids), Run.component.in_(components.split(","))) |
| 29 | + .add_columns(Run.id, bnumdat.label("build_number")) |
| 30 | + .all() |
| 31 | + ) |
| 32 | + |
| 33 | + # get a list of the job IDs |
| 34 | + run_info = {} |
| 35 | + for run in run_data: |
| 36 | + run_info[run.id] = run.build_number |
| 37 | + |
| 38 | + mdat = string_to_column("metadata.importance", Result) |
| 39 | + result_data = ( |
| 40 | + Result.query.filter( |
| 41 | + Result.run_id.in_(run_info.keys()), Result.component.in_(components.split(",")) |
| 42 | + ) |
| 43 | + .add_columns( |
| 44 | + Result.run_id, Result.component, Result.id, Result.result, mdat.label("importance") |
| 45 | + ) |
| 46 | + .all() |
| 47 | + ) |
| 48 | + |
| 49 | + """ |
| 50 | + This starts a (probably) over complicated bit of data maniplation |
| 51 | + to get sdatdict in a proper state to be broken down into |
| 52 | + sdatret, which is the format we need for the widget. |
| 53 | + """ |
| 54 | + sdatdict = {} |
| 55 | + bnums = set() |
| 56 | + importances = ["critical", "high", "medium", "low"] |
| 57 | + for datum in result_data: |
| 58 | + # getting the components from the results |
| 59 | + if datum.component not in sdatdict.keys(): |
| 60 | + sdatdict[datum.component] = {} |
| 61 | + |
| 62 | + # getting the build numbers from the results |
| 63 | + if run_info[datum.run_id] not in sdatdict[datum.component].keys(): |
| 64 | + bnums.add(run_info[datum.run_id]) |
| 65 | + sdatdict[datum.component][run_info[datum.run_id]] = {} |
| 66 | + |
| 67 | + # Adding all importances from our constant |
| 68 | + if datum.importance not in sdatdict[datum.component][run_info[datum.run_id]].keys(): |
| 69 | + sdatdict[datum.component][run_info[datum.run_id]][datum.importance] = [] |
| 70 | + # adding the result value |
| 71 | + sdatdict[datum.component][run_info[datum.run_id]][datum.importance].append( |
| 72 | + {"result": datum.result, "result_id": datum.id} |
| 73 | + ) |
| 74 | + |
| 75 | + # This adds the extra importance values that didn't appear in the results |
| 76 | + for component in sdatdict.keys(): |
| 77 | + for bnum in sdatdict[component].keys(): |
| 78 | + for importance in importances: |
| 79 | + if importance not in sdatdict[component][bnum].keys(): |
| 80 | + sdatdict[component][bnum][importance] = [] |
| 81 | + |
| 82 | + # this is to change result values into numbers |
| 83 | + # TODO: This doesn't handle xpassed, xfailed, skipped, etc. so figure that out |
| 84 | + for component in sdatdict.keys(): |
| 85 | + for bnum in sdatdict[component].keys(): |
| 86 | + for importance in sdatdict[component][bnum].keys(): |
| 87 | + total = 0 |
| 88 | + passed = 0 |
| 89 | + res_list = [] |
| 90 | + for item in sdatdict[component][bnum][importance]: |
| 91 | + total += 1 |
| 92 | + res_list.append(item["result_id"]) |
| 93 | + if item["result"] == "passed": |
| 94 | + passed += 1 |
| 95 | + |
| 96 | + if total != 0: |
| 97 | + sdatdict[component][bnum][importance] = { |
| 98 | + "percentage": round(passed / total, 2), |
| 99 | + "result_list": res_list, |
| 100 | + } |
| 101 | + else: |
| 102 | + sdatdict[component][bnum][importance] = { |
| 103 | + "percentage": 0, |
| 104 | + "result_list": res_list, |
| 105 | + } |
| 106 | + |
| 107 | + for bnum in bnums: |
| 108 | + if bnum not in sdatdict[component].keys(): |
| 109 | + sdatdict[component][bnum] = {} |
| 110 | + for importance in importances: |
| 111 | + sdatdict[component][bnum][importance] = {"percentage": "NA", "result_list": []} |
| 112 | + |
| 113 | + # Need this broken down more for the table |
| 114 | + table_data = [] |
| 115 | + for key in sdatdict.keys(): |
| 116 | + table_data.append( |
| 117 | + { |
| 118 | + "component": key, |
| 119 | + "bnums": sorted(list(bnums)), |
| 120 | + "importances": importances, |
| 121 | + "data": sdatdict[key], |
| 122 | + } |
| 123 | + ) |
| 124 | + |
| 125 | + # return data, for sanity |
| 126 | + data = {"table_data": table_data} |
| 127 | + return data |
0 commit comments