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 pathMonthlyTimeWindows.java
More file actions
71 lines (60 loc) · 2.09 KB
/
MonthlyTimeWindows.java
File metadata and controls
71 lines (60 loc) · 2.09 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
package kafkastreams_dsp.windows;
import org.apache.kafka.streams.kstream.internals.TimeWindow;
import utility.DataCommonTransformation;
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;
/**
* Implementation of a monthly custom window with a given timezone
*/
public class MonthlyTimeWindows extends CustomTimeWindows {
// maximum size is of 31 days, it's shorter for February, June, September, November
private final static long SIZE_IN_MILLIS = Duration.ofDays(31L).toMillis();
/**
* Default constructor
* @param zoneId to be setted
* @param grace Duration representing the grace period
*/
@SuppressWarnings("deprecation")
public MonthlyTimeWindows(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 monthly 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 the first instant of the current month
final Calendar calendar = DataCommonTransformation.getCalendarAtTime(toEpochMilli(zonedDateTime));
calendar.set(Calendar.DAY_OF_MONTH, 1);
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 the last instant of the current month
calendar.add(Calendar.MONTH, 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;
}
}