Skip to content

Commit

Permalink
Merge pull request #56 from hibare/dev
Browse files Browse the repository at this point in the history
Add request round trip time
  • Loading branch information
hibare authored Jul 18, 2021
2 parents a4e21a1 + e28945f commit cd13408
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 11 deletions.
3 changes: 2 additions & 1 deletion backend/jobs/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ class JobsHistoryAdmin(admin.ModelAdmin):
"""Jobs History admin class"""

empty_value_display = '-empty-'
list_display = ['timestamp', 'uuid', 'status_code', 'success']
list_display = ['timestamp', 'uuid',
'status_code', 'success', 'response_time']
list_filter = ['timestamp', 'uuid', 'status_code', 'success']

def has_add_permission(self, request: HttpRequest) -> bool:
Expand Down
18 changes: 18 additions & 0 deletions backend/jobs/migrations/0020_jobshistory_response_time.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.2.5 on 2021-07-18 14:48

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('jobs', '0019_jobs_headers'),
]

operations = [
migrations.AddField(
model_name='jobshistory',
name='response_time',
field=models.FloatField(null=True),
),
]
1 change: 1 addition & 0 deletions backend/jobs/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class JobsHistory(models.Model):
Jobs, related_name="jobs_history_uuid", on_delete=models.CASCADE)
status_code = models.IntegerField(null=True)
success = models.BooleanField()
response_time = models.FloatField(null=True)

objects = JobHistoryManager()

Expand Down
31 changes: 22 additions & 9 deletions backend/jobs/operations.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Job Operations"""

import time
import ssl
import logging
from typing import Dict, Tuple, Union
Expand All @@ -11,9 +12,15 @@
logger = logging.getLogger(__name__)


def request(url: str, headers: Dict = dict, verify_ssl: bool = True, check_redirect: bool = True) -> Tuple[Union[int, None], Union[str, None]]:
def request(url: str, headers: Dict = dict, verify_ssl: bool = True, check_redirect: bool = True) -> Tuple[Union[int, None], Union[str, None], Union[float, None]]:
"""HTTP Request executor"""

DEFAULT_HEADERS = {
"Cache-Control": "no-cache"
}

headers.update(DEFAULT_HEADERS)

try:
if verify_ssl:
cert_reqs = ssl.CERT_REQUIRED
Expand All @@ -22,22 +29,26 @@ def request(url: str, headers: Dict = dict, verify_ssl: bool = True, check_redir

http = urllib3.PoolManager(cert_reqs=cert_reqs)

start = time.time()
response = http.request(
'GET',
url,
headers=headers,
timeout=10,
redirect=check_redirect
)
end = time.time()

elapsed_seconds = end - start

return response.status, response.data.decode()
return response.status, response.data.decode(), elapsed_seconds

except UnicodeDecodeError:
return response.status, response.data
return response.status, response.data, None

except Exception:
logger.exception("URL=%s", url)
return None, None
return None, None, None


def executor(id: str) -> None:
Expand All @@ -56,10 +67,11 @@ def executor(id: str) -> None:
check_redirect = job.check_redirect
notify_url = job.notify_url

status_code, response = request(
status_code, response, elapsed_seconds = request(
url, headers, verify_ssl, check_redirect)
logger.info("Response id=%s, url=%s, status_code=%s",
id, url, status_code)

logger.info("Response id=%s, url=%s, status_code=%s, elapsed_seconds=%s",
id, url, status_code, elapsed_seconds)

success = (status_code in success_status)

Expand All @@ -76,13 +88,14 @@ def executor(id: str) -> None:
_ = JobsHistory.objects.create(
uuid=job,
status_code=status_code,
success=success
success=success,
response_time=elapsed_seconds
)

except Exception:
logger.exception("Failed to execute healthcheck, id=%s", id)

# Notify failure
# Todo - Notify failure


class JobOps:
Expand Down
6 changes: 5 additions & 1 deletion backend/jobs/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@ class JobsHistorySerializer(serializers.HyperlinkedModelSerializer):
read_only=True,
help_text="Status Code"
)
response_time = serializers.FloatField(
read_only=True,
help_text="Round trip time in seconds"
)

class Meta:
model = JobsHistory
fields = ['timestamp', 'uuid', 'status_code', 'success']
fields = ['timestamp', 'uuid', 'status_code', 'success', 'response_time']

0 comments on commit cd13408

Please sign in to comment.