Skip to content
This repository has been archived by the owner on Nov 28, 2023. It is now read-only.

Commit

Permalink
view_count
Browse files Browse the repository at this point in the history
  • Loading branch information
pgmot committed Nov 25, 2023
1 parent 5aa9b47 commit 9efbe64
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 33 deletions.
25 changes: 5 additions & 20 deletions go/livestream_handler.go
Expand Up @@ -40,6 +40,7 @@ type LivestreamModel struct {
ThumbnailUrl string `db:"thumbnail_url" json:"thumbnail_url"`
StartAt int64 `db:"start_at" json:"start_at"`
EndAt int64 `db:"end_at" json:"end_at"`
ViewerCount int64 `db:"viewer_count" json:"-"`
}

type Livestream struct {
Expand Down Expand Up @@ -325,11 +326,6 @@ func enterLivestreamHandler(c echo.Context) error {
return err
}

// error already checked
sess, _ := session.Get(defaultSessionIDKey, c)
// existence already checked
userID := sess.Values[defaultUserIDKey].(int64)

livestreamID, err := strconv.Atoi(c.Param("livestream_id"))
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "livestream_id must be integer")
Expand All @@ -341,14 +337,8 @@ func enterLivestreamHandler(c echo.Context) error {
}
defer tx.Rollback()

viewer := LivestreamViewerModel{
UserID: int64(userID),
LivestreamID: int64(livestreamID),
CreatedAt: time.Now().Unix(),
}

if _, err := tx.NamedExecContext(ctx, "INSERT INTO livestream_viewers_history (user_id, livestream_id, created_at) VALUES(:user_id, :livestream_id, :created_at)", viewer); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "failed to insert livestream_view_history: "+err.Error())
if _, err := tx.ExecContext(ctx, "UPDATE livestreams SET viewer_count = viewer_count + 1 WHERE id = ?", livestreamID); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "failed to update livestreams.viewer_conut: "+err.Error())
}

if err := tx.Commit(); err != nil {
Expand All @@ -365,11 +355,6 @@ func exitLivestreamHandler(c echo.Context) error {
return err
}

// error already checked
sess, _ := session.Get(defaultSessionIDKey, c)
// existence already checked
userID := sess.Values[defaultUserIDKey].(int64)

livestreamID, err := strconv.Atoi(c.Param("livestream_id"))
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "livestream_id in path must be integer")
Expand All @@ -381,8 +366,8 @@ func exitLivestreamHandler(c echo.Context) error {
}
defer tx.Rollback()

if _, err := tx.ExecContext(ctx, "DELETE FROM livestream_viewers_history WHERE user_id = ? AND livestream_id = ?", userID, livestreamID); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "failed to delete livestream_view_history: "+err.Error())
if _, err := tx.ExecContext(ctx, "UPDATE livestreams SET viewer_count = viewer_count - 1 WHERE id = ?", livestreamID); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "failed to update livestreams.viewer_conut: "+err.Error())
}

if err := tx.Commit(); err != nil {
Expand Down
14 changes: 2 additions & 12 deletions go/stats_handler.go
Expand Up @@ -165,11 +165,7 @@ func getUserStatisticsHandler(c echo.Context) error {
// 合計視聴者数
var viewersCount int64
for _, livestream := range livestreams {
var cnt int64
if err := tx.GetContext(ctx, &cnt, "SELECT COUNT(*) FROM livestream_viewers_history WHERE livestream_id = ?", livestream.ID); err != nil && !errors.Is(err, sql.ErrNoRows) {
return echo.NewHTTPError(http.StatusInternalServerError, "failed to get livestream_view_history: "+err.Error())
}
viewersCount += cnt
viewersCount += livestream.ViewerCount
}

// お気に入り絵文字
Expand Down Expand Up @@ -262,12 +258,6 @@ func getLivestreamStatisticsHandler(c echo.Context) error {
rank++
}

// 視聴者数算出
var viewersCount int64
if err := tx.GetContext(ctx, &viewersCount, `SELECT COUNT(*) FROM livestreams l INNER JOIN livestream_viewers_history h ON h.livestream_id = l.id WHERE l.id = ?`, livestreamID); err != nil && !errors.Is(err, sql.ErrNoRows) {
return echo.NewHTTPError(http.StatusInternalServerError, "failed to count livestream viewers: "+err.Error())
}

// 最大チップ額
var maxTip int64
if err := tx.GetContext(ctx, &maxTip, `SELECT IFNULL(MAX(tip), 0) FROM livestreams l INNER JOIN livecomments l2 ON l2.livestream_id = l.id WHERE l.id = ?`, livestreamID); err != nil && !errors.Is(err, sql.ErrNoRows) {
Expand All @@ -292,7 +282,7 @@ func getLivestreamStatisticsHandler(c echo.Context) error {

return c.JSON(http.StatusOK, LivestreamStatistics{
Rank: rank,
ViewersCount: viewersCount,
ViewersCount: livestream.ViewerCount,
MaxTip: maxTip,
TotalReactions: totalReactions,
TotalReports: totalReports,
Expand Down
3 changes: 2 additions & 1 deletion sql/init.sql
Expand Up @@ -45,7 +45,8 @@ CREATE TABLE `livestreams` (
`playlist_url` VARCHAR(255) NOT NULL,
`thumbnail_url` VARCHAR(255) NOT NULL,
`start_at` BIGINT NOT NULL,
`end_at` BIGINT NOT NULL
`end_at` BIGINT NOT NULL,
`viewer_count` BIGINT NOT NULL DEFAULT 0
) ENGINE=InnoDB CHARACTER SET utf8mb4 COLLATE utf8mb4_bin;

-- ライブ配信予約枠
Expand Down

0 comments on commit 9efbe64

Please sign in to comment.