Skip to content

Commit

Permalink
Add one hour and twelve hours to monitoring (#160)
Browse files Browse the repository at this point in the history
Closes #156
  • Loading branch information
LucaBernstein authored Jul 23, 2022
1 parent 0cabb7c commit 2605813
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
3 changes: 1 addition & 2 deletions db/crud/monitoring.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ func (r *Repo) HealthGetUserCount() (count int, err error) {
return
}

func (r *Repo) HealthGetUsersActiveCounts(maxDays int) (count int, err error) {
maxDiffHours := maxDays * 24
func (r *Repo) HealthGetUsersActiveCounts(maxDiffHours int) (count int, err error) {
rows, err := r.db.Query(`
SELECT COUNT("difference") from (
SELECT DISTINCT EXTRACT(EPOCH FROM (NOW() - MAX(l."created"))) / 3600 AS difference
Expand Down
2 changes: 1 addition & 1 deletion db/crud/monitoring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func TestHealthGetUsersActiveCounts(t *testing.T) {
WillReturnRows(sqlmock.NewRows([]string{"count"}).
AddRow(4))

count, err := r.HealthGetUsersActiveCounts(3)
count, err := r.HealthGetUsersActiveCounts(3 * 24)
if err != nil {
t.Errorf("Should not fail for getting active user count: %s", err.Error())
}
Expand Down
24 changes: 20 additions & 4 deletions web/health/monitoring.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ type MonitoringResult struct {

users_count int

users_active_last_1d int
users_active_last_7d int
users_active_last_1h int
users_active_last_12h int
users_active_last_1d int
users_active_last_7d int

cache_entries_accTo int
cache_entries_accFrom int
Expand Down Expand Up @@ -50,6 +52,8 @@ bc_bot_users_count %d
# HELP users_active_last Count of unique users by user ID active in the previous timeframe
# TYPE users_active_last gauge
bc_bot_users_active_last{timeframe="1h"} %d
bc_bot_users_active_last{timeframe="12h"} %d
bc_bot_users_active_last{timeframe="1d"} %d
bc_bot_users_active_last{timeframe="7d"} %d
Expand All @@ -71,6 +75,8 @@ bc_bot_version_information{version="%s"} 1

m.users_count,

m.users_active_last_1h,
m.users_active_last_12h,
m.users_active_last_1d,
m.users_active_last_7d,

Expand Down Expand Up @@ -106,12 +112,22 @@ func gatherMetrics(bc *bot.BotController) (result *MonitoringResult) {
}
result.users_count = count

active_1d, err := bc.Repo.HealthGetUsersActiveCounts(1)
active_1h, err := bc.Repo.HealthGetUsersActiveCounts(1)
if err != nil {
bc.Logf(helpers.ERROR, nil, "Error getting health users: %s", err.Error())
}
result.users_active_last_1h = active_1h
active_12h, err := bc.Repo.HealthGetUsersActiveCounts(12)
if err != nil {
bc.Logf(helpers.ERROR, nil, "Error getting health users: %s", err.Error())
}
result.users_active_last_12h = active_12h
active_1d, err := bc.Repo.HealthGetUsersActiveCounts(24)
if err != nil {
bc.Logf(helpers.ERROR, nil, "Error getting health users: %s", err.Error())
}
result.users_active_last_1d = active_1d
active_7d, err := bc.Repo.HealthGetUsersActiveCounts(7)
active_7d, err := bc.Repo.HealthGetUsersActiveCounts(7 * 24)
if err != nil {
bc.Logf(helpers.ERROR, nil, "Error getting health users: %s", err.Error())
}
Expand Down

0 comments on commit 2605813

Please sign in to comment.