Browse Source

Move timer call to api

pull/7/head
Pēteris Caune 9 years ago
parent
commit
3c57e4026a
6 changed files with 29 additions and 28 deletions
  1. +1
    -0
      hc/api/urls.py
  2. +23
    -0
      hc/api/views.py
  3. +0
    -1
      hc/front/urls.py
  4. +1
    -24
      hc/front/views.py
  5. +3
    -2
      static/js/index.js
  6. +1
    -1
      templates/index.html

+ 1
- 0
hc/api/urls.py View File

@ -5,4 +5,5 @@ from hc.api import views
urlpatterns = [ urlpatterns = [
url(r'^ping/([\w-]+)/$', views.ping, name="hc-ping"), url(r'^ping/([\w-]+)/$', views.ping, name="hc-ping"),
url(r'^ping/([\w-]+)$', views.ping, name="hc-ping"), url(r'^ping/([\w-]+)$', views.ping, name="hc-ping"),
url(r'^status/([\w-]+)/$', views.status, name="hc-status"),
] ]

+ 23
- 0
hc/api/views.py View File

@ -1,3 +1,6 @@
import json
from django.contrib.humanize.templatetags.humanize import naturaltime
from django.http import HttpResponse, HttpResponseBadRequest from django.http import HttpResponse, HttpResponseBadRequest
from django.utils import timezone from django.utils import timezone
@ -17,3 +20,23 @@ def ping(request, code):
check.save() check.save()
return HttpResponse("OK") return HttpResponse("OK")
def status(request, code):
response = {
"last_ping": None,
"last_ping_human": None,
"secs_to_alert": None
}
check = Check.objects.get(code=code)
if check.last_ping and check.alert_after:
response["last_ping"] = check.last_ping.isoformat()
response["last_ping_human"] = naturaltime(check.last_ping)
duration = check.alert_after - timezone.now()
response["secs_to_alert"] = int(duration.total_seconds())
return HttpResponse(json.dumps(response),
content_type="application/javascript")

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

@ -11,5 +11,4 @@ urlpatterns = [
url(r'^pricing/$', views.pricing, name="hc-pricing"), url(r'^pricing/$', views.pricing, name="hc-pricing"),
url(r'^docs/$', views.docs, name="hc-docs"), url(r'^docs/$', views.docs, name="hc-docs"),
url(r'^about/$', views.about, name="hc-about"), url(r'^about/$', views.about, name="hc-about"),
url(r'^welcome/timer/$', views.welcome_timer, name="hc-welcome-timer"),
] ]

+ 1
- 24
hc/front/views.py View File

@ -1,9 +1,5 @@
import json
from django.conf import settings
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
from django.contrib.humanize.templatetags.humanize import naturaltime
from django.http import HttpResponse, HttpResponseForbidden
from django.http import HttpResponseForbidden
from django.shortcuts import redirect, render from django.shortcuts import redirect, render
from django.utils import timezone from django.utils import timezone
@ -52,27 +48,8 @@ def about(request):
return render(request, "about.html", {"page": "about"}) return render(request, "about.html", {"page": "about"})
def welcome_timer(request):
code = request.session["welcome_code"]
check = Check.objects.get(code=code)
if check.last_ping and check.alert_after:
duration = check.alert_after - timezone.now()
response = {
"last_ping": check.last_ping.isoformat(),
"last_ping_human": naturaltime(check.last_ping),
"timer": int(duration.total_seconds())
}
else:
response = {"last_ping": None, "timer": None}
return HttpResponse(json.dumps(response),
content_type="application/javascript")
@login_required @login_required
def checks(request): def checks(request):
checks = Check.objects.filter(user=request.user).order_by("created") checks = Check.objects.filter(user=request.user).order_by("created")
ctx = { ctx = {


+ 3
- 2
static/js/index.js View File

@ -1,6 +1,7 @@
$(function () { $(function () {
$('[data-toggle="tooltip"]').tooltip(); $('[data-toggle="tooltip"]').tooltip();
var code = $("#check-code").text();
var url = $("#pitch-url").text(); var url = $("#pitch-url").text();
var lastPing = null; var lastPing = null;
var lastPingHuman = null; var lastPingHuman = null;
@ -10,10 +11,10 @@ $(function () {
}); });
function checkLastPing() { function checkLastPing() {
$.getJSON("/welcome/timer/", function(data) {
$.getJSON("/status/" + code + "/", function(data) {
if (data.last_ping != lastPing) { if (data.last_ping != lastPing) {
lastPing = data.last_ping; lastPing = data.last_ping;
$("#timer").data("timer", data.timer);
$("#timer").data("timer", data.secs_to_alert);
} }
var lph = data.last_ping_human; var lph = data.last_ping_human;


+ 1
- 1
templates/index.html View File

@ -66,7 +66,7 @@
<div class="row"> <div class="row">
<div id="welcome-status" class="col-sm-6"> <div id="welcome-status" class="col-sm-6">
<h2>Status <h2>Status
<small>{{ check.code }}</small>
<small id="check-code">{{ check.code }}</small>
</h2> </h2>
<table class="table"> <table class="table">
<tr> <tr>


Loading…
Cancel
Save