-
-
Notifications
You must be signed in to change notification settings - Fork 468
Expand file tree
/
Copy pathCheckIn.java
More file actions
254 lines (217 loc) · 7.41 KB
/
CheckIn.java
File metadata and controls
254 lines (217 loc) · 7.41 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package io.sentry;
import io.sentry.protocol.SentryId;
import io.sentry.vendor.gson.stream.JsonToken;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/** A check-in for a monitor (CRON). */
public final class CheckIn implements JsonUnknown, JsonSerializable {
private final @NotNull SentryId checkInId;
private @NotNull String monitorSlug;
private @NotNull String status;
private @Nullable Double duration; // in seconds
private @Nullable String release;
private @Nullable String environment;
private final @NotNull MonitorContexts contexts = new MonitorContexts();
private @Nullable MonitorConfig monitorConfig;
private @Nullable Map<String, Object> unknown;
public CheckIn(final @NotNull String monitorSlug, final @NotNull CheckInStatus status) {
this(null, monitorSlug, status.apiName());
}
public CheckIn(
final @Nullable SentryId id,
final @NotNull String monitorSlug,
final @NotNull CheckInStatus status) {
this(id, monitorSlug, status.apiName());
}
@ApiStatus.Internal
public CheckIn(
final @Nullable SentryId checkInId,
final @NotNull String monitorSlug,
final @NotNull String status) {
this.checkInId = checkInId == null ? new SentryId() : checkInId;
this.monitorSlug = monitorSlug;
this.status = status;
}
// JsonKeys
public static final class JsonKeys {
public static final String CHECK_IN_ID = "check_in_id";
public static final String MONITOR_SLUG = "monitor_slug";
public static final String STATUS = "status";
public static final String DURATION = "duration";
public static final String RELEASE = "release";
public static final String ENVIRONMENT = "environment";
public static final String CONTEXTS = "contexts";
public static final String MONITOR_CONFIG = "monitor_config";
}
public @NotNull SentryId getCheckInId() {
return checkInId;
}
public @NotNull String getMonitorSlug() {
return monitorSlug;
}
public void setMonitorSlug(@NotNull String monitorSlug) {
this.monitorSlug = monitorSlug;
}
public @NotNull String getStatus() {
return status;
}
public void setStatus(@NotNull String status) {
this.status = status;
}
public void setStatus(@NotNull CheckInStatus status) {
this.status = status.apiName();
}
public @Nullable Double getDuration() {
return duration;
}
public void setDuration(@Nullable Double duration) {
this.duration = duration;
}
public @Nullable String getRelease() {
return release;
}
public void setRelease(@Nullable String release) {
this.release = release;
}
public @Nullable String getEnvironment() {
return environment;
}
public void setEnvironment(@Nullable String environment) {
this.environment = environment;
}
public @Nullable MonitorConfig getMonitorConfig() {
return monitorConfig;
}
public void setMonitorConfig(@Nullable MonitorConfig monitorConfig) {
this.monitorConfig = monitorConfig;
}
public @NotNull MonitorContexts getContexts() {
return contexts;
}
// JsonUnknown
@Override
public @Nullable Map<String, Object> getUnknown() {
return unknown;
}
@Override
public void setUnknown(@Nullable Map<String, Object> unknown) {
this.unknown = unknown;
}
// JsonSerializable
@Override
public void serialize(final @NotNull ObjectWriter writer, final @NotNull ILogger logger)
throws IOException {
writer.beginObject();
writer.name(JsonKeys.CHECK_IN_ID);
checkInId.serialize(writer, logger);
writer.name(JsonKeys.MONITOR_SLUG).value(monitorSlug);
writer.name(JsonKeys.STATUS).value(status);
if (duration != null) {
writer.name(JsonKeys.DURATION).value(duration);
}
if (release != null) {
writer.name(JsonKeys.RELEASE).value(release);
}
if (environment != null) {
writer.name(JsonKeys.ENVIRONMENT).value(environment);
}
if (monitorConfig != null) {
writer.name(JsonKeys.MONITOR_CONFIG);
monitorConfig.serialize(writer, logger);
}
if (contexts != null) {
writer.name(JsonKeys.CONTEXTS);
contexts.serialize(writer, logger);
}
if (unknown != null) {
for (String key : unknown.keySet()) {
Object value = unknown.get(key);
writer.name(key).value(logger, value);
}
}
writer.endObject();
}
// JsonDeserializer
public static final class Deserializer implements JsonDeserializer<CheckIn> {
@Override
public @NotNull CheckIn deserialize(@NotNull ObjectReader reader, @NotNull ILogger logger)
throws Exception {
SentryId sentryId = null;
MonitorConfig monitorConfig = null;
String monitorSlug = null;
String status = null;
Double duration = null;
String release = null;
String environment = null;
MonitorContexts contexts = null;
Map<String, Object> unknown = null;
reader.beginObject();
while (reader.peek() == JsonToken.NAME) {
final String nextName = reader.nextName();
switch (nextName) {
case JsonKeys.CHECK_IN_ID:
sentryId = new SentryId.Deserializer().deserialize(reader, logger);
break;
case JsonKeys.MONITOR_SLUG:
monitorSlug = reader.nextStringOrNull();
break;
case JsonKeys.STATUS:
status = reader.nextStringOrNull();
break;
case JsonKeys.DURATION:
duration = reader.nextDoubleOrNull();
break;
case JsonKeys.RELEASE:
release = reader.nextStringOrNull();
break;
case JsonKeys.ENVIRONMENT:
environment = reader.nextStringOrNull();
break;
case JsonKeys.MONITOR_CONFIG:
monitorConfig = new MonitorConfig.Deserializer().deserialize(reader, logger);
break;
case JsonKeys.CONTEXTS:
contexts = new MonitorContexts.Deserializer().deserialize(reader, logger);
break;
default:
if (unknown == null) {
unknown = new HashMap<>();
}
reader.nextUnknown(logger, unknown, nextName);
break;
}
}
reader.endObject();
if (sentryId == null) {
String message = "Missing required field \"" + JsonKeys.CHECK_IN_ID + "\"";
Exception exception = new IllegalStateException(message);
logger.log(SentryLevel.ERROR, message, exception);
throw exception;
}
if (monitorSlug == null) {
String message = "Missing required field \"" + JsonKeys.MONITOR_SLUG + "\"";
Exception exception = new IllegalStateException(message);
logger.log(SentryLevel.ERROR, message, exception);
throw exception;
}
if (status == null) {
String message = "Missing required field \"" + JsonKeys.STATUS + "\"";
Exception exception = new IllegalStateException(message);
logger.log(SentryLevel.ERROR, message, exception);
throw exception;
}
CheckIn checkIn = new CheckIn(sentryId, monitorSlug, status);
checkIn.setDuration(duration);
checkIn.setRelease(release);
checkIn.setEnvironment(environment);
checkIn.setMonitorConfig(monitorConfig);
checkIn.getContexts().putAll(contexts);
checkIn.setUnknown(unknown);
return checkIn;
}
}
}