Skip to content
This repository has been archived by the owner on Dec 15, 2018. It is now read-only.

Commit

Permalink
Increase infra_fail_job_index limit.
Browse files Browse the repository at this point in the history
Summary:
Arguably should be using `paginate`, but that'd require a schema/UI change, and
this is very easy.

Test Plan: None

Reviewers: naphat

Reviewed By: naphat

Subscribers: changesbot, anupc

Differential Revision: https://tails.corp.dropbox.com/D231183
  • Loading branch information
kylec1 committed Sep 22, 2016
1 parent 5c25d4a commit 1fc7b2f
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
10 changes: 9 additions & 1 deletion changes/api/infra_fail_job_index.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,34 @@
from __future__ import absolute_import

from flask.ext.restful import reqparse
from sqlalchemy.orm import joinedload
from datetime import datetime, timedelta

from changes.api.base import APIView
from changes.api.serializer.models.job import JobWithBuildCrumbler
from changes.api.validators.basic import bounded_integer
from changes.constants import Result
from changes.models.job import Job


class InfraFailJobIndexAPIView(APIView):
"""Defines the endpoint for fetching jobs that recently failed for infrastructural reasons."""

get_parser = reqparse.RequestParser()
get_parser.add_argument('limit', type=bounded_integer(0, 1000), location='args',
default=250)

def get(self):
args = self.get_parser.parse_args()

jobs = Job.query.options(
joinedload(Job.build, innerjoin=True),
).filter(
Job.result == Result.infra_failed,
# Only last 24 hours so that a non-empty view can be a matter for concern.
Job.date_created >= datetime.utcnow() - timedelta(hours=24),
).order_by(Job.date_created.desc()
).limit(50) # fairly arbitrary limit
).limit(args.limit)

result = {
'recent': list(jobs),
Expand Down
13 changes: 13 additions & 0 deletions changes/api/validators/basic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from typing import Callable # NOQA


def bounded_integer(lower, upper):
"""Accepts an integer in [lower, upper]."""
# type: (int, int) -> Callable[str, int]
def parse(s):
# type: (str) -> int
iv = int(s)
if iv < lower or iv > upper:
raise ValueError("{} is not in [{}, {}]".format(iv, lower, upper))
return iv
return parse
26 changes: 26 additions & 0 deletions tests/changes/api/validators/test_basic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import pytest
from changes.api.validators.basic import bounded_integer


def test_bounded_integer():
validator = bounded_integer(0, 10)
with pytest.raises(ValueError):
validator("11")

with pytest.raises(ValueError):
validator("4.5")

with pytest.raises(ValueError):
validator("-3")

with pytest.raises(ValueError):
validator("")

with pytest.raises(ValueError):
validator("10000")

# No error expeceted
validator("0")
validator("2")
validator("9")
validator("10")

0 comments on commit 1fc7b2f

Please sign in to comment.