Ejemplo n.º 1
0
def get_users_statistics_months(app_id,
                                platform=None,
                                year=None,
                                start=1,
                                end=12):
    now = timezone.now()
    if not year:
        year = now.year

    if platform:
        install_event_name = 'new_install:{}:{}'.format(app_id, platform)
        update_event_name = 'request:{}:{}'.format(app_id, platform)
    else:
        install_event_name = 'new_install:%s' % app_id
        update_event_name = 'request:%s' % app_id

    installs_by_month = []
    updates_by_month = []
    for m in range(start, end + 1):
        installs_by_month.append(MonthEvents(install_event_name, year, m))
        updates_by_month.append(MonthEvents(update_event_name, year, m))
    installs_data = [(datetime(year, start + i, 1).strftime("%Y-%m"), len(e))
                     for i, e in enumerate(installs_by_month)]
    updates_data = [(datetime(year, start + i, 1).strftime("%Y-%m"), len(e))
                    for i, e in enumerate(updates_by_month)]
    return dict(new=installs_data, updates=updates_data)
Ejemplo n.º 2
0
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)
Ejemplo n.º 3
0
def test_different_buckets():
    now = datetime.utcnow()

    mark_event('active', 123)
    mark_event('tasks:completed', 23232)

    assert MonthEvents('active', now.year, now.month).get_count() == 1
    assert MonthEvents('tasks:completed', now.year, now.month).get_count() == 1
Ejemplo n.º 4
0
def test_mark_unmark():
    now = datetime.utcnow()

    mark_event('active', 125)
    assert 125 in MonthEvents('active', now.year, now.month)

    unmark_event('active', 125)
    assert 125 not in MonthEvents('active', now.year, now.month)
Ejemplo n.º 5
0
def test_mark_counts():
    now = datetime.utcnow()

    assert MonthEvents('active', now.year, now.month).get_count() == 0

    mark_event('active', 123)
    mark_event('active', 23232)

    assert len(MonthEvents('active', now.year, now.month)) == 2
Ejemplo n.º 6
0
def test_events_marked():
    delete_all_events()

    now = datetime.utcnow()

    assert MonthEvents('active', now.year, now.month).get_count() == 0
    assert MonthEvents('active', now.year, now.month).has_events_marked() == False

    mark_event('active', 123, now=now)

    assert MonthEvents('active', now.year, now.month).get_count() == 1
    assert MonthEvents('active', now.year, now.month).has_events_marked() == True
Ejemplo n.º 7
0
def add_app_statistics(userid, platform, app, now=None):
    mark = partial(mark_event, now=now)
    if not now:
        now = timezone.now()
    appid = app.get('appid')
    version = app.get('version')
    channel = app.get('tag') or DEFAULT_CHANNEL

    if is_new_install(appid, userid):
        mark('new_install:%s' % appid, userid)
        mark('new_install:{}:{}'.format(appid, platform), userid)
        redis.setbit("known_users:%s" % appid, userid, 1)
    elif userid not in MonthEvents('new_install:{}:{}'.format(appid, platform),
                                   year=now.year,
                                   month=now.month):
        mark('request:%s' % appid, userid)
        mark('request:{}:{}'.format(appid, platform), userid)

    mark('request:{}:{}'.format(appid, channel), userid)
    mark('request:{}:{}'.format(appid, version), userid, track_hourly=True)
    mark('request:{}:{}:{}'.format(appid, platform, version),
         userid,
         track_hourly=True)
    mark('request:{}:{}:{}:{}'.format(appid, platform, channel, version),
         userid,
         track_hourly=True)
Ejemplo n.º 8
0
def add_app_statistics(userid, platform, app, now=None):
    mark = partial(mark_event, now=now)
    if not now:
        now = timezone.now()
    appid = app.get('appid')
    version = app.get('version')
    channel = parser.get_channel(app)
    events = app.findall('event')
    err_events = filter(lambda x: x.get('eventresult') not in ['1', '2', '3'], events)
    if err_events:
        return
    install_event = filter(lambda x: x.get('eventtype') == '2', events)
    if is_new_install(appid, userid):
        if install_event and install_event[0].get('eventresult') in ['1', '2', '3']:
            mark('new_install:%s' % appid, userid)
            mark('new_install:{}:{}'.format(appid, platform), userid)
            redis.setbit("known_users:%s" % appid, userid, 1)
    elif userid not in MonthEvents('new_install:{}:{}'.format(appid, platform), year=now.year, month=now.month):
        mark('request:%s' % appid, userid)
        mark('request:{}:{}'.format(appid, platform), userid)

    uninstall_event = filter(lambda x: x.get('eventtype') == '4', events)
    if uninstall_event and uninstall_event[0].get('eventresult') in ['1', '2', '3']:
        mark('uninstall:%s' % appid, userid)
        mark('uninstall:{}:{}'.format(appid, platform), userid)

    mark('request:{}:{}'.format(appid, version), userid)
    mark('request:{}:{}'.format(appid, channel), userid)
    mark('request:{}:{}:{}'.format(appid, platform, version), userid)
Ejemplo n.º 9
0
def get_users_versions_by_platform(app_id, platform, date):
    if platform == 'win':
        versions = [str(v.version) for v in Version.objects.filter_by_enabled(app__id=app_id)]
    else:
        versions = [str(v.short_version) for v in SparkleVersion.objects.filter_by_enabled(app__id=app_id)]
    event_name = 'request:{}:{}:{}'
    data = [(v, len(MonthEvents(event_name.format(app_id, platform, v), date.year, date.month))) for v in versions]
    return data
Ejemplo n.º 10
0
def get_channel_statistics(app_id, date=None):
    if not date:
        date = timezone.now()

    event_name = 'request:{}:{}'
    channels = [c.name for c in Channel.objects.all()]
    data = [(channel, len(MonthEvents(event_name.format(app_id, channel), date.year, date.month))) for channel in channels]
    data = filter(lambda x: x[1], data)
    return data
Ejemplo n.º 11
0
def get_users_versions_by_platform(app_id, platform, date):
    if platform == 'mac':
        versions = [str(v) for v in SparkleVersion.objects.filter_by_enabled(app_id=app_id).values_list('short_version', flat=True)]
    else:
        versions = [str(v) for v in Version.objects.filter_by_enabled(app__id=app_id, platform__name=platform).values_list('version', flat=True)]
    event_name = 'request:{}:{}:{}'
    data = [(v, len(MonthEvents(event_name.format(app_id, platform, v), date.year, date.month))) for v in versions]
    data = filter(lambda x: x[1], data)
    return dict(data)
Ejemplo n.º 12
0
def get_users_statistics_months(app_id=None):
    now = timezone.now()
    year = now.year
    event_name = 'request:%s' % app_id if app_id else 'request'

    months = []
    for m in range(1, 13):
        months.append(MonthEvents(event_name, year, m))
    data = [(datetime(year, i + 1, 1).strftime("%B"), len(e))
            for i, e in enumerate(months)]
    return data
Ejemplo n.º 13
0
def test_mark_iter():
    now = datetime.utcnow()
    ev = MonthEvents('active', now.year, now.month)

    assert list(ev) == []

    mark_event('active', 5)
    mark_event('active', 55)
    mark_event('active', 555)
    mark_event('active', 5555)

    assert list(ev) == [5, 55, 555, 5555]
Ejemplo n.º 14
0
def add_app_statistics(userid, platform, app, now=None):
    mark = partial(mark_event, now=now)
    if not now:
        now = timezone.now()
    appid = app.get('appid')
    version = app.get('version')
    channel = parser.get_channel(app)
    events = app.findall('event')
    nextversion = app.get('nextversion')

    err_events = [x for x in events if x.get('eventresult') not in ['1', '2', '3']]
    if err_events:
        return

    install_event = [x for x in events if x.get('eventtype') == '2']
    if is_new_install(appid, userid):
        if install_event:
            mark('new_install:%s' % appid, userid)
            mark('new_install:{}:{}'.format(appid, platform), userid)
            redis.setbit("known_users:%s" % appid, userid, 1)
            mark('request:{}:{}'.format(appid, nextversion), userid, track_hourly=True)
            mark('request:{}:{}:{}'.format(appid, platform, nextversion), userid, track_hourly=True)
            mark('request:{}:{}:{}:{}'.format(appid, platform, channel, nextversion), userid, track_hourly=True)
            mark('request:{}:{}'.format(appid, channel), userid)
            return

    elif userid not in MonthEvents('new_install:{}:{}'.format(appid, platform), year=now.year, month=now.month):
        mark('request:%s' % appid, userid)
        mark('request:{}:{}'.format(appid, platform), userid)
        if nextversion:
            mark('request:{}:{}'.format(appid, nextversion), userid, track_hourly=True)
            mark('request:{}:{}:{}'.format(appid, platform, nextversion), userid, track_hourly=True)
            mark('request:{}:{}:{}:{}'.format(appid, platform, channel, nextversion), userid, track_hourly=True)

    uninstall_event = [x for x in events if x.get('eventtype') == '4']
    if uninstall_event:
        mark('uninstall:%s' % appid, userid)
        mark('uninstall:{}:{}'.format(appid, platform), userid)
    update_event = [x for x in events if x.get('eventtype') == '3']
    if update_event:
        unmark_event('request:{}:{}'.format(appid, version), userid, track_hourly=True)
        unmark_event('request:{}:{}:{}'.format(appid, platform, version), userid, track_hourly=True)
        unmark_event('request:{}:{}:{}:{}'.format(appid, platform, channel, version), userid, track_hourly=True)
        mark('request:{}:{}'.format(appid, nextversion), userid, track_hourly=True)
        mark('request:{}:{}:{}'.format(appid, platform, nextversion), userid, track_hourly=True)
        mark('request:{}:{}:{}:{}'.format(appid, platform, channel, nextversion), userid, track_hourly=True)
    else:
        mark('request:{}:{}'.format(appid, version), userid, track_hourly=True)
        mark('request:{}:{}:{}'.format(appid, platform, version), userid, track_hourly=True)
        mark('request:{}:{}:{}:{}'.format(appid, platform, channel, version), userid, track_hourly=True)
    mark('request:{}:{}'.format(appid, channel), userid)
Ejemplo n.º 15
0
def _(period, userid):
    return get_id(userid) in MonthEvents.from_date('request', timezone.now())
Ejemplo n.º 16
0
def _(period, userid):
    return get_id(userid) in MonthEvents.from_date('request', timezone.now())
Ejemplo n.º 17
0
def _month_events_fn(key, date, system):
    return MonthEvents(key, date.year, date.month, system=system)
Ejemplo n.º 18
0
def test_bit_operations():
    delete_all_events()

    now = datetime.utcnow()
    last_month = datetime.utcnow() - timedelta(days=30)

    # 123 has been active for two months
    mark_event('active', 123, now=now)
    mark_event('active', 123, now=last_month)

    # 224 has only been active last_month
    mark_event('active', 224, now=last_month)

    # Assert basic premises
    assert MonthEvents('active', last_month.year,
                       last_month.month).get_count() == 2
    assert MonthEvents('active', now.year, now.month).get_count() == 1

    # Try out with bit AND operation
    active_2_months = BitOpAnd(
        MonthEvents('active', last_month.year, last_month.month),
        MonthEvents('active', now.year, now.month))
    assert active_2_months.get_count() == 1
    assert 123 in active_2_months
    assert 224 not in active_2_months
    active_2_months.delete()

    # Try out with bit OR operation
    assert BitOpOr(MonthEvents('active', last_month.year, last_month.month),
                   MonthEvents('active', now.year, now.month)).get_count() == 2

    # Try out with a different system
    active_2_months = BitOpAnd(
        'default_copy',
        MonthEvents('active', last_month.year, last_month.month),
        MonthEvents('active', now.year, now.month),
    )
    assert active_2_months.get_count() == 1
    assert active_2_months.system == 'default_copy'
    active_2_months.delete()

    # Try nested operations
    active_2_months = BitOpAnd(
        BitOpAnd(MonthEvents('active', last_month.year, last_month.month),
                 MonthEvents('active', now.year, now.month)),
        MonthEvents('active', now.year, now.month))

    assert 123 in active_2_months
    assert 224 not in active_2_months
    active_2_months.delete()