File size: 648 Bytes
8da2481
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
defmodule Plausible.Stats.CurrentVisitors do
  use Plausible.ClickhouseRepo
  use Plausible.Stats.SQL.Fragments

  @spec current_visitors(Plausible.Site.t(), Duration.duration()) :: non_neg_integer
  def current_visitors(site, duration \\ Duration.new!(minute: -5)) do
    first_datetime =
      NaiveDateTime.utc_now()
      |> NaiveDateTime.shift(duration)
      |> NaiveDateTime.truncate(:second)

    ClickhouseRepo.one(
      from e in "events_v2",
        where: ^Plausible.Sites.site_id_query_filter(site),
        where: e.timestamp >= ^first_datetime,
        where: e.name != "engagement",
        select: uniq(e.user_id)
    )
  end
end