aboutsummaryrefslogblamecommitdiffstats
path: root/lib/logger.js
blob: f64a6e983e4f3cfed4e3f09d3bec244fc0036706 (plain) (tree)
1
2
3
4
5
6
7
8
9


                                                     
                            
                              
                              
                                   
                           
                                 
                                    
                             
                                          
 
                                                        

                                               
 



                                    


                           




                                         































                                                                                     
 
                                                             
                 
                          

                                           
                                 



                                               
                                                   
                                              

                                  
                               
 

                                                                          
                
                                                 
         
     
                   




                                                                       
                                                  
                
  

                                                                    

                                                            
                                            
                    
 
                             







                                            
                        

                                  




                                                     
















                                                                          

   
  

                                                                

                                                   
 

                            
                           




                                                               
                                 
         



                                                     
                                 
                                                               
  
// Released under the MIT/X11 license
// http://www.opensource.org/licenses/mit-license.php
"use strict";
var urlMod = require("url");
var utilMod = require("util");
var fileMod = require("file");
var Color = require("color").Color;
var tabs = require("tabs");
var prompts = require("prompts");
var apiUtils = require("api-utils");
var xrpc = require("xmlrpc");
var myStorage = require("simple-storage");

var Logger = exports.Logger = function Logger(abbsMap) {
    this.EmptyLogsColor = new Color(0, 255, 0);
    this.FullLogsColor = new Color(0, 40, 103);

    if (!myStorage.storage.logs) {
        myStorage.storage.logs = {};
    }

    this.abbsMap = abbsMap;
};

Logger.prototype.size = function size() {
    var size = 0, key;
    for (key in myStorage.storage.logs) {
        size++;
    }
    return size;
};

Logger.prototype.isEmpty = function isEmpty() {
    return (this.size() === 0);
};

Logger.prototype.clearStore = function clearStore(clearLink) {
    myStorage.storage.logs = {};
    var size = this.size();
    clearLink.style.color = this.EmptyLogsColor;
    clearLink.style.fontWeight = "normal";
};

Logger.prototype.importOtherStore = function importOtherStore (filename, clearLink) {
    if (fileMod.exists(filename)) {
        var otherTS = JSON.parse(fileMod.read(filename));
        if (otherTS.logs) {
            for (var rec in otherTS.logs) {
                myStorage.storage.logs[rec] = otherTS.logs[rec];
            }
        } else {
            console.error("This is not a log file!");
        }
    } else {
        console.error("File " + filename + " doesn't exist!");
    }
    if (this.size() > 0) {
        clearLink.style.color = this.FullLogsColor;
        clearLink.style.fontWeight = "bolder";
    }
};

Logger.prototype.addLogRecord = function addLogRecord(page) {
    var rec = {};
    rec.date = new Date();
    rec.url = page.doc.location.toString();
    rec.title = page.title;
    var comment = prompts.prompt(
            "Enter comments for this comment");
    if (comment && comment.length > 0) {
        comment = comment.trim();
        rec.comment = comment;
        var dateStr = utilMod.getISODate(rec.date);
        var urlStr = urlMod.URL(rec.url).host;
        var recKey = dateStr + "+"
            + urlStr
            + "+" + page.bugNo;

        if (myStorage.storage.logs[recKey]) {
            myStorage.storage.logs[recKey].comment += "<br/>\n" + comment;
        } else {
            myStorage.storage.logs[recKey] = rec;
        }
    }
    return comment;
};

Logger.prototype.getBugzillaAbbr = function(url) {
    // for https://bugzilla.redhat.com/show_bug.cgi?id=579123 get RH
    // for https://bugzilla.mozilla.org/show_bug.cgi?id=579123 get MoFo
    var abbr = this.abbsMap[urlMod.URL(url).host];
    return abbr;
};

Logger.prototype.timeSheetRecordsPrinter = function(body, records) {
    var that = this;
    var commentBugRE = new RegExp("[bB]ug\\s+([0-9]+)","g");
    // sort the records into temporary array
    var tmpArr = [];

    for ( var i in records) {
        if (records.hasOwnProperty(i)) {
            tmpArr.push( [ i, records[i] ]);
        }
    }
    tmpArr.sort(function(a, b) {
        return a[0] > b[0] ? 1 : -1;
    });

    var currentDay = "";
    // now print the array
    tmpArr.forEach(function(rec) {
            var x = rec[1];
            var dayStr = utilMod.getISODate(x.date);
            var host = urlMod.URL(x.url).host;
            var BZName = that.getBugzillaAbbr(x.url);
            var bugNo = utilMod.getBugNo(x.url);
            if (dayStr != currentDay) {
                currentDay = dayStr;
                body.innerHTML += "<hr/><p><strong>" + currentDay
                        + "</strong></p>";
            }
            // replace "bug ####" with a hyperlink to the current bugzilla
            var comment = x.comment.replace(commentBugRE,
                "<a href='http://"+host+"/show_bug.cgi?id=$1'>$&</a>");
            body.innerHTML += "<p><em><a href='"
                    + x.url
                    + "'>Bug "
                    + BZName + "/" + bugNo + ": "
                    + x.title
                    + "</a>"
                    + " </em>\n<br/>" + comment + "</p>";
        });
    };

/**
 *
 */
Logger.prototype.createBlankPage = function (ttl, bodyBuildCB) {
    var title = ttl || "Yet another untitled page";
    var that = this;

    var logTab = tabs.open({
        url: "about:blank",
        inBackground: true,
        onOpen: function (tab) {
            var otherDoc = tab.contentDocument;
            otherDoc.title = title;
            otherDoc.body.innerHTML = "<h1>" + title + "</h1>";
            bodyBuildCB.call(that, otherDoc.body);
            tabs.activeTab = tab;
        }
    });
};

Logger.prototype.generateTimeSheet = function(body) {
    var doc = body.ownerDocument;
    this.timeSheetRecordsPrinter(body, myStorage.storage.logs);
};