From a600720062e0806a47c3bd625cf508cc6954cdf3 Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Wed, 30 Sep 2020 10:42:19 +0200 Subject: [PATCH] Add local groups as statistics Signed-off-by: Thomas Citharel --- js/src/graphql/statistics.ts | 1 + js/src/types/statistics.model.ts | 1 + js/src/views/About/AboutInstance.vue | 8 ++++++-- lib/graphql/resolvers/statistics.ex | 3 ++- lib/graphql/schema/statistics.ex | 1 + lib/mobilizon/actors/actors.ex | 19 +++++++++++++++++++ lib/mobilizon/discussions/discussions.ex | 15 +++++++++++++++ lib/mobilizon/events/events.ex | 18 +++++++++++++++++- lib/service/statistics/statistics.ex | 18 +++++++++++++++++- 9 files changed, 79 insertions(+), 5 deletions(-) diff --git a/js/src/graphql/statistics.ts b/js/src/graphql/statistics.ts index 2bef730c..4fd8ff84 100644 --- a/js/src/graphql/statistics.ts +++ b/js/src/graphql/statistics.ts @@ -6,6 +6,7 @@ export const STATISTICS = gql` numberOfUsers numberOfEvents numberOfComments + numberOfGroups } } `; diff --git a/js/src/types/statistics.model.ts b/js/src/types/statistics.model.ts index e30f2ccf..3d862934 100644 --- a/js/src/types/statistics.model.ts +++ b/js/src/types/statistics.model.ts @@ -2,4 +2,5 @@ export interface IStatistics { numberOfUsers: number; numberOfEvents: number; numberOfComments: number; + numberOfGroups: number; } diff --git a/js/src/views/About/AboutInstance.vue b/js/src/views/About/AboutInstance.vue index 9d569ca5..6bd60876 100644 --- a/js/src/views/About/AboutInstance.vue +++ b/js/src/views/About/AboutInstance.vue @@ -13,6 +13,9 @@ {{ statistics.numberOfUsers }} + + {{ statistics.numberOfGroups }} + {{ statistics.numberOfEvents }} @@ -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; & > * { diff --git a/lib/graphql/resolvers/statistics.ex b/lib/graphql/resolvers/statistics.ex index 4cf8b422..3fdafeaf 100644 --- a/lib/graphql/resolvers/statistics.ex +++ b/lib/graphql/resolvers/statistics.ex @@ -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 diff --git a/lib/graphql/schema/statistics.ex b/lib/graphql/schema/statistics.ex index ebb2ba2c..f3c19810 100644 --- a/lib/graphql/schema/statistics.ex +++ b/lib/graphql/schema/statistics.ex @@ -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 diff --git a/lib/mobilizon/actors/actors.ex b/lib/mobilizon/actors/actors.ex index b33801e1..75b55882 100644 --- a/lib/mobilizon/actors/actors.ex +++ b/lib/mobilizon/actors/actors.ex @@ -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. """ diff --git a/lib/mobilizon/discussions/discussions.ex b/lib/mobilizon/discussions/discussions.ex index cd6e7f52..630788b4 100644 --- a/lib/mobilizon/discussions/discussions.ex +++ b/lib/mobilizon/discussions/discussions.ex @@ -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 diff --git a/lib/mobilizon/events/events.ex b/lib/mobilizon/events/events.ex index dbae893a..a0ef3b0d 100644 --- a/lib/mobilizon/events/events.ex +++ b/lib/mobilizon/events/events.ex @@ -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 diff --git a/lib/service/statistics/statistics.ex b/lib/service/statistics/statistics.ex index 105e3304..c3a0efaf 100644 --- a/lib/service/statistics/statistics.ex +++ b/lib/service/statistics/statistics.ex @@ -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