def test_stats(self): day = datetime.utcnow().date() - timedelta(1) session = self.db_master_session stats = [ Stat(key=STAT_TYPE['cell'], time=day, value=2000000), Stat(key=STAT_TYPE['wifi'], time=day, value=2000000), Stat(key=STAT_TYPE['unique_cell'], time=day, value=1000000), Stat(key=STAT_TYPE['unique_wifi'], time=day, value=2000000), ] session.add_all(stats) session.commit() request = DummyRequest() request.db_slave_session = self.db_master_session inst = self._make_view(request) result = inst.stats_view() self.assertEqual(result['page_title'], 'Statistics') self.assertEqual(result['metrics'], [ { 'name': 'Unique Cells', 'value': '1.00' }, { 'name': 'Cell Observations', 'value': '2.00' }, { 'name': 'Unique Wifi Networks', 'value': '2.00' }, { 'name': 'Wifi Observations', 'value': '2.00' }, ])
def test_histogram(self): from ichnaea.content.stats import histogram session = self.db_master_session today = datetime.utcnow().date() one_day = (today - timedelta(1)).strftime('%Y-%m-%d') two_days = (today - timedelta(2)).strftime('%Y-%m-%d') long_ago = (today - timedelta(25)).strftime('%Y-%m-%d') today = today.strftime('%Y-%m-%d') stats = [ Stat(time=long_ago, value=1), Stat(time=two_days, value=3), Stat(time=one_day, value=7), Stat(time=today, value=9), ] for stat in stats: stat.name = 'cell' session.add_all(stats) session.commit() result = histogram(session, 'cell', days=20) self.assertEqual(result, [ { 'num': 3, 'day': two_days }, { 'num': 7, 'day': one_day }, ])
def test_histogram_different_stat_name(self): session = self.db_master_session day = util.utcnow().date() - timedelta(days=1) stat = Stat(time=day, value=9) stat.name = 'unique_cell' session.add(stat) session.commit() result = histogram(session, 'unique_cell') self.assertEqual(result, [[[unixtime(day), 9]]])
def test_histogram_different_stat_name(self): from ichnaea.content.stats import histogram session = self.db_master_session day = util.utcnow().date() - timedelta(days=1) stat = Stat(time=day, value=9) stat.name = 'unique_cell' session.add(stat) session.commit() result = histogram(session, 'unique_cell') self.assertEqual(result, [{'num': 9, 'day': day.strftime('%Y-%m-%d')}])
def test_histogram_different_stat_name(self): from ichnaea.content.stats import histogram session = self.db_master_session day = datetime.utcnow().date() - timedelta(1) day = day.strftime('%Y-%m-%d') stat = Stat(time=day, value=9) stat.name = 'unique_cell' session.add(stat) session.commit() result = histogram(session, 'unique_cell') self.assertEqual(result, [{'num': 9, 'day': day}])
def test_histogram_different_stat_name(self): from ichnaea.content.stats import histogram session = self.db_master_session day = datetime.utcnow().date() - timedelta(1) day = day.strftime("%Y-%m-%d") stat = Stat(time=day, value=9) stat.name = "unique_cell" session.add(stat) session.commit() result = histogram(session, "unique_cell") self.assertEqual(result, [{"num": 9, "day": day}])
def test_stats(self): day = util.utcnow().date() - timedelta(1) session = self.db_master_session stats = [ Stat(key=STAT_TYPE['cell'], time=day, value=2000000), Stat(key=STAT_TYPE['wifi'], time=day, value=2000000), Stat(key=STAT_TYPE['unique_cell'], time=day, value=1000000), Stat(key=STAT_TYPE['unique_ocid_cell'], time=day, value=1500000), Stat(key=STAT_TYPE['unique_wifi'], time=day, value=2000000), ] session.add_all(stats) session.commit() request = DummyRequest() request.db_slave_session = self.db_master_session request.registry.redis_client = self.redis_client inst = self._make_view(request) result = inst.stats_view() self.assertEqual(result['page_title'], 'Statistics') self.assertEqual(result['metrics1'], [ { 'name': 'MLS Cells', 'value': '1.00' }, { 'name': 'OpenCellID Cells', 'value': '1.50' }, { 'name': 'MLS Cell Observations', 'value': '2.00' }, ]) self.assertEqual(result['metrics2'], [ { 'name': 'Wifi Networks', 'value': '2.00' }, { 'name': 'Wifi Observations', 'value': '2.00' }, ]) # call the view again, without a working db session, so # we can be sure to use the cached result inst = self._make_view(request) request.db_slave_session = None second_result = inst.stats_view() self.assertEqual(second_result, result)
def test_stats_unique_cell_json(self): app = self.app today = datetime.utcnow().date() yesterday = today - timedelta(1) yesterday = yesterday.strftime('%Y-%m-%d') session = self.db_slave_session stat = Stat(time=yesterday, value=2) stat.name = 'unique_cell' session.add(stat) session.commit() result = app.get('/stats_unique_cell.json', status=200) self.assertEqual( result.json, {'histogram': [ {'num': 2, 'day': yesterday}, ]} )
def test_stats_wifi_json(self): app = self.app today = util.utcnow().date() yesterday = today - timedelta(1) session = self.db_slave_session stat = Stat(time=yesterday, value=2) stat.name = 'unique_wifi' session.add(stat) session.commit() result = app.get('/stats_wifi.json', status=200) self.assertEqual( result.json, {'series': [ {'data': [[timegm(yesterday.timetuple()) * 1000, 2]], 'title': 'MLS WiFi'}, ]} ) second_result = app.get('/stats_wifi.json', status=200) self.assertEqual(second_result.json, result.json)
def test_stats_wifi_json(self): app = self.app today = util.utcnow().date() yesterday = today - timedelta(1) session = self.db_slave_session stat = Stat(time=yesterday, value=2) stat.name = 'unique_wifi' session.add(stat) session.commit() result = app.get('/stats_wifi.json', status=200) self.assertEqual( result.json, { 'series': [ { 'data': [[timegm(yesterday.timetuple()) * 1000, 2]], 'title': 'MLS WiFi' }, ] }) second_result = app.get('/stats_wifi.json', status=200) self.assertEqual(second_result.json, result.json)
def test_global_stats(self): from ichnaea.content.stats import global_stats session = self.db_master_session day = datetime.utcnow().date() - timedelta(1) stats = [ Stat(key=STAT_TYPE['cell'], time=day, value=6100000), Stat(key=STAT_TYPE['wifi'], time=day, value=3212000), Stat(key=STAT_TYPE['unique_cell'], time=day, value=3289900), Stat(key=STAT_TYPE['unique_wifi'], time=day, value=2009000), ] session.add_all(stats) session.commit() result = global_stats(session) self.assertDictEqual( result, { 'cell': '6.10', 'unique_cell': '3.28', 'wifi': '3.21', 'unique_wifi': '2.00' })
def test_histogram(self): session = self.db_master_session today = util.utcnow().date() one_day = today - timedelta(days=1) two_days = today - timedelta(days=2) one_month = today - timedelta(days=35) two_months = today - timedelta(days=70) long_ago = today - timedelta(days=100) stats = [ Stat(name='cell', time=long_ago, value=40), Stat(name='cell', time=two_months, value=50), Stat(name='cell', time=one_month, value=60), Stat(name='cell', time=two_days, value=70), Stat(name='cell', time=one_day, value=80), Stat(name='cell', time=today, value=90), ] session.add_all(stats) session.commit() result = histogram(session, 'cell', days=90) self.assertTrue([unixtime(one_day), 80] in result[0]) if two_months.month == 12: expected = date(two_months.year + 1, 1, 1) else: expected = date(two_months.year, two_months.month + 1, 1) self.assertTrue([unixtime(expected), 50] in result[0])
def test_global_stats(self): session = self.db_master_session day = util.utcnow().date() - timedelta(1) stats = [ Stat(key=STAT_TYPE['cell'], time=day, value=6100000), Stat(key=STAT_TYPE['wifi'], time=day, value=3212000), Stat(key=STAT_TYPE['unique_cell'], time=day, value=3289900), Stat(key=STAT_TYPE['unique_ocid_cell'], time=day, value=1523000), Stat(key=STAT_TYPE['unique_wifi'], time=day, value=2009000), ] session.add_all(stats) session.commit() result = global_stats(session) self.assertDictEqual( result, { 'cell': '6.10', 'unique_cell': '3.28', 'wifi': '3.21', 'unique_wifi': '2.00', 'unique_ocid_cell': '1.52', })
def test_global_stats_missing_today(self): from ichnaea.content.stats import global_stats session = self.db_master_session day = datetime.utcnow().date() - timedelta(1) yesterday = day - timedelta(days=1) stats = [ Stat(key=STAT_TYPE['cell'], time=yesterday, value=5000000), Stat(key=STAT_TYPE['cell'], time=day, value=6000000), Stat(key=STAT_TYPE['wifi'], time=day, value=3000000), Stat(key=STAT_TYPE['unique_cell'], time=yesterday, value=4000000), ] session.add_all(stats) session.commit() result = global_stats(session) self.assertDictEqual( result, { 'cell': '6.00', 'unique_cell': '4.00', 'wifi': '3.00', 'unique_wifi': '0.00' })
def add_stat(session, name, day, value): stat_key = STAT_TYPE[name] todays_stat = get_stat(session, stat_key, exact=True, date=day) if todays_stat: return yesterday = day - timedelta(days=1) before = get_stat(session, stat_key, exact=False, date=yesterday) old_value = 0 if before is not None: old_value = int(before.value) stat = Stat(key=stat_key, time=day, value=old_value + int(value)) session.add(stat)
def test_global_stats(self): from ichnaea.content.stats import global_stats session = self.db_master_session day = datetime.utcnow().date() - timedelta(1) stats = [ Stat(key=STAT_TYPE['location'], time=day, value=3), Stat(key=STAT_TYPE['cell'], time=day, value=6), Stat(key=STAT_TYPE['wifi'], time=day, value=3), Stat(key=STAT_TYPE['unique_cell'], time=day, value=3), Stat(key=STAT_TYPE['unique_wifi'], time=day, value=2), ] session.add_all(stats) session.commit() result = global_stats(session) self.assertDictEqual( result, { 'location': 3, 'cell': 6, 'unique_cell': 3, 'wifi': 3, 'unique_wifi': 2 })
def _make_one(self, **kw): from ichnaea.content.models import Stat return Stat(**kw)
def make_stat(name, time, value): stat = Stat(time=time, value=int(value)) stat.name = name return stat