Skip to content

Commit

Permalink
wip: backend filters
Browse files Browse the repository at this point in the history
  • Loading branch information
Francisco2002 committed Nov 22, 2024
1 parent 32672d2 commit 2748285
Showing 1 changed file with 54 additions and 42 deletions.
96 changes: 54 additions & 42 deletions backend/kernelCI_app/views/hardwareDetailsView.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
)
from kernelCI_app.constants.general import DEFAULT_ORIGIN
from django.views.decorators.csrf import csrf_exempt
from django.db.models import Q

DEFAULT_DAYS_INTERVAL = 3
SELECTED_VALUE = 'selected'
Expand Down Expand Up @@ -141,10 +140,45 @@ class HardwareDetails(View):
required_params_get = ["origin"]
cache_key_get = "hardwareDetailsGET"

def sanitize_records(self, records, selected_trees):
def get_filters(self, filters):
filters_map = {
"build": [],
"boot": [],
"test": []
}

for key, value in filters.items():
_, filter_item = key.split("_")

filter_split = filter_item.split(".")
if len(filter_split) == 2:
table, field = filter_item.split(".")
else:
table = "build"
field = filter_item

if table == "build":
value = [x == "Success" for x in value]
filters_map[table].append({"field": field, "value": value})

return filters_map

def pass_in_filters(self, data, filters):
for currentFilter in filters:
field = currentFilter.get("field")
value = currentFilter.get("value")

if data[field] not in value:
return False

return True

def sanitize_records(self, records, selected_trees, filters):
processed_builds = set()
builds = {"items": [], "issues": {}}

filters_map = self.get_filters(filters)

tests = generate_test_dict()
boots = generate_test_dict()

Expand All @@ -156,6 +190,11 @@ def sanitize_records(self, records, selected_trees):
build_id = r["build_id"]
issue_id = r["build__incidents__issue__id"]
status = r["status"]
table_test = "boot" if is_boot(r) else "test"

if len(filters_map[table_test]) > 0 and not self.pass_in_filters(r, filters_map[table_test]):
continue

tests_or_boots = boots if is_boot(r) else tests

tests_or_boots["history"].append(get_history(r))
Expand All @@ -177,7 +216,10 @@ def sanitize_records(self, records, selected_trees):

if build_id not in processed_builds:
processed_builds.add(build_id)
builds["items"].append(get_build(r, current_tree["index"]))
build = get_build(r, current_tree["index"])

if len(filters_map['build']) == 0 or self.pass_in_filters(build, filters_map['build']):
builds["items"].append(build)

if issue_id:
currentIssue = get_details_issue(r)
Expand Down Expand Up @@ -233,41 +275,7 @@ def filter_fn(tree):

return list(filter(filter_fn, trees)) if selected_trees else trees

def get_filters(self, hardware_id, origin, start_datetime, end_datetime, filters):
filters_map = Q(environment_compatible__contains=[hardware_id])
filters_map &= Q(build__checkout__start_time__gte=start_datetime)
filters_map &= Q(build__checkout__start_time__lte=end_datetime)
filters_map &= Q(origin__exact=origin)

filter_values = {
'build': Q(),
'boot': Q(path__contains="boot.") | Q(path__exact="boot"),
'test': ~(Q(path__contains="boot.") | Q(path__exact="boot")),
}

for key, value in filters.items():
_, filter_item = key.split("_")

filter_split = filter_item.split(".")
if len(filter_split) == 2:
table, _ = filter_item.split(".")
else:
table = "build"

if table == "build":
value = [x == "Success" for x in value]
filter_values['build'] &= Q(build__checkout__valid__in=value)
else:
filter_values[table] &= Q(status__in=value)

filters_map &= filter_values["build"]
filters_map &= filter_values["boot"] | filter_values["test"]

return filters_map

def get_records(self, *, hardware_id, origin, start_datetime, end_datetime, filters):
filters_map = self.get_filters(hardware_id, origin, start_datetime, end_datetime, filters)

def get_records(self, *, hardware_id, origin, start_datetime, end_datetime):
records = Tests.objects.values(
"id",
"environment_misc",
Expand Down Expand Up @@ -297,7 +305,12 @@ def get_records(self, *, hardware_id, origin, start_datetime, end_datetime, filt
"build__incidents__issue__id",
"build__incidents__issue__comment",
"build__incidents__issue__report_url"
).filter(filters_map)
).filter(
environment_compatible__contains=[hardware_id],
build__checkout__start_time__gte=start_datetime,
build__checkout__start_time__lte=end_datetime,
origin=origin
)
return records

# Using post to receive a body request
Expand Down Expand Up @@ -336,16 +349,15 @@ def post(self, request, hardware_id):
"hardware_id": hardware_id,
"origin": origin,
"start_datetime": start_datetime,
"end_datetime": end_datetime,
"filters": filters
"end_datetime": end_datetime
}

records = getQueryCache(self.cache_key_get, params)
if not records:
records = self.get_records(**params)
setQueryCache(self.cache_key_get, params, records)

builds, tests, boots = self.sanitize_records(records, selected_trees)
builds, tests, boots = self.sanitize_records(records, selected_trees, filters)

return JsonResponse(
{"builds": builds, "tests": tests, "boots": boots, "trees": trees}, safe=False
Expand Down

0 comments on commit 2748285

Please sign in to comment.