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

total_count_metric_spec.rb « instrumentations « metrics « usage « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b357d6ea7e403b248224c2445731bd5da747bbe8 (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Usage::Metrics::Instrumentations::TotalCountMetric, :clean_gitlab_redis_shared_state,
  feature_category: :product_analytics_data_management do
  before do
    allow(Gitlab::InternalEvents::EventDefinitions).to receive(:known_event?).and_return(true)
  end

  context 'with multiple similar events' do
    before do
      last_week = Date.today - 7.days
      two_weeks_ago = last_week - 1.week

      redis_counter_key = described_class.redis_key('my_event', last_week)
      2.times do
        Gitlab::Redis::SharedState.with { |redis| redis.incr(redis_counter_key) }
      end

      redis_counter_key = described_class.redis_key('my_event', two_weeks_ago)
      3.times do
        Gitlab::Redis::SharedState.with { |redis| redis.incr(redis_counter_key) }
      end

      10.times do
        Gitlab::InternalEvents.track_event('my_event')
      end
    end

    context "with an 'all' time_frame" do
      let(:expected_value) { 10 }

      it_behaves_like 'a correct instrumented metric value', { time_frame: 'all', events: [{ name: 'my_event' }] }
    end

    context "with a 7d time_frame" do
      let(:expected_value) { 2 }

      it_behaves_like 'a correct instrumented metric value', { time_frame: '7d', events: [{ name: 'my_event' }] }
    end

    context "with a 28d time_frame" do
      let(:expected_value) { 5 }

      it_behaves_like 'a correct instrumented metric value', { time_frame: '28d', events: [{ name: 'my_event' }] }
    end
  end

  context 'with multiple different events' do
    let(:expected_value) { 2 }

    before do
      last_week = Date.today - 7.days
      two_weeks_ago = last_week - 1.week

      2.times do
        redis_counter_key =
          Gitlab::Usage::Metrics::Instrumentations::TotalCountMetric.redis_key('my_event1', last_week)
        Gitlab::Redis::SharedState.with { |redis| redis.incr(redis_counter_key) }
      end

      3.times do
        redis_counter_key =
          Gitlab::Usage::Metrics::Instrumentations::TotalCountMetric.redis_key('my_event1', two_weeks_ago)
        Gitlab::Redis::SharedState.with { |redis| redis.incr(redis_counter_key) }
      end

      4.times do
        redis_counter_key =
          Gitlab::Usage::Metrics::Instrumentations::TotalCountMetric.redis_key('my_event2', last_week)
        Gitlab::Redis::SharedState.with { |redis| redis.incr(redis_counter_key) }
      end

      Gitlab::InternalEvents.track_event('my_event1')
      Gitlab::InternalEvents.track_event('my_event2')
    end

    context "with an 'all' time_frame" do
      let(:expected_value) { 2 }

      it_behaves_like 'a correct instrumented metric value',
        { time_frame: 'all', events: [{ name: 'my_event1' }, { name: 'my_event2' }] }
    end

    context "with a 7d time_frame" do
      let(:expected_value) { 6 }

      it_behaves_like 'a correct instrumented metric value',
        { time_frame: '7d', events: [{ name: 'my_event1' }, { name: 'my_event2' }] }
    end

    context "with a 28d time_frame" do
      let(:expected_value) { 9 }

      it_behaves_like 'a correct instrumented metric value',
        { time_frame: '28d', events: [{ name: 'my_event1' }, { name: 'my_event2' }] }
    end
  end

  context "with an invalid time_frame" do
    let(:metric) { described_class.new(time_frame: '14d', events: [{ name: 'my_event' }]) }

    it 'raises an exception' do
      expect { metric.value }.to raise_error(/Unknown time frame/)
    end
  end

  describe '.redis_key' do
    it 'adds the key prefix to the event name' do
      expect(described_class.redis_key('my_event')).to eq('{event_counters}_my_event')
    end

    context "with a date" do
      it 'adds the key prefix and suffix to the event name' do
        expect(described_class.redis_key('my_event', Date.new(2023, 10, 19))).to eq("{event_counters}_my_event-2023-42")
      end
    end
  end
end