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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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
defmodule Plausible.Stats.Query.QueryFromTest do
  use Plausible.DataCase, async: true
  alias Plausible.Stats.Query
  alias Plausible.Stats.Legacy.QueryBuilder
  alias Plausible.Stats.DateTimeRange

  doctest Plausible.Stats.Legacy.QueryBuilder

  setup do
    user = new_user()

    site =
      new_site(
        owner: user,
        inserted_at: ~N[2020-01-01T00:00:00],
        stats_start_date: ~D[2020-01-01],
        timezone: "US/Eastern"
      )

    {:ok, site: site, user: user}
  end

  @now ~U[2024-05-03 16:30:00Z]

  @tag :slow
  test "keeps current timestamp so that utc_boundaries don't depend on time passing by", %{
    site: site
  } do
    q1 = %{now: %DateTime{}} = Query.from(site, %{"period" => "realtime"})
    q2 = %{now: %DateTime{}} = Query.from(site, %{"period" => "30m"})
    boundaries1 = Plausible.Stats.Time.utc_boundaries(q1)
    boundaries2 = Plausible.Stats.Time.utc_boundaries(q2)
    :timer.sleep(1500)
    assert ^boundaries1 = Plausible.Stats.Time.utc_boundaries(q1)
    assert ^boundaries2 = Plausible.Stats.Time.utc_boundaries(q2)
  end

  test "parses day format", %{site: site} do
    q = Query.from(site, %{"period" => "day", "date" => "2019-01-01"}, now: @now)

    assert q.utc_time_range.first == ~U[2019-01-01 05:00:00Z]
    assert q.utc_time_range.last == ~U[2019-01-02 04:59:59Z]
  end

  test "day format defaults to today", %{site: site} do
    q = Query.from(site, %{"period" => "day"}, now: @now)

    assert q.utc_time_range.first == ~U[2024-05-03 04:00:00Z]
    assert q.utc_time_range.last == @now
  end

  test "parses realtime format", %{site: site} do
    q = Query.from(site, %{"period" => "realtime"}, now: @now)

    assert q.utc_time_range.first == ~U[2024-05-03 16:25:00Z]
    assert q.utc_time_range.last == ~U[2024-05-03 16:30:05Z]
    assert q.input_date_range == :realtime
  end

  test "parses month format", %{site: site} do
    q = Query.from(site, %{"period" => "month", "date" => "2019-01-01"}, now: @now)

    assert q.utc_time_range.first == ~U[2019-01-01 05:00:00Z]
    assert q.utc_time_range.last == ~U[2019-02-01 04:59:59Z]
  end

  test "parses 6 month format", %{site: site} do
    q = Query.from(site, %{"period" => "6mo"}, now: @now)

    assert q.utc_time_range.first == ~U[2023-11-01 04:00:00Z]
    assert q.utc_time_range.last == ~U[2024-05-01 03:59:59Z]
  end

  test "parses 12 month format", %{site: site} do
    q = Query.from(site, %{"period" => "12mo"}, now: @now)

    assert q.utc_time_range.first == ~U[2023-05-01 04:00:00Z]
    assert q.utc_time_range.last == ~U[2024-05-01 03:59:59Z]
  end

  test "parses year to date format", %{site: site} do
    q = Query.from(site, %{"period" => "year"}, now: @now)

    assert q.utc_time_range.first == ~U[2024-01-01 05:00:00Z]
    assert q.utc_time_range.last == ~U[2025-01-01 04:59:59Z]
  end

  test "parses all time", %{site: site} do
    q = Query.from(site, %{"period" => "all"}, now: @now)

    assert q.utc_time_range.first == ~U[2020-01-01 05:00:00Z]
    assert q.utc_time_range.last == ~U[2024-05-04 03:59:59Z]
    assert q.input_date_range == :all
  end

  test "parses all time in GMT+12 timezone", %{site: site} do
    site = Map.put(site, :timezone, "Etc/GMT+12")
    q = Query.from(site, %{"period" => "all"}, now: @now)

    assert q.utc_time_range.first == ~U[2020-01-01 12:00:00Z]
    assert q.utc_time_range.last == ~U[2024-05-04 11:59:59Z]
  end

  test "all time shows today if site has no start date", %{site: site} do
    site = Map.put(site, :stats_start_date, nil)
    q = Query.from(site, %{"period" => "all"}, now: @now)

    assert q.utc_time_range.first == ~U[2024-05-03 04:00:00Z]
    assert q.utc_time_range.last == ~U[2024-05-04 03:59:59Z]
    assert q.input_date_range == :all
  end

  test "all time shows hourly if site is completely new", %{site: site} do
    site = Map.put(site, :stats_start_date, @now |> DateTime.to_date())
    q = Query.from(site, %{"period" => "all"}, now: @now)

    assert q.utc_time_range.first == ~U[2024-05-03 04:00:00Z]
    assert q.utc_time_range.last == ~U[2024-05-04 03:59:59Z]
    assert q.input_date_range == :all
  end

  test "all time shows daily if site is more than a day old", %{site: site} do
    yesterday = @now |> DateTime.to_date() |> Date.shift(day: -1)
    site = Map.put(site, :stats_start_date, yesterday)

    q = Query.from(site, %{"period" => "all"}, now: @now)

    assert q.utc_time_range.first == ~U[2024-05-02 04:00:00Z]
    assert q.utc_time_range.last == ~U[2024-05-04 03:59:59Z]
    assert q.input_date_range == :all
  end

  test "all time shows monthly if site is more than a month old", %{site: site} do
    last_month = @now |> DateTime.to_date() |> Date.shift(month: -1)
    site = Map.put(site, :stats_start_date, last_month)

    q = Query.from(site, %{"period" => "all"}, now: @now)

    assert q.utc_time_range.first == ~U[2024-04-03 04:00:00Z]
    assert q.utc_time_range.last == ~U[2024-05-04 03:59:59Z]
    assert q.input_date_range == :all
  end

  test "all time uses passed interval different from the default interval", %{site: site} do
    last_month = @now |> DateTime.to_date() |> Date.shift(month: -1)
    site = Map.put(site, :stats_start_date, last_month)

    q = Query.from(site, %{"period" => "all", "interval" => "week"}, now: @now)

    assert q.utc_time_range.first == ~U[2024-04-03 04:00:00Z]
    assert q.utc_time_range.last == ~U[2024-05-04 03:59:59Z]
    assert q.input_date_range == :all
  end

  test "defaults to 30 days format", %{site: site} do
    query_default = Query.from(site, %{}) |> Map.delete(:now)
    query_30d = Query.from(site, %{"period" => "30d"}) |> Map.delete(:now)

    assert query_default == query_30d
  end

  test "parses custom format", %{site: site} do
    q =
      Query.from(
        site,
        %{"period" => "custom", "from" => "2019-01-01", "to" => "2019-01-15"},
        now: @now
      )

    assert q.utc_time_range.first == ~U[2019-01-01 05:00:00Z]
    assert q.utc_time_range.last == ~U[2019-01-16 04:59:59Z]
  end

  @tag :ee_only
  test "adds sample_threshold :no_sampling to query struct", %{site: site} do
    q = Query.from(site, %{"period" => "30d", "sample_threshold" => "infinite"})
    assert q.sample_threshold == :no_sampling
  end

  @tag :ee_only
  test "casts sample_threshold to integer in query struct", %{site: site} do
    q = Query.from(site, %{"period" => "30d", "sample_threshold" => "30000000"})
    assert q.sample_threshold == 30_000_000
  end

  describe "&date_range/2" do
    defp date_range({first, last}, timezone, now \\ nil, opts \\ []) do
      %Query{
        utc_time_range: DateTimeRange.new!(first, last),
        timezone: timezone,
        now: now || last
      }
      |> Query.date_range(opts)
    end

    test "with no options" do
      assert date_range({~U[2024-05-05 00:00:00Z], ~U[2024-05-07 23:59:59Z]}, "Etc/UTC") ==
               Date.range(~D[2024-05-05], ~D[2024-05-07])

      assert date_range({~U[2024-05-05 12:00:00Z], ~U[2024-05-08 11:59:59Z]}, "Etc/GMT+12") ==
               Date.range(~D[2024-05-05], ~D[2024-05-07])

      assert date_range({~U[2024-05-04 12:00:00Z], ~U[2024-05-07 11:59:59Z]}, "Etc/GMT-12") ==
               Date.range(~D[2024-05-05], ~D[2024-05-07])
    end

    test "trim_trailing: true" do
      assert date_range(
               {~U[2024-05-05 00:00:00Z], ~U[2024-05-07 23:59:59Z]},
               "Etc/UTC",
               ~U[2024-05-08 12:00:00Z],
               trim_trailing: true
             ) == Date.range(~D[2024-05-05], ~D[2024-05-07])

      assert date_range(
               {~U[2024-05-05 00:00:00Z], ~U[2024-05-07 23:59:59Z]},
               "Etc/UTC",
               ~U[2024-05-07 12:00:00Z],
               trim_trailing: true
             ) == Date.range(~D[2024-05-05], ~D[2024-05-07])

      assert date_range(
               {~U[2024-05-05 00:00:00Z], ~U[2024-05-07 23:59:59Z]},
               "Etc/UTC",
               ~U[2024-05-06 12:00:00Z],
               trim_trailing: true
             ) == Date.range(~D[2024-05-05], ~D[2024-05-06])

      assert date_range(
               {~U[2024-05-05 12:00:00Z], ~U[2024-05-08 11:59:59Z]},
               "Etc/GMT+12",
               ~U[2024-05-09 00:00:00Z],
               trim_trailing: true
             ) == Date.range(~D[2024-05-05], ~D[2024-05-07])

      assert date_range(
               {~U[2024-05-05 12:00:00Z], ~U[2024-05-08 11:59:59Z]},
               "Etc/GMT+12",
               ~U[2024-05-07 07:00:00Z],
               trim_trailing: true
             ) == Date.range(~D[2024-05-05], ~D[2024-05-06])

      assert date_range(
               {~U[2024-05-05 12:00:00Z], ~U[2024-05-08 11:59:59Z]},
               "Etc/GMT+12",
               ~U[2024-05-03 07:00:00Z],
               trim_trailing: true
             ) == Date.range(~D[2024-05-05], ~D[2024-05-05])
    end
  end

  describe "include_imported" do
    setup [:create_site]

    test "is true when requested via params and imported data exists", %{site: site} do
      insert(:site_import, site: site)

      assert %{include_imported: true} =
               Query.from(site, %{"period" => "day", "with_imported" => "true"})
    end

    test "is false when imported data does not exist", %{site: site} do
      assert %{include_imported: false, skip_imported_reason: :no_imported_data} =
               Query.from(site, %{"period" => "day", "with_imported" => "true"})
    end

    test "is false when imported data exists but is out of the date range", %{site: site} do
      insert(:site_import, site: site, start_date: ~D[2021-01-01], end_date: ~D[2022-01-01])

      assert %{include_imported: false, skip_imported_reason: :out_of_range} =
               Query.from(site, %{"period" => "day", "with_imported" => "true"})
    end

    test "is false in realtime even when imported data from today exists", %{site: site} do
      insert(:site_import, site: site)

      assert %{include_imported: false, skip_imported_reason: :out_of_range} =
               Query.from(site, %{"period" => "realtime", "with_imported" => "true"})
    end

    test "is false when an arbitrary custom property filter is used", %{site: site} do
      insert(:site_import, site: site)

      assert %{include_imported: false, skip_imported_reason: :unsupported_query} =
               Query.from(site, %{
                 "period" => "day",
                 "with_imported" => "true",
                 "property" => "event:props:url",
                 "filters" => Jason.encode!([[:is_not, "event:props:author", ["John Doe"]]])
               })
    end

    test "is true when breaking down by url and filtering by outbound link or file download goal",
         %{site: site} do
      insert(:site_import, site: site)

      Enum.each(["Outbound Link: Click", "File Download"], fn goal_name ->
        insert(:goal, site: site, event_name: goal_name)

        assert %{include_imported: true} =
                 Query.from(site, %{
                   "period" => "day",
                   "with_imported" => "true",
                   "property" => "event:props:url",
                   "filters" => Jason.encode!([[:is, "event:goal", [goal_name]]])
                 })
      end)
    end

    test "is false when breaking down by url but without a special goal filter",
         %{site: site} do
      insert(:site_import, site: site)

      assert %{include_imported: false} =
               Query.from(site, %{
                 "period" => "day",
                 "with_imported" => "true",
                 "property" => "event:props:url"
               })
    end

    test "is false when breaking down by url but with a mismatched special goal filter",
         %{site: site} do
      insert(:site_import, site: site)
      insert(:goal, site: site, event_name: "404")

      assert %{include_imported: false} =
               Query.from(site, %{
                 "period" => "day",
                 "with_imported" => "true",
                 "property" => "event:props:url",
                 "filters" => Jason.encode!([[:is, "event:goal", ["404"]]])
               })
    end

    test "is false when breaking down by url but with a special goal filter and an arbitrary filter",
         %{site: site} do
      insert(:site_import, site: site)
      insert(:goal, site: site, event_name: "Outbound Link: Click")

      assert %{include_imported: false} =
               Query.from(site, %{
                 "period" => "day",
                 "with_imported" => "true",
                 "property" => "event:props:url",
                 "filters" =>
                   Jason.encode!([
                     [:is, "event:goal", ["Outbound Link: Click"]],
                     [:is, "event:page", ["/example"]]
                   ])
               })
    end

    for property <- [nil, "event:goal", "event:name"] do
      test "is true when filtering by custom prop and special goal when breakdown prop is #{property}",
           %{site: site} do
        insert(:site_import, site: site)
        insert(:goal, site: site, event_name: "Outbound Link: Click")

        assert %{include_imported: true} =
                 Query.from(site, %{
                   "period" => "day",
                   "with_imported" => "true",
                   "property" => unquote(property),
                   "filters" =>
                     Jason.encode!([
                       [:is, "event:goal", ["Outbound Link: Click"]],
                       [:is, "event:props:url", ["https://example.com"]]
                     ])
                 })
      end
    end

    test "is true when filtering by matching multiple custom prop values and special goal", %{
      site: site
    } do
      insert(:site_import, site: site)
      insert(:goal, site: site, event_name: "Outbound Link: Click")

      assert %{include_imported: true} =
               Query.from(site, %{
                 "period" => "day",
                 "with_imported" => "true",
                 "property" => nil,
                 "filters" =>
                   Jason.encode!([
                     [:is, "event:goal", ["Outbound Link: Click"]],
                     [
                       :is,
                       "event:props:url",
                       ["https://example.com", "https://another.example.com"]
                     ]
                   ])
               })
    end

    test "is false when filtering by mismatched custom prop values and special goal", %{
      site: site
    } do
      insert(:site_import, site: site)
      insert(:goal, site: site, event_name: "Outbound Link: Click")

      assert %{include_imported: false} =
               Query.from(site, %{
                 "period" => "day",
                 "with_imported" => "true",
                 "property" => nil,
                 "filters" =>
                   Jason.encode!([
                     [:is, "event:goal", ["Outbound Link: Click"]],
                     [:is, "event:props:url", ["https://example.com"]],
                     [:is, "event:props:path", ["/whatever"]]
                   ])
               })
    end

    test "is false with a custom prop + mismatched special goal filter",
         %{site: site} do
      insert(:site_import, site: site)
      insert(:goal, site: site, event_name: "404")

      assert %{include_imported: false} =
               Query.from(site, %{
                 "period" => "day",
                 "with_imported" => "true",
                 "property" => nil,
                 "filters" =>
                   Jason.encode!([
                     [:is, "event:goal", ["404"]],
                     [:is, "event:props:url", ["https://example.com"]]
                   ])
               })
    end

    test "is false with a custom prop + required goal + arbitrary filter",
         %{site: site} do
      insert(:site_import, site: site)
      insert(:goal, site: site, event_name: "Outbound Link: Click")

      assert %{include_imported: false} =
               Query.from(site, %{
                 "period" => "day",
                 "with_imported" => "true",
                 "property" => nil,
                 "filters" =>
                   Jason.encode!([
                     [:is, "event:goal", ["Outbound Link: Click"]],
                     [:is, "event:page", ["/example"]],
                     [:is, "event:props:url", ["https://example.com"]]
                   ])
               })
    end

    test "is false with a custom prop filter and non-matching property", %{site: site} do
      insert(:site_import, site: site)
      insert(:goal, site: site, event_name: "Outbound Link: Click")

      assert %{include_imported: false} =
               Query.from(site, %{
                 "period" => "day",
                 "with_imported" => "true",
                 "property" => "visit:source",
                 "filters" =>
                   Jason.encode!([
                     [:is, "event:goal", ["Outbound Link: Click"]],
                     [:is, "event:props:url", ["https://example.com"]]
                   ])
               })
    end
  end

  on_ee do
    describe "query.consolidated_site_ids" do
      test "is set to nil when site is regular", %{site: site} do
        assert %{consolidated_site_ids: nil} = Query.from(site, %{"period" => "day"})
      end

      test "is set to a list of site_ids when site is consolidated", %{site: site} do
        new_site(team: site.team)
        cv = new_consolidated_view(site.team)

        assert %{consolidated_site_ids: site_ids} = Query.from(cv, %{"period" => "day"})
        assert length(site_ids) == 2
        assert site.id in site_ids
      end
    end
  end
end