More examples implemented

This commit is contained in:
Dominic Bosch 2014-04-24 03:40:07 +02:00
parent a96efe92a7
commit d1fbfd58c4
12 changed files with 211 additions and 43 deletions

View file

@ -128,6 +128,7 @@ fTryToLoadModule = ( userId, oRule, modId, src, modType, dbMod, params, cb ) =>
log: logFunc
debug: console.log
exports: {}
setTimeout: setTimeout # This one allows probably too much
pushEvent: fPushEvent userId, oRule, modType

View file

@ -213,8 +213,11 @@ validConditions = ( evt, rule, userId, ruleId ) ->
return false
try
# maybe we should only allow certain ops for certain types
if cond.type is 'string'
val = selectedProperty[ 0 ]
else if cond.type is 'bool'
val = selectedProperty[ 0 ]
else if cond.type is 'value'
val = parseFloat( selectedProperty[ 0 ] ) || 0

View file

@ -2,12 +2,88 @@
# Helper constructs
#
exports.parseTextToJSON = ( text, infoEvent ) ->
# Parses text to JSON
exports.parseTextToJSON = ( eventname, text ) ->
try
pushEvent
event: infoEvent
event: eventname
payload: JSON.parse text
log "Text successfully parsed"
catch e
log "Error during JSON parsing of #{ text }"
# Parses objects to text
exports.parseObjectToPrettyText = ( eventname, obj ) ->
pushEvent
event: eventname
payload: JSON.stringify text, undefined, 2
lastSend = null
arrEvents = []
exports.accumulateEvents = ( evtname, evt, sendTime ) ->
arrEvents.push evt
sTime = fConvertTimeStringToDate sendTime
yesterday = new Date()
yesterday.setDate sTime.getDate() - 1
if lastSend < yesterday
lastSend = sTime
pushEvent
event: evtname
payload: arrEvents
arrEvents = []
# Changes the speed on how often a event is pushed into the system
isRunning = false
interval = 10 * 60 * 1000
eventname = ''
event = {}
exports.changeEventSpeed = ( evtname, evt, startTime, intervalMins ) ->
eventname = evtname
event = evt
start = fConvertTimeStringToDate startTime
mins = parseInt( intervalMins ) || 10
interval = mins * 60 * 1000
if not isRunning
isRunning = true
now = new Date()
setTimeout fPushEvent, start.getTime() - now.getTime()
fPushEvent = () ->
if eventname isnt ''
log "Pushing changed interval event"
pushEvent
event: eventname
payload: event
setTimeout fPushEvent, interval
fConvertTimeStringToDate = ( text ) ->
start = new Date()
if not text
start.setHours 12
start.setMinutes 0
else
arrInp = text.split ':'
# There's only one string entered: hour
if arrInp.length is 1
txtHr = text
start.setMinutes 0
else
txtHr = arrInp[ 0 ]
intMin = parseInt( arrInp[ 1 ] ) || 0
m = Math.max 0, Math.min intMin, 59
start.setMinutes m
intHour = parseInt( txtHr ) || 12
h = Math.max 0, Math.min intHour, 24
start.setHours h
start.setSeconds 0
start.setMilliseconds 0
if start < new Date()
start.setDate start.getDate() + 1
start

View file

@ -16,7 +16,6 @@ exports.newMail = () ->
# Syntax: needle.request method, url, data, [options], callback
#
needle.request 'get', url, null, null, ( err, resp, body ) ->
log 'Poll function executed'
if err
log 'Error in EmailYak EM newMail: ' + err.message
else

View file

@ -89,3 +89,30 @@ getContent = ( args ) ->
service: args.contentServiceId
callback: args.callback
# Returns an event of the form:
#
# {
# "text": "test subject",
# "id": 127815,
# "createDate": "2014-04-19 16:27:45",
# "lastModified": "2014-04-19 16:27:45",
# "time": "5 days ago",
# "userId": 10595,
# "username": "Dominic Bosch",
# "uri": "https://probinder.com/content/view/id/127815/",
# "localUri": "https://probinder.com/content/view/id/127815/",
# "title": "",
# "serviceId": 27,
# "userIds": [
# 10595
# ],
# "description": "",
# "context": [
# {
# "id": 18749,
# "name": "WebAPI ECA Test Binder",
# "remove": true,
# "uri": "/content/context/id/18749/webapi-eca-test-binder"
# }
# ]
# }

View file

@ -0,0 +1,31 @@
pingHost = ( url, cb ) ->
isDown = true
semaphore = 5
for i in [1..5]
needle.get url, ( err, resp ) ->
if not err
isDown = false
if --semaphore is 0
cb
timestamp: (new Date()).toISOString()
url: url
isdown: isDown
wasDown = false
# Checks whether a remote host is down
exports.pingedRemoteHost = ( url ) ->
pingHost url, ( evt ) ->
if evt.isdown and not wasDown
wasDown = true
pushEvent evt
if not evt.isdown
wasDown = false
# Checks a host uptime and maintains a history which is pushed as an event
oStats = {}
exports.hostUptime = ( url ) ->
pingHost url, ( evt ) ->
oStats[ evt.timestamp ] = evt
pushEvent oStats

View file

@ -3,9 +3,7 @@ OpenWeather EVENT POLLER
------------------------
This module requires user-specific parameters:
- openweatherKey
- tempThreshold
- city
- appid
###
urlService = 'http://api.openweathermap.org/data/2.5/weather'
@ -13,15 +11,15 @@ urlService = 'http://api.openweathermap.org/data/2.5/weather'
###
Fetches the temperature
###
getWeatherData = ( cb ) ->
url = urlService + '?APPID=' + params.openweatherKey + '&q=' + params.city
getWeatherData = ( city, cb ) ->
url = urlService + '?APPID=' + params.appid + '&q=' + params.city
needle.request 'get', url, null, null, cb
###
Pushes the current weather data into the system
###
exports.currentData = () ->
getWeatherData ( err, resp, body ) ->
exports.currentData = ( city ) ->
getWeatherData city, ( err, resp, body ) ->
if err or resp.statusCode isnt 200
log JSON.stringify body
else
@ -30,14 +28,14 @@ exports.currentData = () ->
###
Emits one event per day if the temperature today raises above user defined threshold
###
exports.temperatureOverThreshold = () ->
getWeatherData ( err, resp, body ) ->
exports.temperatureOverThreshold = ( city, tempThreshold ) ->
getWeatherData city, ( 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
if body.main.temp_max - 272.15 > tempThreshold
pushEvent
threshold: params.tempThreshold
threshold: tempThreshold
measured: body.main.temp_max - 272.15

View file

@ -26,7 +26,7 @@ remoteUrl = "http://ec2-54-226-188-9.compute-1.amazonaws.com:8126"
# report.forEach ( item ) ->
# console.log item[ 0 ]
session = ping.createSession()
session = ping.createSession retries: 5
everyMins = 10
oHosts = {}
oPings = {}

View file

@ -142,7 +142,9 @@ fOnLoad = () ->
$( '#event_poller_params' ).html '<b>Required Global Parameters:</b>'
$( '#event_poller_params' ).append table
fFillEventParams id
fDelayed = () ->
fFillEventParams id
setTimeout fDelayed, 200
fFillEventParams = ( moduleId ) ->
obj =
@ -219,7 +221,9 @@ fOnLoad = () ->
$( "#select_actions option" ).each () ->
if $( this ).text() is name
$( this ).remove()
fFillActionFunction arrName[ 0 ]
fDelayed = () ->
fFillActionFunction arrName[ 0 ]
setTimeout fDelayed, 300
fFetchActionParams = ( div, modName ) ->
obj =

View file

@ -18,7 +18,6 @@ exports.newMail = () ->
# Syntax: needle.request method, url, data, [options], callback
#
needle.request 'get', url, null, null, ( err, resp, body ) ->
log 'Poll function executed'
if err
log 'Error in EmailYak EM newMail: ' + err.message
else

View file

@ -528,6 +528,41 @@
"131.152.85.228"
],
"sum": 35
},
"2014-04-23T14:02:50.747Z": {
"ips": [
"131.152.85.64",
"131.152.85.67",
"131.152.85.68",
"131.152.85.69",
"131.152.85.72",
"131.152.85.66",
"131.152.85.73",
"131.152.85.80",
"131.152.85.82",
"131.152.85.81",
"131.152.85.84",
"131.152.85.83",
"131.152.85.93",
"131.152.85.85",
"131.152.85.94",
"131.152.85.96",
"131.152.85.106",
"131.152.85.97",
"131.152.85.74",
"131.152.85.95",
"131.152.85.103",
"131.152.85.120",
"131.152.85.115",
"131.152.85.70",
"131.152.85.254",
"131.152.85.79",
"131.152.85.227",
"131.152.85.225",
"131.152.85.226",
"131.152.85.228"
],
"sum": 30
}
},
"hosts": {

View file

@ -11,31 +11,26 @@
<script>
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('datetime', 'Time');
data.addColumn('number', '#Online');
$.getJSON('data/histochart.json', function(d) {
var data = new google.visualization.DataTable();
data.addColumn('datetime', 'Time');
data.addColumn('number', '#Online');
var options_lines = {
title: 'Online statistics',
curveType:'function',
lineWidth: 2,
intervals: { 'style':'line' },
legend: 'none',
};
var chart_lines = new google.visualization.LineChart(document.getElementById('chart_lines'));
var fRepaint = function () {
$.getJSON('data/histochart.json', function(d) {
var dat = d.pingtimes;
for(var prop in dat) {
data.addRow([new Date(prop), dat[prop].sum ]);
}
chart_lines.draw(data, options_lines);
});
setTimeout(fRepaint, 5*60*1000);
};
fRepaint();
var options_lines = {
title: 'Online statistics',
curveType:'function',
lineWidth: 2,
intervals: { 'style':'line' },
legend: 'none',
};
var dat = d.pingtimes;
for(var prop in dat) {
data.addRow([new Date(prop), dat[prop].sum ]);
}
var chart_lines = new google.visualization.LineChart(document.getElementById('chart_lines'));
chart_lines.draw(data, options_lines);
});
setTimeout(drawChart, 3 * 60 * 1000);
}
</script>
<div id="chart_lines" style="width: 900px; height: 500px;"></div>