-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10_25_2024.js
302 lines (255 loc) · 10 KB
/
10_25_2024.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
Type Employee:
{
start_day: number; // The day the employee started working at the company (in days)
trained_day?: number; // The day the employee was trained (in days), optional
}
Type Status:
{
name: string; // one of ["not_required", "pending", "overdue", "completed"]
days_overdue: number; // 0 if not overdue
}
*/
/**
* getTrainingStatus is a function that evaluates an employee's training status on a specified check_day.
*
* @param {Employee} employee
* @param {number} training_window - The number of days an employee has to complete their training after their start day.
* @param {number} check_day - The day for which we are checking the employee's training status.
*
* @returns {Status} - The employee's training status on the check_day.
*/
function getTrainingStatus(employee, training_window, check_day) {
const { start_day, trained_day } = employee;
// not required:
if (check_day < start_day) {
return {
name: "not_required",
days_overdue: 0,
}
}
if (!!trained_day && check_day >= trained_day) {
//completed, don't care about days overdue
return {
name: "completed",
days_overdue: 0,
};
} else {
let lastDayToNotBeLate = start_day + training_window
// pending if within training window
if (check_day <= lastDayToNotBeLate) {
return {
name: "pending",
days_overdue: 0,
}
} else {
// over if beyond training window
// console.log(getTrainingStatus({ start_day: 100, trained_day: 109 }, 5, 106))
return {
name: "overdue",
days_overdue: check_day - lastDayToNotBeLate,
}
}
}
}
/**
Example 1:
training_window: 10 days
employee: {start_day: 100}
check_day: 104
result: {name: "pending", days_overdue: 0}
|<-----------------------training window------------------->|
Time: |-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|
Days: 99 100 101 102 103 104 105 106 107 108 109 110 111
| |
start_day check_day
**/
// console.log('Expect pending w no overdue days')
// console.log(getTrainingStatus({ start_day: 100 }, 10, 104))
/**
Example 2:
training_window: 10 days
employee: {start_day: 100, trained_day: 105}
check_day: 110
result: {name: "completed", days_overdue: 0}
|<-----------------------training window------------------->|
Time: |-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|
Days: 99 100 101 102 103 104 105 106 107 108 109 110 111
| | |
start_day trained_day check_day
**/
// console.log('Expect completed w no overdue days')
// console.log(getTrainingStatus({ start_day: 100, trained_day: 105 }, 10, 110))
/**
Example 3:
training_window: 5 days
employee: {start_day: 100, trained_day: 107}
check_day: 110
result: {name: "completed", days_overdue: 0}
|<------training window------>|
Time: |-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|
Days: 99 100 101 102 103 104 105 106 107 108 109 110 111
| | |
start_day trained_day check_day
**/
// console.log('Expect completed w no overdue days pt II')
// console.log(getTrainingStatus({ start_day: 100, trained_day: 107 }, 5, 110))
/**
Example 4:
training_window: 5 days
employee: {start_day: 100, trained_day: 109}
check_day: 106
result: {name: "overdue", days_overdue: 1}
|<------training window------>|
Time: |-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|
Days: 99 100 101 102 103 104 105 106 107 108 109 110 111
| | |
start_day check_day trained_day
(employee, training_window, check_day) -> (status)
{"start_day": 100 }, 10, 99 -> {"name": "not_required", "days_overdue": 0}
{"start_day": 100 }, 10, 104 -> {"name": "pending", "days_overdue": 0}
{"start_day": 100, "trained_day": 105}, 10, 110 -> {"name": "completed", "days_overdue": 0}
{"start_day": 100, "trained_day": 107}, 5, 110 -> {"name": "completed", "days_overdue": 0}
{"start_day": 100, "trained_day": 109}, 5, 106 -> {"name": "overdue", "days_overdue": 1}
*/
// console.log('Expect overdue with 1 day late')
// console.log(getTrainingStatus({ start_day: 100, trained_day: 109 }, 5, 106))
// console.log('Expect not required, 0 days overdue')
// console.log(getTrainingStatus({ start_day: 100 }, 10, 99))
// console.log('Expect overdue w 5 overdue days')
// console.log(getTrainingStatus({ start_day: 100 }, 10, 115))
// console.log('Expect pending w 0 overdue days')
// console.log(getTrainingStatus({ start_day: 100, trained_day: 115 }, 10, 102))
// console.log('Expect pending w 0 overdue days')
// console.log(getTrainingStatus({ start_day: 100, trained_day: 103 }, 10, 102))
// console.log('Expect completed w 0 overdue days')
// console.log(getTrainingStatus({ start_day: 100, trained_day: 103 }, 10, 108))
// console.log('Expect completed w 0 overdue days')
// console.log(getTrainingStatus({ start_day: 100, trained_day: 103 }, 10, 103))
// console.log('Expect pending w 0 overdue days')
// console.log(getTrainingStatus({ start_day: 100 }, 10, 110))
/*
Employee:
{
...
group_id: string, // Indicates the group to which the employee belongs
}
Group:
{
id: string, // Unique identifier for the group
parent_id: string (optional), // ID of the parent group, if any
child_ids: Array<string>, // IDs of the child groups
}
Datapoint:
{
group_id: string,
num_employees: number,
total_days_overdue: number,
}
*/
/**
* This function returns the total days overdue for training completion for each
* group, considering both direct and indirect (i.e., through sub-groups)
* employee memberships.
*
* @param {Array<Employee>} employees
* @param {Object<string, Group>} groupsById - A dictionary mapping a group id to the corresponding Group.
* @param {number} trainingWindow - Duration (number of days) employees have to complete their training.
* @param {number} checkDay - The day for which we are checking the training statuses.
*
* @return {Array<Datapoint>} - A list of Datapoints, one for each Group.
*/
// [
// { group_id: "a", num_employees: 4, total_days_overdue: 20 },
// { group_id: "b", num_employees: 3, total_days_overdue: 10 },
// { group_id: "c", num_employees: 1, total_days_overdue: 5 }
// ]
/*
daysLatePerGroup = {
'a': {
totalOverDueForGroup: NUMBER,
employeeCount: NUMBER
}
}
*/
// visitedLevel = [['a','b'],[]];
// function updatedWithGroupings(daysLatePerGroup, groupsById, visitedLevel) {
// // base case => parentLevel and children level are visited
// let finalGroupedArray = [];
// // resursive
// // daysLatePerGroup[parent_level] + daysLatePerGroup[currentLevel] + daysLatePerGroup[childLevel1, childLevel2]
// return finalGroupedArray;
// }
function getTotalDaysOverdueByGroups(
employees,
groupsById,
trainingWindow,
checkDay
) {
let daysLatePerGroup = {};
// getTrainingStatus(employee, training_window, check_day)
for (let employee of employees) {
let employeeTrainingStatus = getTrainingStatus(employee, trainingWindow, checkDay)
let potentialDaysOverdue = employeeTrainingStatus.days_overdue;
let { group_id } = employee;
if (daysLatePerGroup[group_id]) {
daysLatePerGroup[group_id]['totalOverDueForGroup'] += potentialDaysOverdue;
daysLatePerGroup[group_id]['employeeCount'] += 1;
} else {
daysLatePerGroup[group_id] = {
'totalOverDueForGroup': potentialDaysOverdue,
'employeeCount': 1
};
}
// we know c has 5 days overdue
// c should go update b
// only need to update my parents!
let currentGroupId = groupsById[group_id]['parent_id'];
while (currentGroupId) {
console.log(`visiting ${group_id} and I'm updating group level ${currentGroupId}`)
// update
daysLatePerGroup[currentGroupId]['totalOverDueForGroup'] += potentialDaysOverdue;
daysLatePerGroup[currentGroupId]['employeeCount'] += 1;
currentGroupId = groupsById[currentGroupId]['parent_id'];
}
}
// daysLatePerGroup = {
// 'a': {
// totalOverDueForGroup: NUMBER,
// employeeCount: NUMBER
// }
// }
console.log({ daysLatePerGroup });
return daysLatePerGroup;
}
// Example:
const trainingWindow = 10;
const checkDay = 120;
// Note: In this example, no one did training!
// { start_day: 100, group_id: "a", trained_day: 100 }
const employees = [
{ start_day: 100, group_id: "a" }, // days_overdue: 10
{ start_day: 105, group_id: "b" }, // days_overdue: 5
{ start_day: 110, group_id: "b" }, // days_overdue: 0
{ start_day: 105, group_id: "c" }, // days_overdue: 5
];
const groupsById = {
a: { id: "a", child_ids: ["b"] },
b: { id: "b", parent_id: "a", child_ids: ["c"] },
c: { id: "c", parent_id: "b", child_ids: [] },
};
const result = getTotalDaysOverdueByGroups(
employees,
groupsById,
trainingWindow,
checkDay
);
console.log(result);
/*
Expected Output:
[
{ group_id: "a", num_employees: 4, total_days_overdue: 20 },
{ group_id: "b", num_employees: 3, total_days_overdue: 10 },
{ group_id: "c", num_employees: 1, total_days_overdue: 5 }
]
*/