-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
152 lines (124 loc) · 5.09 KB
/
server.js
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
147
148
149
150
151
152
var http = require('http');
var fs = require("fs");
var process = require("process");
var htmlPageTemplate = fs.readFileSync(__dirname + "/page.html", 'utf8')
var userListTemplate = "<li id='%NAME%' onclick='getUserDetail(\"%NAME%\")' class='userListItem' data-expanded='false'><a> %NAME% </a> </li>";
var userTaskInfoTemplate = "<div class='task-summary'+%TASKTIME%>Average time available per task: %TASKTIME% days. </div> <ul class='task-list'>%TASKS%</ul>";
var userTaskTemplate = "<li class='taskListItem'>Task is: \"%TASK%\" -- line %LINE% in %FILE%</li>";
var codeFile = __dirname + "\\sampleCode.js";
var code = fs.readFileSync(codeFile, 'utf8');
var daysLeftInSprint = 10;
var devList = ['Mark', 'Sandeep', 'Sara', 'Rob', 'Arunesh']
var todoMap = null;
//Scan our source files for TODO comments and extract the associated info into the ToDo maps
function buildToDoList() {
todoMap = new Map();
for (var j = 0; j < devList.length; ++j) {
todoMap.set(devList[j], []);
}
var arrayOfLines = code.match(/[^\r\n]+/g);
for (var i = 0; i < arrayOfLines.length; ++i) {
//find todo and put in the map as needed
var lineText = arrayOfLines[i];
var entry = lineText.match(/TODO[' ']*(\w+)[' ']*:[' ']*(.*)/);
if (entry !== null) {
var name = entry[1];
var msg = entry[2];
var line = (i + 1).toString();
var entryArray = todoMap.get(name);
var entryObject = { "name": name, "msg": msg, "file": codeFile, "line": line };
entryArray.push(entryObject);
}
}
}
//Perform the template instantiation for a single html entry and a map of patterns we want to replace
function templateItem(htmlTemplate, replaceMap) {
var res = htmlTemplate;
replaceMap.forEach(function (value, key) {
//Bad values that are likely an error.
//We don't want to abort but we should report them for later triage.
if (value === undefined || value === NaN || value === Infinity) {
var msg = `Potentially bad value encountered in templating -- ${value} @ timestamp ${Date.now()}.`;
console.log(msg);
//force the exit
process.exit();
}
var allregex = new RegExp(key, 'g')
res = res.replace(allregex, value);
});
return res;
}
//Take a html template and instatiate a new version of it for every map in our array
function templateList(htmlTemplate, arrayOfReplaceMaps) {
var res = [];
for (var i = 0; i < arrayOfReplaceMaps.length; ++i) {
res.push(templateItem(htmlTemplate, arrayOfReplaceMaps[i]));
}
return res;
}
//Construct the reponse for the home page
function constructHomepage() {
buildToDoList();
var userArray = [];
var totalTasks = 0;
todoMap.forEach(function (value, key) {
var userMap = new Map();
userMap.set('%NAMETASKLIST%', '/' + key);
userMap.set('%NAME%', key);
userMap.set('%COUNT%', value.length + ' ' + (value.length == 1 ? 'task' : 'tasks'));
userArray.push(userMap);
totalTasks += value.length;
});
var userlist = templateList(userListTemplate, userArray);
var pageMap = new Map();
pageMap.set('%TOTALCOUNT%', totalTasks);
pageMap.set('%USERLIST%', userlist.join('\n'));
pageMap.set('%DAYS%', daysLeftInSprint + ' ' + (daysLeftInSprint == 0 ? 'day' : 'days'));
var homepage = templateItem(htmlPageTemplate, pageMap);
return homepage;
}
//Construct the reponse for a users task page
function constructUserInfo(userTaskArray) {
var taskArray = [];
userTaskArray.forEach(function (value, key) {
var userMap = new Map();
userMap.set('%TASK%', value.msg);
userMap.set('%LINE%', value.line);
userMap.set('%FILE%', value.file);
taskArray.push(userMap);
});
var userlist = templateList(userTaskTemplate, taskArray);
var infoMap = new Map();
infoMap.set('%TASKTIME%', daysLeftInSprint / userTaskArray.length);
infoMap.set('%TASKS%', userlist.join('\n'));
var info = templateItem(userTaskInfoTemplate, infoMap);
return info;
}
function respondHome(request, response) {
var rqUrl = request.url
if (rqUrl === "/") {
response.writeHead(200, { "Content-Type": "text/html", 'Cache-control': 'no-cache' });
response.write(constructHomepage());
response.end();
}
else if (rqUrl === "/exit") {
process.exit(0);
}
else {
var user = rqUrl.slice(1);
if (!todoMap.has(user)) {
response.writeHead(200, { "Content-Type": "text/html", 'Cache-control': 'no-cache' });
response.write('Unknown User "' + user + '".');
response.end();
}
else {
var taskArray = todoMap.get(user);
response.writeHead(200, { "Content-Type": "text/html", 'Cache-control': 'no-cache' });
response.write(constructUserInfo(taskArray));
response.end();
}
}
}
var server = http.createServer(respondHome);
server.listen(1338);
console.log("Server running at http://127.0.0.1:1338/");