File size: 6,591 Bytes
c9c49ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
defmodule Plausible.Stats.SamplingCacheTest do
  use Plausible.DataCase, async: true

  on_ee do
    alias Plausible.Stats.SamplingCache

    @site_id1 100_000
    @site_id2 200_000
    @site_id3 300_000
    @site_id4 400_000
    @site_id5 500_000
    @site_id6 600_000
    @site_id7 700_000
    @site_id8 800_000
    @site_id9 900_000

    setup do
      Plausible.IngestRepo.query!("truncate ingest_counters")

      on_exit(fn ->
        Plausible.IngestRepo.query!("truncate ingest_counters")
      end)

      :ok
    end

    describe "getter" do
      @threshold Plausible.Stats.Sampling.default_sample_threshold()

      test "returns cached values for traffic in past 30 days", %{test: test} do
        now = DateTime.utc_now()

        {6, _} =
          Plausible.IngestRepo.insert_all(Plausible.Ingestion.Counters.Record, [
            %{
              site_id: @site_id1,
              domain: "1.com",
              value: (@threshold * 0.55) |> trunc(),
              event_timebucket: add(now, -1, :day),
              metric: "buffered"
            },
            %{
              site_id: @site_id2,
              domain: "2.com",
              value: (@threshold * 0.55) |> trunc(),
              event_timebucket: add(now, -1, :day),
              metric: "buffered"
            },
            %{
              site_id: @site_id2,
              domain: "2.com",
              value: (@threshold * 0.55) |> trunc(),
              event_timebucket: add(now, -5, :day),
              metric: "buffered"
            },
            %{
              site_id: @site_id2,
              domain: "2.com",
              value: (@threshold * 0.55) |> trunc(),
              event_timebucket: add(now, -35, :day),
              metric: "buffered"
            },
            %{
              site_id: @site_id3,
              domain: "3.com",
              value: (@threshold * 2.05) |> trunc(),
              event_timebucket: add(now, -35, :day),
              metric: "buffered"
            },
            %{
              site_id: @site_id4,
              domain: "4.com",
              value: (@threshold * 0.55) |> trunc(),
              event_timebucket: add(now, -35, :day),
              metric: "buffered"
            }
          ])

        start_test_cache(test)

        assert SamplingCache.get(@site_id1, force?: true, cache_name: test) == 5_500_000
        assert SamplingCache.get(@site_id2, force?: true, cache_name: test) == 1.1 * @threshold
        assert SamplingCache.get(@site_id3, force?: true, cache_name: test) == nil
        assert SamplingCache.get(@site_id4, force?: true, cache_name: test) == nil

        Plausible.IngestRepo.insert_all(Plausible.Ingestion.Counters.Record, [
          %{
            site_id: @site_id1,
            value: (@threshold * 0.55) |> trunc(),
            event_timebucket: add(now, -1, :day),
            metric: "buffered"
          }
        ])

        :ok = SamplingCache.refresh_all(cache_name: test)

        assert SamplingCache.get(@site_id1, force?: true, cache_name: test) == 1.1 * @threshold
        assert SamplingCache.get(@site_id2, force?: true, cache_name: test) == 1.1 * @threshold
      end

      test "returns cached values for traffic in past 30 days for consolidated site", %{
        test: test
      } do
        now = DateTime.utc_now()

        Plausible.IngestRepo.insert_all(Plausible.Ingestion.Counters.Record, [
          %{
            site_id: @site_id5,
            domain: "5.com",
            value: (@threshold * 0.55) |> trunc(),
            event_timebucket: add(now, -1, :day),
            metric: "buffered"
          },
          %{
            site_id: @site_id5,
            domain: "5.com",
            value: (@threshold * 0.55) |> trunc(),
            event_timebucket: add(now, -1, :day),
            metric: "buffered"
          },
          %{
            site_id: @site_id6,
            domain: "6.com",
            value: (@threshold * 0.55) |> trunc(),
            event_timebucket: add(now, -1, :day),
            metric: "buffered"
          },
          %{
            site_id: @site_id6,
            domain: "6.com",
            value: (@threshold * 0.55) |> trunc(),
            event_timebucket: add(now, -5, :day),
            metric: "buffered"
          },
          %{
            site_id: @site_id6,
            domain: "6.com",
            value: (@threshold * 0.55) |> trunc(),
            event_timebucket: add(now, -35, :day),
            metric: "buffered"
          }
        ])

        start_test_cache(test)

        assert SamplingCache.consolidated_get([@site_id5, @site_id6],
                 cache_name: test,
                 force?: true
               ) == 1.1 * @threshold * 2
      end

      test "consolidated_get sum over threshold", %{
        test: test
      } do
        now = DateTime.utc_now()

        Plausible.IngestRepo.insert_all(Plausible.Ingestion.Counters.Record, [
          %{
            site_id: @site_id7,
            domain: "7.com",
            value: div(@threshold, 2),
            event_timebucket: add(now, -1, :day),
            metric: "buffered"
          },
          %{
            site_id: @site_id8,
            domain: "8.com",
            value: div(@threshold, 2),
            event_timebucket: add(now, -1, :day),
            metric: "buffered"
          },
          %{
            site_id: @site_id9,
            domain: "9.com",
            value: div(@threshold, 2),
            event_timebucket: add(now, -1, :day),
            metric: "buffered"
          }
        ])

        start_test_cache(test)

        assert SamplingCache.consolidated_get([@site_id7, @site_id8],
                 cache_name: test,
                 force?: true
               ) == @threshold

        assert SamplingCache.consolidated_get([@site_id9],
                 cache_name: test,
                 force?: true
               ) == div(@threshold, 2)
      end

      test "conslidated_get returns nil", %{test: test} do
        start_test_cache(test)

        assert is_nil(
                 SamplingCache.consolidated_get([@site_id5, @site_id6],
                   cache_name: test,
                   force?: true
                 )
               )
      end
    end

    defp add(datetime, n, unit) do
      DateTime.add(datetime, n, unit) |> DateTime.truncate(:second)
    end

    defp start_test_cache(cache_name) do
      %{start: {m, f, a}} = SamplingCache.child_spec(cache_name: cache_name)

      {:ok, _pid} = apply(m, f, a)
      :ok = SamplingCache.refresh_all(cache_name: cache_name)
    end
  end
end