aboutsummaryrefslogtreecommitdiffstats
path: root/lib/logger.js
blob: 7e0c51651da0a6397a5a32eb2731c595a9c2f40e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// 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 tabs = require("tabs");
var prompts = require("prompts");
var apiUtils = require("api-utils");
var xrpc = require("xmlrpc");
var myStorage = require("simple-storage");
var libbz = require("libbugzilla");

var EmptyLogsColor = "rgb(0, 255, 0)";
var FullLogsColor = "rgb(0, 40, 103)";

var abbsMap = {};

exports.initialize = function initialize(aMap) {
  if (!myStorage.storage.logs) {
    myStorage.storage.logs = {};
  }
  abbsMap = aMap;
};

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

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

function isEmpty() {
  return (storeSize() === 0);
}

exports.clearTimeSheet = function clearTimeSheet() {
  myStorage.storage.logs = {};
  var size = storeSize();
};

exports.importTimeSheet = function importTimeSheet() {
  var filename = prompts.promptFileOpenPicker();
  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!");
  }
};

function getBugzillaAbbr(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
  return abbsMap[urlMod.URL(url).host];
}

exports.generateTimeSheet = function generateTimeSheet() {
  var docHTML = timeSheetRecordsPrinter(myStorage.storage.logs);
  libbz.openURLInNewTab("data:text/html;charset=utf-8," + docHTML);
}; 

function timeSheetRecordsPrinter(records) {
  function leadingZero(n) {
    // pads a single number with a leading zero. Heh.
    var s = n.toString();
    if (s.length === 1) {
      s = "0" + s;
    }
    return s;
  }

  var commentBugRE = new RegExp("[bB]ug\\s+([0-9]+)","g");
  // sort the records into temporary array
  var tmpArr = [];
  var today = new Date();
  var todayStr = today.getFullYear()+"-"+leadingZero(today.getMonth()+1)+
    "-"+leadingZero(today.getDate());
  var outStr = '<!DOCTYPE html>' +
    "<html><head>\n"+
    "<meta charset='utf-8'/>\n"+
    "<title>TimeSheet-"+ todayStr + "</title>\n</head>\n<body>\n" +
    "<h1>TimeSheet</h1>\n";

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

  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 = getBugzillaAbbr(x.url);
      var bugNo = utilMod.getBugNo(x.url);
      if (dayStr != currentDay) {
        currentDay = dayStr;
        outStr += "<hr/><p><strong>" + currentDay
            + "</strong></p>\n";
      }
      // 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>");
      outStr += "<p><em><a href='"
          + x.url
          + "'>Bug "
          + BZName + "/" + bugNo + ": "
          + x.title
          + "</a>"
          + " </em>\n<br/>" + comment + "</p>\n";
  });
  outStr += "</body></html>";
  return outStr;
}