Welcome to mirror list, hosted at ThFree Co, Russian Federation.

unique_visits_spec.rb « analytics « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0c47cbf346c90aea053d7f80e0e5b1a07b0f9cbe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Analytics::UniqueVisits, :clean_gitlab_redis_shared_state do
  let(:unique_visits) { Gitlab::Analytics::UniqueVisits.new }
  let(:target1_id) { 'g_analytics_contribution' }
  let(:target2_id) { 'g_analytics_insights' }
  let(:target3_id) { 'g_analytics_issues' }
  let(:target4_id) { 'g_compliance_dashboard' }
  let(:target5_id) { 'i_compliance_credential_inventory' }
  let(:visitor1_id) { 'dfb9d2d2-f56c-4c77-8aeb-6cddc4a1f857' }
  let(:visitor2_id) { '1dd9afb2-a3ee-4de1-8ae3-a405579c8584' }
  let(:visitor3_id) { '34rfjuuy-ce56-sa35-ds34-dfer567dfrf2' }

  around do |example|
    # We need to freeze to a reference time
    # because visits are grouped by the week number in the year
    # Without freezing the time, the test may behave inconsistently
    # depending on which day of the week test is run.
    reference_time = Time.utc(2020, 6, 1)
    Timecop.freeze(reference_time) { example.run }
  end

  describe '#track_visit' do
    it 'tracks the unique weekly visits for targets' do
      unique_visits.track_visit(visitor1_id, target1_id, 7.days.ago)
      unique_visits.track_visit(visitor1_id, target1_id, 7.days.ago)
      unique_visits.track_visit(visitor2_id, target1_id, 7.days.ago)

      unique_visits.track_visit(visitor2_id, target2_id, 7.days.ago)
      unique_visits.track_visit(visitor1_id, target2_id, 8.days.ago)
      unique_visits.track_visit(visitor1_id, target2_id, 15.days.ago)

      unique_visits.track_visit(visitor3_id, target4_id, 7.days.ago)

      unique_visits.track_visit(visitor3_id, target5_id, 15.days.ago)
      unique_visits.track_visit(visitor2_id, target5_id, 15.days.ago)

      expect(unique_visits.unique_visits_for(targets: target1_id)).to eq(2)
      expect(unique_visits.unique_visits_for(targets: target2_id)).to eq(1)
      expect(unique_visits.unique_visits_for(targets: target4_id)).to eq(1)

      expect(unique_visits.unique_visits_for(targets: target2_id, start_week: 15.days.ago)).to eq(1)

      expect(unique_visits.unique_visits_for(targets: target3_id)).to eq(0)

      expect(unique_visits.unique_visits_for(targets: target5_id, start_week: 15.days.ago)).to eq(2)

      expect(unique_visits.unique_visits_for(targets: :analytics)).to eq(2)
      expect(unique_visits.unique_visits_for(targets: :analytics, start_week: 15.days.ago)).to eq(1)
      expect(unique_visits.unique_visits_for(targets: :analytics, start_week: 30.days.ago)).to eq(0)

      expect(unique_visits.unique_visits_for(targets: :analytics, weeks: 4)).to eq(2)

      expect(unique_visits.unique_visits_for(targets: :compliance)).to eq(1)
      expect(unique_visits.unique_visits_for(targets: :compliance, start_week: 15.days.ago)).to eq(2)
      expect(unique_visits.unique_visits_for(targets: :compliance, start_week: 30.days.ago)).to eq(0)

      expect(unique_visits.unique_visits_for(targets: :compliance, weeks: 4)).to eq(2)
    end

    it 'sets the keys in Redis to expire automatically after 12 weeks' do
      unique_visits.track_visit(visitor1_id, target1_id)

      Gitlab::Redis::SharedState.with do |redis|
        redis.scan_each(match: "{#{target1_id}}-*").each do |key|
          expect(redis.ttl(key)).to be_within(5.seconds).of(12.weeks)
        end
      end
    end

    it 'raises an error if an invalid target id is given' do
      invalid_target_id = "x_invalid"

      expect do
        unique_visits.track_visit(visitor1_id, invalid_target_id)
      end.to raise_error("Invalid target id #{invalid_target_id}")
    end
  end
end