mirror of
https://github.com/IonicaBizau/git-stats.git
synced 2025-01-03 10:22:11 +01:00
Added some util methods and ansiCalendar.
This commit is contained in:
parent
439c15e6a4
commit
9d829d3068
1 changed files with 88 additions and 19 deletions
109
lib/index.js
109
lib/index.js
|
@ -4,7 +4,10 @@ var FsExtra = require("fs-extra")
|
|||
, Moment = require("moment")
|
||||
;
|
||||
|
||||
const STORE_PATH = Ul.USER_DIR + "/.git-stats";
|
||||
// Constants
|
||||
const STORE_PATH = Ul.USER_DIR + "/.git-stats"
|
||||
, LEVELS = 5
|
||||
;
|
||||
|
||||
// Constructor
|
||||
var GitStats = module.exports = {};
|
||||
|
@ -65,30 +68,56 @@ GitStats.record = function (data, callback) {
|
|||
*
|
||||
* @name get
|
||||
* @function
|
||||
* @param {Object} data The stats filter. **Not yet implemented**.
|
||||
* @param {Function} callback The callback function.
|
||||
* @return {undefined}
|
||||
*/
|
||||
GitStats.get = function (data, callback) {
|
||||
if (typeof data === "function") {
|
||||
callback = data;
|
||||
data = {};
|
||||
}
|
||||
GitStats.get = function (callback) {
|
||||
FsExtra.readJSON(STORE_PATH, callback);
|
||||
};
|
||||
|
||||
GitStats.graph = function (data, callback) {
|
||||
GitStats.get(data, function (err, stats) {
|
||||
if (err) { return callback(err); }
|
||||
var year = {}
|
||||
, end = Moment()
|
||||
, start = Moment().subtract({ years: 1 })
|
||||
GitStats.iterateDays = function (data, callback) {
|
||||
|
||||
if (typeof data === "function") {
|
||||
callback = data;
|
||||
data = undefined;
|
||||
}
|
||||
|
||||
// Merge the defaults
|
||||
data = Ul.merge({
|
||||
end: Moment()
|
||||
, start: Moment().subtract(1, "years")
|
||||
, format: "MMM D, YYYY"
|
||||
}, data);
|
||||
|
||||
var start = data.start
|
||||
, end = data.end
|
||||
, cDay = null
|
||||
, cDayObj = null
|
||||
;
|
||||
|
||||
while (!(start > end)) {
|
||||
cDay = start.format("MMM D, YYYY");
|
||||
while (!start.isAfter(end)) {
|
||||
cDay = start.format(data.format);
|
||||
callback(cDay);
|
||||
start.add(1, "days")
|
||||
}
|
||||
};
|
||||
|
||||
GitStats.graph = function (data, callback) {
|
||||
|
||||
if (typeof data === "function") {
|
||||
callback = data;
|
||||
data = undefined;
|
||||
}
|
||||
|
||||
// Get commits
|
||||
GitStats.get(function (err, stats) {
|
||||
if (err) { return callback(err); }
|
||||
|
||||
var cDayObj = null
|
||||
, year = {}
|
||||
;
|
||||
|
||||
// Iterate days
|
||||
GitStats.iterateDays(data, function (cDay) {
|
||||
cDayObj = year[cDay] = {
|
||||
_: stats[cDay] || {}
|
||||
, c: 0
|
||||
|
@ -97,11 +126,51 @@ GitStats.graph = function (data, callback) {
|
|||
Object.keys(cDayObj._).forEach(function (c) {
|
||||
cDayObj.c += Object.keys(cDayObj._[c]).length;
|
||||
});
|
||||
|
||||
start.add(1, "days")
|
||||
}
|
||||
});
|
||||
|
||||
callback(null, year);
|
||||
// ⬚ ▢ ▤ ▣ ⬛
|
||||
});
|
||||
};
|
||||
|
||||
GitStats.calendar = function (data, callback) {
|
||||
GitStats.graph(data, function (err, graph) {
|
||||
if (err) { return callback(err); }
|
||||
var cal = { total: 0, days: {} }
|
||||
, cDay = null
|
||||
, days = Object.keys(graph)
|
||||
, max = 0
|
||||
;
|
||||
|
||||
days.forEach(function (c) {
|
||||
cDay = graph[c];
|
||||
cal.total += cDay.c;
|
||||
if (cDay.c > max) {
|
||||
max = cDay.c;
|
||||
}
|
||||
});
|
||||
|
||||
days.forEach(function (c) {
|
||||
cDay = graph[c];
|
||||
cal.days[c] = {
|
||||
c: cDay.c
|
||||
, level: cDay.c === 0 ? 0 : LEVELS - Math.floor(max / (cDay.c + 2))
|
||||
};
|
||||
});
|
||||
|
||||
callback(null, cal);
|
||||
});
|
||||
};
|
||||
|
||||
GitStats.ansiCalendar = function (data, callback) {
|
||||
|
||||
if (typeof data === "function") {
|
||||
callback = data;
|
||||
data = undefined;
|
||||
}
|
||||
// ⬚ ▢ ▤ ▣ ⬛
|
||||
GitStats.calendar(data, function (err, cal) {
|
||||
if (err) { return callback(err); }
|
||||
|
||||
debugger
|
||||
});
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue