blob: 1cb0114746c24b0deb53cf960a523e9302e4f395 [file] [log] [blame]
Yingyi Bue3e13732015-12-30 16:06:04 -08001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20drop dataverse emergencyTest if exists;
21create dataverse emergencyTest;
22use dataverse emergencyTest;
23
24create type CHPReport as
25{ "rid":uuid, "epicenter":point, "radius":double, "message":string, "timestamp":datetime }
26
27create type userLocation as
28{ "id":uuid, "user-id":int64, "location":point, "timestamp":datetime }
29
30create type tornadoShelter as
31{ "tsid":uuid, "location":point }
32
33create type sub as closed
34{ "id":int, "param0":int64 }
35
36create dataset NearbySheltersDuringTornadoDangerChannelSubscriptions(sub)
37primary key id;
38
39create dataset tornadoShelters(tornadoShelter)
40primary key tsid autogenerated;
41
42create dataset userLocations(userLocation)
43primary key id autogenerated;
44
45create dataset CHPReports(CHPReport)
46primary key rid autogenerated;
47
48create index times on CHPReports(timestamp);
49create index times2 on userLocations(timestamp);
50create index locs on tornadoShelters(location) type rtree;
51create index locs2 on CHPReports(epicenter) type rtree;
52create index locs3 on userLocations(location) type rtree;
53
54for $sub in dataset NearbySheltersDuringTornadoDangerChannelSubscriptions
55for $emergency in dataset CHPReports
56let $dangerzone := create-circle($emergency.epicenter,$emergency.radius)
57let $timewindow := day-time-duration("PT10M")
58where $emergency.etype = "tornado"
59where $emergency.timestamp >= current-datetime()-$timewindow
60where (some $user in dataset userLocations satisfies
61$user.user-id = $sub.param0
62and $user.timestamp >= current-datetime() - $timewindow
63and spatial-intersect($dangerzone,$user.location))
64return
65{ "id":$sub.param0, "message":$emergency.message, "shelter locations":for $shelter in dataset tornadoShelters return $shelter.location }
66;