-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtime-calculate.js
More file actions
153 lines (134 loc) Β· 4.61 KB
/
Copy pathtime-calculate.js
File metadata and controls
153 lines (134 loc) Β· 4.61 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
// time-calculate.js
// Usage: node time-calculate.js <path-to-json-file>
// Import required Node modules
const fs = require("fs");
const path = require("path");
// Get JSON file path from command line argument
const jsonPath = process.argv[2];
if (!jsonPath) {
console.error("Usage: node time-calculate.js <path-to-json-file>");
process.exit(1);
}
// Read and parse the JSON file
const rawData = JSON.parse(fs.readFileSync(jsonPath, "utf-8") || "{}");
const weekHours = rawData["week-hours"] || 40; // Default to 40 if not defined
const logs = rawData.logs || {};
/**
* Convert time string (HH:MM) to total minutes.
* @param {string} timeStr
* @returns {number}
*/
function parseTimeToMinutes(timeStr) {
if (!timeStr || timeStr.trim() === "") return 0;
const [hours, minutes] = timeStr.split(":").map(Number);
return hours * 60 + minutes;
}
/**
* Convert total minutes to HH:MM string.
* @param {number} totalMinutes
* @returns {string}
*/
function formatMinutesToHours(totalMinutes) {
const hours = Math.floor(totalMinutes / 60);
const minutes = totalMinutes % 60;
return `${hours.toString().padStart(2, "0")}:${minutes
.toString()
.padStart(2, "0")}`;
}
/**
* Sum total minutes from an array of time strings.
* @param {string[]} timeArray
* @returns {number}
*/
function sumMinutes(timeArray) {
return timeArray.reduce(
(acc, timeStr) => acc + parseTimeToMinutes(timeStr),
0
);
}
/**
* Calculate statistics for a week:
* total time, required time, leave days, and overworked or remaining time.
*
* @param {string[]} week - Array of time strings for the week
* @param {number} weekNum - Week number
* @param {number} [leaveDays=0] - Number of leaves taken
* @returns {object} - Stats summary for the week
*/
function getWeekStats(week, weekNum, leaveDays = 0) {
const totalMinutes = sumMinutes(week);
const requiredMinutes = (weekHours / 5) * (week.length - leaveDays) * 60; // Required based on working days & leaves
const diff = totalMinutes - requiredMinutes;
// Prepare result object
const states = {
"π
Week": weekNum,
"πΌ Days Worked": week.length,
"π΄ Leave Days": leaveDays,
"β±οΈ Total Time": formatMinutesToHours(totalMinutes),
"π Required Time": formatMinutesToHours(requiredMinutes),
};
// Add either overworked or remaining time info
if (diff > 0) {
states["πͺ Overworked Time"] = formatMinutesToHours(diff);
} else if (diff < 0) {
states["π Remaining Time"] = formatMinutesToHours(-diff);
}
return states;
}
// ----- Main Execution -----
// Collect all weeks' data into arrays
const weekKeys = Object.keys(logs);
const timeList = []; // All time entries across weeks
const leavePerWeek = []; // Leave days per week
const weekArrays = []; // Each week's time entries
// Organize data from JSON structure, merging in-house-session times
const inHouse = rawData["in-house-session"] || {};
// Organize data from JSON structure
weekKeys.forEach((key, idx) => {
const week = logs[key];
const weekTime = week.time || [];
const inHouseWeek = (inHouse[key] && inHouse[key].time) || [];
// Merge each day's time
const mergedTime = weekTime.map((t, i) => {
const inHouseTime = inHouseWeek[i];
if (!inHouseTime || inHouseTime === "-") return t;
// Sum both times
const totalMins = parseTimeToMinutes(t) + parseTimeToMinutes(inHouseTime);
return formatMinutesToHours(totalMins);
});
weekArrays.push(mergedTime);
timeList.push(...mergedTime);
leavePerWeek.push(week.leave || 0);
});
// Total leave days
const totalLeave = leavePerWeek.reduce((a, b) => a + b, 0);
console.log(`\nπ Configured Week Hours: ${weekHours} hrs`);
console.log("π΄ Total Leave in days:", totalLeave);
// Total time and required time for all logs
const totalMinutesWorked = sumMinutes(timeList);
const totalRequiredMinutes =
(weekHours / 5) * (timeList.length - totalLeave) * 60;
console.log("β±οΈ Total Time:", formatMinutesToHours(totalMinutesWorked));
console.log("π Total Required:", formatMinutesToHours(totalRequiredMinutes));
// Check overworked or remaining
const totalDiff = totalMinutesWorked - totalRequiredMinutes;
if (totalDiff < 0) {
console.log("\nβ Not enough time worked.");
console.log(
"π Total this much hours remaining to be worked:",
formatMinutesToHours(-totalDiff),
"\n"
);
} else {
console.log("\nβ
Enough time worked.");
console.log(
"πͺ Total this much hours overworked:",
formatMinutesToHours(totalDiff),
"\n"
);
}
// Compute and display week-wise stats
const weekStats = weekArrays.map((week, idx) =>
getWeekStats(week, idx + 1, leavePerWeek[idx])
);
console.table(weekStats);