-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathDPLParserConfig.java
More file actions
200 lines (183 loc) · 7.01 KB
/
DPLParserConfig.java
File metadata and controls
200 lines (183 loc) · 7.01 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
* Teragrep Data Processing Language (DPL) translator for Apache Spark (pth_10)
* Copyright (C) 2019-2024 Suomen Kanuuna Oy
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*
* Additional permission under GNU Affero General Public License version 3
* section 7
*
* If you modify this Program, or any covered work, by linking or combining it
* with other code, such other code is not for that reason alone subject to any
* of the requirements of the GNU Affero GPL version 3 as long as this Program
* is the same Program as licensed from Suomen Kanuuna Oy without any additional
* modifications.
*
* Supplemented terms under GNU Affero General Public License version 3
* section 7
*
* Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified
* versions must be marked as "Modified version of" The Program.
*
* Names of the licensors and authors may not be used for publicity purposes.
*
* No rights are granted for use of trade names, trademarks, or service marks
* which are in The Program if any.
*
* Licensee must indemnify licensors and authors for any liability that these
* contractual assumptions impose on licensors and authors.
*
* To the extent this program is licensed as part of the Commercial versions of
* Teragrep, the applicable Commercial License may apply to this file if you as
* a licensee so wish it.
*/
package com.teragrep.pth10.ast;
import com.teragrep.pth10.ast.commands.transformstatement.timechart.span.TimeRange;
import com.teragrep.pth10.ast.time.RelativeTimeParser;
import com.teragrep.pth10.ast.time.RelativeTimestamp;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.sql.Timestamp;
import java.util.LinkedHashMap;
import java.util.Map;
import static java.lang.Math.abs;
/**
* Configuration for parser/UI data transfer. For instance latest/earliest values used for default span/time window
* calculation and map which can contain additional named values.
*/
public class DPLParserConfig {
private static final Logger LOGGER = LoggerFactory.getLogger(DPLParserConfig.class);
private Map<String, Object> config = new LinkedHashMap<>();
/**
* Get named value from config map
*
* @param key Name string
* @return value
*/
public Object get(String key) {
return config.get(key);
}
/**
* Put named value object into the map
*
* @param key name string
* @param value as object. String/int/...
*/
public void put(String key, Object value) {
config.put(key, value);
}
/**
* Get earliest flag which is used when calculating window ranges for different spans.
*
* @return earliest as string value
*/
public String getEarliest() {
return (String) config.get("earliest");
}
/**
* Set earliest flag with given string, If flag is relative, calculate it relative to current now-instance. Store
* also that calculated value as epoch
*
* @param earliest string value like -1h or actual timestamp
*/
public void setEarliest(String earliest) {
long earliestEpoch = 0;
Timestamp now = new Timestamp(System.currentTimeMillis());
RelativeTimeParser rtParser = new RelativeTimeParser();
// Try to check if it is relative and catch exception
try {
RelativeTimestamp rtTimestamp = rtParser.parse(earliest); // can throw error if not relative timestamp
earliestEpoch = rtTimestamp.calculate(now);
}
catch (NumberFormatException ne) {
// absolute time
earliestEpoch = new DefaultTimeFormat().getEpoch(earliest);
}
config.put("earliest", earliest);
config.put("earliestEpoch", earliestEpoch);
}
/**
* Get latest flag which is used when calculating window ranges for different spans.
*
* @return latest as string value
*/
public String getLatest() {
return (String) config.get("latest");
}
/**
* Set latest flag with given string, If flag is relative, calculate it relative to current now-instance. Store also
* that calculated value as epoch
*
* @param latest string value like -1h or actual timestamp
*/
public void setLatest(String latest) {
long latestEpoch = 0;
Timestamp now = new Timestamp(System.currentTimeMillis());
RelativeTimeParser rtParser = new RelativeTimeParser();
// Try to check if it is relative and catch exception
try {
RelativeTimestamp rtTimestamp = rtParser.parse(latest); // can throw exception if not relative timestamp
latestEpoch = rtTimestamp.calculate(now);
}
catch (NumberFormatException ne) {
// absolute time
latestEpoch = new DefaultTimeFormat().getEpoch(latest);
}
config.put("latest", latest);
config.put("latestEpoch", latestEpoch);
}
/**
* Use config map and calculate default time range according to it.
*
* @return enum range values 10s,...,1M
*/
public TimeRange getTimeRange() {
TimeRange timeRange = new TimeRange("1 days");
long r = 0;
// Earliest set, latest not
if (config.get("earliest") != null && config.get("latest") == null) {
r = System.currentTimeMillis() - (long) config.get("earliestEpoch");
}
else if (config.get("latest") != null && config.get("earliest") == null) {
r = (long) config.get("latestEpoch");
}
else if (config.get("earliest") != null && config.get("latest") != null) {
// Both set
// Calculate time range according to latest-earliest
LOGGER.info("config=<[{}]>", config);
r = (long) config.get("latestEpoch") - (long) config.get("earliestEpoch");
}
if (r < 0)
r = abs(r);
LOGGER.info("Calculated range=<{}>", r);
if (r <= 15 * 60) {
timeRange = new TimeRange("10 seconds");
}
else if (r <= 60 * 60) {
timeRange = new TimeRange("1 minutes");
}
else if (r <= 4 * 60 * 60) {
timeRange = new TimeRange("5 minutes");
}
else if (r <= 24 * 60 * 60) {
timeRange = new TimeRange("30 minutes");
}
else if (r > 30 * 24 * 60 * 60) {
// Default max value is 1 day
timeRange = new TimeRange("1 days");
}
return timeRange;
}
}