From 796bbdf7f589b34a49a605f0ced2a5787a1861fa Mon Sep 17 00:00:00 2001 From: charithag Date: Tue, 21 Jul 2015 13:36:02 +0530 Subject: [PATCH 1/2] Add CPU temperature graph to analysis --- .../jaggeryapps/iotserver/api/stats-api.jag | 171 +++++++++--------- .../iotserver/units/analytics/analytics.hbs | 3 + .../units/analytics/public/js/graph_util.js | 64 ++++--- .../public/js/graphs/cpu_temperature_graph.js | 70 +++++++ 4 files changed, 196 insertions(+), 112 deletions(-) create mode 100644 modules/distribution/src/repository/jaggeryapps/iotserver/units/analytics/public/js/graphs/cpu_temperature_graph.js diff --git a/modules/distribution/src/repository/jaggeryapps/iotserver/api/stats-api.jag b/modules/distribution/src/repository/jaggeryapps/iotserver/api/stats-api.jag index c1af157b..9f3fa5fb 100644 --- a/modules/distribution/src/repository/jaggeryapps/iotserver/api/stats-api.jag +++ b/modules/distribution/src/repository/jaggeryapps/iotserver/api/stats-api.jag @@ -31,118 +31,111 @@ var statsClient = new Packages.org.wso2.carbon.device.mgt.iot.common.analytics.s if (uriMatcher.match("/{context}/api/stats")) { - deviceId = request.getParameter("deviceId"); - deviceType = request.getParameter("deviceType"); - from = request.getParameter("from"); - to = request.getParameter("to"); - - user = session.get(constants.USER_SESSION_KEY); - if (!user) { - response.sendRedirect(dcProps.appContext + "login?#login-required"); - exit(); - } - - log.info("deviceId : " + deviceId + " from : " + from + " to : " + to); - - switch (deviceType){ - case "firealarm": - result = getFireAlarmData(user.username, deviceId, from, to); - break; - case "sensebot": - result = getSensebotData(user.username, deviceId, from, to); - break; - case "arduino": - result = getArduinoData(user.username, deviceId, from, to); - break; - case "digital_display": - result = getDigitalDisplayData(user.username, deviceId, from, to); - break; - case "android_sense": - result = getAndroidSenseData(user.username, deviceId, from, to); - break; - case "raspberrypi": - result = getDigitalDisplayData(user.username, deviceId, from, to); - break; - default: - result = new Object(); - } + deviceId = request.getParameter("deviceId"); + deviceType = request.getParameter("deviceType"); + from = request.getParameter("from"); + to = request.getParameter("to"); + + user = session.get(constants.USER_SESSION_KEY); + if (!user) { + response.sendRedirect(dcProps.appContext + "login?#login-required"); + exit(); + } + + log.info("deviceId : " + deviceId + " from : " + from + " to : " + to); + + switch (deviceType) { + case "firealarm": + result = getFireAlarmData(user.username, deviceId, from, to); + break; + case "sensebot": + result = getSensebotData(user.username, deviceId, from, to); + break; + case "arduino": + result = getArduinoData(user.username, deviceId, from, to); + break; + case "digital_display": + result = getDigitalDisplayData(user.username, deviceId, from, to); + break; + case "android_sense": + result = getAndroidSenseData(user.username, deviceId, from, to); + break; + case "raspberrypi": + result = getDigitalDisplayData(user.username, deviceId, from, to); + break; + default: + result = new Object(); + } } // returning the result. if (result) { - print(result); + print(result); } -function getFireAlarmData(user, deviceId, from, to){ - result = new Object(); - result['temperatureData'] = getSensorData("DEVICE_TEMPERATURE_SUMMARY","TEMPERATURE",user, deviceId, from, to); - result['fanData'] = getSensorData("DEVICE_FAN_USAGE_SUMMARY","status",user, deviceId, from, to); - result['bulbData'] = getSensorData("DEVICE_BULB_USAGE_SUMMARY","status",user, deviceId, from, to); - return result; +function getFireAlarmData(user, deviceId, from, to) { + result = new Object(); + result['cpuTemperatureData'] = getSensorData("DEVICE_CPU_TEMPERATURE_SUMMARY", "TEMPERATURE", user, deviceId, from, to); + result['temperatureData'] = getSensorData("DEVICE_TEMPERATURE_SUMMARY", "TEMPERATURE", user, deviceId, from, to); + result['fanData'] = getSensorData("DEVICE_FAN_USAGE_SUMMARY", "status", user, deviceId, from, to); + result['bulbData'] = getSensorData("DEVICE_BULB_USAGE_SUMMARY", "status", user, deviceId, from, to); + return result; } -function getSensebotData(user, deviceId, from, to){ - result = new Object(); - result['sonarData'] = getSensorData("SONAR_SENSOR_SUMMARY","sonar",user, deviceId, from, to); - result['motionData'] = getSensorData("PIR_MOTION_SENSOR_SUMMARY","motion",user, deviceId, from, to); - result['lightData'] = getSensorData("LDR_LIGHT_SENSOR_SUMMARY","light",user, deviceId, from, to); - result['temperatureData'] = getSensorData("DEVICE_TEMPERATURE_SUMMARY","TEMPERATURE",user, deviceId, from, to); - return result; +function getSensebotData(user, deviceId, from, to) { + result = new Object(); + result['sonarData'] = getSensorData("SONAR_SENSOR_SUMMARY", "sonar", user, deviceId, from, to); + result['motionData'] = getSensorData("PIR_MOTION_SENSOR_SUMMARY", "motion", user, deviceId, from, to); + result['lightData'] = getSensorData("LDR_LIGHT_SENSOR_SUMMARY", "light", user, deviceId, from, to); + result['temperatureData'] = getSensorData("DEVICE_TEMPERATURE_SUMMARY", "TEMPERATURE", user, deviceId, from, to); + return result; } -function getArduinoData(user, deviceId, from, to){ - result = new Object(); - result['temperatureData'] = getSensorData("DEVICE_TEMPERATURE_SUMMARY","TEMPERATURE",user, deviceId, from, to); - return result; +function getArduinoData(user, deviceId, from, to) { + result = new Object(); + result['temperatureData'] = getSensorData("DEVICE_TEMPERATURE_SUMMARY", "TEMPERATURE", user, deviceId, from, to); + return result; } -function getDigitalDisplayData(user, deviceId, from, to){ - result = new Object(); - result['ramData'] = getSensorData("RAM_USAGE_SUMMARY","motion",user, deviceId, from, to); - result['cpuData'] = getSensorData("CPU_LOAD_SUMMARY","light",user, deviceId, from, to); - result['temperatureData'] = getSensorData("DEVICE_TEMPERATURE_SUMMARY","TEMPERATURE",user, deviceId, from, to); - return result; +function getAndroidSenseData(user, deviceId, from, to) { + result = new Object(); + result['ramData'] = getSensorData("RAM_USAGE_SUMMARY", "motion", user, deviceId, from, to); + result['cpuData'] = getSensorData("CPU_LOAD_SUMMARY", "light", user, deviceId, from, to); + result['temperatureData'] = getSensorData("DEVICE_TEMPERATURE_SUMMARY", "TEMPERATURE", user, deviceId, from, to); + result['motionData'] = getSensorData("PIR_MOTION_SENSOR_SUMMARY", "motion", user, deviceId, from, to); + return result; } -function getAndroidSenseData(user, deviceId, from, to){ - result = new Object(); - result['ramData'] = getSensorData("RAM_USAGE_SUMMARY","motion",user, deviceId, from, to); - result['cpuData'] = getSensorData("CPU_LOAD_SUMMARY","light",user, deviceId, from, to); - result['temperatureData'] = getSensorData("DEVICE_TEMPERATURE_SUMMARY","TEMPERATURE",user, deviceId, from, to); - result['motionData'] = getSensorData("PIR_MOTION_SENSOR_SUMMARY","motion",user, deviceId, from, to); - return result; -} - -function getDigitalDisplayData(user, deviceId, from, to){ - result = new Object(); - result['ramData'] = getSensorData("RAM_USAGE_SUMMARY","motion",user, deviceId, from, to); - result['cpuData'] = getSensorData("CPU_LOAD_SUMMARY","light",user, deviceId, from, to); - result['temperatureData'] = getSensorData("DEVICE_TEMPERATURE_SUMMARY","TEMPERATURE",user, deviceId, from, to); - return result; +function getDigitalDisplayData(user, deviceId, from, to) { + result = new Object(); + result['ramData'] = getSensorData("RAM_USAGE_SUMMARY", "motion", user, deviceId, from, to); + result['cpuData'] = getSensorData("CPU_LOAD_SUMMARY", "light", user, deviceId, from, to); + result['cpuTemperatureData'] = getSensorData("DEVICE_CPU_TEMPERATURE_SUMMARY", "TEMPERATURE", user, deviceId, from, to); + return result; } function getSensorData(table, column, user, deviceId, from, to) { - var fetchedData = null; + var fetchedData = null; - try { - fetchedData = statsClient.getDeviceStats(table, column, user, deviceId, from, to); - }catch(error){ - log.error(error); - } + try { + fetchedData = statsClient.getDeviceStats(table, column, user, deviceId, from, to); + } catch (error) { + log.error(error); + } - var sensorData = []; + var sensorData = []; - if(fetchedData==null) return []; + if (fetchedData == null) return []; - for (var i = 0; i < fetchedData.size(); i++) { - sensorData.push({ - time: fetchedData.get(i).getTime(), - value: fetchedData.get(i).getValue() - }); - } + for (var i = 0; i < fetchedData.size(); i++) { + sensorData.push({ + time: fetchedData.get(i).getTime(), + value: fetchedData.get(i).getValue() + }); + } - return sensorData; + return sensorData; } %> \ No newline at end of file diff --git a/modules/distribution/src/repository/jaggeryapps/iotserver/units/analytics/analytics.hbs b/modules/distribution/src/repository/jaggeryapps/iotserver/units/analytics/analytics.hbs index 9575d03e..27abf139 100644 --- a/modules/distribution/src/repository/jaggeryapps/iotserver/units/analytics/analytics.hbs +++ b/modules/distribution/src/repository/jaggeryapps/iotserver/units/analytics/analytics.hbs @@ -48,6 +48,8 @@
+
+
@@ -70,4 +72,5 @@ + {{/zone}} \ No newline at end of file diff --git a/modules/distribution/src/repository/jaggeryapps/iotserver/units/analytics/public/js/graph_util.js b/modules/distribution/src/repository/jaggeryapps/iotserver/units/analytics/public/js/graph_util.js index c8dbd110..e512cfa6 100644 --- a/modules/distribution/src/repository/jaggeryapps/iotserver/units/analytics/public/js/graph_util.js +++ b/modules/distribution/src/repository/jaggeryapps/iotserver/units/analytics/public/js/graph_util.js @@ -67,12 +67,12 @@ $('#today-btn').on('click', function () { //hour picker $('#hour-btn').on('click', function () { getDateTime(currentDay.getTime() - 3600000, currentDay.getTime()); -}) +}); //week picker $('#week-btn').on('click', function () { getDateTime(currentDay.getTime() - 604800000, currentDay.getTime()); -}) +}); //month picker $('#month-btn').on('click', function () { @@ -148,69 +148,77 @@ function updateGraphs(stats) { console.log(stats); var temperatureData = stats['temperatureData']; - if (typeof temperatureData != 'undefined'){ + if (typeof temperatureData != 'undefined') { $('#div-temperatureData').html("").html("

Temperature



"); updateTemperatureGraph(convertStatsToGraphData(temperatureData)); - }else{ + } else { $('#div-temperatureData').html(""); } var lightData = stats['lightData']; - if (typeof lightData != 'undefined'){ + if (typeof lightData != 'undefined') { $('#div-lightData').html("").html("

Light



"); updateLightGraph(convertStatsToGraphData(lightData)); - }else{ + } else { $('#div-lightData').html(""); } var motionData = stats['motionData']; - if (typeof motionData != 'undefined'){ + if (typeof motionData != 'undefined') { $('#div-motionData').html("").html("

Motion



"); updateMotionGraph(convertStatsToGraphData(motionData)); - }else{ + } else { $('#div-motionData').html(""); } var sonarData = stats['sonarData']; - if (typeof sonarData != 'undefined'){ + if (typeof sonarData != 'undefined') { $('#div-sonarData').html("").html("

Sonar



"); updateSonarGraph(convertStatsToGraphData(sonarData)); - }else{ + } else { $('#div-sonarData').html(""); } var fanData = stats['fanData']; - if (typeof fanData != 'undefined'){ + if (typeof fanData != 'undefined') { $('#div-fanData').html("").html("

Fan Status



"); updateFanGraph(convertStateStatsToGraphData(fanData)); - }else{ + } else { $('#div-fanData').html(""); } var bulbData = stats['bulbData']; - if (typeof bulbData != 'undefined'){ + if (typeof bulbData != 'undefined') { $('#div-bulbData').html("").html("

Bulb Status



"); updateBulbGraph(convertStateStatsToGraphData(bulbData)); - }else{ + } else { $('#div-bulbData').html(""); } var cpuData = stats['cpuData']; - if (typeof cpuData != 'undefined'){ + if (typeof cpuData != 'undefined') { $('#div-CPUData').html("").html("

CPU Load



"); updateCPUGraph(convertStateStatsToGraphData(cpuData)); - }else{ + } else { $('#div-CPUData').html(""); } var ramData = stats['ramData']; - if (typeof ramData != 'undefined'){ + if (typeof ramData != 'undefined') { $('#div-RAMData').html("").html("

RAM Usage



"); updateRAMGraph(convertStateStatsToGraphData(ramData)); - }else{ + } else { $('#div-RAMData').html(""); } + var cpuTemperatureData = stats['cpuTemperatureData']; + if (typeof cpuTemperatureData != 'undefined') { + $('#div-cpuTemperatureData').html("").html("

CPU Temperature



"); + updateCPUTemperatureGraph(convertStatsToGraphData(cpuTemperatureData)); + } else { + $('#div-cpuTemperatureData').html(""); + } + scaleGraphs(); } @@ -219,8 +227,18 @@ function scaleGraphs() { if (sliders.length == 0) { return; } + var graphWidth = 0; + for (var i = 1; i < 10; i++){ + if ($('#canvas-wrapper' + i).length){ + graphWidth = $('#canvas-wrapper' + i).width() - 50; + break; + } + } + + if (graphWidth <= 0){ + return; + } - var graphWidth = $('#canvas-wrapper1').width() - 50; //Scale graphs var sliderX = graphWidth * 60 * 60 / (toDate - fromDate); if (sliderX < graphWidth) { @@ -283,9 +301,9 @@ function convertStateStatsToGraphData(stats) { function convertDate(date) { var month = date.getMonth() + 1; var day = date.getDate(); - var hour=date.getHours(); - var minute=date.getMinutes(); + var hour = date.getHours(); + var minute = date.getMinutes(); return date.getFullYear() + '-' + (('' + month).length < 2 ? '0' : '') - + month + '-' + (('' + day).length < 2 ? '0' : '') + day +" "+ (('' + hour).length < 2 ? '0' : '') - + hour +":"+(('' + minute).length < 2 ? '0' : '')+ minute; + + month + '-' + (('' + day).length < 2 ? '0' : '') + day + " " + (('' + hour).length < 2 ? '0' : '') + + hour + ":" + (('' + minute).length < 2 ? '0' : '') + minute; } \ No newline at end of file diff --git a/modules/distribution/src/repository/jaggeryapps/iotserver/units/analytics/public/js/graphs/cpu_temperature_graph.js b/modules/distribution/src/repository/jaggeryapps/iotserver/units/analytics/public/js/graphs/cpu_temperature_graph.js new file mode 100644 index 00000000..cc381c96 --- /dev/null +++ b/modules/distribution/src/repository/jaggeryapps/iotserver/units/analytics/public/js/graphs/cpu_temperature_graph.js @@ -0,0 +1,70 @@ +function updateCPUTemperatureGraph(temperatureData) { + console.log("cpuTemperatureData"); + renderCPUTemperatureChart(temperatureData); +} + +function renderCPUTemperatureChart(chartDataRaw) { + var chartWrapperElmId = "#canvas-wrapper9"; + var graphWidth = $(chartWrapperElmId).width() - 50; + if (chartDataRaw.length == 0) { + $(chartWrapperElmId).html("No data available..."); + return; + } + + var chartData = []; + for (var i = 0; i < chartDataRaw.length; i++) { + chartData.push({x: parseInt(chartDataRaw[i].x), y: parseInt(chartDataRaw[i].y)}); + } + + //var i = parseInt(fromDate); + //while (i < parseInt(toDate)){ + // var rnd = Math.random() * (30 - 20) + 20; + // chartData.push({x:i * 1000, y:rnd}); + // i += 60 * 5; + //} + + var chartDiv = "chart9"; + var sliderDiv = "slider9"; + var x_axis = "x_axis9"; + var y_axis = "y_axis9"; + $(chartWrapperElmId).html("").html('
'); + + var graph = new Rickshaw.Graph({ + element: document.getElementById(chartDiv), + width: graphWidth, + height: 400, + strokeWidth: 1, + renderer: 'line', + xScale: d3.time.scale(), + padding: {top: 0.2, left: 0.02, right: 0.02, bottom: 0}, + series: [ + {color: '#FF4000', data: chartData} + ] + }); + + graph.render(); + + var xAxis = new Rickshaw.Graph.Axis.X({ + graph: graph, + orientation: 'bottom', + element: document.getElementById(x_axis), + tickFormat: graph.x.tickFormat() + }); + + xAxis.render(); + + var yAxis = new Rickshaw.Graph.Axis.Y({ + graph: graph, + orientation: 'left', + element: document.getElementById(y_axis), + width: 40, + height: 410 + }); + + yAxis.render(); + + var slider = new Rickshaw.Graph.RangeSlider.Preview({ + graph: graph, + element: document.getElementById(sliderDiv) + }); +} \ No newline at end of file From 2e752c61d935490a9381f806d58fabef6fc57020 Mon Sep 17 00:00:00 2001 From: charithag Date: Tue, 21 Jul 2015 16:29:43 +0530 Subject: [PATCH 2/2] Graph time will automatically set to current time when user tried to change graph duration --- .../units/analytics/public/js/graph_util.js | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/modules/distribution/src/repository/jaggeryapps/iotserver/units/analytics/public/js/graph_util.js b/modules/distribution/src/repository/jaggeryapps/iotserver/units/analytics/public/js/graph_util.js index e512cfa6..2a2f76ba 100644 --- a/modules/distribution/src/repository/jaggeryapps/iotserver/units/analytics/public/js/graph_util.js +++ b/modules/distribution/src/repository/jaggeryapps/iotserver/units/analytics/public/js/graph_util.js @@ -1,6 +1,10 @@ var fromDate; var toDate; +var currentDay = new Date(); +var startDate = new Date(currentDay.getTime() - (60 * 60 * 24 * 100)); +var endDate = new Date(currentDay.getTime()); + // create a custom bar renderer that has no gaps Rickshaw.Graph.Renderer.BarNoGap = Rickshaw.Class.create(Rickshaw.Graph.Renderer.Bar, { name: 'bar_no_gap', @@ -11,10 +15,11 @@ Rickshaw.Graph.Renderer.BarNoGap = Rickshaw.Class.create(Rickshaw.Graph.Renderer } }); - -var currentDay = new Date(); -var startDate = new Date(currentDay.getTime() - (60 * 60 * 24 * 100)); -var endDate = new Date(currentDay.getTime()); +function initDate(){ + currentDay = new Date(); + startDate = new Date(currentDay.getTime() - (60 * 60 * 24 * 100)); + endDate = new Date(currentDay.getTime()); +} var configObject = { startOfWeek: 'monday', @@ -38,6 +43,7 @@ var configObject = { var DateRange = convertDate(startDate) + " " + configObject.separator + " " + convertDate(endDate); $(document).ready(function () { + initDate(); $('#date-range').dateRangePicker(configObject) .bind('datepicker-apply', function (event, dateRange) { $(this).addClass('active'); @@ -61,21 +67,25 @@ $(document).ready(function () { //day picker $('#today-btn').on('click', function () { + initDate(); getDateTime(currentDay.getTime() - 86400000, currentDay.getTime()); }); //hour picker $('#hour-btn').on('click', function () { + initDate(); getDateTime(currentDay.getTime() - 3600000, currentDay.getTime()); }); //week picker $('#week-btn').on('click', function () { + initDate(); getDateTime(currentDay.getTime() - 604800000, currentDay.getTime()); }); //month picker $('#month-btn').on('click', function () { + initDate(); getDateTime(currentDay.getTime() - (604800000 * 4), currentDay.getTime()); });