Permalink
Browse files

started work on the server logging

  • Loading branch information...
1 parent f87b3c2 commit 250e939fc94c5aa0c4fef2a7bf3241f8b03e1baf @robflaherty committed Aug 13, 2011
Showing with 89 additions and 0 deletions.
  1. +3 −0 client/test/index.html
  2. +40 −0 server/build/server.js
  3. +46 −0 server/src/server.coffee
View
3 client/test/index.html
@@ -9,6 +9,9 @@
};
</script>
<script src="../build/marzipan.js"></script>
+ <script>
+ marzipan.logPageview();
+ </script>
</head>
<body>
</body>
View
40 server/build/server.js
@@ -0,0 +1,40 @@
+(function() {
+ var cache, express, flushCache, mongo, pushCache, root, server, _;
+ root = this;
+ express = require('express');
+ server = express.createServer();
+ mongo = require('mongodb');
+ _ = require('../vendor/underscore');
+ cache = {};
+ pushCache = function(data) {
+ var user;
+ user = data.user;
+ data.timestamp = Date.now() + '';
+ delete data.user;
+ if (!cache[user]) {
+ cache[user] = [];
+ }
+ cache[user].push(data);
+ };
+ flushCache = function() {
+ var db;
+ db = new mongo.Db('test', new mongo.Server('localhost', 27017, {}), {});
+ db.open(function(err, client) {
+ client.collection("users", function(err, col) {
+ var prop;
+ for (prop in cache) {
+ col.insert({
+ user: prop,
+ pageviews: cache[prop]
+ }, function() {});
+ }
+ });
+ });
+ };
+ server.get('/image.gif', function(req, res) {
+ pushCache(req.query);
+ return res.send(204);
+ });
+ server.listen(3000);
+ setInterval(flushCache, 2000);
+}).call(this);
View
46 server/src/server.coffee
@@ -0,0 +1,46 @@
+root = this
+
+# Node modules
+express = require 'express'
+server = express.createServer()
+mongo = require 'mongodb'
+
+#JS Libraries
+_ = require '../vendor/underscore'
+
+# Local cache
+cache = {}
+
+# Push pageview into cache onto user key
+pushCache = (data) ->
+ user = data.user
+ data.timestamp = Date.now() + ''
+
+ delete data.user
+
+ if (!cache[user])
+ cache[user] = []
+ cache[user].push(data)
+ return
+
+# Function to flush the cache to mongo
+flushCache = () ->
+ db = new mongo.Db('test', new mongo.Server('localhost', 27017, {}), {})
+
+ db.open (err, client) ->
+ client.collection "users", (err, col) ->
+ for prop of cache
+ col.insert({user: prop, pageviews: cache[prop]}, () -> )
+ return
+ return
+ return
+
+# Server listening for beacon request. Returns 204 "No Content" status
+server.get '/image.gif', (req, res) ->
+ pushCache(req.query)
+ res.send(204)
+
+server.listen(3000)
+
+# Flush cache every 2 seconds
+setInterval(flushCache, 2000)

0 comments on commit 250e939

Please sign in to comment.