Skip to content

Commit

Permalink
Unified config. Tweaks. Better defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
stagas committed Mar 14, 2012
1 parent 123eda4 commit dfa68b3
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 39 deletions.
10 changes: 5 additions & 5 deletions bin/maptail
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ while (arg = args.shift()) {
else if (arg == '-p' || arg == '--port') config.port = parseInt(args.shift(), 10) else if (arg == '-p' || arg == '--port') config.port = parseInt(args.shift(), 10)
else if (arg == '--help') console.log(help), process.exit() else if (arg == '--help') console.log(help), process.exit()
else if (arg == '--quiet') config.quiet = true else if (arg == '--quiet') config.quiet = true
else if (arg == '--max-dots' || arg == '--maxdots') maptail.clientConfig.maxDots = parseInt(args.shift(), 10) else if (arg == '--max-dots' || arg == '--maxdots') maptail.config.maxDots = parseInt(args.shift(), 10)
else if (arg == '--decay' || arg == '--ttl') maptail.clientConfig.ttl = parseInt(args.shift(), 10) else if (arg == '--decay' || arg == '--ttl') maptail.config.ttl = parseInt(args.shift(), 10)
else if (arg == '--max-age' || arg == '--maxage') maptail.clientConfig.visitorMaxAge = parseInt(args.shift(), 10) else if (arg == '--max-age' || arg == '--maxage') maptail.config.maxAge = parseInt(args.shift(), 10)
else if (arg == '--fps') maptail.clientConfig.ageInterval = 1000 / parseInt(args.shift(), 10) else if (arg == '--fps') maptail.config.fps = 1000 / parseInt(args.shift(), 10)
else if (arg == '--buffer') maptail.bufferTime = parseInt(args.shift(), 10) else if (arg == '--buffer') maptail.config.bufferTime = parseInt(args.shift(), 10)
else spawnArgs.push(arg) else spawnArgs.push(arg)
} }


Expand Down
8 changes: 6 additions & 2 deletions examples/extreme-traffic.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -16,5 +16,9 @@ function rand () {
} }


setInterval(function () { setInterval(function () {
if (Math.random() * 10 < 10) maptail.emit('ip', [0,0,0,0].map(rand).join('.')) for (var i = 10; i--;) maptail.emit('ip', [0,0,0,0].map(rand).join('.'))
}, 1) }, 100)

maptail.config.bufferMax = 200
maptail.config.bufferTime = 500
maptail.config.maxDots = 200
39 changes: 28 additions & 11 deletions lib/maptail.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -3,13 +3,31 @@ var geoip = require('geoip-lite-with-city-data')


var maptail = exports = module.exports = new EventEmitter var maptail = exports = module.exports = new EventEmitter


maptail.config = {
// maximum history items
historyMax: 100

// maximum markers to buffer
, bufferMax: 100

// buffer time in milliseconds
, bufferTime: 1000

// markers' initial time to live in seconds
, ttl: 100 // seconds

// maximum dots to be displayed (this adjusts ttl automatically)
, maxDots: 100

// report visitors up to that age
, maxAge: 240 // seconds

// aging repaint in fps
, fps: 60
}

maptail.history = [] maptail.history = []
maptail.historyMax = 100
maptail.buffer = [] maptail.buffer = []
maptail.bufferMax = 100
maptail.bufferTime = 250

maptail.clientConfig = {}


maptail.on('ip', function (ip, message) { maptail.on('ip', function (ip, message) {
var geo = ip && maptail.lookup(ip) || {} var geo = ip && maptail.lookup(ip) || {}
Expand Down Expand Up @@ -73,9 +91,8 @@ maptail.attach = function (app) {
.on('subscribe', function (what) { .on('subscribe', function (what) {
if (what === 'geoip') { if (what === 'geoip') {
users.add(socket) users.add(socket)
maptail.clientConfig.dateNow = Date.now() maptail.config.dateNow = Date.now()
maptail.clientConfig.bufferTime = maptail.bufferTime socket.remote.emit('config', maptail.config)
socket.remote.emit('config', maptail.clientConfig)
socket.remote.emit('geoip', maptail.history) socket.remote.emit('geoip', maptail.history)
} }
}) })
Expand All @@ -87,16 +104,16 @@ maptail.attach = function (app) {
maptail.on('geoip', function (geo) { maptail.on('geoip', function (geo) {
maptail.history.push(geo) maptail.history.push(geo)
maptail.buffer.push(geo) maptail.buffer.push(geo)
if (maptail.history.length > maptail.historyMax) maptail.history.shift() if (maptail.history.length > maptail.config.historyMax) maptail.history.shift()
if (Date.now() - before > maptail.bufferTime && maptail.buffer.length) { if (Date.now() - before > maptail.config.bufferTime && maptail.buffer.length) {
users.forEach(function (socket) { users.forEach(function (socket) {
socket.remote.emit('geoip', maptail.buffer) socket.remote.emit('geoip', maptail.buffer)
}) })
maptail.buffer = [] maptail.buffer = []
before = Date.now() before = Date.now()
} }
else { else {
if (maptail.buffer.length > maptail.bufferMax) maptail.buffer.shift() if (maptail.buffer.length > maptail.config.bufferMax) maptail.buffer.shift()
} }
}) })
} }
2 changes: 1 addition & 1 deletion package.json
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"author": "George Stagas <gstagas@gmail.com> (http://stagas.com)", "author": "George Stagas <gstagas@gmail.com> (http://stagas.com)",
"name": "maptail", "name": "maptail",
"description": "maptail is a realtime map view of GeoIP data", "description": "maptail is a realtime map view of GeoIP data",
"version": "0.1.6", "version": "0.1.8",
"repository": { "repository": {
"url": "git://github.com/stagas/maptail.git" "url": "git://github.com/stagas/maptail.git"
}, },
Expand Down
24 changes: 4 additions & 20 deletions public/maptail.js
Original file line number Original file line Diff line number Diff line change
@@ -1,21 +1,5 @@
// config // config
var config = {} var config = { timeDiff: 0 }

// markers' initial time to live in seconds
config.ttl = 100 // seconds

// maximum dots to be displayed (this adjusts ttl automatically)
config.maxDots = 60

// report visitors up to that age
config.visitorMaxAge = 240 // seconds

// aging repaint in milliseconds
config.ageInterval = 1000 / 60

// from the server
config.timeDiff = 0
config.bufferTime = 1000


// visitors // visitors
var visitors = 0 var visitors = 0
Expand Down Expand Up @@ -226,7 +210,7 @@ window.onload = function () {
} else { } else {
marker = this.markers.list[geo.ip] marker = this.markers.list[geo.ip]
clearTimeout(marker.visitorTimeout) clearTimeout(marker.visitorTimeout)
marker.visitorTimeout = setTimeout(visitorsDec, config.visitorMaxAge * 1000) marker.visitorTimeout = setTimeout(visitorsDec, config.maxAge * 1000)
marker.date = geo.date marker.date = geo.date
} }
} }
Expand Down Expand Up @@ -270,7 +254,7 @@ window.onload = function () {
this.ipList.object.className = 'ip' this.ipList.object.className = 'ip'
this.ipList.object.innerHTML = this.ip + ' <span style="color:yellow">' + (geo.country || '??') + '</span>' this.ipList.object.innerHTML = this.ip + ' <span style="color:yellow">' + (geo.country || '??') + '</span>'


this.visitorTimeout = setTimeout(visitorsDec, config.visitorMaxAge * 1000) this.visitorTimeout = setTimeout(visitorsDec, config.maxAge * 1000)
} }


Marker.prototype.paint = function () { Marker.prototype.paint = function () {
Expand Down Expand Up @@ -349,7 +333,7 @@ window.onload = function () {


setInterval(function () { setInterval(function () {
map.markers.age() map.markers.age()
}, config.ageInterval) }, 1000 / config.fps)


connect(function (client) { connect(function (client) {
client.remote.on('config', function (cfg) { client.remote.on('config', function (cfg) {
Expand Down

0 comments on commit dfa68b3

Please sign in to comment.