Example #1
0
def get_events():
    """
    Fetches all events matching the query parameters. Parameters are:
        from: the start object of the page
        size: the size of the page we request
        lat: the latitude of our geocell
        lon: the longitude of our geocell
        distance: distance from the center to take
        tags: tags to filter by
    """
    page_from = request.args.get('from', 0)
    page_size = request.args.get('size', 20)
    lat = request.args.get('lat')
    lon = request.args.get('lon')
    distance = request.args.get('distance', '10km')

    tags = request.args.get('tags', [])
    if tags:
        tags = tags.split()

    if not lat or not lon:
        abort(400, 'Must specify both `lat` and `lon`')

    index = DocIndex()
    raw_hits = index.search(tags, GeoCell(lat, lon, distance), PageInfo(page_from, page_size))
    return jsonify({
        "hits": [hit['_source'] for hit in raw_hits.get('hits', {}).get('hits', [])],
        "total": raw_hits['hits']['total']
    })
Example #2
0
 def test_query_event_within_location_should_return_event(self):
     lat, lng = self.random_lat(), self.random_lng()
     event = self.create_test_event(location=[lng, lat])
     self.flush_es_index()
     index = DocIndex()
     result = index.search([], GeoCell(lat, lng, '1km'))
     
     self.assertTrue(result.get('hits', []))
     loc = result['hits']['hits'][0]['_source']['location']
     self.assertTrue(self.degree_compare(loc[0], lng))
     self.assertTrue(self.degree_compare(loc[1], lat))
Example #3
0
    def test_query_event_within_location_should_not_return_event_outside_of_location(self):
        km2lat = 110.574 # roughly
        lat, lng = self.random_lat(), self.random_lng()
        event = self.create_test_event(location=[lng, lat])
        # now create another event, far away from it
        event = self.create_test_event(location=[lng, lat + 10/km2lat])

        self.flush_es_index()

        index = DocIndex()
        result = index.search([], GeoCell(lat, lng, '9km'))
        
        self.assertEquals(result['hits']['total'], 1)
Example #4
0
    def test_hits_should_return_in_desc_order(self):
        self.create_test_event(message='first', timestamp=2222222222)
        self.create_test_event(message='second', timestamp=1111111111)
        self.create_test_event(message='third', timestamp=3333333333)
        self.flush_es_index()

        index = DocIndex()
        result = index.search([])
        hits = result['hits']['hits']
        self.assertEquals(len(hits), 3)

        hits = result['hits']['hits']
        self.assertEquals(hits[0]['_source']['message'], 'third')
        self.assertEquals(hits[1]['_source']['message'], 'first')
        self.assertEquals(hits[2]['_source']['message'], 'second')
Example #5
0
def create_event():
    """
    Indexes a new event with the given data. All fields are required:
        message
        creator
        location
        site
        timestamp
    """
    if not request.json:
        abort(400, "Invalid content type or request")

    required_fields = 'message', 'creator', 'location', 'site', 'timestamp'

    for field in required_fields:
        if request.json.get(field) is None:
            abort(400, "Missing required field: " + field)

    index = DocIndex()
    index.index(*[request.json[field] for field in required_fields])
    return 'ok'
Example #6
0
    def test_hits_queried_by_tags_should_contain_tag(self):
        msg1 = 'what a horrible #night to have a #curse'
        msg2 = 'the morning #sun has vanquished the horrible #night'

        self.create_test_event(message=msg1)
        self.create_test_event(message=msg2)
        self.flush_es_index()

        index = DocIndex()
        result = index.search(['#curse'])
        hits = result['hits']['hits']
        self.assertEquals(len(hits), 1)
        self.assertEquals(hits[0]['_source']['message'], msg1)

        result = index.search(['#sun'])
        hits = result['hits']['hits']
        self.assertEquals(len(hits), 1)
        self.assertEquals(hits[0]['_source']['message'], msg2)

        result = index.search(['#night'])
        hits = result['hits']['hits']
        self.assertEquals(len(hits), 2)
        self.assertEquals(hits[0]['_source']['message'], msg2)
        self.assertEquals(hits[1]['_source']['message'], msg1)