def make_ocid_cell_import_dict(row): def val(key, default): if key in row and row[key] != '' and row[key] is not None: return row[key] else: return default d = dict() d['created'] = datetime.fromtimestamp(int(val('created', 0))).replace(tzinfo=UTC) d['modified'] = datetime.fromtimestamp(int(val('updated', 0))).replace(tzinfo=UTC) d['lat'] = float(val('lat', -255)) d['lon'] = float(val('lon', -255)) d['radio'] = RADIO_TYPE.get(row['radio'].lower(), -1) for k in ['mcc', 'mnc', 'lac', 'cid', 'psc']: d[k] = int(val(k, -1)) d['range'] = int(float(val('range', 0))) d['total_measures'] = int(val('samples', -1)) d['changeable'] = bool(val('changeable', True)) return OCIDCell.validate(d)
def make_ocid_cell_import_dict(row): def val(key, default): if key in row and row[key] != '' and row[key] is not None: return row[key] else: return default d = dict() d['created'] = datetime.fromtimestamp( int(val('created', 0))).replace(tzinfo=UTC) d['modified'] = datetime.fromtimestamp( int(val('updated', 0))).replace(tzinfo=UTC) d['lat'] = float(val('lat', None)) d['lon'] = float(val('lon', None)) try: d['radio'] = Radio[row['radio'].lower()] except KeyError: # pragma: no cover d['radio'] = None for k in ['mcc', 'mnc', 'lac', 'cid', 'psc']: d[k] = int(val(k, -1)) d['range'] = int(float(val('range', 0))) d['total_measures'] = int(val('samples', -1)) d['changeable'] = bool(val('changeable', True)) return OCIDCell.validate(d)
def test_ocid_cell(self): session = self.db_slave_session london = self.geoip_data['London'] cell_key = { 'radio': RADIO_TYPE['gsm'], 'mcc': GB_MCC, 'mnc': 1, 'lac': 1, } session.add(OCIDCell( lat=GB_LAT, lon=GB_LON, range=6000, cid=1, **cell_key)) session.add(CellArea( lat=GB_LAT, lon=GB_LON, range=9000, **cell_key)) session.flush() result = self._make_query(data={'cell': [dict(cid=1, **cell_key)]}, client_addr=london['ip'], api_key_log=True) self.assertEqual(result, {'lat': GB_LAT, 'lon': GB_LON, 'accuracy': 6000})
def test_monitor_ocid_import(self): session = self.db_master_session now = util.utcnow() cell_args = dict(radio=RADIO_TYPE['gsm'], mcc=262, mnc=1, lac=1) expected = [] results = [] for i in range(30, 0, -5): created = now - timedelta(hours=i) expected.append(i * 3600000) session.add(OCIDCell(created=created, cid=i, **cell_args)) session.flush() results.append(monitor_ocid_import.delay().get()) self.check_stats(gauge=[('table.ocid_cell_age', len(expected))], ) for r, e in zip(results, expected): # The values should be almost equal, ignoring differences # less than 10 seconds (or 9999 milliseconds / 4 places) self.assertAlmostEqual(r, e, -4)
def make_ocid_cell_import_dict(row): def val(key, default, _type): if key in row and row[key] != '' and row[key] is not None: return _type(row[key]) else: return default d = dict() d['created'] = datetime.fromtimestamp( val('created', 0, int)).replace(tzinfo=UTC) d['modified'] = datetime.fromtimestamp( val('updated', 0, int)).replace(tzinfo=UTC) d['lat'] = val('lat', None, float) d['lon'] = val('lon', None, float) try: d['radio'] = Radio[row['radio'].lower()] except KeyError: # pragma: no cover d['radio'] = None for k in ['mcc', 'mnc', 'lac', 'cid', 'psc']: d[k] = val(k, None, int) d['range'] = int(val('range', 0, float)) d['total_measures'] = val('samples', 0, int) d['changeable'] = val('changeable', True, bool) validated = OCIDCell.validate(d) if validated is None: return None for field in ('radio', 'mcc', 'mnc', 'lac', 'cid'): if validated[field] is None: return None return validated
def test_unique_ocid_cell_histogram(self): session = self.db_master_session today = util.utcnow().date() one_day = (today - timedelta(1)) two_days = (today - timedelta(2)) long_ago = (today - timedelta(3)) cells = [ OCIDCell(created=long_ago, radio=0, mcc=1, mnc=2, lac=3, cid=4), OCIDCell(created=two_days, radio=2, mcc=1, mnc=2, lac=3, cid=4), OCIDCell(created=two_days, radio=2, mcc=1, mnc=2, lac=3, cid=5), OCIDCell(created=one_day, radio=0, mcc=2, mnc=2, lac=3, cid=5), OCIDCell(created=today, radio=0, mcc=1, mnc=3, lac=3, cid=4), OCIDCell(created=today, radio=0, mcc=1, mnc=2, lac=4, cid=4), ] session.add_all(cells) session.commit() result = unique_ocid_cell_histogram.delay(ago=3) self.assertEqual(result.get(), 1) stats = session.query(Stat).order_by(Stat.time).all() self.assertEqual(len(stats), 1) self.assertEqual(stats[0].key, STAT_TYPE['unique_ocid_cell']) self.assertEqual(stats[0].time, long_ago) self.assertEqual(stats[0].value, 1) # fill up newer dates unique_ocid_cell_histogram.delay(ago=2).get() unique_ocid_cell_histogram.delay(ago=1).get() unique_ocid_cell_histogram.delay(ago=0).get() # test duplicate execution unique_ocid_cell_histogram.delay(ago=1).get() stats = session.query(Stat).order_by(Stat.time).all() self.assertEqual(len(stats), 4) self.assertEqual(stats[0].time, long_ago) self.assertEqual(stats[0].value, 1) self.assertEqual(stats[1].time, two_days) self.assertEqual(stats[1].value, 3) self.assertEqual(stats[2].time, one_day) self.assertEqual(stats[2].value, 4) self.assertEqual(stats[3].time, today) self.assertEqual(stats[3].value, 6)
def _make_one(self, **kw): from ichnaea.models import OCIDCell return OCIDCell(**kw)