def test_live_statistics_updatecheck(self): request = parse_request(fixtures.request_update_check) apps = request.findall('app') app = apps[0] channel = DEFAULT_CHANNEL now = datetime.utcnow() userid = 1 platform = 'win' appid = app.get('appid') version = app.get('version') events_appid_version = HourEvents( 'request:{}:{}'.format(appid, version), now.year, now.month, now.day, now.hour) events_appid_platform_version = HourEvents( 'request:{}:{}:{}'.format(appid, platform, version), now.year, now.month, now.day, now.hour) events_appid_platform_channel_version = HourEvents( 'request:{}:{}:{}:{}'.format(appid, platform, channel, version), now.year, now.month, now.day, now.hour) self.assertEqual(len(events_appid_version), 0) self.assertEqual(len(events_appid_platform_version), 0) self.assertEqual(len(events_appid_platform_channel_version), 0) userid_counting(userid, apps, platform) self.assertEqual(len(events_appid_version), 1) self.assertEqual(len(events_appid_platform_version), 1) self.assertEqual(len(events_appid_platform_channel_version), 1)
def test_mark_with_diff_days(): delete_all_events() mark_event('active', 123, track_hourly=True) now = datetime.utcnow() # Month assert 123 in MonthEvents('active', now.year, now.month) assert 124 not in MonthEvents('active', now.year, now.month) # Week assert 123 in WeekEvents('active', now.year, now.isocalendar()[1]) assert 124 not in WeekEvents('active', now.year, now.isocalendar()[1]) # Day assert 123 in DayEvents('active', now.year, now.month, now.day) assert 124 not in DayEvents('active', now.year, now.month, now.day) # Hour assert 123 in HourEvents('active', now.year, now.month, now.day, now.hour) assert 124 not in HourEvents('active', now.year, now.month, now.day, now.hour) assert 124 not in HourEvents('active', now.year, now.month, now.day, now.hour - 1)
def test_live_statistics_install(self): request = parse_request(fixtures.request_event_install_success) apps = request.findall('app') app = apps[0] channel = DEFAULT_CHANNEL now = datetime.utcnow() userid = 1 platform = 'win' appid = app.get('appid') version_1 = '0.0.0.1' version_2 = '0.0.0.2' events_appid_version = lambda version: HourEvents( 'request:{}:{}'.format(appid, version), now.year, now.month, now. day, now.hour) events_appid_platform_version = lambda version: HourEvents( 'request:{}:{}:{}'.format(appid, platform, version), now.year, now. month, now.day, now.hour) events_appid_platform_channel_version = lambda version: HourEvents( 'request:{}:{}:{}:{}'.format(appid, platform, channel, version), now.year, now.month, now.day, now.hour) self.assertEqual(len(events_appid_version(version_1)), 0) self.assertEqual(len(events_appid_platform_version(version_1)), 0) self.assertEqual(len(events_appid_platform_channel_version(version_1)), 0) userid_counting(userid, apps, platform) self.assertEqual(len(events_appid_version(version_1)), 1) self.assertEqual(len(events_appid_platform_version(version_1)), 1) self.assertEqual(len(events_appid_platform_channel_version(version_1)), 1) request = parse_request(fixtures.request_event_update_success) apps = request.findall('app') userid_counting(userid, apps, platform) self.assertEqual(len(events_appid_version(version_1)), 0) self.assertEqual(len(events_appid_platform_version(version_1)), 0) self.assertEqual(len(events_appid_platform_channel_version(version_1)), 0) self.assertEqual(len(events_appid_version(version_2)), 1) self.assertEqual(len(events_appid_platform_version(version_2)), 1) self.assertEqual(len(events_appid_platform_channel_version(version_2)), 1) request = parse_request(fixtures.request_event_uninstall_success) apps = request.findall('app') userid_counting(userid, apps, platform) self.assertEqual(len(events_appid_version(version_2)), 1) self.assertEqual(len(events_appid_platform_version(version_2)), 1) self.assertEqual(len(events_appid_platform_channel_version(version_2)), 1)
def test_update_live_statistics(self): userid1 = '{F07B3878-CD6F-4B96-B52F-95C4D23077E0}' user1_id = get_id(userid1) userid2 = '{EC4C5647-F798-4BCA-83DA-926CD448A1D5}' user2_id = get_id(userid2) appid = '{F97917B1-19AB-48C1-9802-CEF305B10804}' version = '0.0.0.1' channel = 'test' test_app = dict(appid=appid, version=version, tag=channel) platform = 'mac' request_version_events = HourEvents('request:{}:{}'.format(appid, version)) request_platform_version_events = HourEvents('request:{}:{}:{}'.format(appid, 'mac', version)) request_appid_platform_channel_version = HourEvents('request:{}:{}:{}:{}'.format(appid, 'mac', channel, version)) self.assertFalse(user1_id in request_version_events) self.assertEqual(len(request_version_events), 0) self.assertFalse(user1_id in request_platform_version_events) self.assertEqual(len(request_platform_version_events), 0) self.assertFalse(user1_id in request_appid_platform_channel_version) self.assertEqual(len(request_appid_platform_channel_version), 0) userid_counting(userid1, test_app, platform) self.assertTrue(user1_id in request_version_events) self.assertEqual(len(request_version_events), 1) self.assertTrue(user1_id in request_platform_version_events) self.assertEqual(len(request_platform_version_events), 1) self.assertTrue(user1_id in request_appid_platform_channel_version) self.assertEqual(len(request_appid_platform_channel_version), 1) userid_counting(userid1, test_app, platform) self.assertTrue(user1_id in request_version_events) self.assertEqual(len(request_version_events), 1) self.assertTrue(user1_id in request_platform_version_events) self.assertEqual(len(request_platform_version_events), 1) self.assertTrue(user1_id in request_appid_platform_channel_version) self.assertEqual(len(request_appid_platform_channel_version), 1) userid_counting(userid2, test_app, platform) self.assertTrue(user2_id in request_version_events) self.assertEqual(len(request_version_events), 2) self.assertTrue(user2_id in request_platform_version_events) self.assertEqual(len(request_platform_version_events), 2) self.assertTrue(user2_id in request_appid_platform_channel_version) self.assertEqual(len(request_appid_platform_channel_version), 2)
def get_hourly_data_by_platform(app_id, end, n_hours, versions, platform, channel, tz='UTC'): def build_event_name(app_id, platform, channel, v): if channel: return "request:{}:{}:{}:{}".format(app_id, platform, channel, v) else: return "request:{}:{}:{}".format(app_id, platform, v) tzinfo = pytz.timezone(tz) start = end - timezone.timedelta(hours=n_hours) hours = [ datetime( start.year, start.month, start.day, start.hour, tzinfo=pytz.UTC) + timezone.timedelta(hours=x) for x in range(1, n_hours + 1) ] data = [(v, [[ hour.astimezone(tzinfo).strftime("%Y-%m-%dT%H:%M:%S.%fZ"), len( HourEvents.from_date( build_event_name(app_id, platform, channel, v), hour)) ] for hour in hours]) for v in versions] data = [ version_data for version_data in data if sum([data[1] for data in version_data[1]]) ] return dict(data)
def test_update_live_statistics_install(self): request = parse_request(fixtures.request_event_install_success) apps = request.findall('app') app = apps[0] now = datetime.utcnow() userid = 1 platform = 'win' appid = app.get('appid') version_1 = '0.0.0.1' version_2 = '0.0.0.2' events_appid_version_1 = HourEvents('online:{}:{}'.format(appid, version_1), now.year, now.month, now.day, now.hour) events_appid_platform_version_1 = HourEvents('online:{}:{}:{}'.format(appid, platform, version_1), now.year, now.month, now.day, now.hour) self.assertEqual(len(events_appid_version_1), 0) self.assertEqual(len(events_appid_platform_version_1), 0) update_live_statistics(userid, apps, platform) self.assertEqual(len(events_appid_version_1), 1) self.assertEqual(len(events_appid_platform_version_1), 1) request = parse_request(fixtures.request_event_update_success) apps = request.findall('app') update_live_statistics(userid, apps, platform) events_appid_version_1 = HourEvents('online:{}:{}'.format(appid, version_1), now.year, now.month, now.day, now.hour) events_appid_platform_version_1 = HourEvents('online:{}:{}:{}'.format(appid, platform, version_1), now.year, now.month, now.day, now.hour) events_appid_version_2 = HourEvents('online:{}:{}'.format(appid, version_2), now.year, now.month, now.day, now.hour) events_appid_platform_version_2 = HourEvents('online:{}:{}:{}'.format(appid, platform, version_2), now.year, now.month, now.day, now.hour) self.assertEqual(len(events_appid_version_1), 0) self.assertEqual(len(events_appid_platform_version_1), 0) self.assertEqual(len(events_appid_version_2), 1) self.assertEqual(len(events_appid_platform_version_2), 1) request = parse_request(fixtures.request_event_uninstall_success) apps = request.findall('app') update_live_statistics(userid, apps, platform) events_appid_version_2 = HourEvents('online:{}:{}'.format(appid, version_2), now.year, now.month, now.day, now.hour) events_appid_platform_version_2 = HourEvents('online:{}:{}:{}'.format(appid, platform, version_2), now.year, now.month, now.day, now.hour) self.assertEqual(len(events_appid_version_2), 1) self.assertEqual(len(events_appid_platform_version_2), 1)
def test_update_live_statistics(self): userid1 = '{F07B3878-CD6F-4B96-B52F-95C4D23077E0}' user1_id = get_id(userid1) userid2 = '{EC4C5647-F798-4BCA-83DA-926CD448A1D5}' user2_id = get_id(userid2) appid = '{F97917B1-19AB-48C1-9802-CEF305B10804}' version = '0.0.0.1' request_version_events = HourEvents('online:{}:{}'.format( appid, version)) request_platform_version_events = HourEvents('online:{}:{}:{}'.format( appid, 'mac', version)) self.assertFalse(user1_id in request_version_events) self.assertEqual(len(request_version_events), 0) self.assertFalse(user1_id in request_platform_version_events) self.assertEqual(len(request_platform_version_events), 0) update_live_statistics(userid1, appid, version) self.assertTrue(user1_id in request_version_events) self.assertEqual(len(request_version_events), 1) self.assertTrue(user1_id in request_platform_version_events) self.assertEqual(len(request_platform_version_events), 1) update_live_statistics(userid1, appid, version) self.assertTrue(user1_id in request_version_events) self.assertEqual(len(request_version_events), 1) self.assertTrue(user1_id in request_platform_version_events) self.assertEqual(len(request_platform_version_events), 1) update_live_statistics(userid2, appid, version) self.assertTrue(user2_id in request_version_events) self.assertEqual(len(request_version_events), 2) self.assertTrue(user2_id in request_platform_version_events) self.assertEqual(len(request_platform_version_events), 2)
def get_versions_data_by_platform(app_id, end, n_hours, versions, platform, tz='UTC'): tzinfo = pytz.timezone(tz) start = end - timezone.timedelta(hours=n_hours) event_name = "online:{}:{}:{}" hours = [datetime(start.year, start.month, start.day, start.hour, tzinfo=pytz.UTC) + timezone.timedelta(hours=x) for x in range(1, n_hours + 1)] data = [(v, [[hour.astimezone(tzinfo).strftime("%Y-%m-%dT%H:%M:%S.%fZ"), len(HourEvents.from_date(event_name.format(app_id, platform, v), hour))] for hour in hours]) for v in versions] data = filter(lambda version_data: sum([data[1] for data in version_data[1]]), data) return dict(data)
def test_add_app_live_statistics(self): request = parse_request(fixtures.request_update_check) app = request.findall('app')[0] now = datetime.utcnow() userid = 1 platform = 'win' appid = app.get('appid') version = app.get('version') events_appid_version = HourEvents('online:{}:{}'.format(appid, version), now.year, now.month, now.day, now.hour) events_appid_platform_version = HourEvents('online:{}:{}:{}'.format(appid, platform, version), now.year, now.month, now.day, now.hour) self.assertEqual(len(events_appid_version), 0) self.assertEqual(len(events_appid_platform_version), 0) add_app_live_statistics(userid, platform, app) self.assertEqual(len(events_appid_version), 1) self.assertEqual(len(events_appid_platform_version), 1) self.assertIn(userid, events_appid_version) self.assertIn(userid, events_appid_platform_version)
def get_hourly_data_by_platform(app_id, end, n_hours, versions, platform, channel, tz='UTC'): def build_event_name(app_id, platform, channel, v): if channel: return "request:{}:{}:{}:{}".format(app_id, platform, channel, v) else: return "request:{}:{}:{}".format(app_id, platform, v) tzinfo = pytz.timezone(tz) start = end - timezone.timedelta(hours=n_hours) hours = [datetime(start.year, start.month, start.day, start.hour, tzinfo=pytz.UTC) + timezone.timedelta(hours=x) for x in range(1, n_hours + 1)] data = [(v, [[hour.astimezone(tzinfo).strftime("%Y-%m-%dT%H:%M:%S.%fZ"), len(HourEvents.from_date(build_event_name(app_id, platform, channel, v), hour))] for hour in hours]) for v in versions] data = filter(lambda version_data: sum([data[1] for data in version_data[1]]), data) return dict(data)