def post(self, request): name = request.POST.get('name') frequency = request.POST.get('frequency') value = request.POST.get('value', 1) api_key = request.POST.get('api_key') status = int(request.POST.get('status', 1)) public = bool(request.POST.get('public')) company = Company.objects.get(public_key=api_key) ip = get_real_ip(request) or '127.0.0.1' if not name: return HttpResponse('name needed') if status == -1: send_alert(company, name) Alert.objects.filter(name=name).delete() Alert.objects.create( name=name, company=company, ip_address=ip, ) elif frequency: expiration_date = ( datetime.datetime.now() + datetime.timedelta(seconds=int(frequency)) ) Alert.objects.filter(name=name).delete() Alert.objects.create( name=name, company=company, notify_at=expiration_date, ip_address=ip, ) Ping.objects.create( name=name, company=company, value=value, ip_address=ip, status_passing=(status == 1), public=public, ) return HttpResponse('ok')
def post(self, request): name = request.POST.get('name') frequency = request.POST.get('frequency') value = request.POST.get('value', 1) api_key = request.POST.get('api_key') status = int(request.POST.get('status', 1)) public = bool(request.POST.get('public')) company = Company.objects.get(public_key=api_key) ip = get_real_ip(request) or '127.0.0.1' if not name: return HttpResponse('name needed') # drop old alerts Alert.objects.filter(name=name).delete() if status == -1: send_alert(company, name) Alert.objects.create( name=name, company=company, ip_address=ip, ) elif frequency: expiration_date = (datetime.datetime.now() + datetime.timedelta(seconds=int(frequency))) Alert.objects.create( name=name, company=company, notify_at=expiration_date, ip_address=ip, ) Ping.objects.create( name=name, company=company, value=value, ip_address=ip, status_passing=(status == 1), public=public, ) return HttpResponse('ok')
def handle(self, *args, **options): # delete old pings for company in Company.objects.all(): pings_by_name = Ping.objects.filter(company=company).distinct('name') if not pings_by_name: continue max_per_ping = 2000 / len(pings_by_name) for name in pings_by_name: pings = Ping.objects.filter(company=company, name=name).order_by('-create')[:max_per_ping] pings = list(pings.values_list("id", flat=True)) Ping.objects.filter(company=company, name=name).exclude(pk__in=pings).delete() # send alerts alerts = Alert.objects.filter(notify_at__lte=datetime.datetime.now) for alert in alerts: send_alert(alert.company, alert.name) alert.notify_at = None alert.save()