Browse Source

Save request body for every ping, not just the last one.

pull/163/head
Pēteris Caune 7 years ago
parent
commit
02f6853d4c
15 changed files with 140 additions and 49 deletions
  1. +25
    -0
      hc/api/migrations/0038_auto_20180318_1216.py
  2. +4
    -4
      hc/api/models.py
  3. +10
    -3
      hc/api/tests/test_ping.py
  4. +1
    -2
      hc/api/views.py
  5. +0
    -21
      hc/front/tests/test_last_ping.py
  6. +37
    -0
      hc/front/tests/test_ping_details.py
  7. +2
    -1
      hc/front/urls.py
  8. +7
    -3
      hc/front/views.py
  9. +6
    -6
      static/css/ping_details.css
  10. +3
    -3
      static/js/checks.js
  11. +17
    -0
      static/js/log.js
  12. +1
    -1
      templates/base.html
  13. +23
    -1
      templates/front/log.html
  14. +2
    -2
      templates/front/my_checks.html
  15. +2
    -2
      templates/front/ping_details.html

+ 25
- 0
hc/api/migrations/0038_auto_20180318_1216.py View File

@ -0,0 +1,25 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.6 on 2018-03-18 12:16
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('api', '0037_auto_20180127_1215'),
]
operations = [
migrations.AddField(
model_name='check',
name='has_confirmation_link',
field=models.BooleanField(default=False),
),
migrations.AddField(
model_name='ping',
name='body',
field=models.CharField(blank=True, max_length=10000),
),
]

+ 4
- 4
hc/api/models.py View File

@ -71,6 +71,7 @@ class Check(models.Model):
n_pings = models.IntegerField(default=0)
last_ping = models.DateTimeField(null=True, blank=True)
last_ping_body = models.CharField(max_length=10000, blank=True)
has_confirmation_link = models.BooleanField(default=False)
alert_after = models.DateTimeField(null=True, blank=True, editable=False)
status = models.CharField(max_length=6, choices=STATUSES, default="new")
@ -182,13 +183,10 @@ class Check(models.Model):
return result
def has_confirmation_link(self):
return "confirm" in self.last_ping_body.lower()
def ping(self, remote_addr, scheme, method, ua, body):
self.n_pings = models.F("n_pings") + 1
self.last_ping = timezone.now()
self.last_ping_body = body[:10000]
self.has_confirmation_link = "confirm" in str(body).lower()
self.alert_after = self.get_alert_after()
if self.status in ("new", "paused"):
self.status = "up"
@ -203,6 +201,7 @@ class Check(models.Model):
ping.method = method
# If User-Agent is longer than 200 characters, truncate it:
ping.ua = ua[:200]
ping.body = body[:10000]
ping.save()
@ -215,6 +214,7 @@ class Ping(models.Model):
remote_addr = models.GenericIPAddressField(blank=True, null=True)
method = models.CharField(max_length=10, blank=True)
ua = models.CharField(max_length=200, blank=True)
body = models.CharField(max_length=10000, blank=True)
class Channel(models.Model):


+ 10
- 3
hc/api/tests/test_ping.py View File

@ -36,11 +36,9 @@ class PingTestCase(TestCase):
content_type="text/plain")
self.assertEqual(r.status_code, 200)
self.check.refresh_from_db()
self.assertEqual(self.check.last_ping_body, "hello world")
ping = Ping.objects.latest("id")
self.assertEqual(ping.method, "POST")
self.assertEqual(ping.body, "hello world")
def test_head_works(self):
csrf_client = Client(enforce_csrf_checks=True)
@ -107,3 +105,12 @@ class PingTestCase(TestCase):
def test_it_never_caches(self):
r = self.client.get("/ping/%s/" % self.check.code)
assert "no-cache" in r.get("Cache-Control")
def test_it_updates_confirmation_flag(self):
payload = "Please Confirm ..."
r = self.client.post("/ping/%s/" % self.check.code, data=payload,
content_type="text/plain")
self.assertEqual(r.status_code, 200)
self.check.refresh_from_db()
self.assertTrue(self.check.has_confirmation_link)

+ 1
- 2
hc/api/views.py View File

@ -28,9 +28,8 @@ def ping(request, code):
scheme = headers.get("HTTP_X_FORWARDED_PROTO", "http")
method = headers["REQUEST_METHOD"]
ua = headers.get("HTTP_USER_AGENT", "")
body = request.body[:10000]
check.ping(remote_addr, scheme, method, ua, body)
check.ping(remote_addr, scheme, method, ua, request.body)
response = HttpResponse("OK")
response["Access-Control-Allow-Origin"] = "*"


+ 0
- 21
hc/front/tests/test_last_ping.py View File

@ -1,21 +0,0 @@
from hc.api.models import Check, Ping
from hc.test import BaseTestCase
class LastPingTestCase(BaseTestCase):
def test_it_works(self):
check = Check(user=self.alice)
check.last_ping_body = "this is body"
check.save()
Ping.objects.create(owner=check)
self.client.login(username="[email protected]", password="password")
r = self.client.post("/checks/%s/last_ping/" % check.code)
self.assertContains(r, "this is body", status_code=200)
def test_it_requires_user(self):
check = Check.objects.create()
r = self.client.post("/checks/%s/last_ping/" % check.code)
self.assertEqual(r.status_code, 403)

+ 37
- 0
hc/front/tests/test_ping_details.py View File

@ -0,0 +1,37 @@
from hc.api.models import Check, Ping
from hc.test import BaseTestCase
class LastPingTestCase(BaseTestCase):
def test_it_works(self):
check = Check(user=self.alice)
check.save()
Ping.objects.create(owner=check, body="this is body")
self.client.login(username="[email protected]", password="password")
r = self.client.post("/checks/%s/last_ping/" % check.code)
self.assertContains(r, "this is body", status_code=200)
def test_it_requires_user(self):
check = Check.objects.create()
r = self.client.post("/checks/%s/last_ping/" % check.code)
self.assertEqual(r.status_code, 403)
def test_it_accepts_n(self):
check = Check(user=self.alice)
check.last_ping_body = "this is body"
check.save()
# remote_addr, scheme, method, ua, body):
check.ping("1.2.3.4", "http", "post", "tester", "foo-123")
check.ping("1.2.3.4", "http", "post", "tester", "bar-456")
self.client.login(username="[email protected]", password="password")
r = self.client.post("/checks/%s/pings/1/" % check.code)
self.assertContains(r, "foo-123", status_code=200)
r = self.client.post("/checks/%s/pings/2/" % check.code)
self.assertContains(r, "bar-456", status_code=200)

+ 2
- 1
hc/front/urls.py View File

@ -8,7 +8,8 @@ check_urls = [
url(r'^pause/$', views.pause, name="hc-pause"),
url(r'^remove/$', views.remove_check, name="hc-remove-check"),
url(r'^log/$', views.log, name="hc-log"),
url(r'^last_ping/$', views.last_ping, name="hc-last-ping"),
url(r'^last_ping/$', views.ping_details, name="hc-last-ping"),
url(r'^pings/([\d]+)/$', views.ping_details, name="hc-ping-details"),
]
channel_urls = [


+ 7
- 3
hc/front/views.py View File

@ -249,7 +249,7 @@ def cron_preview(request):
@require_POST
def last_ping(request, code):
def ping_details(request, code, n=None):
if not request.user.is_authenticated:
return HttpResponseForbidden()
@ -257,14 +257,18 @@ def last_ping(request, code):
if check.user_id != request.team.user.id:
return HttpResponseForbidden()
ping = Ping.objects.filter(owner=check).latest("created")
q = Ping.objects.filter(owner=check)
if n:
q = q.filter(n=n)
ping = q.latest("created")
ctx = {
"check": check,
"ping": ping
}
return render(request, "front/last_ping.html", ctx)
return render(request, "front/ping_details.html", ctx)
@require_POST


static/css/last_ping.css → static/css/ping_details.css View File


+ 3
- 3
static/js/checks.js View File

@ -176,8 +176,8 @@ $(function () {
});
$(".last-ping-cell").on("click", ".last-ping", function() {
$("#last-ping-body").text("Updating...");
$('#last-ping-modal').modal("show");
$("#ping-details-body").text("Updating...");
$('#ping-details-modal').modal("show");
var token = $('input[name=csrfmiddlewaretoken]').val();
$.ajax({
@ -185,7 +185,7 @@ $(function () {
type: "post",
headers: {"X-CSRFToken": token},
success: function(data) {
$("#last-ping-body" ).html(data);
$("#ping-details-body" ).html(data);
}
});


+ 17
- 0
static/js/log.js View File

@ -1,4 +1,21 @@
$(function () {
$(".details-btn").on("click", function() {
$("#ping-details-body").text("Updating...");
$('#ping-details-modal').modal("show");
var token = $('input[name=csrfmiddlewaretoken]').val();
$.ajax({
url: this.dataset.url,
type: "post",
headers: {"X-CSRFToken": token},
success: function(data) {
$("#ping-details-body" ).html(data);
}
});
return false;
});
function switchDateFormat(format) {
$("#log td.datetime").each(function(index, cell) {
var dt = moment(cell.getAttribute("data-raw"));


+ 1
- 1
templates/base.html View File

@ -36,7 +36,7 @@
<link rel="stylesheet" href="{% static 'css/add_pushover.css' %}" type="text/css">
<link rel="stylesheet" href="{% static 'css/add_webhook.css' %}" type="text/css">
<link rel="stylesheet" href="{% static 'css/settings.css' %}" type="text/css">
<link rel="stylesheet" href="{% static 'css/last_ping.css' %}" type="text/css">
<link rel="stylesheet" href="{% static 'css/ping_details.css' %}" type="text/css">
<link rel="stylesheet" href="{% static 'css/profile.css' %}" type="text/css">
<link rel="stylesheet" href="{% static 'css/checkbox.css' %}" type="text/css">
<link rel="stylesheet" href="{% static 'css/radio.css' %}" type="text/css">


+ 23
- 1
templates/front/log.html View File

@ -40,6 +40,7 @@
<th class="ip">IP</th>
<th class="protocol">Protocol</th>
<th class="ua">User Agent</th>
<th></th>
</tr>
{% for event in events %}
{% if event.n %}
@ -66,6 +67,12 @@
<td class="ua">
{{ event.ua }}
</td>
<td>
{% if event.body %}
<a href="#" class="details-btn" data-url="{% url 'hc-ping-details' check.code event.n %}">show body</a>
{% endif %}
</td>
</tr>
{% endif %}
{% if event.check_status %}
@ -112,7 +119,7 @@
{% endfor %}
</table>
{% if show_limit_notice and limit < 10000 %}
{% if show_limit_notice and limit < 1000 %}
<p class="alert alert-info">
<strong>Showing last {{ limit }} pings.</strong>
Want to see more?
@ -128,6 +135,21 @@
{% endif %}
</div>
</div>
<div id="ping-details-modal" class="modal">
<div class="modal-dialog">
<div class="modal-content">
<div id="ping-details-body">Loading</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Got It!</button>
</div>
</div>
</div>
<form>
{% csrf_token %}
</form>
</div>
{% endblock %}
{% block scripts %}


+ 2
- 2
templates/front/my_checks.html View File

@ -354,10 +354,10 @@
</div>
</div>
<div id="last-ping-modal" class="modal">
<div id="ping-details-modal" class="modal">
<div class="modal-dialog">
<div class="modal-content">
<div id="last-ping-body">Loading</div>
<div id="ping-details-body">Loading</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Got It!</button>
</div>


templates/front/last_ping.html → templates/front/ping_details.html View File


Loading…
Cancel
Save