aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--config/prod.exs9
-rw-r--r--installation/pleroma.nginx2
-rw-r--r--lib/pleroma/application.ex1
-rw-r--r--lib/pleroma/stats.ex43
-rw-r--r--lib/pleroma/web/mastodon_api/mastodon_api_controller.ex13
-rw-r--r--lib/pleroma/web/router.ex1
-rw-r--r--test/web/mastodon_api/mastodon_api_controller_test.exs3
7 files changed, 60 insertions, 12 deletions
diff --git a/config/prod.exs b/config/prod.exs
index 732bab2b0..8522c67da 100644
--- a/config/prod.exs
+++ b/config/prod.exs
@@ -14,9 +14,12 @@ use Mix.Config
# manifest is generated by the mix phoenix.digest task
# which you typically run after static files are built.
config :pleroma, Pleroma.Web.Endpoint,
- on_init: {Pleroma.Web.Endpoint, :load_from_system_env, []},
- url: [host: "example.com", port: 80],
- cache_static_manifest: "priv/static/cache_manifest.json"
+ http: [port: 4000],
+ protocol: "http",
+ debug_errors: true,
+ code_reloader: true,
+ check_origin: false,
+ watchers: []
# Do not print debug messages in production
config :logger, level: :info
diff --git a/installation/pleroma.nginx b/installation/pleroma.nginx
index 4e88a426d..92ed8f32e 100644
--- a/installation/pleroma.nginx
+++ b/installation/pleroma.nginx
@@ -34,6 +34,4 @@ server {
proxy_pass http://localhost:4000;
}
- include snippets/well-known.conf;
-
}
diff --git a/lib/pleroma/application.ex b/lib/pleroma/application.ex
index 2969ca3c4..cdfca8b1a 100644
--- a/lib/pleroma/application.ex
+++ b/lib/pleroma/application.ex
@@ -21,6 +21,7 @@ defmodule Pleroma.Application do
]]),
worker(Pleroma.Web.Federator, []),
worker(Pleroma.Web.ChatChannel.ChatChannelState, []),
+ worker(Pleroma.Stats, []),
]
++ if Mix.env == :test, do: [], else: [worker(Pleroma.Web.Streamer, [])]
diff --git a/lib/pleroma/stats.ex b/lib/pleroma/stats.ex
new file mode 100644
index 000000000..938e5c343
--- /dev/null
+++ b/lib/pleroma/stats.ex
@@ -0,0 +1,43 @@
+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_stats do
+ Agent.get(__MODULE__, fn {_, stats} -> stats end)
+ end
+
+ def get_peers do
+ Agent.get(__MODULE__, fn {peers, _} -> peers 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)
+ user_count = Repo.aggregate(User.local_user_query, :count, :id)
+ Agent.update(__MODULE__, fn _ ->
+ {peers, %{domain_count: domain_count, status_count: status_count, user_count: user_count}}
+ end)
+ end
+end
diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex
index ffa6f42c8..0615ac11a 100644
--- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex
+++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex
@@ -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
@@ -93,7 +93,6 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
@instance Application.get_env(:pleroma, :instance)
def masto_instance(conn, _params) do
- user_count = Repo.aggregate(User.local_user_query, :count, :id)
response = %{
uri: Web.base_url,
title: Keyword.get(@instance, :name),
@@ -103,17 +102,17 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
urls: %{
streaming_api: String.replace(Web.base_url, ["http","https"], "wss")
},
- stats: %{
- status_count: 2,
- user_count: user_count,
- domain_count: 3
- },
+ stats: Stats.get_stats,
max_toot_chars: Keyword.get(@instance, :limit)
}
json(conn, response)
end
+ def peers(conn, _params) do
+ json(conn, Stats.get_peers)
+ end
+
defp mastodonized_emoji do
Pleroma.Formatter.get_custom_emoji()
|> Enum.map(fn {shortcode, relative_url} ->
diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex
index 8e6681e68..09104fc86 100644
--- a/lib/pleroma/web/router.ex
+++ b/lib/pleroma/web/router.ex
@@ -106,6 +106,7 @@ defmodule Pleroma.Web.Router 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
diff --git a/test/web/mastodon_api/mastodon_api_controller_test.exs b/test/web/mastodon_api/mastodon_api_controller_test.exs
index 79ad671d9..93b29dfae 100644
--- a/test/web/mastodon_api/mastodon_api_controller_test.exs
+++ b/test/web/mastodon_api/mastodon_api_controller_test.exs
@@ -586,11 +586,14 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
{:ok, _} = TwitterAPI.create_status(user, %{"status" => "cofe"})
+ Pleroma.Stats.update_stats()
+
conn = conn
|> get("/api/v1/instance")
assert result = json_response(conn, 200)
assert result["stats"]["user_count"] == 2
+ assert result["stats"]["status_count"] == 1
end
end