<commit_before>var
    async = require('async'),
    color = require('cli-color'),
    config = require('./config.json'),
    fs = require('fs'),
    lazy = require('lazy'),
    moment = require('moment'),
    request = require('request'),
    timelog = require('./timelog');

var logFileName = getUserHome() + '/' + config.filename;

function getUserHome() {
    return process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'];
}

var formatDate = function(date) {
    var m = moment.duration(date);
    return m.hours() + "h " + m.minutes() + "m ";
}

function report(task, options) {
    var startTime;
    var dates = {};
    var stream = fs.createReadStream(logFileName);

    new lazy(stream)
    .lines
    .forEach(function(lineBuffer){
        var line = lineBuffer.toString();
        var split = line.split('\t');
        var dateTime = new Date(split[0]);
        var date = new Date(split[0].split('T')[0]);

        if (dates[date] == undefined) {
            dates[date] = {
                timeWorked : 0,
                timeSlacked : 0,
                last : dateTime
            };
        } else {
            if (line.indexOf("**") === -1) {
                dates[date].timeWorked += dateTime - dates[date].last;
            } else {
                dates[date].timeSlacked += dateTime - dates[date].last;
            }
            dates[date].last = dateTime;
        }
    });

    stream.on('end', function() {
        var date = Object.keys(dates)[0];
        console.log(
            moment(date, "YYYY-MM-DD").format('dddd') + '\t' +
            formatDate(dates[date].timeWorked) + 'worked.\t\t' +
            formatDate(dates[date].timeSlacked) + 'slacked.'
            // moment.duration(dates[date].timeSlacked)
        );
    });
}

function log(task) {
    var time = (new Date()).toISOString();

    fs.appendFile(logFileName, time + '\t' + task + '\n', function (err) {
        if (err) throw err;
        console.log("Done");
    });
}

module.exports = function (task, options) {

    console.log(task, options);



    if (options.report) {
        report();
    } else {
        log(task, options);
    }
};<commit_msg>Improved report and reports last activity<commit_after>var
    async = require('async'),
    color = require('cli-color'),
    config = require('./config.json'),
    fs = require('fs'),
    lazy = require('lazy'),
    moment = require('moment'),
    request = require('request'),
    timelog = require('./timelog');

var logFileName = getUserHome() + '/' + config.filename;

function getUserHome() {
    return process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'];
}

var formatTimeDiff = function(diff) {
    var m = moment.duration(diff);
    return m.hours() + "h " + m.minutes() + "m ";
}

var getLastLine = function(callback) {
    fs.stat(logFileName, function(err, stat) {
        fs.open(logFileName, 'r', function(err, fd) {
            if(err) throw err;
            var i = 0;
            var line = '';
            var readPrevious = function(buf) {
                fs.read(fd, buf, 0, buf.length, stat.size-buf.length-i, function(err, bytesRead, buffer) {
                    if(err) throw err;
                    line = String.fromCharCode(buffer[0]) + line;
                    if (buffer[0] === 0x0a && line.length > 1) { //0x0a == '\n'
                        //console.log("getLastLine", line.length, line);
                        callback(null, line);
                    } else {
                        i++;
                        readPrevious(new Buffer(1));
                    }
                });
            }
            readPrevious(new Buffer(1));
        });
    });
}

function report(task, options) {
    var startTime;
    var dates = {};
    var stream = fs.createReadStream(logFileName);

    new lazy(stream)
    .lines
    .forEach(function(lineBuffer){
        var line = lineBuffer.toString();
        var split = line.split('\t');
        var dateTime = new Date(split[0]);
        var dateKey = split[0].split('T')[0];
        var date = new Date(dateKey);

        if (dates[dateKey] == undefined) {
            dates[dateKey] = {
                timeWorked : 0,
                timeSlacked : 0,
                last : dateTime
            };
        } else {
            if (line.indexOf("**") === -1) {
                dates[dateKey].timeWorked += dateTime - dates[dateKey].last;
            } else {
                dates[dateKey].timeSlacked += dateTime - dates[dateKey].last;
            }
            dates[dateKey].last = dateTime;
        }
    });

    stream.on('end', function() {
        var dateKeys = Object.keys(dates);
        var totalTimeWorked = 0;
        var totalTimeSlacked = 0;
        for (var i = 0; i < dateKeys.length; i++) {
            var date = dateKeys[i];
            console.log(
                moment(date, "YYYY-MM-DD").format('dddd') + '\t' +
                formatTimeDiff(dates[date].timeWorked) + 'worked.\t\t' +
                formatTimeDiff(dates[date].timeSlacked) + 'slacked.'
                // moment.duration(dates[date].timeSlacked)
            );
            totalTimeWorked += dates[date].timeWorked;
            totalTimeSlacked += dates[date].timeSlacked;
        }
        console.log('----------------------------------------');
        console.log(
            'Total:\t' +
            formatTimeDiff(totalTimeWorked) + 'worked.\t\t' +
            formatTimeDiff(totalTimeSlacked) + 'slacked.'
            // moment.duration(dates[date].timeSlacked)
        );

    });
}

function log(task) {
    var time = new Date();

    getLastLine(function(err, line) {
        var lastDate = time;
        console.log('line', line);
        if (!err) {
            console.log('date', line.split('\t')[0].trim())
            lastDate = new Date(line.split('\t')[0].trim());
            console.log('datedate', lastDate);
        }

        fs.appendFile(logFileName, time.toISOString() + '\t' + task + '\n', function (err) {
            if (err) throw err;

            console.log("Took " + formatTimeDiff(time - lastDate));
        });
    })


}

module.exports = function (task, options) {

    console.log(task, options);



    if (options.report) {
        report();
    } else {
        log(task, options);
    }
};
{'fraction_non_alphanumeric': 0.16373155640171347, 'fraction_numerical': 0.005235602094240838, 'mean_word_length': 1.815542652970076, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4684868', 'n_tokens_mistral': 1912, 'n_tokens_neox': 1851, 'n_words': 375}