Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Openshift-enabling redis connection #13

Open
wants to merge 79 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
79 commits
Select commit Hold shift + click to select a range
208966b
Openshift-enabling redis connection
Sep 11, 2013
3cf77fe
Openshift enabling the app
Jan 22, 2014
cd7951a
Adding cloud9 environment variables for running and debug and such.
peculater Jan 22, 2014
5ea9fd2
Updating package.json to openshift
Jan 22, 2014
4f6e1dc
Merge branch 'master' of https://github.com/peculater/coffee-monitor
Jan 22, 2014
6b80ef4
Getting the app running in cloud9
peculater Jan 22, 2014
d6f2ab5
Adding beginnings of rest push logic
peculater Jan 22, 2014
dd5af2c
Fixing more openshift stuff
peculater Jan 22, 2014
8b60086
Fixing more openshift stuff again
peculater Jan 22, 2014
c0e2e96
POST things
peculater Jan 22, 2014
8c1276a
Not sure why this was invalid
Jan 22, 2014
c6876a2
Trying to fix 'u'
Jan 22, 2014
3edbbbb
Starting to see about json parsing
Jan 22, 2014
a48138a
req.data is not a thing
Jan 22, 2014
176538f
req.body is not a quite a thing
Jan 22, 2014
4879a57
Inspect input
Jan 22, 2014
d74d112
Change from updating brews to updating pots
Jan 22, 2014
b1a21ca
Persist update pot logic
Jan 22, 2014
9a6bfee
Starting view data
Jan 22, 2014
e484fb5
Fixing accidental variable deletion
Jan 22, 2014
77ee5bb
Adding some progressbar libraries
Jan 22, 2014
283040a
Add pots to recent-brews page
Jan 22, 2014
46c19d4
Missed some parens
Jan 22, 2014
fcf9d6a
Missed a brew
Jan 22, 2014
85bc3e6
Adding progressbar to view with colors
Jan 22, 2014
7a7d186
I don't know jade
Jan 22, 2014
3d3065e
Let's try animating this
Jan 22, 2014
6a0f462
I just need a stupid styleshee
Jan 23, 2014
9dbd055
Remove name and fraction from text
Jan 23, 2014
f98d4cd
I hate jade
Jan 23, 2014
2b1a014
I hate jade
Jan 23, 2014
ee4676c
Add signal for updating pot
Jan 23, 2014
8c31fe0
I hate jade
Jan 23, 2014
973cb68
Add code to pull a single pot
Jan 23, 2014
44a817b
I hate jade
Jan 23, 2014
4ab493a
Trying some socket.io stuff
Jan 23, 2014
4510b54
maybe this will help an error?
Jan 23, 2014
85a65f7
Bah, parens
Jan 23, 2014
01fd7fa
Bah, parens
Jan 23, 2014
ba28e2e
Bah, parens
Jan 23, 2014
aa3383a
No tabs? Really jade?
Jan 23, 2014
53667fd
Fixing missing brew error
Jan 23, 2014
d75d6b0
Fixing some kind of bizarre-o catch block
Jan 23, 2014
09db223
Fixing typos
Jan 23, 2014
ada2968
Missed a hyphen
Jan 23, 2014
17f5dc0
Debugging javascript old school
Jan 23, 2014
aa895df
This might need to be here to avoid a long timeout
Jan 23, 2014
78ab5eb
This might need to be here to avoid a long timeout
Jan 23, 2014
31b00b1
Gotta publish
Jan 23, 2014
9d31ea4
Removing some debug statements
Jan 23, 2014
49f00e1
Fade if the thing is removed
Jan 23, 2014
ee04ee8
Create a brew if the lastbrew time is updated
Jan 23, 2014
7573d38
This vs self
Jan 23, 2014
4805d85
Trying to figure out this callback stack
Jan 23, 2014
a64c0cb
More debug code. Not sure what's not going on.
Jan 23, 2014
b099c83
Trying to specify the websockets port
Jan 23, 2014
91db18b
Apparently 'false' means different things than I thought.
Jan 23, 2014
11b78b1
That's not the right place for that
Jan 23, 2014
e0e452c
Point directly to websocket port
Jan 23, 2014
3ec8592
Subscribe to published messages
Jan 23, 2014
1e25241
JSON is magically coerced into an object?
Jan 23, 2014
8f7e6e5
Try to get the jquery to respond to the right thing.
Jan 23, 2014
a5d6c97
Holy cow websockets is working. Add the faded attribute to the dataset
Jan 23, 2014
c26fd31
I really meant the parent
Jan 23, 2014
36bc3b1
Debug code for new brews
Jan 23, 2014
7487cf1
Missed a concat
Jan 23, 2014
a88dcb3
Callbacks and waterfalls are not quite as straightforward as I would …
Jan 23, 2014
d73a85a
Callbacks and waterfalls are not quite as straightforward as I would …
Jan 23, 2014
11f6765
Callbacks and waterfalls are making a bit more sense
Jan 23, 2014
c29cee3
More rational default values for brew
Jan 23, 2014
efa2a50
Adding doc of the new Socket.io signal I wrote
Jan 24, 2014
0888507
Doc for new attributes on a pot
Jan 24, 2014
03d035f
Reduce log verbosity
Jan 29, 2014
ae9e680
Merge branch 'master' of https://github.com/peculater/coffee-monitor
Jan 29, 2014
9a1500c
Don't log every request.
Jan 29, 2014
b5c4ec6
Removing the 'started' time, as that's not really reliable
Jan 29, 2014
e6b584a
Ignoring debug logs
Oct 16, 2014
82aaf27
Remove pot status bar since it's not actually working
Oct 16, 2014
4754213
Merge branch 'master' of github.com:peculater/coffee-monitor
Oct 16, 2014
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/public/stylesheets/*.css
.DS_Store
tags
npm-debug.log
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ are as follows:
* Coffee pots
- name: 'pot:%id%'
- type: hash
- attributes: id, name, color
- attributes: id, name, color, lastBrew, readyAt, currentLevel, removed
* Brews set
- name: 'brews'
- type: sorted set by time of brew
Expand Down Expand Up @@ -72,6 +72,12 @@ about. This is the same as the contents of the '#brews' div.
This event is sent automatically when a client connects to make sure they have
the most recent list of brews.

#### updatePot

This is broadcast to all currently connected clients. It sends the JSON object
representing all of the pots currently in the system, including how full they
are, whether they are in the caddy, etc.

### Emitted by client

#### recentBrews
Expand Down
44 changes: 30 additions & 14 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@

var openshift_app_dir = "./"
if (process.env.OPENSHIFT_NODEJS_IP){
openshift_app_dir = process.env.OPENSHIFT_REPO_DIR + "coffee-monitor/";
}



var express = require('express'),
routes = require('./routes'),
routes = require(openshift_app_dir + 'routes'),
http = require('http'),
passport = require('passport'),
path = require('path'),
brewHelper = require('./helpers/brews'),
redisHelper = require('./helpers/redis'),
signals = require('./helpers/signals'),
userHelper = require('./helpers/users');
brewHelper = require(openshift_app_dir + 'helpers/brews'),
redisHelper = require(openshift_app_dir + 'helpers/redis'),
signals = require(openshift_app_dir + 'helpers/signals'),
userHelper = require(openshift_app_dir + 'helpers/users');

var db = redisHelper.getConnection();
var manager = new brewHelper.BrewManager(db);
Expand Down Expand Up @@ -53,13 +61,14 @@ function ensureAuthenticatedOrKnownIp(req, res, next) {
var app = express();
app.locals.moment = require('moment');
app.configure(function() {
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('port', process.env.OPENSHIFT_NODEJS_PORT || process.env.PORT || 3000);
app.set('ip', process.env.OPENSHIFT_NODEJS_IP || process.env.IP || '127.0.0.1');
app.set('views', openshift_app_dir + 'views/');
app.set('view engine', 'jade');
app.set('trust proxy', true);
app.use(express.favicon(__dirname + '/public/favicon.ico',
app.use(express.favicon(openshift_app_dir + 'public/favicon.ico',
{ maxAge: 365 * 24 * 60 * 60 * 1000 }));
app.use(express.logger('dev'));
// app.use(express.logger('dev'));
app.use(express.compress({ filter: compressFilter }));
app.use(express.bodyParser());
app.use(require('express-validator'));
Expand All @@ -74,8 +83,8 @@ app.configure(function() {
app.use(attachBrewManager);
app.use(attachPassport);
app.use(app.router);
app.use(require('stylus').middleware(__dirname + '/public'));
app.use(express.static(path.join(__dirname, 'public')));
app.use(require('stylus').middleware(openshift_app_dir + 'public'));
app.use(express.static(path.join(openshift_app_dir, 'public')));
});

app.configure('development', function() {
Expand All @@ -93,6 +102,7 @@ app.get('/makers', onlineTracker, routes.makers);

app.get('/pots/add', userHelper.ensureAuthenticated, routes.potAdd);
app.post('/pots/add', userHelper.ensureAuthenticated, routes.potAddSubmit);
app.post('/pots/update', ensureAuthenticatedOrKnownIp, routes.potUpdate);
app.get('/pots/:id', onlineTracker, routes.potDetail);
app.delete('/pots/:id', userHelper.ensureAuthenticated, routes.potDelete);
app.get('/pots', onlineTracker, routes.pots);
Expand All @@ -104,6 +114,7 @@ app.get('/brews/:id', onlineTracker, routes.brewDetail);
app.delete('/brews/:id', userHelper.ensureAuthenticated, routes.brewDelete);
app.get('/brews', onlineTracker, routes.brews);


app.get('/tea', onlineTracker, routes.teapot);
app.get('/qr', onlineTracker, routes.qr);

Expand All @@ -113,10 +124,13 @@ app.post('/login', routes.loginSubmit);
app.get('/logout', routes.logout);

var server = http.createServer(app);
server.listen(app.get('port'), function() {
console.log("Express server listening on port " + app.get('port'));
server.listen(app.get('port'), app.get('ip'), function() {
console.log("Express server listening on port " + app.get('port') + " and ip " + app.get('ip'));
});

//var websocketserver = http.createServer();
//websocketserver.listen(8000, app.get('ip'));

var io = require('socket.io').listen(server);
io.configure(function() {
io.set('log level', 1);
Expand Down Expand Up @@ -147,6 +161,8 @@ ioSub.on('message', function(chan, msg) {
signals.updateBrew(app, io, manager, msg);
} else if (chan === 'deleteBrew') {
signals.deleteBrew(io, msg);
} else if (chan === 'updatePot') {
signals.updatePot(app, io, manager, msg);
}
});
ioSub.subscribe('updateBrew', 'deleteBrew');
ioSub.subscribe('updateBrew', 'deleteBrew', 'updatePot');
43 changes: 41 additions & 2 deletions helpers/brews.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ BrewManager.prototype.getRecentBrews = function(next) {
});
};


function storePartTwo(err, db, obj, type, next) {
// coerce any non-string keys to strings
Object.keys(obj).forEach(function(key) { obj[key] = "" + obj[key]; });
Expand Down Expand Up @@ -80,6 +81,10 @@ BrewManager.prototype.getPots = function(next) {
});
};

BrewManager.prototype.getPot = function(id, next) {
this.db.hgetall('pot:' + id, next);
};

BrewManager.prototype.addPot = function(pot, next) {
var self = this;
async.waterfall([
Expand All @@ -101,6 +106,40 @@ BrewManager.prototype.deletePot = function(id, next) {
.exec(next);
};

BrewManager.prototype.updatePot = function(update, next){
var self = this;
async.waterfall([
function(next) {
self.db.multi()
.hmset('pot:' + update.pot,
'currentLevel', update.currentLevel,
'removed', update.removed
)
.hgetall('pot:' + update.pot)
.publish('updatePot', update.pot)
.exec(next);
},
function(potresult, next) {
var pot = potresult[1];
//Vaguely valid data
if (update.lastBrew != 0 && update.lastBrew != "0" && update.lastBrew != undefined){
if (int10(update.lastBrew) * 1000 > pot.readyAt){
console.log ("lastBrew was updated");
var brew = {
creationIp: '127.0.0.1',
readyAt: int10(update.lastBrew) * 1000,
makerId: 1,
potId: update.pot,
};
self.addBrew(brew, next);
}
}
}
], function(err, results) {
next(err, update);
});
}

BrewManager.prototype.getBrew = function(id, next) {
this.db.hgetall('brew:' + id, next);
};
Expand All @@ -118,7 +157,7 @@ BrewManager.prototype.addBrew = function(brew, next) {
.hmget('pot:' + brew.potId, 'name', 'color', 'readyAt')
.exec(next);
},
function(results, next) {
function(results, next) {
// check preconditions first: we must be > readyAt time for this brew
// to even make sense, otherwise someone is spoofing us
var now = Date.now();
Expand All @@ -139,7 +178,7 @@ BrewManager.prototype.addBrew = function(brew, next) {
if (!brew.readyAt) {
brew.createdAt = now;
} else {
brew.createdAt = brew.readyAt - (1000 * int10(brew.brewTime));
brew.createdAt = int10(brew.readyAt) - (1000 * int10(brew.brewTime));
}
}
if (!brew.readyAt) {
Expand Down
8 changes: 4 additions & 4 deletions helpers/redis.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ function logError(err) {
}

function getConnection() {
var port = process.env.REDIS_PORT || 6379,
host = process.env.REDIS_HOST || '127.0.0.1',
password = process.env.REDIS_PASS,
dbNumber = process.env.REDIS_DB;
var port = process.env.OPENSHIFT_REDIS_PORT || process.env.REDIS_PORT || 16379,
host = process.env.OPENSHIFT_REDIS_HOST || process.env.REDIS_HOST || process.env.IP || '127.0.0.1',
password = process.env.REDIS_PASSWORD || process.env.REDIS_PASS,
dbNumber = process.env.REDIS_DB || 0;
var db = redis.createClient(port, host);
db.on('error', logError);

Expand Down
13 changes: 13 additions & 0 deletions helpers/signals.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,19 @@ exports.updateBrew = function(app, io, manager, brewId) {
});
};


exports.updatePot = function(app, io, manager, potId) {
//Just copying from above
manager.getPot(potId, function(error, pot) {
async.parallel([
function(next) {
io.sockets.emit('updatePot', pot);
next(null);
}
]);
});
};

exports.deleteBrew = function(io, brewId) {
io.sockets.emit('deleteBrew', brewId);
};
38 changes: 23 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
{
"name": "coffee-monitor",
"description": "Monitor when coffee pots were brewed",
"version": "0.1.8",
"author": "Dan McGee <[email protected]>",
"private": true,
"name": "OpenShift-Backstop-Coffee-monitor",
"version": "1.0.0",
"description": "Monitor when coffee gets brewed on openshift",
"keywords": [
"OpenShift",
"Node.js",
"application",
"openshift"
],
"author": {
"name": "Backstop"
},
"bugs": {
"web": "https://github.com/toofishes/coffee-monitor/issues"
},
Expand All @@ -20,15 +27,18 @@
}
],
"homepage": "https://github.com/toofishes/coffee-monitor",
"scripts": {
"start": "node app.js"
"repository": {
"type": "git",
"url": "https://gitub.com/openshift/origin-server"
},

"engines": {
"node": ">= 0.6.0",
"npm": ">= 1.0.0"
},
"keywords": [
"coffee"
],
"dependencies": {
"async": "0.2.x",
"bcrypt": "0.7.x",
"bcrypt": "0.5.x",
"campfire": "0.1.x",
"connect-redis": "1.4.x",
"express": "3.1.x",
Expand All @@ -47,8 +57,6 @@
"jitsu": "*",
"jshint": "*"
},
"engines": {
"node": ">=0.8"
},
"subdomain": "coffee-monitor"
"private": true,
"main": "server.js"
}
Loading