Skip to content

Commit ec7912d

Browse files
committed
added goal stat calculation
1 parent 32c7674 commit ec7912d

File tree

5 files changed

+137
-9
lines changed

5 files changed

+137
-9
lines changed

app/controllers/discourse_kofi/payments_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def index
3333
payment_types: SiteSetting.kofi_dashboard_types.split("|")
3434
)
3535

36-
if visible_details.exclude?("include_unknown")
36+
if visible_details.exclude?("include_unknown_users")
3737
query = query.where("user_id is not null")
3838
end
3939

app/services/discourse_kofi/payment_stats.rb

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,17 @@ def self.calculate_leaderboard
66
leaderboard = []
77
if SiteSetting.kofi_leaderboard_count < 1 ||
88
SiteSetting.kofi_leaderboard_types.empty? ||
9-
SiteSetting.kofi_leaderboard_days < 1
9+
SiteSetting.kofi_leaderboard_days == 0
1010
PluginStore.set(PLUGIN_NAME, :leaderboard, leaderboard)
1111
return leaderboard
1212
end
1313

14+
if SiteSetting.kofi_leaderboard_days == -1
15+
timestamp_offset = DateTime.new
16+
else
17+
timestamp_offset = DateTime.now - SiteSetting.kofi_leaderboard_days.days
18+
end
19+
1420
ActiveRecord::Base
1521
.lease_connection
1622
.select_all(
@@ -29,7 +35,7 @@ def self.calculate_leaderboard
2935
",
3036
"kofi_leaderboard",
3137
[
32-
DateTime.now - SiteSetting.kofi_leaderboard_days.days,
38+
timestamp_offset,
3339
SiteSetting.kofi_leaderboard_types,
3440
SiteSetting.kofi_leaderboard_count
3541
]
@@ -48,6 +54,34 @@ def self.calculate_leaderboard
4854
leaderboard
4955
end
5056

57+
def self.calculate_goal
58+
goal = { progress: 0, target: nil }
59+
if SiteSetting.kofi_goal_amount <= 0 || SiteSetting.kofi_goal_types.empty?
60+
PluginStore.set(PLUGIN_NAME, :goal, goal)
61+
return goal
62+
end
63+
64+
goal[
65+
:target
66+
] = SiteSetting.kofi_goal_amount if SiteSetting.kofi_goal_show_amount
67+
68+
timestamp_offset =
69+
DateTime.now - 1.month if SiteSetting.kofi_goal_period == "monthly"
70+
timestamp_offset =
71+
DateTime.now - 1.year if SiteSetting.kofi_goal_period == "yearly"
72+
73+
total =
74+
Payment
75+
.where("timestamp > ?", timestamp_offset)
76+
.where(payment_type: SiteSetting.kofi_goal_types.split("|"))
77+
.sum(:amount)
78+
79+
goal[:progress] = (total / SiteSetting.kofi_goal_amount * 100).floor
80+
81+
PluginStore.set(PLUGIN_NAME, :goal, goal)
82+
goal
83+
end
84+
5185
def self.calculate_summary
5286
end
5387
end

config/locales/server.en.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,12 @@ en:
1212
kofi_dashboard_anonymous_view: 'What details should be shown for unauthenticated users'
1313
kofi_dashboard_authenticated_view: 'What should be shown for authenticated users'
1414
kofi_leaderboard_count: 'Number of donators to show. 0 will disable the leaderboard'
15-
kofi_leaderboard_days: 'Number of days to consider for the leaderboard. -1 means since ever'
15+
kofi_leaderboard_days: 'Number of days to consider for the leaderboard. -1 means since ever. 0 will disable the leaderboard'
1616
kofi_leaderboard_types: 'Which payment types to consider for the leaderboard'
17+
kofi_goal_amount: 'Goal of payment per period work toward to. 0 disables the goal'
18+
kofi_goal_period: 'Period over which the goal should work against'
19+
kofi_goal_show_amount: 'Show the target amount. Otherwise it will only show the percentage'
20+
kofi_goal_types: 'Which types to consider counting toward the goal'
1721
kofi:
1822
payment_type:
1923
donation: Donation

config/settings.yml

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ discourse_kofi:
2121
type: boolean
2222
default: true
2323
client: true
24-
kofi_dashboard_totals_types:
24+
kofi_dashboard_summary_types:
2525
type: list
2626
default: donation|subscription
2727
client: false
@@ -32,9 +32,19 @@ discourse_kofi:
3232
- subscription
3333
- commission
3434
- shop_order
35-
kofi_dashboard_totals_days:
36-
default: 365
35+
kofi_dashboard_summary_periods:
36+
type: list
37+
default: 'all time'
3738
client: false
39+
allow_any: false
40+
list_type: compact
41+
choices:
42+
- last 7 days
43+
- last month
44+
- last year
45+
- all time
46+
- current month
47+
- current year
3848
kofi_dashboard_count:
3949
default: 50
4050
client: true
@@ -55,20 +65,45 @@ discourse_kofi:
5565
- amount
5666
- user
5767
- message
58-
- include_unknown
68+
- include_unknown_users
5969
kofi_dashboard_authenticated_view:
6070
type: list
61-
default: user|message|include_unknown
71+
default: user|message|include_unknown_users
6272
client: false
6373
allow_any: false
6474
list_type: compact
6575
choices: *visibility
76+
kofi_goal_amount:
77+
default: 0
78+
client: false
79+
kofi_goal_period:
80+
default: monthly
81+
type: enum
82+
choices:
83+
- monthly
84+
- yearly
85+
#- since
86+
#kofi_goal_since:
87+
# default: ''
88+
# type: datetime
89+
kofi_goal_show_amount:
90+
default: false
91+
client: false
92+
kofi_goal_types:
93+
type: list
94+
default: donation|subscription
95+
client: false
96+
allow_any: false
97+
list_type: compact
98+
choices: *payment_types
6699
kofi_leaderboard_count:
67100
default: 10
68101
client: true
102+
min: 0
69103
kofi_leaderboard_days:
70104
default: 90
71105
client: true
106+
min: -1
72107
kofi_leaderboard_types:
73108
type: list
74109
default: donation|subscription|commission|shop_order

spec/services/payment_stats_spec.rb

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,61 @@
7676
{ name: "number4" }
7777
]
7878
)
79+
80+
stored_leaderboard =
81+
PluginStore
82+
.get(DiscourseKofi::PLUGIN_NAME, :leaderboard)
83+
.map { |e| e.symbolize_keys }
84+
expect(stored_leaderboard).to eq(leaderboard)
85+
end
86+
end
87+
88+
describe "goal" do
89+
before(:example) do
90+
SiteSetting.kofi_goal_amount = 100
91+
Fabricate(:kofi_payment, amount: 10)
92+
Fabricate(:kofi_payment, amount: 10, timestamp: DateTime.now - 2.months)
93+
Fabricate(:kofi_subscription, amount: 50)
94+
95+
Fabricate(:kofi_payment, amount: 1000, type: "Commission")
96+
Fabricate(:kofi_payment, amount: 1000, timestamp: DateTime.now - 2.years)
97+
end
98+
99+
it "does not calculate a goal when target is 0" do
100+
SiteSetting.kofi_goal_amount = 0
101+
goal = described_class.calculate_goal
102+
expect(goal[:progress]).to eq(0)
103+
expect(goal[:target]).to be_nil
104+
end
105+
106+
it "calculates a monthly goal" do
107+
goal = described_class.calculate_goal
108+
expect(goal[:progress]).to eq(60)
109+
expect(goal[:target]).to be_nil
110+
end
111+
112+
it "calculates a yearly goal" do
113+
SiteSetting.kofi_goal_period = "yearly"
114+
goal = described_class.calculate_goal
115+
expect(goal[:progress]).to eq(70)
116+
expect(goal[:target]).to be_nil
117+
end
118+
119+
it "target is returned when enabled" do
120+
SiteSetting.kofi_goal_show_amount = true
121+
goal = described_class.calculate_goal
122+
expect(goal[:progress]).to eq(60)
123+
expect(goal[:target]).to eq(100)
124+
125+
stored_goal =
126+
PluginStore.get(DiscourseKofi::PLUGIN_NAME, :goal).symbolize_keys
127+
expect(stored_goal).to eq(goal)
128+
end
129+
130+
it "progress can go over 100%" do
131+
SiteSetting.kofi_goal_amount = 50
132+
goal = described_class.calculate_goal
133+
expect(goal[:progress]).to eq(120)
79134
end
80135
end
81136
end

0 commit comments

Comments
 (0)