Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

staticsのN+1解消 #22

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 30 additions & 16 deletions webapp/go/stats_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,12 @@ GROUP BY u.id`
return c.JSON(http.StatusOK, stats)
}

type LivestreamStats struct {
LivestreamID int64 `db:"livestream_id"`
Reactions int64 `db:"reactions"`
TotalTips int64 `db:"total_tips"`
}

func getLivestreamStatisticsHandler(c echo.Context) error {
ctx := c.Request().Context()

Expand Down Expand Up @@ -254,23 +260,31 @@ func getLivestreamStatisticsHandler(c echo.Context) error {
}

// ランク算出
var ranking LivestreamRanking
for _, livestream := range livestreams {
var reactions int64
if err := tx.GetContext(ctx, &reactions, "SELECT COUNT(*) FROM livestreams l INNER JOIN reactions r ON l.id = r.livestream_id WHERE l.id = ?", livestream.ID); err != nil && !errors.Is(err, sql.ErrNoRows) {
return echo.NewHTTPError(http.StatusInternalServerError, "failed to count reactions: "+err.Error())
}

var totalTips int64
if err := tx.GetContext(ctx, &totalTips, "SELECT IFNULL(SUM(l2.tip), 0) FROM livestreams l INNER JOIN livecomments l2 ON l.id = l2.livestream_id WHERE l.id = ?", livestream.ID); err != nil && !errors.Is(err, sql.ErrNoRows) {
return echo.NewHTTPError(http.StatusInternalServerError, "failed to count tips: "+err.Error())
}

score := reactions + totalTips
ranking = append(ranking, LivestreamRankingEntry{
LivestreamID: livestream.ID,
var livestreamStats []LivestreamStats
query := `
SELECT
l.id AS livestream_id,
IFNULL(COUNT(r.id), 0) AS reactions,
IFNULL(SUM(lc.tip), 0) AS total_tips
FROM
livestreams l
LEFT JOIN reactions r ON l.id = r.livestream_id
LEFT JOIN livecomments lc ON l.id = lc.livestream_id
GROUP BY
l.id
`
if err := tx.SelectContext(ctx, &livestreamStats, query); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "failed to get livestream stats: "+err.Error())
}

// ランキングを作成
ranking := make(LivestreamRanking, len(livestreamStats))
for i, ls := range livestreamStats {
score := ls.Reactions + ls.TotalTips
ranking[i] = LivestreamRankingEntry{
LivestreamID: ls.LivestreamID,
Score: score,
})
}
}
sort.Sort(ranking)

Expand Down