Skip to content

Commit

Permalink
content_keyword の導入 361385
Browse files Browse the repository at this point in the history
  • Loading branch information
mickamy committed Jan 10, 2024
1 parent 21166a4 commit 2f36f61
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 12 deletions.
15 changes: 15 additions & 0 deletions note.go.md
Original file line number Diff line number Diff line change
Expand Up @@ -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}
```
10 changes: 7 additions & 3 deletions webapp/go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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{
Expand Down
64 changes: 55 additions & 9 deletions webapp/go/vote.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package main

import "strings"
import (
"context"
"strings"
)

// Vote Model
type Vote struct {
Expand All @@ -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) {
Expand All @@ -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
}
Expand Down

0 comments on commit 2f36f61

Please sign in to comment.