This repository was archived by the owner on Apr 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathQuery.java
More file actions
251 lines (186 loc) · 6.11 KB
/
Query.java
File metadata and controls
251 lines (186 loc) · 6.11 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
package models;
import com.beust.jcommander.internal.Lists;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.joda.time.DateTimeZone;
import org.joda.time.LocalDate;
import org.opentripplanner.analyst.PointFeature;
import org.opentripplanner.analyst.PointSet;
import org.opentripplanner.analyst.cluster.AnalystClusterRequest;
import org.opentripplanner.analyst.cluster.OneToManyProfileRequest;
import org.opentripplanner.analyst.cluster.OneToManyRequest;
import org.opentripplanner.analyst.cluster.ResultEnvelope;
import org.opentripplanner.common.model.GenericLocation;
import org.opentripplanner.routing.core.TraverseModeSet;
import otp.Analyst;
import play.Logger;
import play.Play;
import utils.DataStore;
import utils.IdUtils;
import utils.JsonUtil;
import utils.QueryResultStore;
import utils.QueueManager;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;
@JsonIgnoreProperties(ignoreUnknown = true)
public class Query implements Serializable {
private static final ObjectMapper objectMapper = JsonUtil.getObjectMapper();
private static HashMap<String, List<ResultEnvelope>> resultsQueue = new HashMap<String, List<ResultEnvelope>>();
private static final long serialVersionUID = 1L;
static DataStore<Query> queryData = new DataStore<Query>("queries", true);
public String id;
public String projectId;
public String name;
public String mode;
public String shapefileId;
public String scenarioId;
public String status;
public Integer totalPoints;
public Integer completePoints;
/** Has this query finished computing _and_ processing? */
public boolean complete = false;
// the from time of this query
public int fromTime;
// the to time of this query
public int toTime;
public LocalDate date;
@JsonIgnore
transient private QueryResultStore results;
public Query() {
}
static public Query create() {
Query query = new Query();
query.save();
return query;
}
public static Collection<Query> getAll() {
return queryData.getAll();
}
/**
* Get the shapefile name. This is used in the UI so that we can display the name of the shapefile.
*/
public String getShapefileName () {
Shapefile l = Shapefile.getShapefile(shapefileId);
if (l == null)
return null;
return l.name;
}
/**
* Does this query use transit?
*/
public Boolean isTransit () {
if (this.mode == null)
return null;
return new TraverseModeSet(this.mode).isTransit();
}
public void save() {
// assign id at save
if(id == null || id.isEmpty()) {
id = IdUtils.getId();
Logger.info("created query q " + id);
}
queryData.save(id, this);
Logger.info("saved query q " +id);
}
public void run() {
QueueManager qm = QueueManager.getManager();
// enqueue all the requests
Shapefile shp = Shapefile.getShapefile(this.shapefileId);
PointSet ps = shp.getPointSet();
TransportScenario scenario = TransportScenario.getScenario(this.scenarioId);
Bundle bundle = Bundle.getBundle(scenario.bundleId);
totalPoints = ps.capacity;
completePoints = 0;
this.save();
QueryResultStore.accumulate(this);
List<AnalystClusterRequest> requests = Lists.newArrayList();
// TODO batch?
long now = System.currentTimeMillis();
for (int i = 0; i < ps.capacity; i++) {
PointFeature pf = ps.getFeature(i);
AnalystClusterRequest req;
if (this.isTransit()) {
OneToManyProfileRequest pr = new OneToManyProfileRequest();
pr.options = Analyst.buildProfileRequest(this.mode, this.date, this.fromTime, this.toTime, pf.getLat(), pf.getLon());
pr.options.bannedRoutes = scenario.bannedRoutes.stream().map(rs -> rs.agencyId + "_" + rs.id).collect(Collectors.toList());
req = pr;
} else {
OneToManyRequest rr = new OneToManyRequest();
GenericLocation from = new GenericLocation(pf.getLat(), pf.getLon());
rr.options = Analyst.buildRequest(rr.graphId, this.date, this.fromTime, from, this.mode, 120, DateTimeZone.forID(bundle.timeZone));
req = rr;
}
req.destinationPointsetId = this.shapefileId;
req.graphId = scenario.bundleId;
req.outputQueue = Play.application().configuration().getString("cluster.results-bucket");
req.jobId = this.id;
req.id = pf.getId() != null ? pf.getId() : "" + i;
req.includeTimes = false;
requests.add(req);
}
qm.enqueueBatch(requests);
Logger.info("Enqueued {} items in {}ms", ps.capacity, System.currentTimeMillis() - now);
}
public void delete() throws IOException {
queryData.delete(id);
Logger.info("delete query q" +id);
}
private synchronized void makeResultDb() {
if (results == null) {
results = new QueryResultStore(this);
}
}
@JsonIgnore
public QueryResultStore getResults() {
if (results == null) {
makeResultDb();
}
return results;
}
/** close the results database, ensuring it is written to disk */
public synchronized void closeResults () {
if (results != null) {
results.close();
results = null;
}
}
public Integer getPercent() {
if(this.totalPoints != null && this.completePoints != null && this.totalPoints > 0)
return Math.round((float)((float)this.completePoints / (float)this.totalPoints) * 100);
else
return 0;
}
static public Query getQuery(String id) {
return queryData.getById(id);
}
static public Collection<Query> getQueries(String projectId) {
if(projectId == null)
return queryData.getAll();
else {
Collection<Query> data = new ArrayList<Query>();
for(Query sd : queryData.getAll()) {
if(sd.projectId != null && sd.projectId.equals(projectId))
data.add(sd);
}
return data;
}
}
/**
* Get all the queries for a point set.
*/
public static Collection<Query> getQueriesByPointSet(String shapefileId) {
Collection<Query> ret = new ArrayList<Query>();
for (Query q : queryData.getAll()) {
if (q.shapefileId != null && q.shapefileId.equals(shapefileId)) {
ret.add(q);
}
}
return ret;
}
}