I want to return average of records for every two hour for a week. I have start and date time stamp, I returned the calculated average. But I have to check if there is no record for 6 hours then have to return 0 for every tow hours in this case.
What I have done looks like this:
db query with start-end date(func {
for Each(func {
got record
}
calculate average for tow hours
)
res.send(average and other requirements );
})
result looks like
average: 2450,
time: "Sun Mar 22 2015 02:00:00",
hour: "2:00",
average: 2780,
time: "Sun Mar 22 2015 04:00:00",
hour: "4:00",
average: 2360,
time: "Sun Mar 22 2015 08:00:00",
hour: "8:00",
Note that there is nothing for 6th hour. I want to check if there is no record between two hours then send 0 average.
// Selecting table from DB in this case "gateway_status"
var gateway_status = db.collection("gateway_status");
// This will contain records from DB
var temp_record = [];
// This will send average record and other data of device to client side
var temp_list_data = {};
//gateway_id & device_id start date and end date from client side
var gateway_id = parseInt(event.params.gateway_id);
var device_id = parseInt(event.params.device_id);
var start_date = parseInt(event.params.start_date);
var end_date = parseInt(event.params.end_date);
temp_list_data.device_id = device_id;
temp_list_data.gateway_id = gateway_id;
temp_list_data.data = [];
// query criteria
var query_criteria;
if (start_date && end_date != null) {
query_criteria = { "date_time": { "$gte": start_date, "$lte": end_date},
"device_id": device_id, "gateway_id": gateway_id };
// Getting records from DB
gateway_status.find(query_criteria).sort({"date_time": 1}).toArray(function (err, res_data) {
if (err) {
// resp.send(err);
} else {
// will contain indoor temperature average
var temp_average = 0;
// will contain set point average
var setPoint_average = 0;
// sum of indoor temperature
var temp_sum = 0;
// sum of set point temperature
var setPoint_sum = 0;
// for graph weekly bases
var hours_difference = 120;
// Counting the quantity of records
var record_count = 0;
// Date time of first record
var first_time = null;
// Time difference of records
var time_difference = null;
var record_day = '';
var record_check = 0;
res_data.forEach(function (data) {
// Local object
var device_record = {};
if (data.data != null && data.data.length != null) {
var json_data = data.data;
// for indoor temp
for (var i = 0; i < json_data.length; i++) {
if (json_data[i].r == 203) {
//Pass in the data of indoor temp and time
device_record.indoor = json_data[i].v;
device_record.time = data.date_time;
}
// for set point temp
else if (json_data[i].r == 1004) {
//Pass in the data of set point
device_record.setPoint = json_data[i].v;
}
}
}
// Start time
if (first_time == null) {
first_time = new Date(device_record.time);
}
// Time difference of records
time_difference = helper.MinutesBetweenTwoDates(new Date(device_record.time), first_time);
// If we have all records of last two hours
if (time_difference >= hours_difference) {
record_check = 2;
if(new Date(device_record.time).getHours() > record_check){
console.log(new Date(device_record.time).getHours());
}
// Date time of first record
first_time = new Date(device_record.time);
// Calculating average
temp_average = Math.round(temp_sum / record_count);
setPoint_average = Math.round(setPoint_sum / record_count);
var temp_Date = new Date(device_record.time);
temp_Date.setMinutes(0);
temp_Date.setSeconds(0);
temp_Date.setMilliseconds(0);
// console.log(temp_Date,temp_Date.getDay());
if (temp_Date.getDay() == 1) {
record_day = "Monday";
} else if (temp_Date.getDay() == 2) {
record_day = "Tuesday";
} else if (temp_Date.getDay() == 3) {
record_day = "Wednesday";
} else if (temp_Date.getDay() == 4) {
record_day = "Thursday";
} else if (temp_Date.getDay() == 5) {
record_day = "Friday";
} else if (temp_Date.getDay() == 6) {
record_day = "Saturday";
} else if (temp_Date.getDay() == 0) {
record_day = "Sunday";
}
// Push average data of month for sending to client side
temp_list_data.data.push({"indoor": temp_average,
"time": temp_Date.toString(),
"day": record_day,
"hour": temp_Date.getHours().toString() + ":00",
"setPoint": setPoint_average
});
// Set them for next two hours
record_count = 0;
temp_sum = 0;
setPoint_sum = 0;
}
else {
//keep adding values in sum till two hours are filled
temp_sum += device_record.indoor;
setPoint_sum += device_record.setPoint;
//keep counting records
record_count++;
}
});
}
resp.send(temp_list_data);
}
);
}
Aucun commentaire:
Enregistrer un commentaire