From bc999b6012677fc86c8c6e62b741bcb216bdb5b0 Mon Sep 17 00:00:00 2001 From: Dominic Bosch Date: Mon, 21 Apr 2014 01:34:49 +0200 Subject: [PATCH] ISO time declarations added. Import.io example added --- coffee/event-poller.coffee | 6 +- coffee/http-listener.coffee | 2 +- coffee/persistence.coffee | 2 +- coffee/request-handler.coffee | 2 +- examples/event-pollers/continuously.coffee | 4 +- examples/event-pollers/emailyak.coffee | 4 +- examples/event-pollers/importio.coffee | 93 +++++++++++++------ examples/event-pollers/probinder.coffee | 8 +- examples/event-pollers/system.coffee | 4 +- examples/event-pollers/weather.coffee | 8 +- examples/runscript.coffee | 11 +-- examples/runscript.js | 14 ++- js/event-poller.js | 6 +- js/http-listener.js | 2 +- js/persistence.js | 2 +- js/request-handler.js | 2 +- webpages/handlers/coffee/edit_rules.coffee | 2 +- webpages/handlers/js/edit_rules.js | 2 +- .../handlers/remote-scripts/forge_module.html | 7 +- 19 files changed, 106 insertions(+), 75 deletions(-) diff --git a/coffee/event-poller.coffee b/coffee/event-poller.coffee index a7aefc9..217dc9b 100644 --- a/coffee/event-poller.coffee +++ b/coffee/event-poller.coffee @@ -108,7 +108,7 @@ fLoadModule = ( msg ) -> log.info "EP | New event module '#{ arrName[0] }' loaded for user #{ msg.user }, - in rule #{ msg.rule.id }, starting at #{ ts.toISOString() } + in rule #{ msg.rule.id }, starting at UTC|#{ ts.toISOString() } and polling every #{ msg.rule.event_interval } minutes" setTimeout fCheckAndRun( msg.user, msg.rule.id, ts ), 1000 @@ -121,7 +121,7 @@ fPushEvent = ( userId, ruleId, oRule ) -> ( obj ) -> db.pushEvent event: oRule.id - eventid: "polled #{ oRule.id } #{ userId }_#{ ( new Date() ).toISOString() }" + eventid: "polled #{ oRule.id } #{ userId }_UTC|#{ ( new Date() ).toISOString() }" payload: obj fCheckAndRun = ( userId, ruleId, timestamp ) -> @@ -137,7 +137,7 @@ fCheckAndRun = ( userId, ruleId, timestamp ) -> setTimeout fCheckAndRun( userId, ruleId, timestamp ), oRule.event_interval else log.info "EP | We found a newer polling interval and discontinue this one which - was created at #{ timestamp.toISOString() }" + was created at UTC|#{ timestamp.toISOString() }" # We have to register the poll function in belows anonymous function # because we're fast iterating through the listUserModules and references will diff --git a/coffee/http-listener.coffee b/coffee/http-listener.coffee index 45bff7e..43fb6fa 100644 --- a/coffee/http-listener.coffee +++ b/coffee/http-listener.coffee @@ -46,7 +46,7 @@ indexEvent = ( event, body, resp ) -> timestamp = ( new Date() ).toISOString() rand = ( Math.floor Math.random() * 10e9 ).toString( 16 ).toUpperCase() obj.event = event - obj.eventid = "#{ obj.event }_#{ timestamp }_#{ rand }" + obj.eventid = "#{ obj.event }_UTC|#{ timestamp }_#{ rand }" db.pushEvent obj resp.send 200, "Thank you for the event: #{ obj.eventid }" catch err diff --git a/coffee/persistence.coffee b/coffee/persistence.coffee index defd3c3..9ad5015 100644 --- a/coffee/persistence.coffee +++ b/coffee/persistence.coffee @@ -386,7 +386,7 @@ Appends a log entry. ### exports.appendLog = ( userId, ruleId, moduleId, message ) => @db.append "#{ userId }:#{ ruleId }:log", - "[#{ ( new Date() ).toISOString() }] {#{ moduleId }} #{ message }\n" + "[UTC|#{ ( new Date() ).toISOString() }] {#{ moduleId }} #{ message }\n" ### Retrieves a log entry. diff --git a/coffee/request-handler.coffee b/coffee/request-handler.coffee index 545a04d..3f6a39c 100644 --- a/coffee/request-handler.coffee +++ b/coffee/request-handler.coffee @@ -79,7 +79,7 @@ exports.handleEvent = ( req, resp ) -> if obj and obj.event and not err timestamp = ( new Date() ).toISOString() rand = ( Math.floor Math.random() * 10e9 ).toString( 16 ).toUpperCase() - obj.eventid = "#{ obj.event }_#{ timestamp }_#{ rand }" + obj.eventid = "#{ obj.event }_UTC|#{ timestamp }_#{ rand }" answ = code: 200 message: "Thank you for the event: #{ obj.eventid }" diff --git a/examples/event-pollers/continuously.coffee b/examples/event-pollers/continuously.coffee index 51af39a..a264598 100644 --- a/examples/event-pollers/continuously.coffee +++ b/examples/event-pollers/continuously.coffee @@ -1,6 +1,6 @@ # # Pushes an event into the system each time the function is polled # -exports.push = ( pushEvent ) -> - pushEvent +exports.push = () -> + exports.pushEvent content: "This is an event that will be sent again and again every ten seconds" \ No newline at end of file diff --git a/examples/event-pollers/emailyak.coffee b/examples/event-pollers/emailyak.coffee index 5575bf1..521bd4e 100644 --- a/examples/event-pollers/emailyak.coffee +++ b/examples/event-pollers/emailyak.coffee @@ -8,7 +8,7 @@ url = 'https://api.emailyak.com/v1/' + params.apikey + '/json/get/new/email/' -exports.newMail = ( pushEvent ) -> +exports.newMail = () -> # needlereq allows the user to make calls to API's # Refer to https://github.com/tomas/needle for more information @@ -23,7 +23,7 @@ exports.newMail = ( pushEvent ) -> if resp.statusCode is 200 if body.Emails.length > 0 log "#{ body.Emails.length } mail events pushed into the system" - pushEvent mail for mail in body.Emails + exports.pushEvent mail for mail in body.Emails ### This will emit events of the form: diff --git a/examples/event-pollers/importio.coffee b/examples/event-pollers/importio.coffee index e624333..fe440ff 100644 --- a/examples/event-pollers/importio.coffee +++ b/examples/event-pollers/importio.coffee @@ -4,46 +4,81 @@ required module params: - apikey - userGuid -- queryGuid ### params.apikey = "Cc8AX35d4B89ozzmn5bpm7k70HRon5rrfUxZvOwkVRj31/oBGHzVfQSRp5mEvlOgxyh7xi+tFSL66iAFo1W/sQ==" params.userGuid = "d19f0d08-bf73-4115-90a8-ac045ad4f225" -params.queryGuid = "2a1d789a-4d24-4942-bdca-ffa0e9f99c85" -params.queryGuid = "2a1d789a-4d24-4942-bdca-ffa0e9f99c85" -# params.queryGuid = "4f833315-7aa0-4fcd-b8d0-c65f6a6bafcf" io = new importio params.userGuid, params.apikey, "query.import.io" -exports.queryData = ( pushEvent ) -> - debug params.apikey - debug params.queryGuid - debug params.userGuid - +tryToConnect = ( numAttempt, cb ) -> io.connect ( connected ) -> - if not connected - log "ERROR: Unable to connect" + if connected + cb true + else + log "Unable to connect, attempting again... ##{ numAttempt++ }" + if numAttempt is 5 + cb false + else + tryToConnect numAttempt, cb + +arrPages = [ + "http://www.meteoblue.com/en/switzerland/weather-basel" + "http://www.meteoblue.com/en/switzerland/weather-z%C3%BCrich" + "http://www.meteoblue.com/en/switzerland/weather-luzern" + "http://www.meteoblue.com/en/switzerland/weather-liestal" + "http://www.meteoblue.com/en/switzerland/weather-bern" + "http://www.meteoblue.com/en/switzerland/weather-lugano" + "http://www.meteoblue.com/en/switzerland/weather-sankt-gallen" +] + +getCityUrl = ( idCity ) -> + id = parseInt( idCity ) || 0 + if id < 0 or id >= arrPages.length + id = 0 + arrPages[ id ] + +queryService = ( inputParams ) -> + tryToConnect 0, ( connected ) -> + if not connected + log 'ERROR: Cannot execute query because connection failed!' else - log "Connected!" data = [] - inp = { "webpage/url": "http://www.meteoblue.com/en/switzerland/weather-sankt-gallen" } - io.query "input": inp, "connectorGuids": [ params.queryGuid ], ( finished, msg ) -> - log 'query returned' - log msg + io.query inputParams, ( finished, msg ) -> if msg.type is "MESSAGE" - log "Adding #{ msg.data.results.length } results" data = data.concat msg.data.results if finished - log "Done" log JSON.stringify data - log 'all work done' - log io - io = null - io.query({ - "connectorGuids": [ - "2a1d789a-4d24-4942-bdca-ffa0e9f99c85" - ], - "input": { - "webpage/url": "http://www.meteoblue.com/en/switzerland/weather-sankt-gallen" - } - }, getCallbackFunction()); \ No newline at end of file + exports.pushEvent data + +exports.meteoblueWeekData = ( idCity ) -> + params = + input: "webpage/url": getCityUrl idCity + connectorGuids: [ "2a1d789a-4d24-4942-bdca-ffa0e9f99c85" ] + queryService params + # [ + # { + # wind: '9 mph', + # day_identifier: 'Today', + # day_name: 'Mon', + # temp_max: '61 °F', + # temp_min: '50 °F', + # sunlight: '0 h', + # rain: '0-2mm' + # }, + # [...] + # ] + +exports.meteoblueCurrentData = ( idCity ) -> + params = + input: "webpage/url": getCityUrl idCity + connectorGuids: [ "06394265-b4e1-4b48-be82-a9f2acb9040f" ] + queryService params + # [ + # { + # current_time_wind_desc: '01:00 | Overcast', + # current_temp: '53°F', + # coordinates: '47.56°N 7.59°E 260m asl', + # city: 'Basel-Stadt' + # } + # ] diff --git a/examples/event-pollers/probinder.coffee b/examples/event-pollers/probinder.coffee index 487e01c..0fff979 100644 --- a/examples/event-pollers/probinder.coffee +++ b/examples/event-pollers/probinder.coffee @@ -47,27 +47,27 @@ callService = ( args ) -> ### Calls the user's unread content service. ### -exports.unreadContentInfo = ( pushEvent ) -> +exports.unreadContentInfo = () -> callService service: '36' method: 'unreadcontent' callback: ( err, resp, body ) -> if not err and resp.statusCode is 200 - pushEvent oEntry for oEntry in body + exports.pushEvent oEntry for oEntry in body else log 'Error: ' + body.error.message ### Fetches unread contents ### -exports.unreadContent = ( pushEvent ) -> +exports.unreadContent = () -> exports.unreadContentInfo ( evt ) -> getContent contentId: evt.id contentServiceId: evt.serviceId callback: ( err, resp, body ) -> if not err and resp.statusCode is 200 - pushEvent + exports.pushEvent id: body.id content: body.text object: body diff --git a/examples/event-pollers/system.coffee b/examples/event-pollers/system.coffee index b8d021e..7f0e404 100644 --- a/examples/event-pollers/system.coffee +++ b/examples/event-pollers/system.coffee @@ -8,9 +8,9 @@ isRunning = false ### This event is emitted if the system had a restart. ### -exports.hasRestarted = ( pushEvent ) -> +exports.hasRestarted = () -> if not isRunning isRunning = true - pushEvent + exports.pushEvent content: "The system has been restarted at #{ ( new Date ).toISOString() }" diff --git a/examples/event-pollers/weather.coffee b/examples/event-pollers/weather.coffee index 379b314..c7baec2 100644 --- a/examples/event-pollers/weather.coffee +++ b/examples/event-pollers/weather.coffee @@ -20,24 +20,24 @@ getWeatherData = ( cb ) -> ### Pushes the current weather data into the system ### -exports.currentData = ( pushEvent ) -> +exports.currentData = () -> getWeatherData ( err, resp, body ) -> if err or resp.statusCode isnt 200 log JSON.stringify body else - pushEvent body + exports.pushEvent body ### Emits one event per day if the temperature today raises above user defined threshold ### -exports.temperatureOverThreshold = ( pushEvent ) -> +exports.temperatureOverThreshold = () -> getWeatherData ( err, resp, body ) -> if err or resp.statusCode isnt 200 log JSON.stringify body else #If temperature is above threshold if body.main.temp_max - 272.15 > params.tempThreshold - pushEvent + exports.pushEvent threshold: params.tempThreshold measured: body.main.temp_max - 272.15 diff --git a/examples/runscript.coffee b/examples/runscript.coffee index a8478df..c245cf2 100644 --- a/examples/runscript.coffee +++ b/examples/runscript.coffee @@ -26,7 +26,7 @@ src = cs.compile code sandbox = id: 'test.vm' - params: params.userparams + params: params needle: needle request: request cryptoJS: crypto @@ -35,13 +35,12 @@ sandbox = debug: console.log exports: {} +sandbox.exports.pushEvent = ( obj ) -> + console.log obj + vm.runInNewContext src, sandbox, sandbox.id -if process.argv[ 3 ] is 'ep' - sandbox.exports[ process.argv[ 4 ] ] ( evt ) -> - console.log evt -else - sandbox.exports[ process.argv[ 3 ] ] params.event +sandbox.exports[ process.argv[ 3 ] ].apply null, [ "param1", "param2", "param3", "param4" ] console.log "If no error happened until here it seems the script compiled and ran correctly! Congrats!" \ No newline at end of file diff --git a/examples/runscript.js b/examples/runscript.js index 8271233..1332780 100644 --- a/examples/runscript.js +++ b/examples/runscript.js @@ -38,7 +38,7 @@ compilation and running of module code sandbox = { id: 'test.vm', - params: params.userparams, + params: params, needle: needle, request: request, cryptoJS: crypto, @@ -48,15 +48,13 @@ compilation and running of module code exports: {} }; + sandbox.exports.pushEvent = function(obj) { + return console.log(obj); + }; + vm.runInNewContext(src, sandbox, sandbox.id); - if (process.argv[3] === 'ep') { - sandbox.exports[process.argv[4]](function(evt) { - return console.log(evt); - }); - } else { - sandbox.exports[process.argv[3]](params.event); - } + sandbox.exports[process.argv[3]].apply(null, ["param1", "param2", "param3", "param4"]); console.log("If no error happened until here it seems the script compiled and ran correctly! Congrats!"); diff --git a/js/event-poller.js b/js/event-poller.js index 653feed..c10a268 100644 --- a/js/event-poller.js +++ b/js/event-poller.js @@ -103,7 +103,7 @@ Dynamic Modules timestamp: ts }; oUser[msg.rule.id].module.pushEvent = fPushEvent(msg.user, msg.rule.id, oUser[msg.rule.id]); - log.info("EP | New event module '" + arrName[0] + "' loaded for user " + msg.user + ", in rule " + msg.rule.id + ", starting at " + (ts.toISOString()) + " and polling every " + msg.rule.event_interval + " minutes"); + log.info("EP | New event module '" + arrName[0] + "' loaded for user " + msg.user + ", in rule " + msg.rule.id + ", starting at UTC|" + (ts.toISOString()) + " and polling every " + msg.rule.event_interval + " minutes"); return setTimeout(fCheckAndRun(msg.user, msg.rule.id, ts), 1000); }); } @@ -118,7 +118,7 @@ Dynamic Modules return function(obj) { return db.pushEvent({ event: oRule.id, - eventid: "polled " + oRule.id + " " + userId + "_" + ((new Date()).toISOString()), + eventid: "polled " + oRule.id + " " + userId + "_UTC|" + ((new Date()).toISOString()), payload: obj }); }; @@ -134,7 +134,7 @@ Dynamic Modules fCallFunction(userId, ruleId, oRule); return setTimeout(fCheckAndRun(userId, ruleId, timestamp), oRule.event_interval); } else { - return log.info("EP | We found a newer polling interval and discontinue this one which was created at " + (timestamp.toISOString())); + return log.info("EP | We found a newer polling interval and discontinue this one which was created at UTC|" + (timestamp.toISOString())); } } }; diff --git a/js/http-listener.js b/js/http-listener.js index 3e5400d..85dbd1c 100644 --- a/js/http-listener.js +++ b/js/http-listener.js @@ -49,7 +49,7 @@ HTTP Listener timestamp = (new Date()).toISOString(); rand = (Math.floor(Math.random() * 10e9)).toString(16).toUpperCase(); obj.event = event; - obj.eventid = "" + obj.event + "_" + timestamp + "_" + rand; + obj.eventid = "" + obj.event + "_UTC|" + timestamp + "_" + rand; db.pushEvent(obj); return resp.send(200, "Thank you for the event: " + obj.eventid); } catch (_error) { diff --git a/js/persistence.js b/js/persistence.js index 718cfa2..c0e2dbb 100644 --- a/js/persistence.js +++ b/js/persistence.js @@ -507,7 +507,7 @@ Persistence exports.appendLog = (function(_this) { return function(userId, ruleId, moduleId, message) { - return _this.db.append("" + userId + ":" + ruleId + ":log", "[" + ((new Date()).toISOString()) + "] {" + moduleId + "} " + message + "\n"); + return _this.db.append("" + userId + ":" + ruleId + ":log", "[UTC|" + ((new Date()).toISOString()) + "] {" + moduleId + "} " + message + "\n"); }; })(this); diff --git a/js/request-handler.js b/js/request-handler.js index 5762ffc..d46dd29 100644 --- a/js/request-handler.js +++ b/js/request-handler.js @@ -87,7 +87,7 @@ Request Handler if (obj && obj.event && !err) { timestamp = (new Date()).toISOString(); rand = (Math.floor(Math.random() * 10e9)).toString(16).toUpperCase(); - obj.eventid = "" + obj.event + "_" + timestamp + "_" + rand; + obj.eventid = "" + obj.event + "_UTC|" + timestamp + "_" + rand; answ = { code: 200, message: "Thank you for the event: " + obj.eventid diff --git a/webpages/handlers/coffee/edit_rules.coffee b/webpages/handlers/coffee/edit_rules.coffee index 5a3101c..100194e 100644 --- a/webpages/handlers/coffee/edit_rules.coffee +++ b/webpages/handlers/coffee/edit_rules.coffee @@ -70,7 +70,7 @@ fOnLoad = () -> .done ( data ) -> ts = ( new Date() ).toISOString() log = data.message.replace new RegExp("\n", 'g'), "
" - $( '#log_col' ).html "

#{ ruleName } Log ( #{ ts } ):

#{ log }" + $( '#log_col' ).html "

#{ ruleName } Log:

( updated UTC|#{ ts } )

#{ log }" .fail fErrHandler 'Could not get rule log! ' window.addEventListener 'load', fOnLoad, true diff --git a/webpages/handlers/js/edit_rules.js b/webpages/handlers/js/edit_rules.js index 367b872..c2de6cc 100644 --- a/webpages/handlers/js/edit_rules.js +++ b/webpages/handlers/js/edit_rules.js @@ -88,7 +88,7 @@ var log, ts; ts = (new Date()).toISOString(); log = data.message.replace(new RegExp("\n", 'g'), "
"); - return $('#log_col').html("

" + ruleName + " Log ( " + ts + " ):

" + log); + return $('#log_col').html("

" + ruleName + " Log:

( updated UTC|" + ts + " )

" + log); }).fail(fErrHandler('Could not get rule log! ')); }); }; diff --git a/webpages/handlers/remote-scripts/forge_module.html b/webpages/handlers/remote-scripts/forge_module.html index 1a3fa99..8b336da 100644 --- a/webpages/handlers/remote-scripts/forge_module.html +++ b/webpages/handlers/remote-scripts/forge_module.html @@ -10,7 +10,7 @@ url = 'https://api.emailyak.com/v1/' + params.apikey + '/json/get/new/email/' -exports.newMail = ( pushEvent ) -> +exports.newMail = () -> # needlereq allows the user to make calls to API's # Refer to https://github.com/tomas/needle for more information @@ -24,9 +24,8 @@ exports.newMail = ( pushEvent ) -> else if resp.statusCode is 200 if body.Emails.length > 0 - log JSON.stringify body.Emails[0] - log 'Poll function executed' - pushEvent mail for mail in body.Emails + log "#{ body.Emails.length } mail events pushed into the system" + exports.pushEvent mail for mail in body.Emails ### This will emit events of the form: