This repository was archived by the owner on Oct 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWeeklyTimeWindows.java
More file actions
72 lines (59 loc) · 2.22 KB
/
WeeklyTimeWindows.java
File metadata and controls
72 lines (59 loc) · 2.22 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
package kafkastreams_dsp.windows;
import org.apache.kafka.streams.kstream.internals.TimeWindow;
import java.time.Duration;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.LinkedHashMap;
import java.util.Map;
import static utility.DataCommonTransformation.getCalendarAtTime;
/**
* Implementation of a weekly custom window with a given timezone
*/
public class WeeklyTimeWindows extends CustomTimeWindows {
private final static long SIZE_IN_MILLIS = Duration.ofDays(7L).toMillis();
/**
* Default constructor
* @param zoneId to be setted
* @param grace Duration representing the grace period
*/
@SuppressWarnings("deprecation")
public WeeklyTimeWindows(final ZoneId zoneId, final Duration grace) {
super(zoneId, grace);
// set up retention time
this.until(SIZE_IN_MILLIS + grace.toMillis());
}
/**
* Function that assign an event to the correct weekly window based on the timestamp
* @param timestamp of the event
* @return the map of the windows
*/
@Override
public Map<Long, TimeWindow> windowsFor(final long timestamp) {
final Instant instant = Instant.ofEpochMilli(timestamp);
final ZonedDateTime zonedDateTime = instant.atZone(zoneId);
// get first instant of the current week (monday-sunday)
final Calendar calendar = getCalendarAtTime(toEpochMilli(zonedDateTime));
calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
final long startTime = calendar.getTimeInMillis();
// get last instant of the current week
calendar.add(Calendar.WEEK_OF_YEAR, 1);
final long endTime = calendar.getTimeInMillis() - 1;
final Map<Long, TimeWindow> windows = new LinkedHashMap<>();
windows.put(startTime, new TimeWindow(startTime, endTime));
return windows;
}
/**
* Getter for the size
* @return the size of the window
*/
@Override
public long size() {
return SIZE_IN_MILLIS;
}
}