From dfed7671bfe76c362bb307da23f39ffd770c435c Mon Sep 17 00:00:00 2001 From: Rasika Perera Date: Fri, 7 Aug 2015 15:49:22 +0530 Subject: [PATCH] refactoring events api --- .../jaggeryapps/iotserver/api/event-api.jag | 55 ++------------ .../jaggeryapps/iotserver/modules/event.js | 74 +++++++++++++++++++ .../iotserver/units/events/events.hbs | 2 +- .../events/public/templates/event-stream.hbs | 7 -- 4 files changed, 81 insertions(+), 57 deletions(-) create mode 100644 modules/distribution/src/repository/jaggeryapps/iotserver/modules/event.js diff --git a/modules/distribution/src/repository/jaggeryapps/iotserver/api/event-api.jag b/modules/distribution/src/repository/jaggeryapps/iotserver/api/event-api.jag index bf7853b7..409ff440 100644 --- a/modules/distribution/src/repository/jaggeryapps/iotserver/api/event-api.jag +++ b/modules/distribution/src/repository/jaggeryapps/iotserver/api/event-api.jag @@ -25,60 +25,17 @@ var log = new Log("api/event-api.jag"); var constants = require("/modules/constants.js"); var dcProps = require('/config/dc-props.js').config(); -var user = session.get(constants.USER_SESSION_KEY); -if (!user) { - response.sendRedirect(dcProps.appContext + "login?#login-required"); - exit();//stop execution -} +var eventModule = require("/modules/event.js").eventModule; var result; - if (uriMatcher.match("/{context}/api/event/list")) { - - /* - var timeInterval = 30; - var i, rnd; - result = []; - var currentDay = new Date(); - var startDate = currentDay.getTime() - (60 * 60 * 24 * 100); - var endDate = currentDay.getTime(); - - var i = parseInt(startDate / 1000); - while (i < parseInt(endDate / 1000)) { - rnd = rnd = Math.random() * 50; - result.push({time: i * 1000, deviceName: 'device' + rnd, activity:'Event number ' + rnd}); - i += timeInterval; - } - */ - - try { - result = statsClient.getRecentDeviceStats(user, 10); - } catch (error) { - log.error(error); - } - -<<<<<<< HEAD - var eventsData = []; - - if (fetchedData == null) return []; - - for (var i = 0; i < fetchedData.size(); i++) { - //eventsData.push({ - // time: fetchedData.get(i).getTime(), - // deviceName: fetchedData.get(i).getDeviceName(), - // activity: fetchedData.get(i).getDeviceActivity() - //}); - - eventsData.push({ - time: 234234, - deviceName: "my device", - activity: "test" - }); + var user = session.get(constants.USER_SESSION_KEY); + if (!user) { + response.sendRedirect(dcProps.appContext + "login?#login-required"); + exit();//stop execution } - return eventsData; -======= ->>>>>>> 91ab732dba19d87bc74860cfc7eca8953beb2120 + result = eventModule.getEventsData(user.username, 10); } // returning the result. diff --git a/modules/distribution/src/repository/jaggeryapps/iotserver/modules/event.js b/modules/distribution/src/repository/jaggeryapps/iotserver/modules/event.js new file mode 100644 index 00000000..75594e97 --- /dev/null +++ b/modules/distribution/src/repository/jaggeryapps/iotserver/modules/event.js @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +var eventModule; +eventModule = function () { + var log = new Log("modules/event.js"); + + var constants = require("/modules/constants.js"); + var utility = require("/modules/utility.js").utility; + + var publicMethods = {}; + var privateMethods = {}; + + var statsClient = new Packages.org.wso2.carbon.device.mgt.iot.common.analytics.statistics.IoTEventsStatisticsClient; + + publicMethods.getEventsData = function (username, recordLimit) { + var fetchedData = null; + + try { + fetchedData = statsClient.getRecentDeviceStats(username, recordLimit); + } catch (error) { + log.error(error); + } + + var eventsData = []; + + // -- start of dummy data + + var timeInterval = 30; + var i, rnd; + var currentDay = new Date(); + var startDate = currentDay.getTime() - (60 * 60 * 24 * 5); + var endDate = currentDay.getTime(); + + var i = parseInt(startDate / 1000); + while (i < parseInt(endDate / 1000)) { + rnd = rnd = Math.random() * 50; + eventsData.push({time: i*1000, deviceName: 'device' + rnd, activity:'Event number ' + rnd}); + i += timeInterval; + } + + // -- end of dummy data + + // for (var i = 0; i < fetchedData.size(); i++) { + // eventsData.push({ + // time: fetchedData.get(i).getTime(), + // deviceName: fetchedData.get(i).getDeviceName(), + // activity: fetchedData.get(i).getDeviceActivity() + // }); + // }; + + return eventsData; + }; + + + return publicMethods; +}(); + + diff --git a/modules/distribution/src/repository/jaggeryapps/iotserver/units/events/events.hbs b/modules/distribution/src/repository/jaggeryapps/iotserver/units/events/events.hbs index c87c6592..f0492b0f 100644 --- a/modules/distribution/src/repository/jaggeryapps/iotserver/units/events/events.hbs +++ b/modules/distribution/src/repository/jaggeryapps/iotserver/units/events/events.hbs @@ -3,7 +3,7 @@
-

Events

+

Device Events Stream


Events Analyzer

diff --git a/modules/distribution/src/repository/jaggeryapps/iotserver/units/events/public/templates/event-stream.hbs b/modules/distribution/src/repository/jaggeryapps/iotserver/units/events/public/templates/event-stream.hbs index accd43dc..a80c4042 100644 --- a/modules/distribution/src/repository/jaggeryapps/iotserver/units/events/public/templates/event-stream.hbs +++ b/modules/distribution/src/repository/jaggeryapps/iotserver/units/events/public/templates/event-stream.hbs @@ -1,10 +1,3 @@ -
- -
-
-