-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataGrouper.java
More file actions
171 lines (144 loc) · 6.32 KB
/
Copy pathDataGrouper.java
File metadata and controls
171 lines (144 loc) · 6.32 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
class SessionRecord {
String usn, name, sessionId;
LocalDateTime loginTime, logoutTime;
Map<String, String> attributes;
static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd[ ]['T']HH:mm[:ss][.SSSSSSSSS]");
public SessionRecord(String loginTime, String logoutTime, String usn, String name, Map<String, String> attributes, String sessionId) {
this.loginTime = LocalDateTime.parse(loginTime,FORMATTER );
if (logoutTime == null || logoutTime.equals("null"))
this.logoutTime = null;
else
this.logoutTime = LocalDateTime.parse(logoutTime, FORMATTER);
this.usn = usn;
this.name = name;
this.attributes = attributes;
this.sessionId = sessionId;
}
public LocalDateTime getLoginTime() {
return this.loginTime;
}
public LocalDateTime getLogoutTime() {
return this.logoutTime;
}
public String getUsn() {
return this.usn;
}
public String getName() {
return this.name;
}
public String getSessionId() {
return this.sessionId;
}
}
class SessionGroup {
public Map<String,String> attributes;
public String slot;
public String date;
public List<SessionRecord> records = new ArrayList<>();
public SessionGroup(String date, String slot, Map<String, String> attributes) {
this.date = date;
this.slot = slot;
this.attributes = attributes;
}
}
class SessionGrouper {
public static List<SessionGroup> groupSessions(List<SessionRecord> allRecords) {
// Group by date+slot+dept+sem+batch
Map<String, List<SessionRecord>> grouped = new HashMap<>();
for (SessionRecord r : allRecords) {
StringBuilder attrKey = new StringBuilder();
r.attributes.entrySet().stream()
.sorted(Map.Entry.comparingByKey())
.forEach(e -> {
if (!e.getKey().equals("SysNo")) // Do not group SysNo
attrKey.append(e.getValue()).append("_");
});
String key = attrKey.toString() + getSlot(r.getLoginTime(), r.getLogoutTime()) + r.getLoginTime().toLocalDate();
grouped.computeIfAbsent(key, k -> new ArrayList<>()).add(r);
}
List<SessionGroup> result = new ArrayList<>();
for (Map.Entry<String, List<SessionRecord>> entry : grouped.entrySet()) {
List<SessionRecord> groupRecords = entry.getValue();
// Sort by login time
groupRecords.sort(Comparator.comparing(r -> r.usn));
SessionRecord first_ = groupRecords.get(0);
// Date Comparision
DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd, EE");
LocalDate selectedFromDate = LocalDate.parse(DataPlace.dateFrom.getText(), formatter2);
LocalDate selectedToDate = (DataPlace.dateTo.isVisible())
? LocalDate.parse(DataPlace.dateTo.getText(), formatter2)
: selectedFromDate;
String recorDate = first_.getLoginTime().toLocalDate().toString();
LocalDate entryDate = LocalDate.parse(recorDate, formatter1);
// slot comparision
String slot = getSlot(first_.getLoginTime(), first_.getLogoutTime());
String[] from_to_time = slot.split("\\s*-\\s*");
LocalTime selectedFromTime = LocalTime.parse(DataPlace.startTime.getSelectedItem().toString());
String selectedToTime = DataPlace.endTime.getSelectedItem().toString();
boolean conn;
if (selectedToTime.equals("Ongoing") || from_to_time[1].equals("Ongoing"))
if (from_to_time[1].equals("Ongoing"))
conn = true;
else
conn = false;
else
conn = !LocalTime.parse(from_to_time[1]).isAfter(LocalTime.parse(selectedToTime));
boolean slotCondition = (DataPlace.matchSlotConditon)
? LocalTime.parse(from_to_time[0]).equals(selectedFromTime)
&& (from_to_time[1].equals("Ongoing") || (from_to_time[1]).equals(selectedToTime))
: !LocalTime.parse(from_to_time[0]).isBefore(selectedFromTime)
&& conn;
// Grouping
if ((DataPlace.everything.isSelected()
|| (!entryDate.isBefore(selectedFromDate) && !entryDate.isAfter(selectedToDate)))) {
if (slotCondition) {
SessionGroup sg_ = new SessionGroup(recorDate, slot, first_.attributes);
sg_.records.addAll(groupRecords);
result.add(sg_);
}
}
}
Comparator<SessionGroup> timeComparator = Comparator.comparing((SessionGroup r) -> r.date + r.slot);
result.sort(timeComparator.reversed());
return result;
}
private static String getSlot(LocalDateTime starts, LocalDateTime ends) {
LocalTime start = starts.toLocalTime();
LocalTime end = (ends == null) ? null : ends.toLocalTime();
String from;
if (!start.isBefore(LocalTime.of(16, 0))) from = "16:00 - ";
else if (!start.isBefore(LocalTime.of(15, 10))) from = "15:10 - ";
else if (!start.isBefore(LocalTime.of(14, 20))) from = "14:20 - ";
else if (!start.isBefore(LocalTime.of(13, 30))) from = "13:30 - ";
else if (!start.isBefore(LocalTime.of(12, 05))) from = "12:05 - ";
else if (!start.isBefore(LocalTime.of(11, 15))) from = "11:15 - ";
else if (!start.isBefore(LocalTime.of(10, 10))) from = "10:00 - ";
else if (!start.isBefore(LocalTime.of(9, 20))) from = "09:20 - ";
else from = "08:30 - ";
String to;
if (end == null) {
to = "Ongoing";
} else {
if (!end.isBefore(LocalTime.of(15, 20))) to = "16:00";
else if (!end.isBefore(LocalTime.of(14, 30))) to = "15:10";
else if (!end.isBefore(LocalTime.of(13, 30))) to = "14:20";
else if (!end.isBefore(LocalTime.of(12, 15))) to = "12:55";
else if (!end.isBefore(LocalTime.of(11, 15))) to = "12:05";
else if (!end.isBefore(LocalTime.of(10, 20))) to = "11:00";
else if (!end.isBefore(LocalTime.of(9, 30))) to = "10:10";
else to = "08:30";
}
return from + to;
}
}