I'm building a web interface for a home monitoring sensor array, and fetching JSON from the backend framework. I want to start putting statistics together for monitoring different areas of the home, inside and outside.
Each thermal sensor provide the reading, in regular intervals, of temperature and time of day, so I end up with a JSON object looking like this:
{ "sensor_1" : [ { "location" : "garage_east", "temp_f" : [{ "value" : "71.2", "timestamp" : "2017-05-01T08:15:00" }, { "value" : "70.8", "timestamp" : "2017-05-01T08:30:00" }, { "value" : "72.6", "timestamp" : "2017-05-01T08:45:00" } ]}], "sensor_2" : [{ "location" : "kitchen", "temp_f" : [{ "value" : "78.2", "timestamp" : "2017-05-01T08:15:00" }, { "value" : "78.8", "timestamp" : "2017-05-01T08:30:00" }, { "value" : "78.6", "timestamp" : "2017-05-01T08:45:00" }] }]}
and so on, with multiple sensors each displaying pretty large JSON objects for the reporting periods.
I want to derive the highest and lowest temperatures around the house, and at what time they occurred, using JavaScript (with AngularJS). Currently, I'm looking to optimize this (which does work):
/* The entire array of data, within time frame set by the user * and for the sensors defined by the user * from the RESTful API (params) */ api.sensor_data.get(params, function(data){ $scope.sensorStats = { "kitchen" : {}, "garage" : {} } /* find the highest temperature for a single sensor */ let hightemp_kitchen = Math.max.apply(Math, data.sensor_1.temp_f.map(function(t){ return t.value; })); /* grab the associated timestamp and put them both into an object */ $scope.sensorStats.kitchen.high = data.sensor_1.temp_f.find(function(obj){ return obj.value == hightemp_kitchen; }); /* Repeat for each sensor of concern (user defined in params) */ etc... }); }, ...
and then I use the in the DOM the expression {{ sensorStats.kitchen.high }} to display the high temp and date/time to the user for the kitchen.
Like I say, this works, but I'm doing this for each of the 15+ sensors in the array around the property. This method not the easiest to read and maintain - I think it can be faster and more efficient to maintain, but I don't have the lexicon to know what to research.
Is this efficient? Is there a "better" or more efficient way to filter out the data and return the temp and time? I'm reading up on ES6, and discovered the filter() method, but it seems to be useful only on arrays and not objects.