From 2f36f61be3c793c59ed0b7a90480dbcbfc4b258e Mon Sep 17 00:00:00 2001 From: mickamy Date: Wed, 10 Jan 2024 11:33:36 +0900 Subject: [PATCH] =?UTF-8?q?content=5Fkeyword=20=E3=81=AE=E5=B0=8E=E5=85=A5?= =?UTF-8?q?=20361385?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- note.go.md | 15 +++++++++++ webapp/go/main.go | 10 +++++--- webapp/go/vote.go | 64 ++++++++++++++++++++++++++++++++++++++++------- 3 files changed, 77 insertions(+), 12 deletions(-) diff --git a/note.go.md b/note.go.md index 7c70569..6e7cdf7 100644 --- a/note.go.md +++ b/note.go.md @@ -149,3 +149,18 @@ docker exec -i ishocon2-bench-1 sh -c "./benchmark --ip app:443 --workload 7" 2024/01/10 11:04:50 投票者の感心がなくなりました 2024/01/10 11:04:50 {"score": 344721, "success": 233409, "failure": 0} ``` + +content_keyword の導入 361385 + +``` +❯ make bench +docker exec -i ishocon2-bench-1 sh -c "./benchmark --ip app:443 --workload 7" +2024/01/10 11:31:22 Start GET /initialize +2024/01/10 11:31:22 期日前投票を開始します +2024/01/10 11:31:22 期日前投票が終了しました +2024/01/10 11:31:22 投票を開始します Workload: 7 +2024/01/10 11:32:08 投票が終了しました +2024/01/10 11:32:08 投票者が結果を確認しています +2024/01/10 11:32:23 投票者の感心がなくなりました +2024/01/10 11:32:23 {"score": 361385, "success": 240017, "failure": 0} +``` diff --git a/webapp/go/main.go b/webapp/go/main.go index 4d428ff..f1b672e 100644 --- a/webapp/go/main.go +++ b/webapp/go/main.go @@ -52,10 +52,11 @@ func main() { pass := getEnv("ISHOCON2_DB_PASSWORD", "ishocon") dbname := getEnv("ISHOCON2_DB_NAME", "ishocon2") db, _ = sql.Open("mysql", user+":"+pass+"@/"+dbname) - db.SetMaxIdleConns(5) + db.SetMaxIdleConns(12) gin.SetMode(gin.ReleaseMode) - r := gin.Default() + r := gin.New() + r.Use(gin.Recovery()) r.Use(static.Serve("/css", static.LocalFile("public/css", true))) r.HTMLRender = createRender() @@ -224,7 +225,10 @@ func main() { //for i := 1; i <= voteCount; i++ { // createVote(user.ID, candidate.ID, c.PostForm("keyword")) //} - createVote(user.ID, candidate.ID, c.PostForm("keyword"), voteCount) + err := createVote(c, user.ID, candidate.ID, c.PostForm("keyword"), voteCount) + if err != nil { + panic(err) + } message = "投票に成功しました" } c.HTML(http.StatusOK, "vote", gin.H{ diff --git a/webapp/go/vote.go b/webapp/go/vote.go index 79a86c9..f4bf8a2 100644 --- a/webapp/go/vote.go +++ b/webapp/go/vote.go @@ -1,6 +1,9 @@ package main -import "strings" +import ( + "context" + "strings" +) // Vote Model type Vote struct { @@ -22,9 +25,49 @@ func getUserVotedCount(userID int) (count int) { return } -func createVote(userID int, candidateID int, keyword string, count int) { - db.Exec("INSERT INTO votes (user_id, candidate_id, keyword, count) VALUES (?, ?, ?, ?)", +func createVote(c context.Context, userID int, candidateID int, keyword string, count int) error { + tx, err := db.BeginTx(c, nil) + if err != nil { + return err + } + _, err = db.ExecContext(c, "INSERT INTO votes (user_id, candidate_id, keyword, count) VALUES (?, ?, ?, ?)", userID, candidateID, keyword, count) + if err != nil { + tx.Rollback() + return err + } + rows, err := db.QueryContext(c, + "SELECT id from candidate_keywords WHERE candidate_id = ? AND content = ? FOR UPDATE", + candidateID, + keyword, + ) + if err != nil { + tx.Rollback() + return err + } + var can_key_id int64 + rows.Scan(&can_key_id) + rows.Close() + if &can_key_id == nil { + _, err = db.ExecContext(c, + "INSERT INTO candidate_keywords (candidate_id, content, count) VALUES (?, ?, ?)", + candidateID, + keyword, + count, + ) + } else { + _, err = db.ExecContext(c, + "UPDATE candidate_keywords SET count = count + ? WHERE id = ?", + count, + can_key_id, + ) + } + if err != nil { + tx.Rollback() + return err + } + err = tx.Commit() + return err } func getVoiceOfSupporter(candidateIDs []int) (voices []string) { @@ -33,12 +76,15 @@ func getVoiceOfSupporter(candidateIDs []int) (voices []string) { args = append(args, candidateID) } rows, err := db.Query(` - SELECT keyword - FROM votes - WHERE candidate_id IN (`+strings.Join(strings.Split(strings.Repeat("?", len(candidateIDs)), ""), ",")+`) - GROUP BY keyword - ORDER BY IFNULL(SUM(count), 0) DESC - LIMIT 10`, args...) +SELECT content as keyword +FROM candidate_keywords +WHERE candidate_id IN (`+strings.Join(strings.Split(strings.Repeat("?", len(candidateIDs)), ""), ",")+`) +group by content +ORDER BY IFNULL(SUM(count), 0) DESC +LIMIT 10; +`, + args..., + ) if err != nil { return nil }