Skip to content

Commit

Permalink
return early if querystring data is missing
Browse files Browse the repository at this point in the history
  • Loading branch information
robflaherty committed Aug 27, 2011
1 parent 7961a09 commit 606a24b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
29 changes: 25 additions & 4 deletions server.coffee
@@ -1,3 +1,8 @@
###
Marzipan
###

# Require modules
express = require 'express'
config = require './config/config'
mongo = require 'mongodb'
Expand All @@ -16,28 +21,44 @@ cache = {}

# Push pageview into cache onto user key
pushCache = (data) ->
user = data.user
data.timestamp = Date.now() + ''

# Return early if data is empty
return if _.isEmpty(data)

# Pull user out of data
user = data.user
delete data.user

# Add timestamp to data
data.timestamp = Date.now() + ''

# Add user to the cache
if (!cache[user])
cache[user] = []

# Push pageview data onto user key
cache[user].push(data)

return

# Flush the cache to mongo
flushCache = () ->

# Return early if the cache is empty
return if _.isEmpty(cache)

# Save and reset cache
data = cache
cache = {}

# Print data to console
console.log data


# Save each pageview into database
db.open (err, client) ->
client.collection "users", (err, col) ->
for prop of data
col.update( {user: prop}, {$pushAll: { pageviews: data[prop] }}, {upsert: true}, () -> )
col.update( { user: prop }, { $pushAll: { pageviews: data[prop] } }, { upsert: true }, () -> )
db.close()
return
return
Expand Down
9 changes: 7 additions & 2 deletions server.js
@@ -1,5 +1,7 @@
(function() {
var app, cache, config, dashboard, db, express, flushCache, mongo, pushCache, _;
/*
Marzipan
*/ var app, cache, config, dashboard, db, express, flushCache, mongo, pushCache, _;
express = require('express');
config = require('./config/config');
mongo = require('mongodb');
Expand All @@ -9,9 +11,12 @@
cache = {};
pushCache = function(data) {
var user;
if (_.isEmpty(data)) {
return;
}
user = data.user;
data.timestamp = Date.now() + '';
delete data.user;
data.timestamp = Date.now() + '';
if (!cache[user]) {
cache[user] = [];
}
Expand Down

0 comments on commit 606a24b

Please sign in to comment.