Potential bug in HLLRedisCounter#weekly_redis_keys

Following up from master:broken #295173 (closed), there might be a bug in how HLLRedisCounter#weekly_redis_keys calculates the redis key.

https://gitlab.com/gitlab-org/gitlab/blob/5a73f50f2fa1735290e535a2bafd62e588f04101/lib/gitlab/usage_data_counters/hll_redis_counter.rb#L338-345

        def weekly_redis_keys(events:, start_date:, end_date:, context: '')
          weeks = end_date.to_date.cweek - start_date.to_date.cweek
          weeks = 1 if weeks == 0

          (0..(weeks - 1)).map do |week_increment|
            events.map { |event| redis_key(event, start_date + week_increment * 7.days, context) }
          end.flatten
        end

Given a start date and end date where the end date occurs earlier in calendar week compared to start date, HLLRedisCounter#weekly_redis_keys would return an empty array.

For example, start_date of 2020-12-26 and end_date of 2021-02-01 would correspond to cweek of 52 and 5 respectively.

As a result weeks = end_date.to_date.cweek - start_date.to_date.cweek would equal to -47 and subsequently the range (0..-47) would be an empty array.

Example test to reproduce:

      context 'when data crosses into new year' do
        it 'does not raise error' do
          expect { described_class.unique_events(event_names: [weekly_event], start_date: DateTime.parse('2020-12-26'), end_date: DateTime.parse('2021-02-01')) }
            .not_to raise_error(Gitlab::Instrumentation::RedisClusterValidator::CrossSlotError)
        end
      end
Edited by Albert Salim