Skip to content

Commit

Permalink
add more statistics
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaktushose committed Jul 6, 2024
1 parent e860f5e commit d1d1560
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public class KarmaTokenTask {

@ScheduledTask(startAtMidnight = true, period = 24, unit = TimeUnit.HOURS)
public void onResetKarmaTokens(Bot bot) {
// we need to do this here so this runs before the token reset
bot.getDatabase().getRankService().updateStatistics();
bot.getDatabase().getKarmaService().resetTokens();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,15 @@ public void resetDailyStatistics() {
}
}

public void updateStatistics() {
log.debug("Resetting start_xp for all users");
try (Connection connection = dataSource.getConnection()) {
connection.prepareStatement("SELECT * FROM update_rank_statistics()").execute();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}

public void increaseTotalMessageCount() {
log.debug("Increasing total message count by one");
try (Connection connection = dataSource.getConnection()) {
Expand Down
137 changes: 137 additions & 0 deletions src/main/resources/db/migration/V1.0.1__add_rank_statistics.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
ALTER TABLE rank_statistics
ADD COLUMN users_with_boosters INT NOT NULL DEFAULT 0,
ADD COLUMN users_with_premium INT NOT NULL DEFAULT 0,
ADD COLUMN daily_info_users INT NOT NULL DEFAULT 0,
ADD COLUMN starboard_count INT NOT NULL DEFAULT 0,
ADD COLUMN active_karma_users INT NOT NULL DEFAULT 0,
ADD COLUMN rank_1_count INT NOT NULL DEFAULT 0,
ADD COLUMN rank_2_count INT NOT NULL DEFAULT 0,
ADD COLUMN rank_3_count INT NOT NULL DEFAULT 0,
ADD COLUMN rank_4_count INT NOT NULL DEFAULT 0,
ADD COLUMN rank_5_count INT NOT NULL DEFAULT 0,
ADD COLUMN rank_6_count INT NOT NULL DEFAULT 0,
ADD COLUMN rank_7_count INT NOT NULL DEFAULT 0,
ADD COLUMN rank_8_count INT NOT NULL DEFAULT 0,
ADD COLUMN rank_9_count INT NOT NULL DEFAULT 0,
ADD COLUMN rank_10_count INT NOT NULL DEFAULT 0,
ADD COLUMN rank_11_count INT NOT NULL DEFAULT 0,
ADD COLUMN rank_12_count INT NOT NULL DEFAULT 0,
ADD COLUMN rank_13_count INT NOT NULL DEFAULT 0,
ADD COLUMN rank_14_count INT NOT NULL DEFAULT 0,
ADD COLUMN rank_15_count INT NOT NULL DEFAULT 0;

CREATE OR REPLACE FUNCTION update_rank_statistics()
RETURNS VOID AS
$$
DECLARE
_users_with_boosters INT;
_users_with_premium INT;
_daily_info_users INT;
_starboard_count INT;
_active_karma_users INT;
_rank_1_count INT;
_rank_2_count INT;
_rank_3_count INT;
_rank_4_count INT;
_rank_5_count INT;
_rank_6_count INT;
_rank_7_count INT;
_rank_8_count INT;
_rank_9_count INT;
_rank_10_count INT;
_rank_11_count INT;
_rank_12_count INT;
_rank_13_count INT;
_rank_14_count INT;
_rank_15_count INT;
BEGIN
SELECT COUNT(*) FROM transactions JOIN items ON transactions.item_id = items.item_id JOIN item_types ON item_types.base_type_id = 1 INTO _users_with_boosters;
SELECT COUNT(*) FROM transactions JOIN items ON transactions.item_id = items.item_id JOIN item_types ON item_types.base_type_id = 2 INTO _users_with_premium;
SELECT COUNT(*) FROM users WHERE daily_message = TRUE INTO _daily_info_users;
SELECT COUNT(*) FROM starboard_entries INTO _starboard_count;
SELECT COUNT(*) FROM users WHERE karma_tokens < 5 INTO _active_karma_users;
SELECT COUNT(*) FROM users WHERE rank_id = 1 INTO _rank_1_count;
SELECT COUNT(*) FROM users WHERE rank_id = 2 INTO _rank_2_count;
SELECT COUNT(*) FROM users WHERE rank_id = 3 INTO _rank_3_count;
SELECT COUNT(*) FROM users WHERE rank_id = 4 INTO _rank_4_count;
SELECT COUNT(*) FROM users WHERE rank_id = 5 INTO _rank_5_count;
SELECT COUNT(*) FROM users WHERE rank_id = 6 INTO _rank_6_count;
SELECT COUNT(*) FROM users WHERE rank_id = 7 INTO _rank_7_count;
SELECT COUNT(*) FROM users WHERE rank_id = 8 INTO _rank_8_count;
SELECT COUNT(*) FROM users WHERE rank_id = 9 INTO _rank_9_count;
SELECT COUNT(*) FROM users WHERE rank_id = 10 INTO _rank_10_count;
SELECT COUNT(*) FROM users WHERE rank_id = 11 INTO _rank_11_count;
SELECT COUNT(*) FROM users WHERE rank_id = 12 INTO _rank_12_count;
SELECT COUNT(*) FROM users WHERE rank_id = 13 INTO _rank_13_count;
SELECT COUNT(*) FROM users WHERE rank_id = 14 INTO _rank_14_count;
SELECT COUNT(*) FROM users WHERE rank_id = 15 INTO _rank_15_count;

INSERT INTO rank_statistics(
"date",
"users_with_boosters",
"users_with_premium",
"daily_info_users",
"starboard_count",
"active_karma_users",
"rank_1_count",
"rank_2_count",
"rank_3_count",
"rank_4_count",
"rank_5_count",
"rank_6_count",
"rank_7_count",
"rank_8_count",
"rank_9_count",
"rank_10_count",
"rank_11_count",
"rank_12_count",
"rank_13_count",
"rank_14_count",
"rank_15_count"
)
VALUES(
CURRENT_DATE,
_users_with_boosters,
_users_with_premium,
_daily_info_users,
_starboard_count,
_active_karma_users,
_rank_1_count,
_rank_2_count,
_rank_3_count,
_rank_4_count,
_rank_5_count,
_rank_6_count,
_rank_7_count,
_rank_8_count,
_rank_9_count,
_rank_10_count,
_rank_11_count,
_rank_12_count,
_rank_13_count,
_rank_14_count,
_rank_15_count
)
ON CONFLICT (DATE) DO UPDATE SET
rank_statistics.users_with_boosters = _users_with_boosters,
rank_statistics.users_with_premium = _users_with_premium,
rank_statistics.daily_info_users = _daily_info_users,
rank_statistics.starboard_count = _starboard_count,
rank_statistics.active_karma_users = _active_karma_users,
rank_statistics.rank_1_count = _rank_1_count,
rank_statistics.rank_2_count = _rank_2_count,
rank_statistics.rank_3_count = _rank_3_count,
rank_statistics.rank_4_count = _rank_4_count,
rank_statistics.rank_5_count = _rank_5_count,
rank_statistics.rank_6_count = _rank_6_count,
rank_statistics.rank_7_count = _rank_7_count,
rank_statistics.rank_8_count = _rank_8_count,
rank_statistics.rank_9_count = _rank_9_count,
rank_statistics.rank_10_count = _rank_10_count,
rank_statistics.rank_11_count = _rank_11_count,
rank_statistics.rank_12_count = _rank_12_count,
rank_statistics.rank_13_count = _rank_13_count,
rank_statistics.rank_14_count = _rank_14_count,
rank_statistics.rank_15_count = _rank_15_count;
END;
$$ LANGUAGE plpgsql;

0 comments on commit d1d1560

Please sign in to comment.