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

Commit db10bce

Browse files
committed
refactor(contributes): unify naming && add missing doc && clean up
1 parent 7b2b896 commit db10bce

File tree

9 files changed

+55
-40
lines changed

9 files changed

+55
-40
lines changed

lib/groupher_server/application.ex

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ defmodule GroupherServer.Application do
55
# for more information on OTP Applications
66
def start(_type, _args) do
77
import Supervisor.Spec
8+
import Cachex.Spec
89

910
# Define workers and child supervisors to be supervised
1011
children = [
@@ -14,7 +15,22 @@ defmodule GroupherServer.Application do
1415
supervisor(GroupherServerWeb.Endpoint, []),
1516
# Start your own worker by calling: GroupherServer.Worker.start_link(arg1, arg2, arg3)
1617
# worker(GroupherServer.Worker, [arg1, arg2, arg3]),
17-
worker(Cachex, [:site_cache, []])
18+
worker(Cachex, [
19+
:site_cache,
20+
[
21+
limit:
22+
limit(
23+
# the limit provided
24+
size: 5000,
25+
# the policy to use for eviction
26+
policy: Cachex.Policy.LRW,
27+
# how much to reclaim on bound expiration
28+
reclaim: 0.1,
29+
# options to pass to the policy
30+
options: []
31+
)
32+
]
33+
])
1834
]
1935

2036
# See https://hexdocs.pm/elixir/Supervisor.html

lib/groupher_server/statistics/delegates/contribute.ex

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,43 @@ defmodule GroupherServer.Statistics.Delegate.Contribute do
1212
@community_contribute_days get_config(:general, :community_contribute_days)
1313
@user_contribute_months get_config(:general, :user_contribute_months)
1414

15+
@doc """
16+
update community's contributes record
17+
"""
1518
def make_contribute(%Community{id: id}) do
1619
today = Timex.today() |> Date.to_iso8601()
1720

18-
with {:ok, contribute} <- ORM.find_by(CommunityContribute, community_id: id, date: today) do
19-
contribute |> inc_contribute_count(:community) |> done
20-
else
21+
case ORM.find_by(CommunityContribute, community_id: id, date: today) do
22+
{:ok, contribute} ->
23+
contribute |> inc_contribute_count(:community) |> done
24+
2125
{:error, _} ->
2226
CommunityContribute |> ORM.create(%{community_id: id, date: today, count: 1})
2327
end
2428
end
2529

30+
@doc """
31+
update user's contributes record
32+
"""
2633
def make_contribute(%User{id: id}) do
2734
today = Timex.today() |> Date.to_iso8601()
2835

29-
with {:ok, contribute} <- ORM.find_by(UserContribute, user_id: id, date: today) do
30-
contribute |> inc_contribute_count(:user) |> done
31-
else
36+
case ORM.find_by(UserContribute, user_id: id, date: today) do
37+
{:ok, contribute} ->
38+
contribute |> inc_contribute_count(:user) |> done
39+
3240
{:error, _} ->
3341
UserContribute |> ORM.create(%{user_id: id, date: today, count: 1})
3442
end
3543
end
3644

3745
@doc """
38-
Returns the list of user_contribute by latest 6 months.
46+
Returns the list of user_contribute by latest 6 days.
3947
"""
40-
def list_contributes(%User{id: id}) do
48+
def list_contributes_digest(%User{id: id}) do
4149
user_id = integerfy(id)
4250

43-
"user_contributes"
51+
UserContribute
4452
|> where([c], c.user_id == ^user_id)
4553
|> QueryBuilder.recent_inserted(months: @user_contribute_months)
4654
|> select([c], %{date: c.date, count: c.count})
@@ -49,13 +57,9 @@ defmodule GroupherServer.Statistics.Delegate.Contribute do
4957
|> done
5058
end
5159

52-
def list_contributes(%Community{id: id}) do
53-
%Community{id: id}
54-
|> get_contributes()
55-
|> to_counts_digest(days: @community_contribute_days)
56-
|> done
57-
end
58-
60+
@doc """
61+
Returns the list of community_contribute by latest 6 days.
62+
"""
5963
def list_contributes_digest(%Community{id: id}) do
6064
%Community{id: id}
6165
|> get_contributes()
@@ -66,7 +70,7 @@ defmodule GroupherServer.Statistics.Delegate.Contribute do
6670
defp get_contributes(%Community{id: id}) do
6771
community_id = integerfy(id)
6872

69-
"community_contributes"
73+
CommunityContribute
7074
|> where([c], c.community_id == ^community_id)
7175
|> QueryBuilder.recent_inserted(days: @community_contribute_days)
7276
|> select([c], %{date: c.date, count: c.count})

lib/groupher_server/statistics/statistics.ex

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@ defmodule GroupherServer.Statistics do
55

66
alias GroupherServer.Statistics.Delegate.{
77
Contribute,
8-
Throttle,
98
Geo,
10-
Status
9+
Status,
10+
Throttle
1111
}
1212

1313
# contributes
1414
defdelegate make_contribute(info), to: Contribute
15-
defdelegate list_contributes(info), to: Contribute
1615
defdelegate list_contributes_digest(community), to: Contribute
1716

1817
# publish Throttle

lib/groupher_server_web/resolvers/statistics_resolver.ex

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,22 @@ defmodule GroupherServerWeb.Resolvers.Statistics do
66
# alias Helper.ORM
77

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

13-
def list_contributes(%Accounts.User{id: id}, _args, _info) do
14-
Statistics.list_contributes(%Accounts.User{id: id})
15-
end
16-
17-
def list_contributes(%CMS.Community{id: id}, _args, _info) do
18-
Statistics.list_contributes(%CMS.Community{id: id})
13+
def list_contributes_digest(%Accounts.User{id: id}, _args, _info) do
14+
Statistics.list_contributes_digest(%Accounts.User{id: id})
1915
end
2016

2117
def list_contributes_digest(%CMS.Community{id: id}, _args, _info) do
2218
Statistics.list_contributes_digest(%CMS.Community{id: id})
2319
end
2420

21+
# def list_contributes_digest(%CMS.Community{id: id}, _args, _info) do
22+
# Statistics.list_contributes_digest(%CMS.Community{id: id})
23+
# end
24+
2525
def make_contrubute(_root, %{user_id: user_id}, _info) do
2626
Statistics.make_contribute(%Accounts.User{id: user_id})
2727
end

lib/groupher_server_web/schema/account/account_types.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ defmodule GroupherServerWeb.Schema.Account.Types do
243243
end
244244

245245
field :contributes, :contribute_map do
246-
resolve(&R.Statistics.list_contributes/3)
246+
resolve(&R.Statistics.list_contributes_digest/3)
247247
end
248248

249249
# TODO, for msg-bell UI

lib/groupher_server_web/schema/cms/cms_types.ex

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -314,11 +314,6 @@ defmodule GroupherServerWeb.Schema.CMS.Types do
314314
middleware(M.ConvertToInt)
315315
end
316316

317-
field :contributes, list_of(:contribute) do
318-
# TODO add complex here to warning N+1 problem
319-
resolve(&R.Statistics.list_contributes/3)
320-
end
321-
322317
field :threads_count, :integer do
323318
resolve(&R.CMS.threads_count/3)
324319
end

lib/groupher_server_web/schema/statistics/statistics_queries.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ defmodule GroupherServerWeb.Schema.Statistics.Queries do
99
field :user_contributes, list_of(:user_contribute) do
1010
arg(:id, non_null(:id))
1111

12-
resolve(&R.Statistics.list_contributes/3)
12+
resolve(&R.Statistics.list_contributes_digest/3)
1313
end
1414

1515
@desc "list cities geo info"

test/groupher_server/statistics/statistics_test.exs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ defmodule GroupherServer.Test.Statistics do
2020
alias Statistics.UserContribute
2121

2222
test "list_contributes return empty list when theres no records", ~m(user)a do
23-
{:ok, contributes} = Statistics.list_contributes(%User{id: user.id})
23+
{:ok, contributes} = Statistics.list_contributes_digest(%User{id: user.id})
2424
assert contributes.records == []
2525
assert contributes.total_count == 0
2626
end
2727

2828
test "list_contributes return proper format ", ~m(user)a do
2929
Statistics.make_contribute(%User{id: user.id})
30-
{:ok, contributes} = Statistics.list_contributes(%User{id: user.id})
30+
{:ok, contributes} = Statistics.list_contributes_digest(%User{id: user.id})
3131
# contributes[0]
3232
assert contributes |> Map.has_key?(:start_date)
3333
assert contributes |> Map.has_key?(:end_date)
@@ -56,7 +56,7 @@ defmodule GroupherServer.Test.Statistics do
5656
}
5757
])
5858

59-
{:ok, contributes} = Statistics.list_contributes(%User{id: user.id})
59+
{:ok, contributes} = Statistics.list_contributes_digest(%User{id: user.id})
6060
assert length(contributes.records) == 1
6161
end
6262

@@ -143,7 +143,7 @@ defmodule GroupherServer.Test.Statistics do
143143
}
144144
])
145145

146-
{:ok, contributes} = Statistics.list_contributes(%Community{id: community.id})
146+
{:ok, contributes} = Statistics.list_contributes_digest(%Community{id: community.id})
147147
assert length(contributes) == @community_contribute_days + 1
148148
end
149149
end

test/helper/cache_test.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ defmodule GroupherServer.Test.Helper.Cache do
2222
assert {:ok, true} = Cache.put(:data, :value)
2323
assert {:ok, :value} = Cache.get(:data)
2424

25+
# complex data
2526
assert {:ok, true} = Cache.put("namespace.aaa.bbb", [1, %{a: "2"}])
2627
assert {:ok, [1, %{a: "2"}]} = Cache.get("namespace.aaa.bbb")
2728
end

0 commit comments

Comments
 (0)