Skip to content

Commit

Permalink
Bug 1667866 - Add the GET API to get the tasks by the id of the alert…
Browse files Browse the repository at this point in the history
… summary (#7717)
  • Loading branch information
junngo authored Aug 3, 2023
1 parent ce6d33c commit 98ae596
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 0 deletions.
40 changes: 40 additions & 0 deletions tests/webapp/api/test_performance_data_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
from django.urls import reverse
from collections import defaultdict

from tests.conftest import create_perf_alert
from treeherder.model.models import MachinePlatform, Push
from treeherder.webapp.api.performance_data import PerformanceSummary
from treeherder.perf.models import (
PerformanceAlert,
PerformanceDatum,
PerformanceFramework,
PerformanceSignature,
Expand Down Expand Up @@ -707,3 +709,41 @@ def test_filter_out_retriggers():

filtered_data = PerformanceSummary._filter_out_retriggers(copy.deepcopy(no_retriggers_data))
assert filtered_data == no_retriggers_data


def test_alert_summary_tasks_get(client, test_perf_alert_summary, test_perf_data):
create_perf_alert(
summary=test_perf_alert_summary,
series_signature=test_perf_data.first().signature,
related_summary=test_perf_alert_summary,
status=PerformanceAlert.REASSIGNED,
)
resp = client.get(
reverse('performance-alertsummary-tasks') + '?id={}'.format(test_perf_alert_summary.id)
)
assert resp.status_code == 200
assert resp.json() == {
"id": test_perf_alert_summary.id,
"tasks": [
"Mochitest Browser Chrome",
"Inari Device Image Build",
"B2G Emulator Image Build",
"Nexus 4 Device Image Build",
],
}


def test_alert_summary_tasks_get_failure(client, test_perf_alert_summary):
# verify that we fail if PerformanceAlertSummary does not exist
not_exist_summary_id = test_perf_alert_summary.id
test_perf_alert_summary.delete()
resp = client.get(
reverse('performance-alertsummary-tasks') + '?id={}'.format(not_exist_summary_id)
)
assert resp.status_code == 400
assert resp.json() == {"message": ["PerformanceAlertSummary does not exist."]}

# verify that we fail if id does not exist as a query parameter
resp = client.get(reverse('performance-alertsummary-tasks'))
assert resp.status_code == 400
assert resp.json() == {"id": ["This field is required."]}
27 changes: 27 additions & 0 deletions treeherder/webapp/api/performance_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
IssueTrackerSerializer,
PerformanceAlertSerializer,
PerformanceAlertSummarySerializer,
PerformanceAlertSummaryTasksSerializer,
PerfAlertSummaryTasksQueryParamSerializer,
PerformanceBugTemplateSerializer,
PerformanceFrameworkSerializer,
PerformanceQueryParamsSerializer,
Expand Down Expand Up @@ -765,6 +767,31 @@ def _filter_out_retriggers(serialized_data: List[dict]) -> List[dict]:
return serialized_data


class PerformanceAlertSummaryTasks(generics.ListAPIView):
serializer_class = PerformanceAlertSummaryTasksSerializer
queryset = None

def list(self, request):
query_params = PerfAlertSummaryTasksQueryParamSerializer(data=request.query_params)
if not query_params.is_valid():
return Response(data=query_params.errors, status=HTTP_400_BAD_REQUEST)

alert_summary_id = query_params.validated_data['id']
signature_ids = PerformanceAlertSummary.objects.filter(id=alert_summary_id).values_list(
'alerts__series_signature__id', 'related_alerts__series_signature__id'
)
signature_ids = [id for id_set in signature_ids for id in id_set]
tasks = (
PerformanceDatum.objects.filter(signature__in=signature_ids)
.values_list('job__job_type__name', flat=True)
.distinct()
)
self.queryset = {"id": alert_summary_id, "tasks": tasks}
serializer = self.get_serializer(self.queryset)

return Response(data=serializer.data)


class PerfCompareResults(generics.ListAPIView):
serializer_class = PerfCompareResultsSerializer
queryset = None
Expand Down
19 changes: 19 additions & 0 deletions treeherder/webapp/api/performance_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,25 @@ def get_name(self, value):
return '{} {} {}'.format(test_suite, value['option_name'], value['extra_options'])


class PerfAlertSummaryTasksQueryParamSerializer(serializers.Serializer):
id = serializers.IntegerField()

def validate(self, data):
try:
PerformanceAlertSummary.objects.get(id=data['id'])
except PerformanceAlertSummary.DoesNotExist:
raise serializers.ValidationError(
{'message': 'PerformanceAlertSummary does not exist.'}
)

return data


class PerformanceAlertSummaryTasksSerializer(serializers.Serializer):
id = serializers.IntegerField()
tasks = serializers.ListField(child=serializers.CharField(), default=[])


class PerfCompareResultsQueryParamsSerializer(serializers.Serializer):
base_revision = serializers.CharField(required=False, allow_null=True, default=None)
new_revision = serializers.CharField()
Expand Down
5 changes: 5 additions & 0 deletions treeherder/webapp/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,11 @@
performance_data.PerformanceSummary.as_view(),
name='performance-summary',
),
re_path(
r'^performance/alertsummary-tasks/$',
performance_data.PerformanceAlertSummaryTasks.as_view(),
name='performance-alertsummary-tasks',
),
re_path(
r'^perfcompare/results/$',
performance_data.PerfCompareResults.as_view(),
Expand Down

0 comments on commit 98ae596

Please sign in to comment.