Skip to content
This repository has been archived by the owner on Jan 15, 2018. It is now read-only.

Commit

Permalink
Simplifying database inserts.
Browse files Browse the repository at this point in the history
Adding cookie storeage for basic UI selections.
  • Loading branch information
Josh Mervine committed Aug 26, 2013
1 parent bf019d6 commit f99d36d
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 207 deletions.
31 changes: 24 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#! /usr/bin/env node
var
MongoClient = require('mongodb').MongoClient,
spawn = require('child_process').spawn,
Bench = require('./lib/bench'),
DB = require('./lib/db'),
cli = require('./lib/cli');

var bench = new Bench(cli.url);
Expand All @@ -23,8 +23,6 @@ switch (cli.action) {
spawn('node', ['./server/app.js'], { stdio: 'inherit', env: process.env });
break;
default:
db = new DB(cli.database, cli.collection);

function handleRun(result) {
if (!result.json) {
console.log(result.raw);
Expand All @@ -36,10 +34,29 @@ switch (cli.action) {

if (runs.length === cli.runs) {
var set = median(runs);
db.add(set, function() {
console.log("-------------------------------------------");
console.log("Complete! (median httpTrafficCompleted: %sms)", set.metrics.httpTrafficCompleted);
process.exit(0);
set.created_at = Date.now();
MongoClient.connect(cli.database, {db: {native_parser: true}}, function(err, db) {
if (err) {
console.trace(err);
process.exit(1);
}
var collection = db.collection(cli.collection);
collection.insert(set, function (err, obj) {
console.log("-------------------------------------------");
console.log("Complete! (median httpTrafficCompleted: %sms)", set.metrics.httpTrafficCompleted);
console.log("-------------------------------------------");
if (err) { console.trace(err); }
if (!obj) { console.log('Error inserting set:\n%s', JSON.stringify(set, null, 2)); }

if (err || !obj) {
console.log("-------------------------------------------");
}

console.log('MongoDB Database:\n - %s', cli.database);
console.log('MongoDB Collection:\n - %s', cli.collection);
console.log("-------------------------------------------");
process.exit(0);
});
});
}
}
Expand Down
84 changes: 0 additions & 84 deletions lib/db.js

This file was deleted.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
"commander": "~2.0.0",
"mongodb": "~1.3.19",
"express": "2.5.8",
"jade": ">= 0.0.1"
"jade": ">= 0.0.1",
"connect": "~2.8.5"
},
"devDependencies": {
"nodeunit": "~0.8.1"
Expand Down
20 changes: 11 additions & 9 deletions server/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,22 @@
*/

var express = require('express')
, routes = require('./routes');
, routes = require('./routes')
, connect = require('connect');

var app = module.exports = express.createServer();

// Configuration

app.configure('production', function(){
app.use(connect.logger());
});

app.configure('development', function(){
app.use(connect.logger('dev'));
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});

app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
Expand All @@ -18,14 +28,6 @@ app.configure(function(){
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(__dirname + '/public'));

//console.log('database: %s', process.env.database);
//console.log('collection: %s', process.env.collection);

});

app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});

app.configure('production', function(){
Expand Down
25 changes: 24 additions & 1 deletion server/public/javascripts/charts.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,31 @@
var start, xRotation, currentCategory, count = {}, limit = 10, disabled = {};
var yTitle = 'Duration (ms)', chartTitle = 'Results';

var cookieOptions = { expires: 365 };

$(window).load(function() {
$.getJSON(apiPath('urls'), function(result) {
result.forEach(function(url) {
setPagination();
$('#urlselect').append($('<option>', { value: url, text: url }));

if ($.cookie('selected_limit') !== 'undefined') {
$('#limitselect option').each(function() {
if ($(this).attr('value') === $.cookie('selected_limit')) {
$(this).attr('selected', 'selected');
$('#limitselect').change();
}
});
}

if ($.cookie('selected_url') !== 'undefined') {
$('#urlselect option').each(function() {
if ($(this).attr('value') === $.cookie('selected_url')) {
$(this).attr('selected', 'selected');
$('#urlselect').change();
}
});
}
});
});

Expand All @@ -19,16 +39,18 @@ $(window).load(function() {
}
$('#keyselect').append($('<option>', optionOptions));
});
$('#keyselect').attr('size', keyCount);
//$('#keyselect').attr('size', keyCount);
});

$('#urlselect').change(function() {
$.cookie('selected_url', $('#urlselect option:selected').val(), cookieOptions);
setPagination();
draw();
});

$('#limitselect').change(function() {
limit = parseInt($('#limitselect option:selected').val(), 10);
$.cookie('selected_limit', limit, cookieOptions);
switch (limit) {
case 10: xRotation = undefined; break;
case 25: xRotation = 45; break;
Expand Down Expand Up @@ -187,6 +209,7 @@ var yTitles = {
};

function clearCategories() {
currentCategory = undefined;
$('ul.nav li').each(function() { $(this).removeClass('active'); });
}

Expand Down
2 changes: 1 addition & 1 deletion server/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var isError = require('util').isError;
var MongoClient = require('mongodb').MongoClient;

function dbc(collection, index, callback) {
MongoClient.connect(process.env.database, function(err, db) {
MongoClient.connect(process.env.database, {db: {native_parser: true}}, function(err, db) {
if (err) {
console.trace(err);
callback(err);
Expand Down
4 changes: 2 additions & 2 deletions server/views/index.jade
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
.row-fluid
.span2
.well.sidebar-nav
select#keyselect.form-control(multiple="multiple" style="margin: 0; width:100%")
select#keyselect.form-control(multiple="multiple" style="margin: 0; width:100%" size="40")
.span10
.row-fluid
.span2
Expand Down Expand Up @@ -47,4 +47,4 @@

.row-fluid
.span12
#highcharts(style="width:100%; height:400px;")
#highcharts(style="width:100%; height:600px;")
102 changes: 0 additions & 102 deletions test/db_test.js

This file was deleted.

0 comments on commit f99d36d

Please sign in to comment.