-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6d21c99
commit baab65f
Showing
14 changed files
with
345 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from typing import Annotated | ||
from pydantic import BaseModel, BeforeValidator, Field | ||
|
||
from kernelCI_app.constants.general import DEFAULT_INTERVAL_IN_DAYS, DEFAULT_ORIGIN | ||
|
||
|
||
class ListingQueryParameters(BaseModel): | ||
origin: Annotated[ | ||
str, | ||
Field(default=DEFAULT_ORIGIN), | ||
BeforeValidator(lambda o: DEFAULT_ORIGIN if o is None else o), | ||
] | ||
interval_in_days: Annotated[ | ||
int, | ||
Field(default=DEFAULT_INTERVAL_IN_DAYS, gt=0), | ||
BeforeValidator(lambda i: DEFAULT_INTERVAL_IN_DAYS if i is None else i), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from pydantic import BaseModel | ||
|
||
from kernelCI_app.typeModels.databases import ( | ||
Issue__Comment, | ||
Issue__CulpritCode, | ||
Issue__CulpritHarness, | ||
Issue__CulpritTool, | ||
Issue__Id, | ||
Issue__Version, | ||
Timestamp, | ||
) | ||
from kernelCI_app.typeModels.issues import FirstIncident | ||
|
||
|
||
class IssueListingItem(BaseModel): | ||
field_timestamp: Timestamp | ||
id: Issue__Id | ||
comment: Issue__Comment | ||
version: Issue__Version | ||
culprit_code: Issue__CulpritCode | ||
culprit_tool: Issue__CulpritTool | ||
culprit_harness: Issue__CulpritHarness | ||
|
||
|
||
class IssueListingResponse(BaseModel): | ||
issues: list[IssueListingItem] | ||
extras: dict[str, FirstIncident] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
from datetime import datetime, timedelta, timezone | ||
from http import HTTPStatus | ||
from drf_spectacular.utils import extend_schema | ||
from rest_framework.views import APIView | ||
from rest_framework.response import Response | ||
from pydantic import ValidationError | ||
|
||
from kernelCI_app.helpers.errorHandling import ( | ||
create_api_error_response, | ||
) | ||
from kernelCI_app.helpers.issueExtras import assign_issue_first_seen | ||
from kernelCI_app.models import Issues | ||
from kernelCI_app.typeModels.commonListing import ListingQueryParameters | ||
from kernelCI_app.typeModels.issueListing import ( | ||
IssueListingResponse, | ||
) | ||
from kernelCI_app.typeModels.issues import FirstIncident, ProcessedExtraDetailedIssues | ||
|
||
|
||
def get_issue_listing_data(origin: str, interval_date): | ||
issues_records = Issues.objects.values( | ||
"id", | ||
"field_timestamp", | ||
"comment", | ||
"version", | ||
"culprit_code", | ||
"culprit_harness", | ||
"culprit_tool", | ||
).filter( | ||
origin=origin, | ||
field_timestamp__gte=interval_date, | ||
) | ||
return issues_records | ||
|
||
|
||
class IssueView(APIView): | ||
def __init__(self): | ||
self.issue_records: list[dict[str]] = [] | ||
self.processed_extra_issue_details: ProcessedExtraDetailedIssues = {} | ||
self.first_incidents: dict[str, FirstIncident] = {} | ||
|
||
def _format_processing_for_response(self) -> None: | ||
for ( | ||
issue_extras_id, | ||
issue_extras_data, | ||
) in self.processed_extra_issue_details.items(): | ||
self.first_incidents[issue_extras_id] = issue_extras_data["first_incident"] | ||
|
||
@extend_schema( | ||
parameters=[ListingQueryParameters], | ||
responses=IssueListingResponse, | ||
methods=["GET"], | ||
) | ||
def get(self, _request) -> Response: | ||
try: | ||
request_params = ListingQueryParameters( | ||
origin=(_request.GET.get("origin")), | ||
interval_in_days=_request.GET.get("intervalInDays"), | ||
) | ||
except ValidationError as e: | ||
return Response(data=e.json(), status=HTTPStatus.BAD_REQUEST) | ||
|
||
origin_param = request_params.origin | ||
interval_in_days = request_params.interval_in_days | ||
|
||
interval_date = datetime.now(timezone.utc) - timedelta(days=interval_in_days) | ||
interval_param = interval_date.replace( | ||
hour=0, minute=0, second=0, microsecond=0 | ||
) | ||
|
||
self.issue_records = get_issue_listing_data( | ||
origin=origin_param, interval_date=interval_param | ||
) | ||
|
||
if len(self.issue_records) == 0: | ||
return create_api_error_response( | ||
error_message="No issues found", status_code=HTTPStatus.OK | ||
) | ||
|
||
issue_key_list = [ | ||
(issue["id"], issue["version"]) for issue in self.issue_records | ||
] | ||
assign_issue_first_seen( | ||
issue_key_list=issue_key_list, | ||
processed_issues_table=self.processed_extra_issue_details, | ||
) | ||
|
||
try: | ||
self._format_processing_for_response() | ||
valid_data = IssueListingResponse( | ||
issues=self.issue_records, extras=self.first_incidents | ||
) | ||
except ValidationError as e: | ||
return Response(data=e.json(), status=HTTPStatus.INTERNAL_SERVER_ERROR) | ||
return Response(data=valid_data.model_dump()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.