Skip to content

Commit

Permalink
keyword をテーブルにする 27440
Browse files Browse the repository at this point in the history
  • Loading branch information
mickamy committed Jan 2, 2024
1 parent c3501cd commit 34883eb
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
15 changes: 15 additions & 0 deletions note.md
Original file line number Diff line number Diff line change
Expand Up @@ -393,3 +393,18 @@ docker exec -i ishocon2-bench-1 sh -c "./benchmark --ip app:443 --workload 6"
2024/01/02 06:42:26 投票者の感心がなくなりました
2024/01/02 06:42:26 {"score": 31606, "success": 26078, "failure": 0}
```

- keyword をテーブルにする 28084

```
❯ make bench
docker exec -i ishocon2-bench-1 sh -c "./benchmark --ip app:443 --workload 6"
2024/01/02 07:23:59 Start GET /initialize
2024/01/02 07:23:59 期日前投票を開始します
2024/01/02 07:24:00 期日前投票が終了しました
2024/01/02 07:24:00 投票を開始します Workload: 6
2024/01/02 07:24:46 投票が終了しました
2024/01/02 07:24:46 投票者が結果を確認しています
2024/01/02 07:25:23 投票者の感心がなくなりました
2024/01/02 07:25:23 {"score": 28084, "success": 23692, "failure": 0}
```
24 changes: 17 additions & 7 deletions webapp/ruby/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ def transaction(&block)
yield
db.query("COMMIT")
rescue => e
puts e.backtrace.&join("\n")
puts e.message
puts e.backtrace || 'no backtrace'
db.query("ROLLBACK")
end
end
Expand Down Expand Up @@ -81,10 +82,11 @@ def election_results

def voice_of_supporter(candidate_ids)
query = <<SQL
SELECT keyword
FROM votes
WHERE candidate_id IN (?)
GROUP BY keyword
SELECT content as keyword
FROM candidate_keywords k
JOIN votes v on k.candidate_id = v.candidate_id
WHERE k.candidate_id IN (?)
GROUP BY k.content
ORDER BY IFNULL(SUM(count), 0) DESC
LIMIT 10
SQL
Expand All @@ -93,10 +95,14 @@ def voice_of_supporter(candidate_ids)

def db_initialize
db.query('DELETE FROM votes')
db.query('CREATE TABLE IF NOT EXISTS candidate_keywords (id int(11) NOT NULL AUTO_INCREMENT, candidate_id int(11) NOT NULL, content text NOT NULL, PRIMARY KEY (id)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4')
db.query('ALTER TABLE candidate_keywords ADD CONSTRAINT fk_keyword_to_candidate FOREIGN KEY (candidate_id) REFERENCES candidates (id)')
db.query('DELETE FROM candidate_keywords')
db.query('ALTER TABLE votes ADD INDEX idx_votes_candidate_id (candidate_id)')
db.query('ALTER TABLE votes ADD COLUMN count int(4) NOT NULL')
db.query('ALTER TABLE votes ADD CONSTRAINT fk_votes_to_candidate FOREIGN KEY (candidate_id) REFERENCES candidates (id)')
db.query('ALTER TABLE votes ADD INDEX idx_votes_candidate_and_count (candidate_id, count)')
db.query('ALTER TABLE votes DROP COLUMN keyword')
end
end

Expand Down Expand Up @@ -187,8 +193,12 @@ def db_initialize
return erb :vote, locals: { candidates: cs, message: '投票理由を記入してください' }
end

insert_query = 'INSERT INTO votes (user_id, candidate_id, keyword, count) VALUES (?, ?, ?, ?)'
result = db.xquery(insert_query, user[:id], candidate[:id], params[:keyword], params[:vote_count].to_i)
transaction do
insert_vote_query = 'INSERT INTO votes (user_id, candidate_id, count) VALUES (?, ?, ?)'
db.xquery(insert_vote_query, user[:id], candidate[:id], params[:vote_count].to_i)
insert_keyword_query = 'INSERT INTO candidate_keywords (candidate_id, content) VALUES (?, ?)'
db.xquery(insert_keyword_query, candidate[:id], params[:keyword])
end

# transaction do
# values = params[:vote_count].to_i.times.map { "(#{user_id}, #{candidate_id}, '#{keyword}')" }.join(', ')
Expand Down

0 comments on commit 34883eb

Please sign in to comment.