Skip to content
This repository was archived by the owner on Nov 8, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/groupher_server/accounts/delegates/collect_folder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ defmodule GroupherServer.Accounts.Delegate.CollectFolder do
alias GroupherServer.{Accounts, CMS, Repo}

alias Accounts.{CollectFolder, Embeds, User}
alias CMS.{ArticleCollect}
alias CMS.ArticleCollect

alias Ecto.Multi

Expand Down
3 changes: 2 additions & 1 deletion lib/groupher_server/cms/community.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ defmodule GroupherServer.CMS.Community do

@required_fields ~w(title desc user_id logo raw)a
# @required_fields ~w(title desc user_id)a
@optional_fields ~w(label geo_info index aka)a
@optional_fields ~w(label geo_info index aka contributes_digest)a

def max_pinned_article_count_per_thread, do: @max_pinned_article_count_per_thread

Expand Down Expand Up @@ -53,6 +53,7 @@ defmodule GroupherServer.CMS.Community do

field(:viewer_has_subscribed, :boolean, default: false, virtual: true)
field(:viewer_is_editor, :boolean, default: false, virtual: true)
field(:contributes_digest, {:array, :integer}, default: [])

has_one(:wiki, CommunityWiki)
has_one(:cheatsheet, CommunityCheatsheet)
Expand Down
44 changes: 29 additions & 15 deletions lib/groupher_server/statistics/delegates/contribute.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ defmodule GroupherServer.Statistics.Delegate.Contribute do
alias GroupherServer.{Accounts, CMS, Repo, Statistics}

alias Accounts.User
alias CMS.Community
alias CMS.{Community, Delegate}
alias Statistics.{CommunityContribute, UserContribute}

alias Delegate.CommunityCURD

alias Helper.{Cache, Later, ORM, QueryBuilder}
alias Ecto.Multi

@community_contribute_days get_config(:general, :community_contribute_days)
@user_contribute_months get_config(:general, :user_contribute_months)
Expand All @@ -39,13 +42,23 @@ defmodule GroupherServer.Statistics.Delegate.Contribute do
def make_contribute(%Community{id: id}) do
today = Timex.today() |> Date.to_iso8601()

case ORM.find_by(CommunityContribute, community_id: id, date: today) do
{:ok, contribute} ->
update_contribute_record(contribute)
Multi.new()
|> Multi.run(:make_contribute, fn _, _ ->
case ORM.find_by(CommunityContribute, %{community_id: id, date: today}) do
{:ok, contribute} -> update_contribute_record(contribute)
{:error, _} -> insert_contribute_record(%Community{id: id})
end
end)
|> Multi.run(:update_community_field, fn _, _ ->
contributes_digest =
%Community{id: id}
|> do_get_contributes()
|> to_counts_digest(days: @community_contribute_days)

{:error, _} ->
insert_contribute_record(%Community{id: id})
end
CommunityCURD.update_community(id, %{contributes_digest: contributes_digest})
end)
|> Repo.transaction()
|> result()
end

@doc """
Expand Down Expand Up @@ -161,15 +174,13 @@ defmodule GroupherServer.Statistics.Delegate.Contribute do
return_count = abs(count) + 1
enmpty_tuple = return_count |> repeat(0) |> List.to_tuple()

results =
Enum.reduce(record, enmpty_tuple, fn record, acc ->
diff = Timex.diff(Timex.to_date(record.date), today, :days)
index = diff + abs(count)
Enum.reduce(record, enmpty_tuple, fn record, acc ->
diff = Timex.diff(Timex.to_date(record.date), today, :days)
index = diff + abs(count)

put_elem(acc, index, record.count)
end)

results |> Tuple.to_list()
put_elem(acc, index, record.count)
end)
|> Tuple.to_list()
end
end

Expand All @@ -194,4 +205,7 @@ defmodule GroupherServer.Statistics.Delegate.Contribute do

put_in(contribute.count, result)
end

defp result({:ok, %{make_contribute: result}}), do: {:ok, result}
defp result({:error, _, result, _steps}), do: {:error, result}
end
10 changes: 5 additions & 5 deletions lib/groupher_server_web/resolvers/statistics_resolver.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ defmodule GroupherServerWeb.Resolvers.Statistics do
resolvers for Statistics
"""
alias GroupherServer.{Accounts, CMS, Statistics}
# alias Helper.ORM
alias Accounts.User

# tmp for test
def list_contributes_digest(_root, %{id: id}, _info) do
Statistics.list_contributes_digest(%Accounts.User{id: id})
Statistics.list_contributes_digest(%User{id: id})
end

def list_contributes_digest(%Accounts.User{id: id}, _args, _info) do
Statistics.list_contributes_digest(%Accounts.User{id: id})
def list_contributes_digest(%User{id: id}, _args, _info) do
Statistics.list_contributes_digest(%User{id: id})
end

def list_contributes_digest(%CMS.Community{id: id}, _args, _info) do
Expand All @@ -23,7 +23,7 @@ defmodule GroupherServerWeb.Resolvers.Statistics do
# end

def make_contrubute(_root, %{user_id: user_id}, _info) do
Statistics.make_contribute(%Accounts.User{id: user_id})
Statistics.make_contribute(%User{id: user_id})
end

def list_cities_geo_info(_root, _args, _info) do
Expand Down
5 changes: 0 additions & 5 deletions lib/groupher_server_web/schema/cms/cms_types.ex
Original file line number Diff line number Diff line change
Expand Up @@ -273,11 +273,6 @@ defmodule GroupherServerWeb.Schema.CMS.Types do
resolve(&R.CMS.threads_count/3)
end

field :contributes_digest, list_of(:integer) do
# TODO add complex here to warning N+1 problem
resolve(&R.Statistics.list_contributes_digest/3)
end

timestamp_fields()
end

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule GroupherServer.Repo.Migrations.AddContributesDigestFieldToCommunity do
use Ecto.Migration

def change do
alter table(:communities) do
add(:contributes_digest, {:array, :integer}, default: [])
end
end
end
1 change: 0 additions & 1 deletion test/groupher_server/cms/community/community_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ defmodule GroupherServer.Test.CMS.Community do
end

describe "[cms community article_tag]" do
@tag :wip2
test "articleTagsCount should work", ~m(community article_tag_attrs user)a do
{:ok, tag} = CMS.create_article_tag(community, :post, article_tag_attrs, user)
{:ok, tag2} = CMS.create_article_tag(community, :job, article_tag_attrs, user)
Expand Down
44 changes: 44 additions & 0 deletions test/groupher_server/statistics/statistics_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,50 @@ defmodule GroupherServer.Test.Statistics do
assert contribute.date == Timex.today()
end

@tag :wip2
test "should update community's field after contribute being make", ~m(community)a do
community_id = community.id
assert {:error, _} = ORM.find_by(CommunityContribute, ~m(community_id)a)

Statistics.make_contribute(%Community{id: community.id})

{:ok, community} = ORM.find(Community, community.id)

assert community.contributes_digest == [
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
1
]
end

test "should update a contribute when make communityContribute before", ~m(community)a do
community_id = community.id

Expand Down