2019-09-09 09:31:08 +02:00
|
|
|
defmodule Mobilizon.Service.Statistics do
|
|
|
|
@moduledoc """
|
|
|
|
A module that provides cached statistics
|
|
|
|
"""
|
2020-01-23 00:55:07 +01:00
|
|
|
|
2020-09-30 10:42:19 +02:00
|
|
|
alias Mobilizon.{Actors, Discussions, Events, Users}
|
2020-10-07 10:05:07 +02:00
|
|
|
alias Mobilizon.Federation.ActivityPub.Relay
|
2019-09-09 09:31:08 +02:00
|
|
|
|
2021-09-28 19:40:37 +02:00
|
|
|
@spec get_cached_value(String.t()) :: any() | nil
|
2019-09-09 09:31:08 +02:00
|
|
|
def get_cached_value(key) do
|
|
|
|
case Cachex.fetch(:statistics, key, fn key ->
|
|
|
|
case create_cache(key) do
|
|
|
|
value when not is_nil(value) -> {:commit, value}
|
|
|
|
err -> {:ignore, err}
|
|
|
|
end
|
|
|
|
end) do
|
|
|
|
{status, value} when status in [:ok, :commit] -> value
|
|
|
|
_err -> nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp create_cache(:local_users) do
|
|
|
|
Users.count_users()
|
|
|
|
end
|
|
|
|
|
|
|
|
defp create_cache(:local_events) do
|
|
|
|
Events.count_local_events()
|
|
|
|
end
|
|
|
|
|
2020-10-15 11:04:05 +02:00
|
|
|
defp create_cache(:confirmed_participations_to_local_events) do
|
|
|
|
Events.count_confirmed_participants_for_local_events()
|
|
|
|
end
|
|
|
|
|
2019-09-09 09:31:08 +02:00
|
|
|
defp create_cache(:local_comments) do
|
2020-10-15 11:04:05 +02:00
|
|
|
Discussions.count_local_comments_under_events()
|
2019-09-09 09:31:08 +02:00
|
|
|
end
|
2020-09-30 10:42:19 +02:00
|
|
|
|
|
|
|
defp create_cache(:local_groups) do
|
|
|
|
Actors.count_local_groups()
|
|
|
|
end
|
|
|
|
|
|
|
|
defp create_cache(:federation_events) do
|
|
|
|
Events.count_events()
|
|
|
|
end
|
|
|
|
|
|
|
|
defp create_cache(:federation_comments) do
|
2020-10-15 11:04:05 +02:00
|
|
|
Discussions.count_comments_under_events()
|
2020-09-30 10:42:19 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
defp create_cache(:federation_groups) do
|
|
|
|
Actors.count_groups()
|
|
|
|
end
|
2020-10-07 10:05:07 +02:00
|
|
|
|
|
|
|
defp create_cache(:instance_followers) do
|
|
|
|
relay_actor = Relay.get_actor()
|
|
|
|
Actors.count_followers_for_actor(relay_actor)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp create_cache(:instance_followings) do
|
|
|
|
relay_actor = Relay.get_actor()
|
|
|
|
Actors.count_followings_for_actor(relay_actor)
|
|
|
|
end
|
2019-09-09 09:31:08 +02:00
|
|
|
end
|