Merge pull request 'Stats: read peers from the instances table' (#8030) from lambadalambda/pleroma:perf/stats-peers-from-instances into develop

Reviewed-on: https://git.pleroma.social/pleroma/pleroma/pulls/8030
This commit is contained in:
lain 2026-10-29 10:15:17 +00:00
commit 160c5c8f7d
8 changed files with 118 additions and 11 deletions

View file

@ -0,0 +1,33 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2026 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Repo.Migrations.BackfillInstancesFromUsersTest do
use Pleroma.DataCase
import Pleroma.Factory
import Pleroma.Tests.Helpers
alias Pleroma.Instances.Instance
setup_all do: require_migration("20261028000000_backfill_instances_from_users")
test "up/0 adds the hosts of remote users and keeps existing records", %{migration: migration} do
unreachable_since = ~N[2020-01-01 00:00:00.000000]
insert(:instance, host: "known.example", unreachable_since: unreachable_since)
insert(:user, local: false, ap_id: "https://known.example/users/a")
insert(:user, local: false, ap_id: "https://new.example/users/a")
insert(:user, local: false, ap_id: "https://new.example/users/b")
insert(:user, local: false, ap_id: "https://ported.example:8443/users/c")
insert(:user, local: false, ap_id: "https://user@userinfo.example/users/d")
insert(:user, local: false, ap_id: "https://[2001:db8::1]/users/e")
insert(:user, local: true)
migration.up()
hosts = Instance |> Repo.all() |> Enum.map(& &1.host) |> Enum.sort()
assert hosts == ["known.example", "new.example", "ported.example", "userinfo.example"]
assert %{unreachable_since: ^unreachable_since} = Repo.get_by(Instance, host: "known.example")
end
end

View file

@ -184,6 +184,34 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
assert user.follower_address == "http://mastodon.example.org/users/admin/followers"
end
test "it records the user's instance" do
refute Repo.get_by(Pleroma.Instances.Instance, host: "mastodon.example.org")
{:ok, _user} = ActivityPub.make_user_from_ap_id("http://mastodon.example.org/users/admin")
assert %{unreachable_since: nil} =
Repo.get_by(Pleroma.Instances.Instance, host: "mastodon.example.org")
end
test "it records the instance of a known user when refreshing it" do
insert(:user, local: false, ap_id: "http://mastodon.example.org/users/admin")
refute Repo.get_by(Pleroma.Instances.Instance, host: "mastodon.example.org")
{:ok, _user} = ActivityPub.make_user_from_ap_id("http://mastodon.example.org/users/admin")
assert Repo.get_by(Pleroma.Instances.Instance, host: "mastodon.example.org")
end
test "it keeps an existing instance record" do
unreachable_since = ~N[2020-01-01 00:00:00.000000]
insert(:instance, host: "mastodon.example.org", unreachable_since: unreachable_since)
{:ok, _user} = ActivityPub.make_user_from_ap_id("http://mastodon.example.org/users/admin")
assert %{unreachable_since: ^unreachable_since} =
Repo.get_by(Pleroma.Instances.Instance, host: "mastodon.example.org")
end
test "it returns a user that is invisible" do
user_id = "http://mastodon.example.org/users/relay"
{:ok, user} = ActivityPub.make_user_from_ap_id(user_id)

View file

@ -63,8 +63,8 @@ defmodule Pleroma.Web.MastodonAPI.InstanceControllerTest do
user2 = insert(:user, %{local: true})
{:ok, _user2} = User.set_activation(user2, false)
insert(:user, %{local: false, nickname: "u@peer1.com"})
insert(:user, %{local: false, nickname: "u@peer2.com"})
insert(:instance, host: "peer1.com")
insert(:instance, host: "peer2.com")
{:ok, _} = Pleroma.Web.CommonAPI.post(user, %{status: "cofe"})
@ -83,8 +83,11 @@ defmodule Pleroma.Web.MastodonAPI.InstanceControllerTest do
end
test "get peers", %{conn: conn} do
insert(:user, %{local: false, nickname: "u@peer1.com"})
insert(:user, %{local: false, nickname: "u@peer2.com"})
# Peers come from the instances table, which remote users add to.
insert(:instance, host: "peer1.com")
insert(:instance, host: "peer2.com")
# Favicon lookups for local accounts can add our own host.
insert(:instance, host: Pleroma.Web.Endpoint.host())
Pleroma.Stats.force_update()