-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprojection.js
242 lines (198 loc) · 7.36 KB
/
projection.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
const axios = require('axios');
// const { assign } = require('lodash');
async function fetchteamsPage(params, apiKey) {
const baseUrl = 'https://app.runn.io/api/v0';
try {
const response = await axios.get(`${baseUrl}/teams`, {
headers: {
Authorization: `Bearer ${apiKey}`,
},
params: params,
});
return response.data;
} catch (error) {
throw error;
}
}
async function fetchpeopleById(apiKey, options, id) {
const params = {};
if (options.include_actuals) {
params.include_actuals = options.include_actuals;
}
if (options.include_assignments) {
params.include_assignments = options.include_assignments;
}
if (options.start) {
params.start = options.start;
}
if (options.end) {
params.end = options.end;
}
const baseUrl = `https://app.runn.io/api/v0/`;
try {
const response = await axios.get(`${baseUrl}/people/${id}`, {
headers: {
Authorization: `Bearer ${apiKey}`,
},
params: params,
});
return response.data;
} catch (error) {
return undefined;
}
}
async function fetchteams(apiKey, params) {
let currentPage = 1;
const fetchedPeople = [];
let dateOfRun = new Date();
const fetchNextPage = async () => {
params.page = currentPage;
try {
// console.log("currentPage",currentPage);
const response = await fetchteamsPage(params, apiKey);
fetchedPeople.push(...response);
if (response.length > 0) {
currentPage++;
if (currentPage % 120 == 0) {
const SecondToComplteMinute = 60000 - (new Date().getTime() / 1000 - dateOfRun.getTime() / 1000);
if (SecondToComplteMinute > 0) {
await new Promise(resolve => setTimeout(resolve, SecondToComplteMinute));
}
dateOfRun = new Date();
}
await fetchNextPage();
}
} catch (error) {
console.error('An error occurred:', error);
}
};
await fetchNextPage();
return fetchedPeople;
}
function formatDate(date) {
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
return `${year}-${month}-${day}`;
}
function getMonthRange(year, monthOffset) {
const firstDayOfMonth = new Date(year, monthOffset, 1);
const lastDayOfMonth = new Date(year, monthOffset + 1, 0);
return [formatDate(firstDayOfMonth), formatDate(lastDayOfMonth)];
}
function getNextMonths(manymonths) {
const today = new Date();
const currentYear = today.getFullYear();
const currentMonth = today.getMonth();
const monthNames = [
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
];
const nextMonths = [];
for (let i = 0; i < manymonths; i++) {
const monthOffset = currentMonth + i;
const yearOffset = Math.floor(monthOffset / 12);
const monthIndex = monthOffset % 12;
const year = currentYear + yearOffset;
const [startDate, endDate] = getMonthRange(year, monthIndex);
const monthName = monthNames[monthIndex];
const workingDaysCount = getWorkingDaysCount(new Date(startDate), new Date(endDate));
nextMonths.push({ month: monthName, start: startDate, end: endDate, workingDaysCount: workingDaysCount });
}
return nextMonths;
}
function parseDateString(dateString) {
const year = parseInt(dateString.substring(0, 4));
const month = parseInt(dateString.substring(4, 6)) - 1;
const day = parseInt(dateString.substring(6, 8));
return new Date(year, month, day);
}
function getWorkingDaysCount(startDate, endDate) {
let workingDaysCount = 0;
let currentDate = new Date(startDate);
while (currentDate <= endDate) {
const dayOfWeek = currentDate.getDay();
// Check if the day is a working day (Monday to Friday)
if (dayOfWeek >= 1 && dayOfWeek <= 5) {
workingDaysCount++;
}
currentDate.setDate(currentDate.getDate() + 1); // Move to the next day
}
return workingDaysCount;
}
// async function decimalToHHMM(decimal) {
// // Separate integer and decimal parts
// const integerPart = Math.floor(decimal);
// const decimalPart = decimal - integerPart;
// // Calculate hours and minutes
// const hours = integerPart;
// const minutes = Math.round(decimalPart * 60);
// // Format as HH:MM
// const HH = String(hours).padStart(2, '0');
// const MM = String(minutes).padStart(2, '0');
// return `${HH}:${MM}`;
// }
async function projection(id, options, config) {
const nextMonths = getNextMonths(options.months);
const params = {};
let startday = "";
let endday = "";
params.include_assignments = true;
const allprojection = [];
const teams = await fetchteams(config.apikey, {});
let peoplelist = {};
for (const team of teams) {
if (["wposey4w", "9w4s8om9"].includes(team.id)) {
continue;
}
peoplelist[team.name] = [];
const people = team.people;
for (const person of people) {
if (peoplelist[team.name].includes(person)) {
continue;
}
else {
peoplelist[team.name].push(person);
}
}
}
// console.log(peoplelist);
for (const team of Object.keys(peoplelist)) {
if (options.byteam)
console.log("Team :", team);
// for (const person of peoplelist[team]) {
// }
const teamprojection = [];
for (const person of peoplelist[team]) {
let persondata = {};
for (const month of nextMonths) {
persondata.name = person.name;
let totalHours = 0;
params.start = month.start;
params.end = month.end;
const fetchePeople = await fetchpeopleById(config.apikey, params, person.id);
for (const assignment of fetchePeople.assignments) {
if (new Date(parseDateString(assignment.start_date)) < new Date(month.start))
startday = new Date(month.start);
else
startday = new Date(parseDateString(assignment.start_date));
if (new Date(parseDateString(assignment.end_date)) > new Date(month.end))
endday = new Date(month.end);
else
endday = new Date(parseDateString(assignment.end_date));
const workingDaysCount = getWorkingDaysCount(startday, endday);
totalHours += workingDaysCount * assignment.minutes_per_day / 60;
}
const procentage = totalHours / (month.workingDaysCount * 7.5) * 100;
persondata[month.month] = procentage.toFixed(0) + "%";
}
allprojection.push(persondata);
if (options.byteam)
teamprojection.push(persondata);
}
if (options.byteam)
console.table(teamprojection);
}
return allprojection;
}
module.exports = projection;