Skip to content

Commit

Permalink
feat: update interval parameter handling in views
Browse files Browse the repository at this point in the history
- Replaced `daysInterval` with `intervalInDays` in `hardwareDetailsView.py`
  and `treeView.py`.
- Updated import statements to include `parseIntervalInDaysGetParameter` and
  `ExceptionWithJsonResponse`.
- Modified `hardware.sh` to use the new `intervalInDays` parameter.
- Adjusted error handling to use `ExceptionWithJsonResponse` for better
  consistency.
  • Loading branch information
WilsonNet committed Nov 4, 2024
1 parent 00133f5 commit 11b5d66
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 43 deletions.
85 changes: 44 additions & 41 deletions backend/kernelCI_app/views/hardwareDetailsView.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
from collections import defaultdict
from datetime import timedelta
from django.http import HttpResponseBadRequest, JsonResponse
from django.views import View
from django.db.models import F
from django.utils import timezone
from django.http import JsonResponse
from kernelCI_app.viewCommon import create_details_build_summary, get_details_issue
from kernelCI_app.models import Tests
from kernelCI_app.utils import (
convert_issues_dict_to_list,
extract_error_message,
extract_platform,
getErrorResponseBody,
)
from kernelCI_app.helpers.date import parseIntervalInDaysGetParameter
from kernelCI_app.helpers.errorHandling import ExceptionWithJsonResponse


DEFAULT_DAYS_INTERVAL = 7
Expand Down Expand Up @@ -40,7 +41,7 @@ def get_history(record):
"status": record["status"],
"path": record["path"],
"duration": record["duration"],
"startTime": record["start_time"]
"startTime": record["start_time"],
}


Expand All @@ -63,7 +64,7 @@ def generate_test_dict():
"statusSummary": defaultdict(int),
"failReasons": defaultdict(int),
"configs": defaultdict(lambda: defaultdict(int)),
"issues": {}
"issues": {},
}


Expand All @@ -87,7 +88,9 @@ def sanitize_records(self, records):
tests_or_boots["configs"][r["build_config_name"]][r["status"]] += 1

if status == "ERROR" or status == "FAIL" or status == "MISS":
tests_or_boots["platformsFailing"].add(extract_platform(r["environment_misc"]))
tests_or_boots["platformsFailing"].add(
extract_platform(r["environment_misc"])
)
tests_or_boots["failReasons"][extract_error_message(r["misc"])] += 1

if build_id not in processed_builds:
Expand All @@ -106,55 +109,55 @@ def sanitize_records(self, records):
boots["platformsFailing"] = list(boots["platformsFailing"])
boots["issues"] = convert_issues_dict_to_list(boots["issues"])

return (
builds,
tests,
boots
)
return (builds, tests, boots)

def get(self, request, hardware_id):
try:
days_interval = int(request.GET.get("daysInterval", DEFAULT_DAYS_INTERVAL))
except ValueError:
return HttpResponseBadRequest(getErrorResponseBody("Invalid daysInterval"))

if days_interval < 1:
return HttpResponseBadRequest(getErrorResponseBody("daysInterval must be a positive integer"))
days_interval = parseIntervalInDaysGetParameter(
request.GET.get("intervalInDays", DEFAULT_DAYS_INTERVAL)
)
except ExceptionWithJsonResponse as e:
return e.getJsonResponse()

start_time = timezone.now() - timedelta(days=days_interval)

target_tests = Tests.objects.filter(
environment_compatible__contains=[hardware_id],
start_time__gte=start_time
environment_compatible__contains=[hardware_id], start_time__gte=start_time
).values(
'id', 'origin', 'environment_comment', 'environment_misc', 'path', 'comment',
'log_url', 'status', 'waived', 'start_time', 'duration', 'misc',
'build_id', 'environment_compatible'
"id",
"origin",
"environment_comment",
"environment_misc",
"path",
"comment",
"log_url",
"status",
"waived",
"start_time",
"duration",
"misc",
"build_id",
"environment_compatible",
)

records = target_tests.annotate(
build_architecture=F('build__architecture'),
build_config_name=F('build__config_name'),
build_misc=F('build__misc'),
build_config_url=F('build__config_url'),
build_compiler=F('build__compiler'),
build_valid=F('build__valid'),
build_duration=F('build__duration'),
build_log_url=F('build__log_url'),
build_start_time=F('build__start_time'),
checkout_git_repository_url=F('build__checkout__git_repository_url'),
checkout_git_repository_branch=F('build__checkout__git_repository_branch'),
issue_id=F('build__incidents__issue__id'),
issue_comment=F('build__incidents__issue__comment'),
issue_report_url=F('build__incidents__issue__report_url')
build_architecture=F("build__architecture"),
build_config_name=F("build__config_name"),
build_misc=F("build__misc"),
build_config_url=F("build__config_url"),
build_compiler=F("build__compiler"),
build_valid=F("build__valid"),
build_duration=F("build__duration"),
build_log_url=F("build__log_url"),
build_start_time=F("build__start_time"),
checkout_git_repository_url=F("build__checkout__git_repository_url"),
checkout_git_repository_branch=F("build__checkout__git_repository_branch"),
issue_id=F("build__incidents__issue__id"),
issue_comment=F("build__incidents__issue__comment"),
issue_report_url=F("build__incidents__issue__report_url"),
)
builds, tests, boots = self.sanitize_records(records)

return JsonResponse(
{
'builds': builds,
'tests': tests,
'boots': boots
},
safe=False
{"builds": builds, "tests": tests, "boots": boots}, safe=False
)
10 changes: 9 additions & 1 deletion backend/kernelCI_app/views/treeView.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from kernelCI_app.models import Checkouts
from kernelCI_app.serializers import TreeSerializer
from kernelCI_app.utils import getQueryTimeInterval
from kernelCI_app.helpers.errorHandling import ExceptionWithJsonResponse
from kernelCI_app.helpers.date import parseIntervalInDaysGetParameter


DEFAULT_ORIGIN = 'maestro'
Expand All @@ -11,7 +13,13 @@
class TreeView(View):
def get(self, request):
origin_param = request.GET.get('origin', DEFAULT_ORIGIN)
interval_days = int(request.GET.get('intervalInDays', '7'))
try:
interval_days = parseIntervalInDaysGetParameter(
request.GET.get("intervalInDays", 3)
)
except ExceptionWithJsonResponse as e:
return e.getJsonResponse()

interval_days_data = {"days": interval_days}

params = {
Expand Down
2 changes: 1 addition & 1 deletion backend/requests/hardware.sh
Original file line number Diff line number Diff line change
@@ -1 +1 @@
http 'http://localhost:8000/api/hardware/raspberrypi,2-model-b?daysInterval=1'
http 'http://localhost:8000/api/hardware/raspberrypi,2-model-b?intervalInDays=1'

0 comments on commit 11b5d66

Please sign in to comment.