Exemplo n.º 1
0
def get_message_alert(alert):
    """
    Returns an alert mesage represetnation
    """
    measurement = get_measurement_from_rule(alert.alert)
    return "At site %s \n %s is %s %s%s" % (alert.site, get_measurement_verbose_name(measurement),
                                            alert.alert.get_operator_display(), alert.alert.value,
                                            get_measurement_unit(measurement))
Exemplo n.º 2
0
def get_latest_bom_data(request):
    """
    Returns the latest information of a site to be displayed in the status card
    The data is got from the influx db
    """
    # getting current site and latest rmc status object
    site_id = request.POST.get('siteId')
    site = Sesh_Site.objects.filter(id=site_id).first()

    # The measurement list contains attributes to be displayed in the status card,
    measurement_list = get_status_card_items(site)

    if measurement_list != 0:
        latest_points = get_measurements_latest_point(site, measurement_list)

        latest_point_data = []

        # If the points exist and the points returned are equal to the items in measurement list
        for measurement, point in latest_points.items():
            latest_point_data.append({"item":get_measurement_verbose_name(measurement),
                                      "value":str(round(latest_points[measurement]['value'], 2))
                                              + get_measurement_unit(measurement)
                             })

        if 'last_contact' in measurement_list:
            # Adding the last contact from the rmc status
            rmc_latest = RMC_status.objects.filter(site=site).last()
            if rmc_latest:
                last_contact = rmc_latest.minutes_last_contact
                last_contact_seconds = last_contact * 60
                last_contact = time_utils.format_timesince_seconds(last_contact_seconds)
                latest_point_data.append({"item":"Last Contact", "value": last_contact})
            else:
                logger.debug("No rmc_status points for site ")

        return HttpResponse(json.dumps(latest_point_data))
Exemplo n.º 3
0
def graphs(request): 
    """
    Returns json, containing data that is used in data analysis graphs
    """
    results = []

    # Getting values from Post request
    time = request.GET.get('time', '') # This is the time range it has to be: 24h, 7d or 30d
    choices = request.GET.getlist('choice[]')
    active_id = request.GET.get('active_site_id', None)
    start_time = request.GET.get('start_time', datetime.now() - timedelta(weeks=1))
    end_time = request.GET.get('end_time', datetime.now())
    resolution = request.GET.get('resolution', '1h')
    current_site = Sesh_Site.objects.filter(id=active_id).first()
    

    if (not current_site) or current_site.organisation != request.user.organisation:
        return HttpResponseBadRequest("Invalid site id, No site was found for the given site id")

    time_delta_dict = {'24h':{'hours':24},
                       '7d': {'days':7},
                       '30d':{'days':30},
                    }

    time_bucket_dict = {'24h':'1h',
                        '7d':'1d',
                        '30d':'5d',
                    }

    # processing post request values to be used in the influx queries
    for choice in choices:
        choice_dict = {}
        choice_dict['measurement'] = choice
        #time_delta = time_delta_dict[time]
        #time_bucket= time_bucket_dict[time]
        choice_dict['si_unit'] = get_measurement_unit(choice)
       
        # Gettting the values of the given element
        client = Influx()

        query_results = client.get_measurement_range_bucket(choice, start_time, end_time, group_by=resolution)

        
        #looping into values
        choice_data = []
        for result in query_results:
            choice_data_dict = {}
            result_time = parser.parse(result['time'])
            result_time = get_epoch_from_datetime(result_time)
            if result['mean'] is not None:
                result_value = round(result['mean'], 2)
            else:
                result_value = 0
            choice_data_dict['time'] = result_time
            choice_data_dict['value'] = result_value
            choice_data.append([result_time, result_value])


        choice_dict['data'] = choice_data
        results.append(choice_dict)

    return HttpResponse(json.dumps(results))