From 44bc25785e4836362579af7bface4458188014b6 Mon Sep 17 00:00:00 2001 From: William Duyck Date: Sat, 24 May 2014 02:20:03 +0100 Subject: [PATCH] initial pass at generating needed stats for MP heatmap --- .env-dist | 7 ++++ app.js | 110 +++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 4 +- 3 files changed, 120 insertions(+), 1 deletion(-) diff --git a/.env-dist b/.env-dist index 9658b00..779d1ad 100644 --- a/.env-dist +++ b/.env-dist @@ -3,3 +3,10 @@ PORT=5000 # i18n Language Support export SUPPORTED_LANGS='[ "*" ]' + +# Events service to use +export EVENTS_SERVICE='http://events-api.webmaker.org' + +# When to start getting events from (must work in Date.parse) +# leave blank for all events +export EVENTS_START_DATE='' diff --git a/app.js b/app.js index 7f43238..8d5499a 100644 --- a/app.js +++ b/app.js @@ -3,6 +3,9 @@ var express = require('express'); var nunjucks = require('nunjucks'); var path = require('path'); var i18n = require('webmaker-i18n'); +var request = require('request'); +var NodeCache = require('node-cache') +var cache = new NodeCache({stdTTL: 300, checkperiod: 300}); var nunjucksEnv = new nunjucks.Environment( new nunjucks.FileSystemLoader(path.join(__dirname, 'views'))); habitat.load(); @@ -65,3 +68,110 @@ app.get('/strings/:lang?', i18n.stringsRoute('en-US')); app.listen(env.get('PORT'), function () { console.log('Now listening on %d', env.get('PORT')); }); + +// Maker Party Event Stats +app.get('/event-stats', function(req, res) { + var data = cache.get('eventStats'); + res.json( data.eventStats ); +}); + +function generateEventStats() { + request.get(env.get('EVENTS_SERVICE') + '/events?after=' + env.get('EVENTS_START_DATE'), function(error, response, events) { + if(!error && response.statusCode === 200) { + events = JSON.parse( events ); + // arrays to dedupe things w/ + var event_hosts = []; + var countries = []; + + // init stats object + var event_stats = { + hosts: 0, + attendees: 0, + events: 0, + byCountry: {} + }; + + // aleady know number of events :) + event_stats.events = events.length; + var geocodes = events.length; + + // make access to other stats easier to figure + events.forEach( function( event, idx ) { + var new_host = 0; // little trick to count hosts per country + + /* + get people stats + */ + + // get attendee stats + event_stats.attendees += event.attendees; + + // get host stats + if(event_hosts.indexOf(event.organizerId) === -1){ + event_hosts.push(event.organizerId); + + new_host = 1; // this is a new host, add to country host count ;) + } + + /* + get country level stats + */ + if( event.country === null ) { + event.country = 'UNKNOWN'; + } + + if( countries.indexOf( event.country ) === -1 ) { + event_stats.byCountry[ event.country ] = { + name: event.country, + events: 0, + hosts: 0, + attendees: 0 + }; + countries.push( event.country ); + } + + var country = event_stats.byCountry[ event.country ]; + + country = { + name: country.name, + events: country.events + 1, + hosts: country.hosts + new_host, + attendees: country.attendees + event.attendees + }; + + event_stats.byCountry[ event.country ] = country; + }); + + // now we can count number organizers (deduped) + event_stats.hosts = event_hosts.length; + + // sort the country based data using keys + var country_keys = Object.keys(event_stats.byCountry); + var new_byCountry = {}; + + country_keys.sort(); + + for ( var i = 0; i < country_keys.length; i++ ) { + var key = country_keys[ i ]; + new_byCountry[ country_keys[ i ] ] = event_stats.byCountry[ key ]; + } + + event_stats.byCountry = new_byCountry; + + cache.set('eventStats', event_stats); + } + else if(error) { + cache.set('eventStats', { processing: true }); + } + }); +} +cache.set('eventStats', { processing: true }); +generateEventStats(); + + +cache.on( 'expired', function( key, value ) { + if( key === 'eventStats' ) { + cache.set('eventStats', value); + generateEventStats(); + } +}); diff --git a/package.json b/package.json index 184db26..874a482 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,9 @@ "nunjucks": "0.1.10", "webmaker-download-locales": "^0.1.3", "webmaker-i18n": "0.3.21", - "webmaker-locale-mapping": "latest" + "webmaker-locale-mapping": "latest", + "request": "^2.36.0", + "node-cache": "^1.0.0" }, "engines": { "node": ">=0.10"