Skip to content

Commit

Permalink
feat: add filter capabilities to the treeDetails endpoint
Browse files Browse the repository at this point in the history
- now we can use ?filter_<field_name>=<value> to filter the response
  • Loading branch information
lfjnascimento committed Jul 18, 2024
1 parent 4df9528 commit 8a60e82
Showing 1 changed file with 29 additions and 24 deletions.
53 changes: 29 additions & 24 deletions backend/kernelCI_app/views.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from django.http import JsonResponse
from django.views import View

from querybuilder.query import Query
from kernelCI_app.models import Checkouts, Builds
from kernelCI_app.serializers import TreeSerializer, TreeDetailsSerializer
from kernelCI_app.serializers import TreeSerializer
from kernelCI_app.utils import get_visible_record_identifiers


Expand Down Expand Up @@ -108,25 +108,30 @@ def get_test_staus(self, build_id):
return {k: getattr(builds[0], k) for k in status_keys}

def get(self, request, commit_hash):
builds = Builds.objects.raw(
"""
SELECT
builds.id, builds.architecture, builds.config_name, builds.misc,
builds.config_url, builds.compiler, builds.valid,
builds.start_time, builds.duration, builds.log_url
FROM
builds
INNER JOIN
checkouts ON checkouts.id = builds.checkout_id
WHERE checkouts.git_commit_hash = %s;
""",
[commit_hash]
)

for build in builds:
build.test_status = self.get_test_staus(build.id)

data = TreeDetailsSerializer(builds, many=True).data
summary = self.create_summary(data)
resp = {"builds": data, "summary": summary}
return JsonResponse(resp, safe=False)
build_fields = [
'id', 'architecture', 'config_name', 'misc', 'config_url',
'compiler', 'valid', 'duration', 'log_url', 'start_time']
checkout_fields = [
'git_repository_branch', 'git_repository_url', 'git_repository_branch']

query = Query().from_table(Builds, build_fields).join(
'checkouts',
condition='checkouts.id = builds.checkout_id',
fields=checkout_fields
).where(git_commit_hash__eq=commit_hash)

for k in request.GET.keys():
if k.startswith('filter_'):
field = k[7:]
if field in build_fields or field in checkout_fields:
filter_list = request.GET.getlist(k)
query.where({field: filter_list})

records = query.select()
for r in records:
status = self.get_test_staus(r.get('id'))
r['status'] = status

summary = self.create_summary(records)

return JsonResponse({"builds": records, "summary": summary}, safe=False)

0 comments on commit 8a60e82

Please sign in to comment.