Add a stats agent for storing data from expensive queries.

This commit is contained in:
eal 2018-01-13 18:24:16 +02:00
parent d49109ca17
commit 9717166d10
4 changed files with 49 additions and 3 deletions

View File

@ -21,6 +21,7 @@ def start(_type, _args) do
]]),
worker(Pleroma.Web.Federator, []),
worker(Pleroma.Web.ChatChannel.ChatChannelState, []),
worker(Pleroma.Stats, []),
]
++ if Mix.env == :test, do: [], else: [worker(Pleroma.Web.Streamer, [])]

38
lib/pleroma/stats.ex Normal file
View File

@ -0,0 +1,38 @@
defmodule Pleroma.Stats do
use Agent
import Ecto.Query
alias Pleroma.{User, Repo, Activity}
def start_link do
agent = Agent.start_link(fn -> %{} end, name: __MODULE__)
schedule_update()
agent
end
def get do
Agent.get(__MODULE__, fn stats -> stats end)
end
def schedule_update do
update_stats()
spawn(fn ->
Process.sleep(1000 * 60 * 60 * 1) # 1 hour
schedule_update()
end)
end
def update_stats do
peers = from(u in Pleroma.User,
select: fragment("?->'host'", u.info),
where: u.local != ^true)
|> Repo.all() |> Enum.uniq()
domain_count = Enum.count(peers)
status_query = from p in Activity,
where: p.local == ^true,
where: fragment("?->'object'->>'type' = ?", p.data, ^"Note")
status_count = Repo.aggregate(status_query, :count, :id)
Agent.update(__MODULE__, fn _ ->
%{peers: peers, domain_count: domain_count, status_count: status_count}
end)
end
end

View File

@ -1,6 +1,6 @@
defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
use Pleroma.Web, :controller
alias Pleroma.{Repo, Activity, User, Notification}
alias Pleroma.{Repo, Activity, User, Notification, Stats}
alias Pleroma.Web
alias Pleroma.Web.MastodonAPI.{StatusView, AccountView, MastodonView}
alias Pleroma.Web.ActivityPub.ActivityPub
@ -94,6 +94,7 @@ def user(conn, %{"id" => id}) do
def masto_instance(conn, _params) do
user_count = Repo.aggregate(User.local_user_query, :count, :id)
%{domain_count: domain_count, status_count: status_count} = Stats.get()
response = %{
uri: Web.base_url,
title: Keyword.get(@instance, :name),
@ -104,9 +105,9 @@ def masto_instance(conn, _params) do
streaming_api: String.replace(Web.base_url, ["http","https"], "wss")
},
stats: %{
status_count: 2,
status_count: status_count,
user_count: user_count,
domain_count: 3
domain_count: domain_count
},
max_toot_chars: Keyword.get(@instance, :limit)
}
@ -114,6 +115,11 @@ def masto_instance(conn, _params) do
json(conn, response)
end
def peers(conn, _params) do
%{peers: peers} = Stats.get()
json(conn, peers)
end
defp mastodonized_emoji do
Pleroma.Formatter.get_custom_emoji()
|> Enum.map(fn {shortcode, relative_url} ->

View File

@ -106,6 +106,7 @@ def user_fetcher(username) do
scope "/api/v1", Pleroma.Web.MastodonAPI do
pipe_through :api
get "/instance", MastodonAPIController, :masto_instance
get "/instance/peers", MastodonAPIController, :peers
post "/apps", MastodonAPIController, :create_app
get "/custom_emojis", MastodonAPIController, :custom_emojis