-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyTime.js
More file actions
153 lines (134 loc) · 4.41 KB
/
Copy pathmyTime.js
File metadata and controls
153 lines (134 loc) · 4.41 KB
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
let request = require('request');
var moment = require('moment');
let url = process.env.url + "user/_doc/_search";
let postGetTimeDays = function (id, days, callback) {
let document = {
json: {
"from": 0,
"size": 10000,
"query": {
"bool": {
"must": [
{
"query_string": {
"default_field": "id",
"query": id
}
},
{
"range": {
"timestamp": {
"gte": "now-" + days + "d/d",
"lt": "now/d"
}
}
}
]
}
},
"sort": [
{"timestamp": {"order": "desc"}}
]
}
};
request.post(
url,
document,
function (error, response, body) {
if (userName !== "*") {
//console.log(body);
callback(getTimeFromRequest(body.hits.hits));
} else
callback(getTimeFromRequestAllDiscord(body.hits.hits));
}
);
};
let postGetAllTime = function (userName, callback) {
let document = {
json: {
"from": 0,
"size": 10000,
"query": {
"bool": {
"must": [
{
"query_string": {
"default_field": "id",
"query": id
}
}
]
}
},
"sort": [
{"timestamp": {"order": "desc"}}
]
}
};
request.post(
url,
document,
function (error, response, body) {
//console.log(body);
callback(getTimeFromRequest(body.hits.hits));
}
);
};
let getTimeFromRequest = function (json) {
let totalTime = 0;
for (let i = 0; i < json.length; i = i + 2) {
//skip if people are actually on the server
if (i + 1 < json.length && i === 0 && json[i]._source.action === "join") {
i++;
console.log("skip");
}
if (i + 1 < json.length && json[i]._source.action === "leave" && json[i + 1]._source.action === "join") {
//console.log("i = ",i , " : " , json[i]._source.action + " channel = " + json[i]._source.channel);
//console.log("i + 1 = ", json[i + 1]._source.action + " channel = " + json[i + 1]._source.channel);
let date1 = moment(json[i]._source.timestamp)
let date2 = moment(json[i + 1]._source.timestamp)
var duration = moment.duration(date1.diff(date2));
var min = duration.asMinutes();
totalTime += Math.round(min);
console.log(date1, " - ", date2, " duration : " , duration.asMinutes(), " total : ", totalTime);
//console.log(date1)
//console.log(date2)
//console.log(Math.round(min))
}
}
console.log(totalTime);
return convertMinsToHrsMins(totalTime);
};
let getTimeFromRequestAllDiscord = function (json) {
let find = 0;
let tab = {};
for (let i = 0; i < json.length; i++) {
for (let y = 0; y < Object.keys(tab).length; y++) {
if (Object.keys(tab)[y] === json[i]._source.userName)
find = 1
}
if (find === 0)
tab[json[i]._source.userName] = [];
else
find = 0;
tab[json[i]._source.userName].push(json[i]);
}
//console.log(JSON.stringify(tab));
let finalTab = [];
for (let key in tab) {
let value = tab[key];
finalTab.push(key + ";" + getTimeFromRequest(value));
}
return finalTab.sort();
};
function convertMinsToHrsMins(mins) {
let h = Math.floor(mins / 60);
let m = mins % 60;
h = h < 10 ? '0' + h : h;
m = m < 10 ? '0' + m : m;
return `${h}:${m}`;
}
module.exports.postGetTimeDays = postGetTimeDays;
module.exports.postGetAllTime = postGetAllTime;
module.exports.getTimeFromRequest = getTimeFromRequest;
module.exports.getTimeFromRequestAllDiscord = getTimeFromRequestAllDiscord;