-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstats.js
275 lines (238 loc) · 10.6 KB
/
stats.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
exports.update = function (tName, tCode, tId, tHourly) {
console.log("Stats for board name:", tName);
var config = require('./config');
var http = require('http');
var request = require('request');
var MongoClient = require('mongodb').MongoClient;
var assert = require('assert');
// data
var mainData = "";
var checkData = "";
var mainName = "??";
var cardCount = 0;
var totalCards = 0;
var totalTodo = 0;
var totalDone = 0;
var plainCards = 0;
// initialised to show the structure
var statData = {
name: "board name",
logDate: "",
id: 1,
plainCards: 0,
totalCards: 0,
totalDone: 0,
totalTodo: 0,
lists: [
{
name: "list one",
id: 11,
cards: [
{
name: "card 1",
id: 111,
done: 0,
todo: 0,
checklists: [
{
name: "check 1",
id: "1234"
},
{
name: "check 2",
id: "2345" }
]
},
{
name: "card 2",
id: 112,
done: 0,
todo: 0,
checklists: []
}
]
},
{
name: "list two",
id: 12,
cards: []}
]
};
statData.lists = [];
// Connection URL
var db = process.env.MONGODB_URI || 'mongodb://localhost:27017/myproject';
// Use connect method to connect to the server
MongoClient.connect(db, function(err, db) {
assert.equal(null, err);
// console.log("STATS Connected to server");
var boardName = tName;
var boardCode = tCode;
var boardId = tId;
var hourly = tHourly;
var options = "cards=open&card_fields=idChecklists,name,labels&idChecklists=all&checkItem_fields=name";
var url = "https://api.trello.com/1/boards/" + boardId + "/lists?" + options + "&key=" + process.env.TRELLO_KEY + "&token=" + process.env.TRELLO_TOKEN;
// console.log("Url", url);
request(url, function (error, response, body) {
// console.log("Response code: " + response.statusCode + ", " + url);
console.log("Response code: " + response.statusCode);
if (error) {
console.log("Error: " + error);
}
if (!error && response.statusCode === 200) {
// Use body; no need to handle chunks of data *or* redirects!
// console.log("Length: " + body.length + "\n");
mainData = JSON.parse(body);
console.log("TRELLO 1...processing");
// calculate the stats
if (Array.isArray(mainData)) {
if (mainData.length > 0) {
for (var i=0; i<mainData.length; i++) {
mainName = mainData[i].name;
cardCount = mainData[i].cards.length;
totalCards += cardCount;
statData.lists.push({
name: mainName,
id: mainData[i].id,
cards: []
});
if (cardCount > 0) {
for (var j=0; j<cardCount; j++) {
statData.lists[i].cards.push({
name: mainData[i].cards[j].name,
id: mainData[i].cards[j].id,
done: 0,
todo: 0,
checklists: [],
labels: []
});
// for each card, add colours of labels
if (mainData[i].cards[j].labels.length > 0) {
for (var k=0; k<mainData[i].cards[j].labels.length; k++) {
statData.lists[i].cards[j].labels.push({
name: mainData[i].cards[j].labels[k].name,
colour: mainData[i].cards[j].labels[k].color
});
}
}
// for each card, add id of checklists
if (mainData[i].cards[j].idChecklists.length > 0) {
for (var ck=0; ck<mainData[i].cards[j].idChecklists.length; ck++) {
statData.lists[i].cards[j].checklists.push({
id: mainData[i].cards[j].idChecklists[ck]
});
}
}
else {
// have a card with no checklists
plainCards++;
}
}
}
console.log(mainName, "cards, ", cardCount);
}
}
}
// then read another url, to get checklist data
// for each checklist, save items done/todo
url = "https://api.trello.com/1/boards/" + boardId + "/checklists?" + "&key=" + process.env.TRELLO_KEY + "&token=" + process.env.TRELLO_TOKEN;
// console.log(url);
request(url, function (error, response, body) {
console.log("TRELLO 2nd response code: " + response.statusCode);
if (error) {
console.log("Error: " + error);
}
if (!error && response.statusCode === 200) {
// Use body; no need to handle chunks of data *or* redirects!
// console.log("Length: " + body.length + "\n");
// console.log(JSON.stringify(JSON.parse(body),null, 2));
checkData = JSON.parse(body);
// if respoonse has array
if (Array.isArray(checkData)) {
// if array > 0 length
if (checkData.length > 0) {
// for each object,
for (var i=0; i<checkData.length; i++) {
checkItems = checkData[i].checkItems.length;
// this should have items to be in the list, but check anyway
if (checkItems > 0) {
for (var j=0; j<checkItems; j++) {
totalTodo++;
if (checkData[i].checkItems[j].state == "complete") totalDone++;
// find card with this checklist id, get list and card indexes
cardId = checkData[i].idCard;
var bla = findCardById(statData, cardId);
if (bla) {
bla.todo++;
if (checkData[i].checkItems[j].state == "complete") bla.done++;
} else {
console.log("card id not found:", cardId);
}
}
// update todo/done based on state == incomplete/complete
}
}
}
}
// format the date/time
var date = new Date();
var day = "0" + date.getDate();
day = day.substring(day.length - 2);
var monthIndex = date.getMonth();
var month = "0" + (monthIndex+1);
month = month.substring(month.length - 2);
var year = date.getFullYear();
var hour = "0" + date.getHours();
hour = hour.substring(hour.length - 2);
var minute = date.getMinutes();
var seconds = date.getSeconds();
var fdate = year + '-' + month + '-' + day;
if (hourly) fdate = fdate + '-' + hour;
// var fdate = year + '-' + month + '-' + day + '-' + hour;
statData.id = boardId;
statData.id = boardCode;
statData.name = boardName;
statData.logDate = fdate;
statData.totalCards = totalCards;
statData.totalDone = totalDone;
statData.totalTodo = totalTodo;
statData.plainCards = plainCards;
console.log("done/todo: ", totalDone, totalTodo);
console.log("plain/cards: ", plainCards, totalCards);
console.log(fdate, minute);
// col.updateOne({a:3}, {$set: {b: 2}}, {
// write/update the document - {title:mainName, code:boardCode, date: fdate, cards:cardCount}
db.collection('counts').updateOne(
{code:boardCode, logDate:fdate},
{$set: statData},
{upsert: true},
function(err, r) {
if (err){
console.warn("Upsert error: " + err.message); // returns error if no matching object found
}else{
console.log("rc:", r.upsertedCount);
assert.equal(null, err);
// assert.equal(1, r.upsertedCount);
console.log(r.upsertedCount + " written...");
}
db.close();
});
}
});
}
});
});
};
function findCardById(root, id) {
if (root.lists.length > 0) {
for (var k in root.lists) {
if (root.lists[k].cards.length > 0) {
for (var c in root.lists[k].cards) {
// console.log(root.lists[k].cards[c].id);
if (root.lists[k].cards[c].id == id) {
return root.lists[k].cards[c];
}
}
}
}
}
}