Add local groups as statistics

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2020-09-30 10:42:19 +02:00
parent 2dccd5eccd
commit a600720062
No known key found for this signature in database
GPG key ID: A061B9DDE0CA0773
9 changed files with 79 additions and 5 deletions

View file

@ -6,6 +6,7 @@ export const STATISTICS = gql`
numberOfUsers
numberOfEvents
numberOfComments
numberOfGroups
}
}
`;

View file

@ -2,4 +2,5 @@ export interface IStatistics {
numberOfUsers: number;
numberOfEvents: number;
numberOfComments: number;
numberOfGroups: number;
}

View file

@ -13,6 +13,9 @@
<i18n tag="p" path="Home to {number} users">
<strong slot="number">{{ statistics.numberOfUsers }}</strong>
</i18n>
<i18n tag="p" path="and {number} groups">
<strong slot="number">{{ statistics.numberOfGroups }}</strong>
</i18n>
<i18n tag="p" path="Who published {number} events">
<strong slot="number">{{ statistics.numberOfEvents }}</strong>
</i18n>
@ -135,10 +138,11 @@ section {
&.contact-statistics {
margin: 2px auto;
.statistics {
display: flex;
display: grid;
grid-template-columns: repeat(auto-fit, 150px);
grid-template-rows: repeat(2, 1fr);
p {
text-align: right;
flex: 1;
padding: 0 15px;
& > * {

View file

@ -13,7 +13,8 @@ defmodule Mobilizon.GraphQL.Resolvers.Statistics do
%{
number_of_users: StatisticsModule.get_cached_value(:local_users),
number_of_events: StatisticsModule.get_cached_value(:local_events),
number_of_comments: StatisticsModule.get_cached_value(:local_comments)
number_of_comments: StatisticsModule.get_cached_value(:local_comments),
number_of_groups: StatisticsModule.get_cached_value(:local_groups)
}}
end
end

View file

@ -12,6 +12,7 @@ defmodule Mobilizon.GraphQL.Schema.StatisticsType do
field(:number_of_users, :integer, description: "The number of local users")
field(:number_of_events, :integer, description: "The number of local events")
field(:number_of_comments, :integer, description: "The number of local comments")
field(:number_of_groups, :integer, description: "The number of local groups")
end
object :statistics_queries do

View file

@ -603,6 +603,25 @@ defmodule Mobilizon.Actors do
"""
def delete_group!(%Actor{type: :Group} = group), do: Repo.delete!(group)
@doc """
Counts the local groups
"""
@spec count_local_groups :: integer()
def count_local_groups do
groups_query()
|> filter_local()
|> Repo.aggregate(:count)
end
@doc """
Counts all the groups
"""
@spec count_groups :: integer()
def count_groups do
groups_query()
|> Repo.aggregate(:count)
end
@doc """
Lists the groups.
"""

View file

@ -288,6 +288,12 @@ defmodule Mobilizon.Discussions do
@spec count_local_comments :: integer
def count_local_comments, do: Repo.one(count_local_comments_query())
@doc """
Counts all comments.
"""
@spec count_comments :: integer
def count_comments, do: Repo.one(count_comments_query())
def get_discussion(discussion_id) do
Discussion
|> Repo.get(discussion_id)
@ -424,6 +430,15 @@ defmodule Mobilizon.Discussions do
)
end
@spec count_comments_query :: Ecto.Query.t()
defp count_comments_query do
from(
c in Comment,
select: count(c.id),
where: c.visibility in ^@public_visibility
)
end
@spec preload_for_comment(Ecto.Query.t()) :: Ecto.Query.t()
defp preload_for_comment(query), do: preload(query, ^@comment_preloads)
end

View file

@ -491,6 +491,17 @@ defmodule Mobilizon.Events do
|> Repo.one()
end
@doc """
Counts all events.
"""
@spec count_events :: integer
def count_events do
count_events_query()
|> filter_unlisted_and_public_visibility()
|> filter_draft()
|> Repo.one()
end
@doc """
Builds a page struct for events by their name.
"""
@ -1417,6 +1428,11 @@ defmodule Mobilizon.Events do
from(e in Event, select: count(e.id), where: e.local == ^true)
end
@spec count_events_query :: Ecto.Query.t()
defp count_events_query do
from(e in Event, select: count(e.id))
end
@spec tag_by_slug_query(String.t()) :: Ecto.Query.t()
defp tag_by_slug_query(slug) do
from(t in Tag, where: t.slug == ^slug)
@ -1537,7 +1553,7 @@ defmodule Mobilizon.Events do
from(
p in Participant,
where: p.event_id == ^event_id,
preload: [:actor]
preload: [:actor, :event]
)
end

View file

@ -3,7 +3,7 @@ defmodule Mobilizon.Service.Statistics do
A module that provides cached statistics
"""
alias Mobilizon.{Discussions, Events, Users}
alias Mobilizon.{Actors, Discussions, Events, Users}
def get_cached_value(key) do
case Cachex.fetch(:statistics, key, fn key ->
@ -28,4 +28,20 @@ defmodule Mobilizon.Service.Statistics do
defp create_cache(:local_comments) do
Discussions.count_local_comments()
end
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
Discussions.count_comments()
end
defp create_cache(:federation_groups) do
Actors.count_groups()
end
end