示例#1
0
def get_alerts(request):
    site_id = request.POST.get('site_id','')


    alerts = Sesh_Alert.objects.filter(site=site_id, isSilence=False).order_by('-date')[:5]

    alert_data = []


    # Loop to generate alert data
    for alert in alerts:
        alert_data.append({
            "alertId": alert.id,
            "site":alert.site.site_name,
            "alert":str(alert),
            "date":get_timesince(alert.date),
            })

    return HttpResponse(json.dumps(alert_data))
示例#2
0
def display_alert_data(request):
    # Getting the clicked alert via ajax
    alert_id = request.POST.get("alertId",'')

    if alert_id.isdigit():
        alert = Sesh_Alert.objects.filter(id=alert_id).first()
    else:
        raise Exception("Invalid alert id, alert id is not an integer")

    alert_values = {}
    point = alert_utils.get_alert_point(alert)

    if point:
        """
        If the point is from influx it comes as a dict
        if the point is from an sql db it comes as a model that needs to
        be converted to a dict
        """
        if type(point) != dict:
            alert_values = model_to_dict(point)
        else:
            alert_values = point

        # If the the time is from influx then we convert it to a python datetime
        if type(alert_values['time'])  == unicode:
            alert_values['time'] = parser.parse(alert_values['time'])

        alert_values['time'] = get_timesince(alert_values['time'])

        # Setting the id to the alert id (NEEDED FOR SILENCING ALERTS)
        alert_values['id'] =  alert_id
        return HttpResponse(json.dumps(alert_values))

    else:
        # Handling unecpexted data failure
        raise Exception("The alert has not related point,")