def computeIndicators(self,indicator_type,sensor,day_of_computation,ts_from,ts_to):
		self.__dataFetcher.compileUrl("viabasse",sensor,ts_from,ts_to)

		if self.__dataFetcher.call() == 1 and self.__dataFetcher.getStatusCode() == 200:

			data = self.__dataFetcher.getJson()

			min_value = Statistic(day=day_of_computation,k="min_temp",v=10000.0,sensor=sensor,type=indicator_type)
			max_value = Statistic(day=day_of_computation,k="max_temp",v=-10000.0,sensor=sensor,type=indicator_type)
			sum_value = 0
			count_value = 0

			for line in data:

				if line["temperature"] < min_value.v:
					min_value.v = float(line["temperature"])
					min_value.when = datetime.fromtimestamp((int(line["timestamp"])/1000))
				if line["temperature"] > max_value.v:
					max_value.v = float(line["temperature"])
					max_value.when = datetime.fromtimestamp((int(line["timestamp"])/1000))

				sum_value = sum_value + line["temperature"]
				count_value = count_value + 1

			avg_value = Statistic(day=day_of_computation,k="avg_temp",v=round(sum_value/count_value,2),sensor=sensor,type=indicator_type)
			range_value = Statistic(day=day_of_computation,k="range_temp",v=max_value.v-min_value.v,sensor=sensor,type=indicator_type)
			return {'min':min_value,'max':max_value,'avg':avg_value,'range':range_value}
		else:
			return None
예제 #2
0
def stats(request, sensor):

    yesterday = date.today()-timedelta(1)
    tmp_ts_from = int(mktime(yesterday.timetuple()))
    print tmp_ts_from
    tmp_ts_to = tmp_ts_from + 24*60*60
    ts_from = tmp_ts_from * 1000
    ts_to = tmp_ts_to * 1000

    result = makeRequest(
        sensor,
        ts_from,
        ts_to
        )

    if result.status_code == 200:

        data = json.loads(result.content)

        min_value = Statistic(day=datetime.fromtimestamp(tmp_ts_from).date(),k="min_temp",v=10000.0,sensor=sensor)
        max_value = Statistic(day=datetime.fromtimestamp(tmp_ts_from).date(),k="max_temp",v=-10000.0,sensor=sensor)
        sum_value = 0
        count_value = 0

        for line in data:

            if line["temperature"] < min_value.v:
                min_value.v = float(line["temperature"])
                min_value.when = datetime.fromtimestamp((int(line["timestamp"])/1000)+settings.TMZDIFF)
            if line["temperature"] > max_value.v:
                max_value.v = float(line["temperature"])
                max_value.when = datetime.fromtimestamp((int(line["timestamp"])/1000)+settings.TMZDIFF)
            
            sum_value = sum_value + line["temperature"]
            count_value = count_value + 1

        avg_value = Statistic(day=datetime.fromtimestamp(tmp_ts_from).date(),k="avg_temp",v=round(sum_value/count_value,2),sensor=sensor)

    db.put(min_value)
    db.put(max_value)
    db.put(avg_value)

    return HttpResponse("min: {}, max: {}, avg: {}".format(min_value.v,max_value.v,avg_value.v))