-
Notifications
You must be signed in to change notification settings - Fork 9
/
QueryParametersDeserializer.java
154 lines (138 loc) · 7.31 KB
/
QueryParametersDeserializer.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
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
/**********************************************************************
* Copyright (c) 2021, 2022 Ericsson
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License 2.0 which
* accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
**********************************************************************/
package org.eclipse.tracecompass.incubator.internal.trace.server.jersey.rest.core.webapp;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.tracecompass.incubator.internal.trace.server.jersey.rest.core.model.views.Filter;
import org.eclipse.tracecompass.incubator.internal.trace.server.jersey.rest.core.model.views.QueryParameters;
import org.eclipse.tracecompass.internal.provisional.tmf.core.model.annotations.Annotation;
import org.eclipse.tracecompass.internal.provisional.tmf.core.model.annotations.IAnnotation.AnnotationType;
import org.eclipse.tracecompass.statesystem.core.StateSystemUtils;
import org.eclipse.tracecompass.tmf.core.dataprovider.DataProviderParameterUtils;
import org.eclipse.tracecompass.tmf.core.model.OutputElementStyle;
import org.eclipse.tracecompass.tmf.core.model.timegraph.TimeGraphArrow;
import org.eclipse.tracecompass.tmf.core.model.timegraph.TimeGraphState;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.ObjectCodec;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
/**
* Deserializer for query parameters {@link QueryParameters}
*
* @author Patrick Tasse
*/
public class QueryParametersDeserializer extends StdDeserializer<QueryParameters> {
private static final long serialVersionUID = 4511735530369955327L;
private interface ElementType {
String STATE = "state"; //$NON-NLS-1$
String ANNOTATION = "annotation"; //$NON-NLS-1$
String ARROW = "arrow"; //$NON-NLS-1$
}
private static final String DESTINATION_ID = "destinationId"; //$NON-NLS-1$
private static final String DURATION = "duration"; //$NON-NLS-1$
private static final String ELEMENT_TYPE = "elementType"; //$NON-NLS-1$
private static final String ENTRY_ID = "entryId"; //$NON-NLS-1$
private static final String FILTERS = "filters"; //$NON-NLS-1$
private static final String PARAMETERS = "parameters"; //$NON-NLS-1$
private static final String TIME = "time"; //$NON-NLS-1$
private static final String REQUESTED_TIMERANGE_KEY = "requested_timerange"; //$NON-NLS-1$
private static final String START = "start"; //$NON-NLS-1$
private static final String END = "end"; //$NON-NLS-1$
private static final String NBTIMES = "nbTimes"; //$NON-NLS-1$
private static final @NonNull OutputElementStyle EMPTY_STYLE = new OutputElementStyle(null, Collections.emptyMap());
private static final long MAX_NBTIMES = 1 << 16;
/**
* Constructor
*/
protected QueryParametersDeserializer() {
super(QueryParameters.class);
}
@Override
public QueryParameters deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
ObjectCodec codec = jp.getCodec();
JsonNode node = codec.readTree(jp);
Map<String, Object> parameters = new HashMap<>();
JsonNode parametersNode = node.get(PARAMETERS);
if (parametersNode != null) {
JsonParser parametersParser = parametersNode.traverse(codec);
parametersParser.nextToken();
Map<String, Object> params = ctxt.readValue(parametersParser, Map.class);
/* Replace default deserialized map with the correct element object */
params.computeIfPresent(DataProviderParameterUtils.REQUESTED_ELEMENT_KEY, (k, v) -> {
if (v instanceof Map) {
Map<String, Object> map = (Map<String, Object>) v;
Object elementType = map.get(ELEMENT_TYPE);
long time = ((Number) map.getOrDefault(TIME, 0L)).longValue();
long duration = ((Number) map.getOrDefault(DURATION, 0L)).longValue();
if (ElementType.STATE.equals(elementType)) {
return new TimeGraphState(time, duration, null, null);
} else if (ElementType.ANNOTATION.equals(elementType)) {
long entryId = ((Number) map.getOrDefault(ENTRY_ID, -1L)).longValue();
return new Annotation(time, duration, entryId, AnnotationType.CHART, null, EMPTY_STYLE);
} else if (ElementType.ARROW.equals(elementType)) {
long sourceId = ((Number) map.getOrDefault(ENTRY_ID, -1L)).longValue();
long destinationId = ((Number) map.getOrDefault(DESTINATION_ID, -1L)).longValue();
return new TimeGraphArrow(sourceId, destinationId, time, duration, EMPTY_STYLE);
}
}
return null;
});
/* Transform requested timerange to requested times array */
AtomicReference requestedTimes = new AtomicReference<>();
params.computeIfPresent(REQUESTED_TIMERANGE_KEY, (k, v) -> {
if (v instanceof Map) {
Map<String, Object> map = (Map<String, Object>) v;
Object startObj = map.get(START);
Object endObj = map.get(END);
Object nbTimesObj = map.get(NBTIMES);
if (!(startObj instanceof Number && endObj instanceof Number)) {
return null;
}
long start = ((Number) startObj).longValue();
long end = ((Number) endObj).longValue();
if (!(nbTimesObj instanceof Number)) {
requestedTimes.set(Arrays.asList(start, end));
return null;
}
long nbTimes = Math.min(((Number) nbTimesObj).longValue(), MAX_NBTIMES);
if (nbTimes <= 1) {
requestedTimes.set(Arrays.asList(start, end));
return null;
}
long resolution = (end - start) / (nbTimes - 1);
requestedTimes.set(StateSystemUtils.getTimes(start, end, resolution));
return null;
}
return v;
});
if (requestedTimes.get() != null) {
params.put(DataProviderParameterUtils.REQUESTED_TIME_KEY, requestedTimes.get());
}
parameters = params;
}
List<Filter> filters = null;
JsonNode filtersNode = node.get(FILTERS);
if (filtersNode != null) {
JsonParser filtersParser = filtersNode.traverse(codec);
filtersParser.nextToken();
filters = ctxt.readValue(filtersParser, List.class);
}
return new QueryParameters(parameters, filters);
}
}