Yingyi Bu | e3e1373 | 2015-12-30 16:06:04 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 20 | drop dataverse emergencyTest if exists; |
| 21 | create dataverse emergencyTest; |
| 22 | |
| 23 | use dataverse emergencyTest; |
| 24 | |
| 25 | create type CHPReport as |
| 26 | { "rid":uuid, "etype":string, "timestamp":datetime, "epicenter":point, "radius":double, "message":string } |
| 27 | |
| 28 | create type userLocation as |
| 29 | { "id":uuid, "user-id":int64, "timestamp":datetime, "location":point } |
| 30 | |
| 31 | create type tornadoShelter as |
| 32 | { "tsid":uuid, "name":string, "location":point } |
| 33 | |
| 34 | create dataset tornadoShelters(tornadoShelter) |
| 35 | primary key tsid autogenerated; |
| 36 | |
| 37 | create dataset userLocations(userLocation) |
| 38 | primary key id autogenerated; |
| 39 | |
| 40 | create dataset CHPReports(CHPReport) |
| 41 | primary key rid autogenerated; |
| 42 | |
| 43 | create index reportTimes on CHPReports(timestamp); |
| 44 | create index userTimes on userLocations(timestamp); |
| 45 | create index shelterloc on tornadoShelters(location) type rtree; |
| 46 | |
| 47 | create function NearbySheltersDuringTornadoDanger($userid) { |
| 48 | for $emergency in dataset CHPReports |
| 49 | let $dangerzone := create-circle($emergency.epicenter,$emergency.radius) |
| 50 | let $timewindow := day-time-duration("PT1M") |
| 51 | where (some $user in dataset userLocations satisfies |
| 52 | $user.user-id = $userid |
| 53 | ) |
| 54 | return |
| 55 | { "shelter locations":for $shelter in dataset tornadoShelters where spatial-intersect( $dangerzone,$shelter.location) return $shelter.location} |
| 56 | }; |
| 57 | |
| 58 | create type sub as closed |
| 59 | { "id":int, "param0":int64 } |
| 60 | |
| 61 | create dataset NearbySheltersDuringTornadoDangerChannelSubscriptions(sub) |
| 62 | primary key id; |
| 63 | |
| 64 | create type result as open |
| 65 | { "id": uuid } |
| 66 | |
| 67 | create dataset NearbySheltersDuringTornadoDangerChannelResults(result) |
| 68 | primary key id autogenerated; |
| 69 | |
| 70 | insert into dataset NearbySheltersDuringTornadoDangerChannelResults ( |
| 71 | for $sub in dataset NearbySheltersDuringTornadoDangerChannelSubscriptions |
| 72 | for $result in NearbySheltersDuringTornadoDanger($sub.param0) |
| 73 | return |
| 74 | { "result":$result} |
| 75 | ); |