analytics / lib /plausible /stats /current_visitors.ex
Leon4gr45's picture
Upload folder using huggingface_hub (part 2)
8da2481 verified
Raw
History Blame Contribute Delete
648 Bytes
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