Skip to content

Commit

Permalink
Use source instead of build number when filtering results in Importan…
Browse files Browse the repository at this point in the history
…ce by Component widget (#526)

* Use source instead of build number to not mix same number on different envs

* Implement a subquery to find the jenkins builds and filter based on that

* Fixed linter issues

* Fixed formatting issues

* differentiate a 0 pct because no tests and a 0 pct because everything failed

* modified empty results text

* Try to use keys to filter when possible

* fixed format issues

* refactor query to use only Results table
  • Loading branch information
Fynardo authored Dec 3, 2024
1 parent 800b871 commit 770c768
Showing 1 changed file with 43 additions and 51 deletions.
94 changes: 43 additions & 51 deletions backend/ibutsu_server/widgets/importance_component.py
Original file line number Diff line number Diff line change
@@ -1,64 +1,56 @@
from collections import defaultdict

from ibutsu_server.constants import BARCHART_MAX_BUILDS, JJV_RUN_LIMIT
from ibutsu_server.db.models import Result, Run
from ibutsu_server.filters import string_to_column
from ibutsu_server.widgets.jenkins_job_view import get_jenkins_job_view

from sqlalchemy import desc

def get_importance_component(
env="prod",
group_field="component",
job_name="",
builds=5,
components="",
project=None,
count_skips=False,
):
# taken from get_jenkins_line_chart in jenkins_job_analysis.py
run_limit = int((JJV_RUN_LIMIT / BARCHART_MAX_BUILDS) * builds)
jobs = get_jenkins_job_view(
filter_=f"job_name={job_name}",
page_size=builds,
project=project,
run_limit=run_limit,
).get("jobs")
from ibutsu_server.db.base import session
from ibutsu_server.db.models import Result
from ibutsu_server.filters import string_to_column

# A list of job build numbers to filter our runs by
job_ids = []
for job in jobs:
job_ids.append(job["build_number"])

# query for RUN ids
# metadata has to have a string to column to work
# because it is a sqlalchemy property otherwise (AFAIK)
bnumdat = string_to_column("metadata.jenkins.build_number", Run)
run_data = (
Run.query.filter(bnumdat.in_(job_ids), Run.component.in_(components.split(",")))
.add_columns(Run.id, bnumdat.label("build_number"))
.all()
def _get_results(job_name, builds, components, project):
mdat = string_to_column("metadata.importance", Result).label("importance")
bnumdat = string_to_column("metadata.jenkins.build_number", Result).label("build_number")
jnamedat = string_to_column("metadata.jenkins.job_name", Result).label("job_name")
# Get the last 'builds' runs from a specific Jenkins Job as a subquery
build_numbers_subquery = (
session.query(bnumdat.label("build_number"))
.filter(jnamedat == job_name)
.filter(Result.project_id == project)
.group_by(bnumdat)
.order_by(desc(bnumdat))
.limit(builds)
.subquery()
)

# get a list of the job IDs
run_info = {}
for run in run_data:
run_info[run.id] = run.build_number

mdat = string_to_column("metadata.importance", Result)
# Actually filter the results based on build_numbers, job_name, project_id and component.
result_data = (
Result.query.filter(
Result.run_id.in_(run_info.keys()),
bnumdat.in_(build_numbers_subquery),
jnamedat == job_name,
Result.component.in_(components.split(",")),
Result.project_id == project,
)
.add_columns(
Result.run_id,
Result.component,
Result.id,
Result.result,
mdat.label("importance"),
mdat,
bnumdat,
)
.all()
)
return result_data


def get_importance_component(
env="prod",
group_field="component",
job_name="",
builds=5,
components="",
project=None,
count_skips=False,
):
result_data = _get_results(job_name, builds, components, project)

"""
This starts a (probably) over complicated bit of data maniplation
Expand All @@ -74,15 +66,15 @@ def get_importance_component(
sdatdict[datum.component] = {}

# getting the build numbers from the results
if run_info[datum.run_id] not in sdatdict[datum.component].keys():
bnums.add(run_info[datum.run_id])
sdatdict[datum.component][run_info[datum.run_id]] = {}
if datum.build_number not in sdatdict[datum.component].keys():
bnums.add(datum.build_number)
sdatdict[datum.component][datum.build_number] = {}

# Adding all importances from our constant
if datum.importance not in sdatdict[datum.component][run_info[datum.run_id]].keys():
sdatdict[datum.component][run_info[datum.run_id]][datum.importance] = []
if datum.importance not in sdatdict[datum.component][datum.build_number].keys():
sdatdict[datum.component][datum.build_number][datum.importance] = []
# adding the result value
sdatdict[datum.component][run_info[datum.run_id]][datum.importance].append(
sdatdict[datum.component][datum.build_number][datum.importance].append(
{"result": datum.result, "result_id": datum.id}
)

Expand Down Expand Up @@ -127,7 +119,7 @@ def get_importance_component(
}
else:
sdatdict[component][bnum][importance] = {
"percentage": 0,
"percentage": "N/A",
"result_list": res_list,
}

Expand Down

0 comments on commit 770c768

Please sign in to comment.