Skip to content
This repository was archived by the owner on Nov 8, 2022. It is now read-only.

Commit 7de9e82

Browse files
authored
refactor(community): contribute field (#370)
1 parent 1ba431b commit 7de9e82

File tree

8 files changed

+90
-28
lines changed

8 files changed

+90
-28
lines changed

lib/groupher_server/accounts/delegates/collect_folder.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ defmodule GroupherServer.Accounts.Delegate.CollectFolder do
1717
alias GroupherServer.{Accounts, CMS, Repo}
1818

1919
alias Accounts.{CollectFolder, Embeds, User}
20-
alias CMS.{ArticleCollect}
20+
alias CMS.ArticleCollect
2121

2222
alias Ecto.Multi
2323

lib/groupher_server/cms/community.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ defmodule GroupherServer.CMS.Community do
2222

2323
@required_fields ~w(title desc user_id logo raw)a
2424
# @required_fields ~w(title desc user_id)a
25-
@optional_fields ~w(label geo_info index aka)a
25+
@optional_fields ~w(label geo_info index aka contributes_digest)a
2626

2727
def max_pinned_article_count_per_thread, do: @max_pinned_article_count_per_thread
2828

@@ -53,6 +53,7 @@ defmodule GroupherServer.CMS.Community do
5353

5454
field(:viewer_has_subscribed, :boolean, default: false, virtual: true)
5555
field(:viewer_is_editor, :boolean, default: false, virtual: true)
56+
field(:contributes_digest, {:array, :integer}, default: [])
5657

5758
has_one(:wiki, CommunityWiki)
5859
has_one(:cheatsheet, CommunityCheatsheet)

lib/groupher_server/statistics/delegates/contribute.ex

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,13 @@ defmodule GroupherServer.Statistics.Delegate.Contribute do
1010
alias GroupherServer.{Accounts, CMS, Repo, Statistics}
1111

1212
alias Accounts.User
13-
alias CMS.Community
13+
alias CMS.{Community, Delegate}
1414
alias Statistics.{CommunityContribute, UserContribute}
1515

16+
alias Delegate.CommunityCURD
17+
1618
alias Helper.{Cache, Later, ORM, QueryBuilder}
19+
alias Ecto.Multi
1720

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

42-
case ORM.find_by(CommunityContribute, community_id: id, date: today) do
43-
{:ok, contribute} ->
44-
update_contribute_record(contribute)
45+
Multi.new()
46+
|> Multi.run(:make_contribute, fn _, _ ->
47+
case ORM.find_by(CommunityContribute, %{community_id: id, date: today}) do
48+
{:ok, contribute} -> update_contribute_record(contribute)
49+
{:error, _} -> insert_contribute_record(%Community{id: id})
50+
end
51+
end)
52+
|> Multi.run(:update_community_field, fn _, _ ->
53+
contributes_digest =
54+
%Community{id: id}
55+
|> do_get_contributes()
56+
|> to_counts_digest(days: @community_contribute_days)
4557

46-
{:error, _} ->
47-
insert_contribute_record(%Community{id: id})
48-
end
58+
CommunityCURD.update_community(id, %{contributes_digest: contributes_digest})
59+
end)
60+
|> Repo.transaction()
61+
|> result()
4962
end
5063

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

164-
results =
165-
Enum.reduce(record, enmpty_tuple, fn record, acc ->
166-
diff = Timex.diff(Timex.to_date(record.date), today, :days)
167-
index = diff + abs(count)
177+
Enum.reduce(record, enmpty_tuple, fn record, acc ->
178+
diff = Timex.diff(Timex.to_date(record.date), today, :days)
179+
index = diff + abs(count)
168180

169-
put_elem(acc, index, record.count)
170-
end)
171-
172-
results |> Tuple.to_list()
181+
put_elem(acc, index, record.count)
182+
end)
183+
|> Tuple.to_list()
173184
end
174185
end
175186

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

195206
put_in(contribute.count, result)
196207
end
208+
209+
defp result({:ok, %{make_contribute: result}}), do: {:ok, result}
210+
defp result({:error, _, result, _steps}), do: {:error, result}
197211
end

lib/groupher_server_web/resolvers/statistics_resolver.ex

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ defmodule GroupherServerWeb.Resolvers.Statistics do
33
resolvers for Statistics
44
"""
55
alias GroupherServer.{Accounts, CMS, Statistics}
6-
# alias Helper.ORM
6+
alias Accounts.User
77

88
# tmp for test
99
def list_contributes_digest(_root, %{id: id}, _info) do
10-
Statistics.list_contributes_digest(%Accounts.User{id: id})
10+
Statistics.list_contributes_digest(%User{id: id})
1111
end
1212

13-
def list_contributes_digest(%Accounts.User{id: id}, _args, _info) do
14-
Statistics.list_contributes_digest(%Accounts.User{id: id})
13+
def list_contributes_digest(%User{id: id}, _args, _info) do
14+
Statistics.list_contributes_digest(%User{id: id})
1515
end
1616

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

2525
def make_contrubute(_root, %{user_id: user_id}, _info) do
26-
Statistics.make_contribute(%Accounts.User{id: user_id})
26+
Statistics.make_contribute(%User{id: user_id})
2727
end
2828

2929
def list_cities_geo_info(_root, _args, _info) do

lib/groupher_server_web/schema/cms/cms_types.ex

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -273,11 +273,6 @@ defmodule GroupherServerWeb.Schema.CMS.Types do
273273
resolve(&R.CMS.threads_count/3)
274274
end
275275

276-
field :contributes_digest, list_of(:integer) do
277-
# TODO add complex here to warning N+1 problem
278-
resolve(&R.Statistics.list_contributes_digest/3)
279-
end
280-
281276
timestamp_fields()
282277
end
283278

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
defmodule GroupherServer.Repo.Migrations.AddContributesDigestFieldToCommunity do
2+
use Ecto.Migration
3+
4+
def change do
5+
alter table(:communities) do
6+
add(:contributes_digest, {:array, :integer}, default: [])
7+
end
8+
end
9+
end

test/groupher_server/cms/community/community_test.exs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ defmodule GroupherServer.Test.CMS.Community do
5959
end
6060

6161
describe "[cms community article_tag]" do
62-
@tag :wip2
6362
test "articleTagsCount should work", ~m(community article_tag_attrs user)a do
6463
{:ok, tag} = CMS.create_article_tag(community, :post, article_tag_attrs, user)
6564
{:ok, tag2} = CMS.create_article_tag(community, :job, article_tag_attrs, user)

test/groupher_server/statistics/statistics_test.exs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,50 @@ defmodule GroupherServer.Test.Statistics do
104104
assert contribute.date == Timex.today()
105105
end
106106

107+
@tag :wip2
108+
test "should update community's field after contribute being make", ~m(community)a do
109+
community_id = community.id
110+
assert {:error, _} = ORM.find_by(CommunityContribute, ~m(community_id)a)
111+
112+
Statistics.make_contribute(%Community{id: community.id})
113+
114+
{:ok, community} = ORM.find(Community, community.id)
115+
116+
assert community.contributes_digest == [
117+
0,
118+
0,
119+
0,
120+
0,
121+
0,
122+
0,
123+
0,
124+
0,
125+
0,
126+
0,
127+
0,
128+
0,
129+
0,
130+
0,
131+
0,
132+
0,
133+
0,
134+
0,
135+
0,
136+
0,
137+
0,
138+
0,
139+
0,
140+
0,
141+
0,
142+
0,
143+
0,
144+
0,
145+
0,
146+
0,
147+
1
148+
]
149+
end
150+
107151
test "should update a contribute when make communityContribute before", ~m(community)a do
108152
community_id = community.id
109153

0 commit comments

Comments
 (0)