/
Splicer.java
91 lines (73 loc) · 3 KB
/
Splicer.java
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
/**
* Copyright 2015-2016 The Splicer Query Engine Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.turn.splicer;
import com.turn.splicer.tsdbutils.TsQuery;
import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Splicer {
private static final Logger LOG = LoggerFactory.getLogger(Splicer.class);
public static final int SLICE_SIZE = 3600;
public static final int OVERFLOW = 300;
private final TsQuery tsQuery;
public Splicer(TsQuery tsQuery) {
this.tsQuery = tsQuery;
}
/**
* Slices a query into pieces with each with time frame atmost of {@link #SLICE_SIZE}.
*
* NOTE: It must be noted that this method does not take into consideration the downsample.
*
* What does this mean? If we have a downsample of 30m, and each slice is divided into one hour
* blocks, then we will only get one point per hour slice. This will reduce the output of the
* final query by 50%. Thus, if {@link #SLICE_SIZE} is one hour, we must add some overflow
* time to accomodate for these points. By default, this is set to {@link #OVERFLOW}.
*
* This can be better tuned, than hardcoding to a fixed {@link #OVERFLOW} value. As most queries
* use a downsample of 1-5 minutes, this would mean dropping 1 out of 60-12 points respectively.
*
* @return list of queries
*/
public List<TsQuery> sliceQuery()
{
final long bucket_size = SLICE_SIZE * 1000;
final long overflow_in_millis;
if (Config.get().getBoolean("slice.overflow.enable")) {
overflow_in_millis = OVERFLOW * 1000;
} else {
overflow_in_millis = 0;
}
long startTime = tsQuery.startTime();
long endTime = tsQuery.endTime();
List<TsQuery> slices = new ArrayList<>();
long end = startTime - (startTime % bucket_size) + bucket_size;
TsQuery first = TsQuery.sliceOf(tsQuery, startTime - (startTime % bucket_size), end + overflow_in_millis);
slices.add(first);
LOG.debug("First interval is {} to {}", Const.tsFormat(first.startTime()), Const.tsFormat(first.endTime()));
while (end + bucket_size < endTime) {
TsQuery slice = TsQuery.sliceOf(tsQuery, end, end + bucket_size + overflow_in_millis);
slices.add(slice);
end = end + bucket_size;
LOG.debug("Add interval# {} from {} to {}", slices.size(),
Const.tsFormat(slice.startTime()),
Const.tsFormat(slice.endTime()));
}
slices.add(TsQuery.sliceOf(tsQuery, end, endTime));
LOG.debug("Last interval is {} to {}", Const.tsFormat(end), Const.tsFormat(endTime));
return slices;
}
}