Fixed #38. Implemented the new schema.

Now we only store the date days and the commit hashes. The remote url is not used anymore. Fixes #29.
After this change, the data file is simplified a lot.
This commit is contained in:
Ionică Bizău 2015-07-13 08:26:51 +03:00
parent 6965300e5b
commit 12182f1537

View file

@ -14,12 +14,14 @@ var Ul = require("ul")
, Typpy = require("typpy") , Typpy = require("typpy")
, Exec = ChildProcess.exec , Exec = ChildProcess.exec
, Spawn = ChildProcess.spawn , Spawn = ChildProcess.spawn
, LowDb = require("lowdb")
; ;
// Constants // Constants
const DATE_FORMAT = "MMM D, YYYY" const DATE_FORMAT = "MMM D, YYYY"
, DEFAULT_STORE = Abs("~/.git-stats") , DEFAULT_STORE = Abs("~/.git-stats")
, DEFAULT_DATA = {
commits: {}
}
; ;
function GitStats(dataPath) { function GitStats(dataPath) {
@ -65,20 +67,18 @@ GitStats.prototype.record = function (data, callback) {
} }
if (typeof data.url !== "string" || !data.url) { if (typeof data.url !== "string" || !data.url) {
callback(new Error("Invalid url field. This commit is not recorded into the git-stats history since you haven't added the remote url. You can import the previous commits using the git-stats-importer tool.")); delete data.url;
return GitStats;
} }
// Get stats // Get stats
self.get(function (err, stats) { self.get(function (err, stats) {
stats = stats || {};
var day = data.date.format(DATE_FORMAT) var commits = stats.commits
, today = stats[day] = Object(stats[day]) , day = data.date.format(DATE_FORMAT)
, repo = today[data.url] = Object(today[data.url]) , today = commits[day] = Object(commits[day])
; ;
repo[data.hash] = { date: data.date }; today[data.hash] = 1;
self.save(stats, callback); self.save(stats, callback);
}); });
@ -100,8 +100,8 @@ GitStats.prototype.get = function (callback) {
ReadJson(self.path, function (err, data) { ReadJson(self.path, function (err, data) {
if (err && err.code === "ENOENT") { if (err && err.code === "ENOENT") {
return self.save({}, function (err) { return self.save(DEFAULT_DATA, function (err) {
callback(err, {}); callback(err, DEFAULT_DATA);
}); });
} }
@ -198,14 +198,11 @@ GitStats.prototype.graph = function (data, callback) {
// Iterate days // Iterate days
self.iterateDays(data, function (cDay) { self.iterateDays(data, function (cDay) {
cDayObj = Object(stats.commits[cDay]);
cDayObj = year[cDay] = { cDayObj = year[cDay] = {
_: stats[cDay] || {} _: cDayObj
, c: 0 , c: Object.keys(cDayObj).length
}; };
Object.keys(cDayObj._).forEach(function (c) {
cDayObj.c += Object.keys(cDayObj._[c]).length;
});
}); });
callback(null, year); callback(null, year);