Skip to content

Commit

Permalink
feat: add param timeframe
Browse files Browse the repository at this point in the history
  • Loading branch information
lmquang committed Jan 21, 2025
1 parent c1760b2 commit a328e21
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 8 deletions.
9 changes: 7 additions & 2 deletions pkg/handler/discord/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,11 @@ func (h *handler) NotifyTopMemoAuthors(c *gin.Context) {
return
}

topAuthors, err := h.store.MemoLog.GetTopAuthors(h.repo.DB(), in.Limit)
now := time.Now()
end := time.Date(now.Year(), now.Month(), now.Day(), 23, 59, 59, 999999999, now.Location())
start := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()).AddDate(0, 0, -in.Days+1)

topAuthors, err := h.store.MemoLog.GetTopAuthors(h.repo.DB(), in.Limit, &start, &end)
if err != nil {
h.logger.Error(err, "failed to retrieve top memo authors")
c.JSON(http.StatusInternalServerError, view.CreateResponse[any](nil, nil, err, nil, ""))
Expand All @@ -439,8 +443,9 @@ func (h *handler) NotifyTopMemoAuthors(c *gin.Context) {
targetChannelID = discordRandomChannel
}

title := fmt.Sprintf("Top %d Memo Authors (Last %d Days)", in.Limit, in.Days)
msg := &discordgo.MessageEmbed{
Title: "Top Memo Authors",
Title: title,
Description: topAuthorsStr,
}

Expand Down
18 changes: 18 additions & 0 deletions pkg/handler/discord/request/top_memo_authors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package request

import "errors"

type TopMemoAuthorsInput struct {
Limit int `json:"limit"`
Days int `json:"days"`
}

func (i *TopMemoAuthorsInput) Validate() error {
if i.Limit <= 0 {
return errors.New("limit must be greater than 0")
}
if i.Days <= 0 {
return errors.New("days must be greater than 0")
}
return nil
}
9 changes: 8 additions & 1 deletion pkg/handler/memologs/memo_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,14 @@ func (h *handler) GetTopAuthors(c *gin.Context) {
},
)

topAuthors, err := h.store.MemoLog.GetTopAuthors(h.repo.DB(), 10)
limit, _ := strconv.Atoi(c.DefaultQuery("limit", "10"))
days, _ := strconv.Atoi(c.DefaultQuery("days", "30"))

now := time.Now()
end := time.Date(now.Year(), now.Month(), now.Day(), 23, 59, 59, 999999999, now.Location())
start := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()).AddDate(0, 0, -days+1)

topAuthors, err := h.store.MemoLog.GetTopAuthors(h.repo.DB(), limit, &start, &end)
if err != nil {
l.Error(err, "failed to get top authors")
c.JSON(http.StatusInternalServerError, view.CreateResponse[any](nil, nil, err, nil, ""))
Expand Down
2 changes: 1 addition & 1 deletion pkg/store/memolog/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ type IStore interface {
GetRankByDiscordID(db *gorm.DB, discordID string) (*model.DiscordAccountMemoRank, error)
ListNonAuthor(db *gorm.DB) ([]model.MemoLog, error)
CreateMemoAuthor(db *gorm.DB, memoAuthor *model.MemoAuthor) error
GetTopAuthors(db *gorm.DB, limit int) ([]model.DiscordAccountMemoRank, error)
GetTopAuthors(db *gorm.DB, limit int, from, to *time.Time) ([]model.DiscordAccountMemoRank, error)
}
9 changes: 5 additions & 4 deletions pkg/store/memolog/memo_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ func (s *store) CreateMemoAuthor(db *gorm.DB, memoAuthor *model.MemoAuthor) erro
return fmt.Errorf("memo_authors table no longer exists")
}

// GetTopAuthors gets the top authors by memo count
func (s *store) GetTopAuthors(db *gorm.DB, limit int) ([]model.DiscordAccountMemoRank, error) {
// GetTopAuthors gets the top authors by memo count within a time range
func (s *store) GetTopAuthors(db *gorm.DB, limit int, from, to *time.Time) ([]model.DiscordAccountMemoRank, error) {
query := `
WITH memo_count AS (
SELECT
Expand All @@ -139,7 +139,8 @@ func (s *store) GetTopAuthors(db *gorm.DB, limit int) ([]model.DiscordAccountMem
jsonb_array_elements_text(ml.discord_account_ids) AS account_id
WHERE
ml.deleted_at IS NULL AND
da.id::text = account_id
da.id::text = account_id AND
ml.published_at BETWEEN ? AND ?
GROUP BY
da.discord_id,
da.discord_username,
Expand All @@ -158,5 +159,5 @@ func (s *store) GetTopAuthors(db *gorm.DB, limit int) ([]model.DiscordAccountMem
LIMIT ?;
`
var topAuthors []model.DiscordAccountMemoRank
return topAuthors, db.Raw(query, limit).Scan(&topAuthors).Error
return topAuthors, db.Raw(query, from, to, limit).Scan(&topAuthors).Error
}

0 comments on commit a328e21

Please sign in to comment.