Example #1
0
    def test_ok_cell(self):
        app = self.app
        session = self.db_slave_session
        cell = Cell()
        cell.lat = 123456781
        cell.lon = 234567892
        cell.radio = 0
        cell.mcc = 123
        cell.mnc = 1
        cell.lac = 2
        cell.cid = 1234
        session.add(cell)
        session.commit()

        res = app.post_json(
            '/v1/geolocate?key=test', {
                "radioType": "gsm",
                "cellTowers": [
                    {"mobileCountryCode": 123, "mobileNetworkCode": 1,
                     "locationAreaCode": 2, "cellId": 1234},
                ]},
            status=200)
        self.assertEqual(res.content_type, 'application/json')
        self.assertEqual(res.body, '{"location": {"lat": 12.3456781, '
                                   '"lng": 23.4567892}, "accuracy": 35000.0}')
Example #2
0
    def test_ok_cell(self):
        app = self.app
        session = self.get_session()
        cell = Cell()
        cell.lat = PARIS_LAT
        cell.lon = PARIS_LON
        cell.radio = RADIO_TYPE['gsm']
        cell.mcc = FRANCE_MCC
        cell.mnc = 1
        cell.lac = 2
        cell.cid = 1234
        session.add(cell)
        session.commit()

        res = app.post_json(
            '%s?key=test' % self.url, {
                "radioType": "gsm",
                "cellTowers": [
                    {"mobileCountryCode": FRANCE_MCC,
                     "mobileNetworkCode": 1,
                     "locationAreaCode": 2,
                     "cellId": 1234},
                ]},
            status=200)

        self.check_stats(
            counter=[self.metric_url + '.200',
                     self.metric + '.api_key.test',
                     self.metric + '.api_log.test.cell_hit']
        )

        self.assertEqual(res.content_type, 'application/json')
        self.assertEqual(res.json, {"location": {"lat": PARIS_LAT,
                                                 "lng": PARIS_LON},
                                    "accuracy": CELL_MIN_ACCURACY})
Example #3
0
 def test_ok_cell_radio_in_celltowers_dupes(self):
     # This test covered a bug related to FxOS calling the
     # geolocate API incorrectly.
     app = self.app
     session = self.db_slave_session
     cell = Cell()
     cell.lat = PARIS_LAT
     cell.lon = PARIS_LON
     cell.radio = 0
     cell.mcc = FRANCE_MCC
     cell.mnc = 1
     cell.lac = 2
     cell.cid = 1234
     cell.range = 1000
     session.add(cell)
     session.commit()
     res = app.post_json(
         '%s?key=test' % self.url, {
             "cellTowers": [
                 {"radio": "gsm",
                  "mobileCountryCode": FRANCE_MCC,
                  "mobileNetworkCode": 1,
                  "locationAreaCode": 2,
                  "cellId": 1234},
                 {"radio": "gsm",
                  "mobileCountryCode": FRANCE_MCC,
                  "mobileNetworkCode": 1,
                  "locationAreaCode": 2,
                  "cellId": 1234},
             ]},
         status=200)
     self.assertEqual(res.content_type, 'application/json')
     self.assertEqual(res.json, {"location": {"lat": PARIS_LAT,
                                              "lng": PARIS_LON},
                                 "accuracy": CELL_MIN_ACCURACY})
Example #4
0
    def test_ok_cell(self):
        app = self.app
        session = self.db_slave_session
        cell = Cell()
        cell.lat = 123456781
        cell.lon = 234567892
        cell.radio = 0
        cell.mcc = 123
        cell.mnc = 1
        cell.lac = 2
        cell.cid = 1234
        session.add(cell)
        session.commit()

        res = app.post_json('/v1/geolocate', {
            "radioType":
            "gsm",
            "cellTowers": [
                {
                    "mobileCountryCode": 123,
                    "mobileNetworkCode": 1,
                    "locationAreaCode": 2,
                    "cellId": 1234
                },
            ]
        },
                            status=200)
        self.assertEqual(res.content_type, 'application/json')
        self.assertEqual(
            res.body, '{"location": {"lat": 12.3456781, '
            '"lng": 23.4567892}, "accuracy": 35000.0}')
Example #5
0
 def test_ok_cell_radio_in_celltowers_dupes(self):
     # This test covered a bug related to FxOS calling the
     # geolocate API incorrectly.
     app = self.app
     session = self.get_session()
     cell = Cell()
     cell.lat = PARIS_LAT
     cell.lon = PARIS_LON
     cell.radio = 0
     cell.mcc = FRANCE_MCC
     cell.mnc = 1
     cell.lac = 2
     cell.cid = 1234
     session.add(cell)
     session.commit()
     res = app.post_json(
         '%s?key=test' % self.url, {
             "cellTowers": [
                 {"radio": "gsm",
                  "mobileCountryCode": FRANCE_MCC,
                  "mobileNetworkCode": 1,
                  "locationAreaCode": 2,
                  "cellId": 1234},
                 {"radio": "gsm",
                  "mobileCountryCode": FRANCE_MCC,
                  "mobileNetworkCode": 1,
                  "locationAreaCode": 2,
                  "cellId": 1234},
             ]},
         status=200)
     self.assertEqual(res.content_type, 'application/json')
     self.assertEqual(res.json, {"location": {"lat": PARIS_LAT,
                                              "lng": PARIS_LON},
                                 "accuracy": CELL_MIN_ACCURACY})
Example #6
0
    def test_ok_cell(self):
        app = self.app
        session = self.db_slave_session
        cell = Cell()
        cell.lat = 123456781
        cell.lon = 234567892
        cell.radio = 0
        cell.mcc = 123
        cell.mnc = 1
        cell.lac = 2
        cell.cid = 1234
        session.add(cell)
        session.commit()

        res = app.post_json(
            '/v1/geolocate?key=test', {
                "radioType": "gsm",
                "cellTowers": [
                    {"mobileCountryCode": 123, "mobileNetworkCode": 1,
                     "locationAreaCode": 2, "cellId": 1234},
                ]},
            status=200)

        find_msg = self.find_heka_messages
        self.assertEquals(
            len(find_msg('counter', 'http.request')), 1)
        self.assertEqual(1, len(find_msg('counter', 'geolocate.api_key.test')))

        self.assertEqual(res.content_type, 'application/json')
        self.assertEqual(res.json, {"location": {"lat": 12.3456781,
                                                 "lng": 23.4567892},
                                    "accuracy": 35000.0})
Example #7
0
    def test_ok_cell(self):
        app = self.app
        session = self.db_slave_session
        cell = Cell()
        cell.lat = 123456781
        cell.lon = 234567892
        cell.radio = 0
        cell.mcc = 123
        cell.mnc = 1
        cell.lac = 2
        cell.cid = 1234
        session.add(cell)
        session.commit()

        res = app.post_json(
            '/v1/geolocate?key=test', {
                "radioType": "gsm",
                "cellTowers": [
                    {"mobileCountryCode": 123, "mobileNetworkCode": 1,
                     "locationAreaCode": 2, "cellId": 1234},
                ]},
            status=200)

        self.check_expected_heka_messages(
            counter=['http.request', 'geolocate.api_key.test']
        )

        self.assertEqual(res.content_type, 'application/json')
        self.assertEqual(res.json, {"location": {"lat": 12.3456781,
                                                 "lng": 23.4567892},
                                    "accuracy": 35000.0})
Example #8
0
    def test_ok_cell_radio_in_celltowers(self):
        app = self.app
        session = self.db_slave_session
        cell = Cell()
        cell.lat = 123456781
        cell.lon = 234567892
        cell.radio = 0
        cell.mcc = 123
        cell.mnc = 1
        cell.lac = 2
        cell.cid = 1234
        session.add(cell)
        session.commit()

        res = app.post_json(
            '/v1/geolocate?key=test', {
                "cellTowers": [
                    {"radio": "gsm",
                     "mobileCountryCode": 123,
                     "mobileNetworkCode": 1,
                     "locationAreaCode": 2,
                     "cellId": 1234},
                ]},
            status=200)

        self.check_expected_heka_messages(
            counter=['http.request', 'geolocate.api_key.test']
        )

        self.assertEqual(res.content_type, 'application/json')
        self.assertEqual(res.json, {"location": {"lat": 12.3456781,
                                                 "lng": 23.4567892},
                                    "accuracy": CELL_MIN_ACCURACY})
Example #9
0
    def test_ok_cell(self):
        app = self.app
        session = self.get_session()
        cell = Cell()
        cell.lat = PARIS_LAT
        cell.lon = PARIS_LON
        cell.radio = RADIO_TYPE['gsm']
        cell.mcc = FRANCE_MCC
        cell.mnc = 1
        cell.lac = 2
        cell.cid = 1234
        session.add(cell)
        session.commit()

        res = app.post_json(
            '%s?key=test' % self.url, {
                "radioType": "gsm",
                "cellTowers": [
                    {"mobileCountryCode": FRANCE_MCC,
                     "mobileNetworkCode": 1,
                     "locationAreaCode": 2,
                     "cellId": 1234},
                ]},
            status=200)

        self.check_stats(
            counter=[self.metric_url + '.200', self.metric + '.api_key.test']
        )

        self.assertEqual(res.content_type, 'application/json')
        self.assertEqual(res.json, {"location": {"lat": PARIS_LAT,
                                                 "lng": PARIS_LON},
                                    "accuracy": CELL_MIN_ACCURACY})
Example #10
0
    def test_ok_cell(self):
        app = self.app
        session = self.db_master_session
        cell = Cell()
        cell.lat = GB_LAT
        cell.lon = GB_LON
        cell.radio = 0
        cell.mcc = GB_MCC
        cell.mnc = 1
        cell.lac = 2
        cell.cid = 1234
        cell.total_measures = 1
        cell.new_measures = 0

        session.add(cell)
        session.commit()

        res = app.post_json('/v1/geosubmit?key=test',
                            {'items': [{"latitude": GB_LAT + 0.1,
                                        "longitude": GB_LON + 0.1,
                                        "accuracy": 12.4,
                                        "radioType": "gsm",
                                        "cellTowers": [{
                                            "cellId": 1234,
                                            "locationAreaCode": 2,
                                            "mobileCountryCode": GB_MCC,
                                            "mobileNetworkCode": 1,
                                        }]},
                                       {"latitude": GB_LAT - 0.1,
                                        "longitude": GB_LON - 0.1,
                                        "accuracy": 22.4,
                                        "radioType": "gsm",
                                        "cellTowers": [{
                                            "cellId": 2234,
                                            "locationAreaCode": 22,
                                            "mobileCountryCode": GB_MCC,
                                            "mobileNetworkCode": 2,
                                        }]}]},
                            status=200)

        # check that we get an empty response
        self.assertEqual(res.content_type, 'application/json')
        self.assertEqual(res.json, {})

        # check that two new CellMeasure records are created
        self.assertEquals(2, session.query(CellMeasure).count())
        cm1 = session.query(CellMeasure).filter(
            CellMeasure.cid == 1234).count()
        cm2 = session.query(CellMeasure).filter(
            CellMeasure.cid == 2234).count()
        self.assertEquals(1, cm1)
        self.assertEquals(1, cm2)

        self.check_stats(
            counter=['geosubmit.api_key.test',
                     'items.uploaded.batches',
                     'items.uploaded.reports',
                     ],
            timer=['items.uploaded.batch_size'])
Example #11
0
    def test_ok_cell(self):
        app = self.app
        session = self.db_master_session
        cell = Cell()
        cell.lat = GB_LAT + 0.1
        cell.lon = GB_LON + 0.1
        cell.radio = 0
        cell.mcc = GB_MCC
        cell.mnc = 1
        cell.lac = 2
        cell.cid = 1234
        cell.total_measures = 1
        cell.new_measures = 0

        session.add(cell)
        session.commit()

        res = app.post_json('/v1/geosubmit?key=test', {
                            "latitude": GB_LAT,
                            "longitude": GB_LON,
                            "accuracy": 12.4,
                            "radioType": "gsm",
                            "cellTowers": [{
                                "cellId": 1234,
                                "locationAreaCode": 2,
                                "mobileCountryCode": GB_MCC,
                                "mobileNetworkCode": 1,
                            }]},
                            status=200)

        # check that we get back a location
        self.assertEqual(res.content_type, 'application/json')
        self.assertEqual(res.json, {"location": {"lat": GB_LAT,
                                                 "lng": GB_LON},
                                    "accuracy": 12.4})

        cell = session.query(Cell).first()
        self.assertEqual(2, cell.total_measures)
        self.assertEqual(1, cell.new_measures)

        # check that one new CellMeasure record is created
        self.assertEquals(1, session.query(CellMeasure).count())

        self.check_stats(
            counter=['geosubmit.api_key.test',
                     'items.api_log.test.uploaded.batches',
                     'items.api_log.test.uploaded.reports',
                     'items.api_log.test.uploaded.cell_observations',
                     'items.uploaded.cell_observations',
                     'items.uploaded.batches',
                     'items.uploaded.reports',
                     'request.v1.geosubmit.200',
                     ],
            timer=['items.api_log.test.uploaded.batch_size',
                   'items.uploaded.batch_size',
                   'request.v1.geosubmit'])
Example #12
0
    def test_geoip_match(self):
        app = self.app
        london = self.geoip_data['London']
        session = self.db_master_session
        cell = Cell()
        cell.lat = GB_LAT
        cell.lon = GB_LON
        cell.radio = 0
        cell.mcc = GB_MCC
        cell.mnc = 1
        cell.lac = 2
        cell.cid = 1234
        cell.total_measures = 1
        cell.new_measures = 0

        session.add(cell)
        session.commit()

        res = app.post_json(
            '/v1/geosubmit?key=test',
            {'items': [{"latitude": GB_LAT + 0.1,
                        "longitude": GB_LON + 0.1,
                        "accuracy": 12.4,
                        "radioType": "gsm",
                        "cellTowers": [{
                            "cellId": 1234,
                            "locationAreaCode": 2,
                            "mobileCountryCode": GB_MCC,
                            "mobileNetworkCode": 1,
                        }]},
                       {"latitude": GB_LAT - 0.1,
                        "longitude": GB_LON - 0.1,
                        "accuracy": 22.4,
                        "radioType": "gsm",
                        "cellTowers": [{
                            "cellId": 2234,
                            "locationAreaCode": 22,
                            "mobileCountryCode": GB_MCC,
                            "mobileNetworkCode": 2,
                        }]}]},
            extra_environ={'HTTP_X_FORWARDED_FOR': london['ip']},
            status=200)

        # check that we get an empty response
        self.assertEqual(res.content_type, 'application/json')
        self.assertEqual(res.json, {})

        # check that two new CellMeasure records are created
        self.assertEquals(2, session.query(CellMeasure).count())
        cm1 = session.query(CellMeasure).filter(
            CellMeasure.cid == 1234).count()
        cm2 = session.query(CellMeasure).filter(
            CellMeasure.cid == 2234).count()
        self.assertEquals(1, cm1)
        self.assertEquals(1, cm2)
Example #13
0
def update_lac(self, radio, mcc, mnc, lac):

    with self.db_session() as session:

        # Select all the cells in this LAC that aren't the virtual
        # cell itself, and derive a bounding box for them.

        q = session.query(Cell).filter(Cell.radio == radio).filter(
            Cell.mcc == mcc).filter(Cell.mnc == mnc).filter(
                Cell.lac == lac).filter(Cell.cid != CELLID_LAC)

        cells = q.all()
        points = [(to_degrees(c.lat), to_degrees(c.lon)) for c in cells]
        min_lat = to_degrees(min([c.min_lat for c in cells]))
        min_lon = to_degrees(min([c.min_lon for c in cells]))
        max_lat = to_degrees(max([c.max_lat for c in cells]))
        max_lon = to_degrees(max([c.max_lon for c in cells]))

        bbox_points = [(min_lat, min_lon), (min_lat, max_lon),
                       (max_lat, min_lon), (max_lat, max_lon)]

        ctr = centroid(points)
        rng = range_to_points(ctr, bbox_points)

        # switch units back to DB preferred centimicrodegres angle
        # and meters distance.
        ctr_lat = from_degrees(ctr[0])
        ctr_lon = from_degrees(ctr[1])
        rng = int(round(rng * 1000.0))

        # Now create or update the LAC virtual cell

        q = session.query(Cell).filter(Cell.radio == radio).filter(
            Cell.mcc == mcc).filter(Cell.mnc == mnc).filter(
                Cell.lac == lac).filter(Cell.cid == CELLID_LAC)

        lac = q.first()

        if lac is None:
            lac = Cell(radio=radio,
                       mcc=mcc,
                       mnc=mnc,
                       lac=lac,
                       cid=CELLID_LAC,
                       lat=ctr_lat,
                       lon=ctr_lon,
                       range=rng)
        else:
            lac.new_measures = 0
            lac.lat = ctr_lat
            lac.lon = ctr_lon
            lac.range = rng

        session.commit()
Example #14
0
    def test_geoip_match(self):
        app = self.app
        session = self.db_master_session
        cell = Cell()
        cell.lat = PARIS_LAT
        cell.lon = PARIS_LON
        cell.radio = 0
        cell.mcc = FRANCE_MCC
        cell.mnc = 1
        cell.lac = 2
        cell.cid = 1234
        cell.total_measures = 1
        cell.new_measures = 0

        session.add(cell)
        session.commit()

        res = app.post_json('/v1/geosubmit?key=test',
                            {'items': [{"latitude": PARIS_LAT + 0.1,
                                        "longitude": PARIS_LON + 0.1,
                                        "accuracy": 12.4,
                                        "radioType": "gsm",
                                        "cellTowers": [{
                                            "cellId": 1234,
                                            "locationAreaCode": 2,
                                            "mobileCountryCode": FRANCE_MCC,
                                            "mobileNetworkCode": 1,
                                        }]},
                                       {"latitude": PARIS_LAT - 0.1,
                                        "longitude": PARIS_LON - 0.1,
                                        "accuracy": 22.4,
                                        "radioType": "gsm",
                                        "cellTowers": [{
                                            "cellId": 2234,
                                            "locationAreaCode": 22,
                                            "mobileCountryCode": FRANCE_MCC,
                                            "mobileNetworkCode": 2,
                                        }]}]},
                            extra_environ={'HTTP_X_FORWARDED_FOR': PARIS_IP},
                            status=200)

        # check that we get an empty response
        self.assertEqual(res.content_type, 'application/json')
        self.assertEqual(res.json, {})

        # check that two new CellMeasure records are created
        self.assertEquals(2, session.query(CellMeasure).count())
        cm1 = session.query(CellMeasure).filter(
            CellMeasure.cid == 1234).count()
        cm2 = session.query(CellMeasure).filter(
            CellMeasure.cid == 2234).count()
        self.assertEquals(1, cm1)
        self.assertEquals(1, cm2)
Example #15
0
    def test_inconsistent_cell_radio_in_towers(self):
        app = self.app
        session = self.db_slave_session
        cell = Cell()
        cell.lat = 123456781
        cell.lon = 234567892
        cell.radio = 0
        cell.mcc = 123
        cell.mnc = 1
        cell.lac = 2
        cell.cid = 1234
        session.add(cell)
        session.commit()

        res = app.post_json(
            '/v1/geolocate?key=test', {
                "cellTowers": [
                    {"radio": "gsm",
                     "mobileCountryCode": 123,
                     "mobileNetworkCode": 1,
                     "locationAreaCode": 2,
                     "cellId": 1234},
                    {"radio": "cdma",
                     "mobileCountryCode": 123,
                     "mobileNetworkCode": 1,
                     "locationAreaCode": 2,
                     "cellId": 1234},
                ]},
            status=400)

        self.check_expected_heka_messages(
            counter=['http.request', 'geolocate.api_key.test']
        )

        self.assertEqual(res.content_type, 'application/json')
        self.assertEqual(
            res.json, {"error": {
                "errors": [{
                    "domain": "global",
                    "reason": "parseError",
                    "message": "Parse Error",
                }],
                "code": 400,
                "message": "Parse Error"
            }}
        )
Example #16
0
    def test_ok_cell(self):
        app = self.app
        session = self.db_master_session
        cell = Cell()
        cell.lat = PARIS_LAT + 0.1
        cell.lon = PARIS_LON + 0.1
        cell.radio = 0
        cell.mcc = FRANCE_MCC
        cell.mnc = 1
        cell.lac = 2
        cell.cid = 1234
        cell.total_measures = 1
        cell.new_measures = 0

        session.add(cell)
        session.commit()

        res = app.post_json('/v1/geosubmit?key=test', {
                            "latitude": PARIS_LAT,
                            "longitude": PARIS_LON,
                            "accuracy": 12.4,
                            "radioType": "gsm",
                            "cellTowers": [{
                                "cellId": 1234,
                                "locationAreaCode": 2,
                                "mobileCountryCode": FRANCE_MCC,
                                "mobileNetworkCode": 1,
                            }]},
                            status=200)

        # check that we get back a location
        self.assertEqual(res.content_type, 'application/json')
        self.assertEqual(res.json, {"location": {"lat": PARIS_LAT,
                                                 "lng": PARIS_LON},
                                    "accuracy": 12.4})

        cell = session.query(Cell).first()
        self.assertEqual(2, cell.total_measures)
        self.assertEqual(1, cell.new_measures)

        # check that one new CellMeasure record is created
        self.assertEquals(1, session.query(CellMeasure).count())
Example #17
0
    def test_ok_cell(self):
        app = self.app
        session = self.db_slave_session
        cell = Cell()
        cell.lat = 123456781
        cell.lon = 234567892
        cell.radio = 2
        cell.mcc = 123
        cell.mnc = 1
        cell.lac = 2
        cell.cid = 1234
        session.add(cell)
        session.commit()

        res = app.post_json('/v1/search',
                            {"radio": "gsm",
                             "cell": [{"radio": "umts", "mcc": 123, "mnc": 1,
                                       "lac": 2, "cid": 1234}]},
                            status=200)
        self.assertEqual(res.content_type, 'application/json')
        self.assertEqual(res.body, '{"status": "ok", "lat": 12.3456781, '
                                   '"lon": 23.4567892, "accuracy": 35000}')
Example #18
0
def update_lac(self, radio, mcc, mnc, lac):
    try:
        utcnow = util.utcnow()
        with self.db_session() as session:
            # Select all the cells in this LAC that aren't the virtual
            # cell itself, and derive a bounding box for them.

            cell_query = session.query(Cell).filter(
                Cell.radio == radio).filter(Cell.mcc == mcc).filter(
                    Cell.mnc == mnc).filter(
                        Cell.lac == lac).filter(Cell.cid != CELLID_LAC).filter(
                            Cell.lat.isnot(None)).filter(Cell.lon.isnot(None))

            cells = cell_query.all()

            lac_query = session.query(Cell).filter(Cell.radio == radio).filter(
                Cell.mcc == mcc).filter(Cell.mnc == mnc).filter(
                    Cell.lac == lac).filter(Cell.cid == CELLID_LAC)

            if len(cells) == 0:
                # If there are no more underlying cells, delete the lac entry
                lac_query.delete()
            else:
                # Otherwise update the lac entry based on all the cells
                lac_obj = lac_query.first()

                points = [(c.lat, c.lon) for c in cells]
                min_lat = min([c.min_lat for c in cells])
                min_lon = min([c.min_lon for c in cells])
                max_lat = max([c.max_lat for c in cells])
                max_lon = max([c.max_lon for c in cells])

                bbox_points = [(min_lat, min_lon), (min_lat, max_lon),
                               (max_lat, min_lon), (max_lat, max_lon)]

                ctr = centroid(points)
                rng = range_to_points(ctr, bbox_points)

                # Switch units back to DB preferred centimicrodegres angle
                # and meters distance.
                ctr_lat = ctr[0]
                ctr_lon = ctr[1]
                rng = int(round(rng * 1000.0))

                # Now create or update the LAC virtual cell
                if lac_obj is None:
                    lac_obj = Cell(radio=radio,
                                   mcc=mcc,
                                   mnc=mnc,
                                   lac=lac,
                                   cid=CELLID_LAC,
                                   lat=ctr_lat,
                                   lon=ctr_lon,
                                   created=utcnow,
                                   modified=utcnow,
                                   range=rng,
                                   total_measures=len(cells))
                    session.add(lac_obj)
                else:
                    lac_obj.total_measures = len(cells)
                    lac_obj.lat = ctr_lat
                    lac_obj.lon = ctr_lon
                    lac_obj.modified = utcnow
                    lac_obj.range = rng

            session.commit()
    except Exception as exc:  # pragma: no cover
        self.heka_client.raven('error')
        raise self.retry(exc=exc)
Example #19
0
def update_lac(self, radio, mcc, mnc, lac):
    try:
        utcnow = util.utcnow()
        with self.db_session() as session:
            # Select all the cells in this LAC that aren't the virtual
            # cell itself, and derive a bounding box for them.

            cell_query = session.query(Cell).filter(
                Cell.radio == radio).filter(
                Cell.mcc == mcc).filter(
                Cell.mnc == mnc).filter(
                Cell.lac == lac).filter(
                Cell.cid != CELLID_LAC).filter(
                Cell.lat.isnot(None)).filter(
                Cell.lon.isnot(None))

            cells = cell_query.all()

            lac_query = session.query(Cell).filter(
                Cell.radio == radio).filter(
                Cell.mcc == mcc).filter(
                Cell.mnc == mnc).filter(
                Cell.lac == lac).filter(
                Cell.cid == CELLID_LAC)

            if len(cells) == 0:
                # If there are no more underlying cells, delete the lac entry
                lac_query.delete()
            else:
                # Otherwise update the lac entry based on all the cells
                lac_obj = lac_query.first()

                points = [(c.lat, c.lon) for c in cells]
                min_lat = min([c.min_lat for c in cells])
                min_lon = min([c.min_lon for c in cells])
                max_lat = max([c.max_lat for c in cells])
                max_lon = max([c.max_lon for c in cells])

                bbox_points = [(min_lat, min_lon),
                               (min_lat, max_lon),
                               (max_lat, min_lon),
                               (max_lat, max_lon)]

                ctr = centroid(points)
                rng = range_to_points(ctr, bbox_points)

                # Switch units back to DB preferred centimicrodegres angle
                # and meters distance.
                ctr_lat = ctr[0]
                ctr_lon = ctr[1]
                rng = int(round(rng * 1000.0))

                # Now create or update the LAC virtual cell
                if lac_obj is None:
                    lac_obj = Cell(radio=radio,
                                   mcc=mcc,
                                   mnc=mnc,
                                   lac=lac,
                                   cid=CELLID_LAC,
                                   lat=ctr_lat,
                                   lon=ctr_lon,
                                   created=utcnow,
                                   modified=utcnow,
                                   range=rng,
                                   total_measures=len(cells))
                    session.add(lac_obj)
                else:
                    lac_obj.total_measures = len(cells)
                    lac_obj.lat = ctr_lat
                    lac_obj.lon = ctr_lon
                    lac_obj.modified = utcnow
                    lac_obj.range = rng

            session.commit()
    except Exception as exc:  # pragma: no cover
        self.heka_client.raven('error')
        raise self.retry(exc=exc)
Example #20
0
def update_lac(self, radio, mcc, mnc, lac):

    with self.db_session() as session:

        # Select all the cells in this LAC that aren't the virtual
        # cell itself, and derive a bounding box for them.

        q = session.query(Cell).filter(
            Cell.radio == radio).filter(
            Cell.mcc == mcc).filter(
            Cell.mnc == mnc).filter(
            Cell.lac == lac).filter(
            Cell.cid != CELLID_LAC).filter(
            Cell.new_measures == 0).filter(
            Cell.lat.isnot(None)).filter(
            Cell.lon.isnot(None))

        cells = q.all()
        if len(cells) == 0:
            return

        points = [(to_degrees(c.lat),
                   to_degrees(c.lon)) for c in cells]
        min_lat = to_degrees(min([c.min_lat for c in cells]))
        min_lon = to_degrees(min([c.min_lon for c in cells]))
        max_lat = to_degrees(max([c.max_lat for c in cells]))
        max_lon = to_degrees(max([c.max_lon for c in cells]))

        bbox_points = [(min_lat, min_lon),
                       (min_lat, max_lon),
                       (max_lat, min_lon),
                       (max_lat, max_lon)]

        ctr = centroid(points)
        rng = range_to_points(ctr, bbox_points)

        # switch units back to DB preferred centimicrodegres angle
        # and meters distance.
        ctr_lat = from_degrees(ctr[0])
        ctr_lon = from_degrees(ctr[1])
        rng = int(round(rng * 1000.0))

        # Now create or update the LAC virtual cell

        q = session.query(Cell).filter(
            Cell.radio == radio).filter(
            Cell.mcc == mcc).filter(
            Cell.mnc == mnc).filter(
            Cell.lac == lac).filter(
            Cell.cid == CELLID_LAC)

        lac = q.first()

        if lac is None:
            lac = Cell(radio=radio,
                       mcc=mcc,
                       mnc=mnc,
                       lac=lac,
                       cid=CELLID_LAC,
                       lat=ctr_lat,
                       lon=ctr_lon,
                       range=rng)
        else:
            lac.new_measures = 0
            lac.lat = ctr_lat
            lac.lon = ctr_lon
            lac.range = rng

        session.commit()
Example #21
0
    def test_ok_cell(self):
        app = self.app
        session = self.db_master_session
        cell = Cell()
        cell.lat = GB_LAT
        cell.lon = GB_LON
        cell.radio = RADIO_TYPE['cdma']
        cell.mcc = GB_MCC
        cell.mnc = 1
        cell.lac = 2
        cell.cid = 1234
        cell.range = 10000
        cell.total_measures = 1
        cell.new_measures = 0

        session.add(cell)
        session.commit()

        res = app.post_json('/v1/geosubmit?key=test', {
            "items": [
                {
                    "latitude":
                    GB_LAT + 0.1,
                    "longitude":
                    GB_LON + 0.1,
                    "accuracy":
                    12.4,
                    "radioType":
                    "cdma",
                    "cellTowers": [{
                        "cellId": 1234,
                        "locationAreaCode": 2,
                        "mobileCountryCode": GB_MCC,
                        "mobileNetworkCode": 1,
                    }]
                },
                {
                    "latitude":
                    GB_LAT - 0.1,
                    "longitude":
                    GB_LON - 0.1,
                    "accuracy":
                    22.4,
                    "cellTowers": [{
                        "radioType": "wcdma",
                        "cellId": 2234,
                        "locationAreaCode": 22,
                        "mobileCountryCode": GB_MCC,
                        "mobileNetworkCode": 2,
                    }]
                },
            ]
        },
                            status=200)

        # check that we get an empty response
        self.assertEqual(res.content_type, 'application/json')
        self.assertEqual(res.json, {})

        self.assertEqual(session.query(Cell).count(), 2)

        observations = session.query(CellObservation).all()
        self.assertEqual(len(observations), 2)
        radios = set([obs.radio for obs in observations])
        self.assertEqual(radios, set([RADIO_TYPE['cdma'], RADIO_TYPE['umts']]))

        self.check_stats(counter=[
            'geosubmit.api_key.test',
            'items.api_log.test.uploaded.batches',
            'items.api_log.test.uploaded.reports',
            'items.api_log.test.uploaded.cell_observations',
            'items.uploaded.cell_observations',
            'items.uploaded.batches',
            'items.uploaded.reports',
            'request.v1.geosubmit.200',
        ],
                         timer=[
                             'items.api_log.test.uploaded.batch_size',
                             'items.uploaded.batch_size',
                             'request.v1.geosubmit'
                         ])