Beispiel #1
0
    def test_core_add_sotw_whenCalledWithNoNeutralZones_persistsData(self):
        core = AlbaSotwCore()

        core.add_sotw(123456)
        key_name = core.get_sotw_key_name()

        segment_id = redisclient.get('%s_segment' % key_name)
        neutral_zones = redisclient.smembers('%s_neutral_zones' % key_name)

        self.assertEqual(segment_id, '123456')
        self.assertEqual(len(neutral_zones), 0)

        return True
Beispiel #2
0
    def test_core_add_sotw_whenCalledWithNeutralZones_persistsData(self):
        core = AlbaSotwCore()
        key_name = core.get_sotw_key_name()

        redisclient.delete('%s_segment' % key_name)
        redisclient.delete('%s_neutral_zones' % key_name)

        core.add_sotw(123456, [234567, 345678, 456789])

        segment_id = redisclient.get('%s_segment' % key_name)
        neutral_zones = redisclient.smembers('%s_neutral_zones' % key_name)

        self.assertEqual(segment_id, '123456')
        self.assertEqual(len(neutral_zones), 3)
        self.assertTrue('234567' in neutral_zones)
        self.assertTrue('345678' in neutral_zones)
        self.assertTrue('456789' in neutral_zones)
Beispiel #3
0
    def compile_efforts(self, year=None, week_number=None):
        log.info('Method=compile_efforts Year=%s WeekNumber=%s' %
                 (year, week_number))
        leagues = {}

        starttime = datetime.combine(
            date.today() - timedelta(date.today().weekday()),
            datetime.min.time())
        key_name = self.get_sotw_key_name()

        if year != None and week_number != None:
            key_name = 'sotw_%s_%s' % (year, week_number)
            d = "%s-W%s" % (year, week_number)
            r = datetime.strptime(d + '-0', "%Y-W%W-%w")
            starttime = datetime.combine(r - timedelta(r.weekday()),
                                         datetime.min.time())

        endtime = starttime + timedelta(days=6, hours=20)
        log.info('Method=compile_efforts ResultKey=%s' % key_name)

        log.info(
            'Method=compile_efforts Message="Getting segment data from redis"')

        segment = redisclient.get('%s_segment' % key_name)
        log.info('Method=compile_efforts Segment=%s' % segment)

        neutral_zones = redisclient.smembers('%s_neutral_zones' % key_name)
        log.info('Method=compile_efforts NeutralZones=%s' % neutral_zones)

        log.info(
            'Method=compile_efforts Message="Getting segment data from Strava" Segment=%s StartTime=%s EndTime=%s'
            % (segment, starttime, endtime))
        try:
            segment_efforts = Strava().get_efforts(segment, starttime, endtime)
            log.info(
                'Method=compile_efforts Message="Data from Strava" ResultCount=%s'
                % (len(segment_efforts)))
        except Exception as e:
            log.exception(
                'Method=compile_efforts Message="Strava call failed"')
            raise (e)

        for segment_effort in segment_efforts:
            segment_effort['neutralised'] = {}

        for neutral_zone in neutral_zones:
            log.info(
                'Method=compile_efforts Message="Getting segment data from Strava" Segment=%s StartTime=%s EndTime=%s'
                % (neutral_zone, starttime, endtime))
            try:
                neutral_zone_efforts = Strava().get_efforts(
                    neutral_zone, starttime, endtime)
                log.info(
                    'Method=compile_efforts Message="Data from Strava" ResultCount=%s'
                    % (len(neutral_zone_efforts)))
            except Exception as e:
                log.exception(
                    'Method=compile_efforts Message="Strava call failed"')
                raise (e)

            # neutral_zone_efforts = Strava().get_efforts(neutral_zone, starttime, endtime)

            for neutral_zone_effort in neutral_zone_efforts:
                for segment_effort in segment_efforts:
                    if neutral_zone_effort['activity']['id'] == segment_effort[
                            'activity']['id']:
                        segment_effort['neutralised'][
                            neutral_zone] = neutral_zone_effort['elapsed_time']

        divisions = redisclient.smembers('divisions')

        for divisionId in divisions:
            division = divisionId
            # division_name = redisclient.hgetasstring(division, 'name')
            leagues[division] = self.compile_league(division, segment_efforts)

        return leagues
Beispiel #4
0
def efforts(year=None, week_number=None):
    transaction_start_time = datetime.now()
    log.info('Method=efforts Year=%s WeekNumber=%s' % (year, week_number))
    user_profile = None
    error = None
    sorted_results = None

    if year == None or week_number == None:
        result_set = get_results_key()
    else:
        result_set = 'results_%s_%s' % (year, week_number)

    if 'profile' in session:
        user_profile = session['profile']

    log.debug('Method=efforts Message="Getting data from redis" ResultSet=%s' %
              result_set)
    results_json = redisclient.get(result_set)
    log.debug(
        'Method=efforts Message="Retrieved data from redis" Length=%s DataSample="%s"'
        % (len(results_json), results_json[:20]))

    if results_json != None and results_json != 'null':
        results = json.loads(results_json)
        log.debug('Method=efforts Message="Data parsed" ElementCount=%s' %
                  len(results))

        sorted_results = {}

        log.debug('Method=efforts Message="Sorting Results"')
        for division, table in results.items():
            sorted_results[division] = OrderedDict(
                sorted(table.items(), key=lambda t: t[1]['rank']))
    else:
        log.info('Method=efforts Message="No data found for selected period"')

    log.debug('Method=efforts Message="Getting result sets list from redis"')
    result_sets = []
    result_keys = redisclient.keys("results_*")
    log.debug(
        'Method=efforts Message="Results keys retrieved" ResultKeys="%s"' %
        result_keys)

    for result_key in result_keys:
        result_year = result_key.split('_')[1]
        result_week_number = result_key.split('_')[2]
        result_sets.append([result_year, result_week_number])

    log.debug('Method=efforts Message="Rendering page"')
    rendered_page = render_template('efforts.html',
                                    leagues=sorted_results,
                                    user=user_profile,
                                    error=error,
                                    result_sets=result_sets,
                                    this_week=[year, week_number])
    log.debug(
        'Method=efforts Message="Page rendered" Length=%s DataSample="%s"' %
        (len(rendered_page), rendered_page[:10]))
    time_taken = datetime.now() - transaction_start_time
    log.info('PERF Method=efforts ms=%s' % (time_taken.microseconds))
    return rendered_page
Beispiel #5
0
 def get_access_token(self):
     return redisclient.get('api_token')
Beispiel #6
0
def test(param):
    key_name = 'test_value'
    redisclient.set(key_name, param)
    persisted_value = redisclient.get(key_name)
    return "OK %s" % persisted_value
Beispiel #7
0
    def compile_efforts(self, year=None, week_number=None):
        log.info('Method=compile_efforts Year=%s WeekNumber=%s' % (year, week_number))
        leagues = {}

        starttime = datetime.combine(date.today() - timedelta(date.today().weekday()), datetime.min.time())
        key_name = self.get_sotw_key_name()

        if year != None and week_number != None:
            key_name = 'sotw_%s_%s' % (year, week_number)
            d = "%s-W%s" % (year, week_number)
            r = datetime.strptime(d + '-0', "%Y-W%W-%w")
            starttime = datetime.combine(r - timedelta(r.weekday()), datetime.min.time())

        endtime = starttime + timedelta(days=6, hours=20)
        log.info('Method=compile_efforts ResultKey=%s' % key_name)

        log.info('Method=compile_efforts Message="Getting segment data from redis"')

        segment = redisclient.get('%s_segment' % key_name)
        log.info('Method=compile_efforts Segment=%s' % segment)

        neutral_zones = redisclient.smembers('%s_neutral_zones' % key_name)
        log.info('Method=compile_efforts NeutralZones=%s' % neutral_zones)

        log.info(
            'Method=compile_efforts Message="Getting segment data from Strava" Segment=%s StartTime=%s EndTime=%s'
            % (segment, starttime, endtime))
        try:
            segment_efforts = Strava().get_efforts(segment, starttime, endtime)
            log.info('Method=compile_efforts Message="Data from Strava" ResultCount=%s' % (len(segment_efforts)))
        except Exception as e:
            log.exception('Method=compile_efforts Message="Strava call failed"')
            raise(e)

        for segment_effort in segment_efforts:
            segment_effort['neutralised'] = {}

        for neutral_zone in neutral_zones:
            log.info(
                'Method=compile_efforts Message="Getting segment data from Strava" Segment=%s StartTime=%s EndTime=%s'
                % (neutral_zone, starttime, endtime))
            try:
                neutral_zone_efforts = Strava().get_efforts(neutral_zone, starttime, endtime)
                log.info('Method=compile_efforts Message="Data from Strava" ResultCount=%s' % (len(neutral_zone_efforts)))
            except Exception as e:
                log.exception('Method=compile_efforts Message="Strava call failed"')
                raise (e)

            # neutral_zone_efforts = Strava().get_efforts(neutral_zone, starttime, endtime)

            for neutral_zone_effort in neutral_zone_efforts:
                for segment_effort in segment_efforts:
                    if neutral_zone_effort['activity']['id'] == segment_effort['activity']['id']:
                        segment_effort['neutralised'][neutral_zone] = neutral_zone_effort['elapsed_time']

        divisions = redisclient.smembers('divisions')

        for divisionId in divisions:
            division = divisionId
            # division_name = redisclient.hgetasstring(division, 'name')
            leagues[division] = self.compile_league(division, segment_efforts)

        return leagues
Beispiel #8
0
 def get_access_token(self):
     return redisclient.get('api_token')