stats.ex 1.1 KB
Newer Older
1
2
3
4
5
defmodule Pleroma.Stats do
  import Ecto.Query
  alias Pleroma.{User, Repo, Activity}

  def start_link do
eal's avatar
eal committed
6
    agent = Agent.start_link(fn -> {[], %{}} end, name: __MODULE__)
7
    spawn(fn -> schedule_update() end)
8
9
10
    agent
  end

eal's avatar
eal committed
11
12
13
14
15
16
  def get_stats do
    Agent.get(__MODULE__, fn {_, stats} -> stats end)
  end

  def get_peers do
    Agent.get(__MODULE__, fn {peers, _} -> peers end)
17
18
19
20
21
22
23
  end

  def schedule_update do
    spawn(fn ->
      Process.sleep(1000 * 60 * 60 * 1) # 1 hour
      schedule_update()
    end)
24
    update_stats()
25
26
27
28
  end

  def update_stats do
    peers = from(u in Pleroma.User,
29
      select: fragment("distinct ?->'host'", u.info),
30
      where: u.local != ^true)
31
    |> Repo.all()
32
    domain_count = Enum.count(peers)
33
34
35
    status_query = from(u in User.local_user_query,
      select: fragment("sum((?->>'note_count')::int)", u.info))
    status_count = Repo.one(status_query) |> IO.inspect
eal's avatar
eal committed
36
    user_count = Repo.aggregate(User.local_user_query, :count, :id)
37
    Agent.update(__MODULE__, fn _ ->
eal's avatar
eal committed
38
      {peers, %{domain_count: domain_count, status_count: status_count, user_count: user_count}}
39
40
41
    end)
  end
end