Skip to content

Commit

Permalink
moved teh v1 date to its own file because it really is a different ta…
Browse files Browse the repository at this point in the history
…ble as other date. Almost have v1 polling up again- date formating needs to be corrected
  • Loading branch information
kans committed Apr 17, 2012
1 parent 4ef661b commit 6dba633
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 24 deletions.
65 changes: 43 additions & 22 deletions lib/web/db.js
Expand Up @@ -16,54 +16,69 @@
*/

var fs = require('fs');
var _ = require('underscore');
var path = require('path');

var _ = require('underscore');
var async = require('async');

var settings = require('../settings');
var utils = require('../utils');

stats = utils.make_class({
init: function(){
var stats_dir;
var data;
var data, raw_v1_info, version_one_info;
var now;
var self = this;
var stats_path = path.join(__dirname, settings.stats_path);
self.stats_path = stats_path;
try{
// try to find the stats dir
stats_dir = fs.lstatSync(stats_path);
// is the stats dir a dir?
if (!stats_dir.isDirectory()){
console.error('The stats dir isn\'t a dir!');
process.exit(1);
}
// oh noes, better try to make it
}catch(e){
if (e.errno === 34){
try{
fs.mkdirSync(stats_path);
// no way to recover from this
}catch(e){
console.error("failed to make the stats dir");
process.exit(1);
}
}
}
now = new Date();
self._stats_file = self._get_stats_path(now.getUTCFullYear(), now.getUTCMonth());
self.now = new Date();
self.__version_one_date = null;
self._stats_file = self._get_stats_path(self.now);
self._version_one_file = path.join(self.stats_path, 'version_one.js');
try{
data = fs.readFileSync(self._stats_file, 'utf8');
self._data = JSON.parse(data);
raw_v1_info = fs.readFileSync(self._version_one_file, 'utf8');
version_one_info = JSON.parse(raw_v1_info);
self.set_previous_v1_poll_date(version_one_info);
}catch(e){}
process.on('exit', self._flush);
},
set_previous_v1_poll_date: function(date){
debugger;
var self = this;
self._data.__version_one_date = date;
self.__version_one_date = date;
},
get_previous_v1_poll_date: function(){
var self = this;
return self._data.__version_one_date;
return self.__version_one_date;
},
add_event: function(event, repo){
var self = this;
if (new Date().month !== self.now){
self._flush();
}
if (!_.has(self._data, repo)){
self._data[repo] = {};
}
Expand All @@ -72,32 +87,38 @@ stats = utils.make_class({
}
self._data[repo][event.user] += self._get_event_score(event);
},
_get_stats_path: function(year, month){
_get_stats_path: function(now){
var self = this;
var file_path;
var year = now.getUTCFullYear();
var month = now.getUTCMonth();
if (!year || !month){
console.error('Give me a year and month!');
process.exit(1);
}
var self = this;

var file_path = path.join(self.stats_path, year.toString()+ '-' + month.toString()+ '.json');
file_path = path.join(self.stats_path, year.toString()+ '-' + month.toString()+ '.json');
return file_path;
},
_get_event_score: function(event){
return parseInt(event.status, 10);
},
_flush: function(start_new_file){
_flush: function(){
var self = this;
var last_v1_date = self.get_previous_v1_poll_date();
fs.writeFile(self._stats_file, JSON.stringify(self._data), 'utf8', function(err, results){
if (err){
console.log(err);
return;
}
if (start_new_file){
self._data = {};
self.set_previous_v1_poll_date(last_v1_date);
}
//TODO: set next flush
self._old_data = self._data;
self._data = {};
self.now = new Date();
async.parallel([
function(cb){
fs.writeFile(self._stats_file, JSON.stringify(self._old_data), 'utf8', cb);
}, function(cb){
debugger;
fs.writeFile(self._version_one_file, JSON.stringify(self.__version_one_date), 'utf8', cb);
}], function(err, results){
if (err){
console.log(err);
return;
}
self._stats_file = self._get_stats_path(self.now);
});
},
_data: {}
Expand Down
3 changes: 1 addition & 2 deletions lib/web/version_one/poller.js
Expand Up @@ -38,7 +38,7 @@ var selection = ["ChangedBy",

exports.install = function(){
var now = utils.now();
var date = db.get_previous_v1_poll_date();
var date = db.get_previous_v1_poll_date() || new Date();
poller(date, now);
// TODO: update db with now
};
Expand Down Expand Up @@ -124,7 +124,6 @@ var poller = function(start_time, end_time){
function(err, data){
if (!err){
db.set_previous_v1_poll_date(end_time);
db._flush();
}
return console.log(err, data);
});
Expand Down

0 comments on commit 6dba633

Please sign in to comment.