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

kill count #1

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.bundle
vendor/

1 change: 1 addition & 0 deletions .ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2.4.0
37 changes: 28 additions & 9 deletions app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,29 @@
require 'rack-flash'
require 'json'



module Isucon4
class App < Sinatra::Base
use Rack::Session::Cookie, secret: ENV['ISU4_SESSION_SECRET'] || 'shirokane'
use Rack::Flash
set :public_folder, File.expand_path('../../public', __FILE__)

def initialize
super()
setup
end

def setup
last_succeeds = db.xquery('SELECT user_id, login, MAX(id) AS last_login_id FROM login_log WHERE user_id IS NOT NULL AND succeeded = 1 GROUP BY user_id')

$last_succeeds_count = {}
last_succeeds.each do |row|
count = db.xquery('SELECT COUNT(id) AS cnt FROM login_log WHERE user_id = ? AND ? < id', row['user_id'], row['last_login_id']).first['cnt']
$last_succeeds_count[row['user_id'].to_s] = { count: count, login: row['login'] }
end
end

helpers do
def config
@config ||= {
Expand Down Expand Up @@ -38,6 +55,16 @@ def login_log(succeeded, login, user_id = nil)
" (`created_at`, `user_id`, `login`, `ip`, `succeeded`)" \
" VALUES (?,?,?,?,?)",
Time.now, user_id, login, request.ip, succeeded ? 1 : 0)

if succeeded
$last_succeeds_count.delete(user_id.to_s)
else
if $last_succeeds_count.has_key?(user_id.to_s)
$last_succeeds_count[user_id.to_s][:count] = $last_succeeds_count[user_id.to_s][:count] + 1
else
$last_succeeds_count[user_id.to_s] = { count: 1, login: login }
end
end
end

def user_locked?(user)
Expand Down Expand Up @@ -124,15 +151,7 @@ def locked_users
not_succeeded = db.xquery('SELECT user_id, login FROM (SELECT user_id, login, MAX(succeeded) as max_succeeded, COUNT(1) as cnt FROM login_log GROUP BY user_id) AS t0 WHERE t0.user_id IS NOT NULL AND t0.max_succeeded = 0 AND t0.cnt >= ?', threshold)

user_ids.concat not_succeeded.each.map { |r| r['login'] }

last_succeeds = db.xquery('SELECT user_id, login, MAX(id) AS last_login_id FROM login_log WHERE user_id IS NOT NULL AND succeeded = 1 GROUP BY user_id')

last_succeeds.each do |row|
count = db.xquery('SELECT COUNT(1) AS cnt FROM login_log WHERE user_id = ? AND ? < id', row['user_id'], row['last_login_id']).first['cnt']
if threshold <= count
user_ids << row['login']
end
end
user_ids.concat $last_succeeds_count.select { |k,v| v[:count] >= threshold }.map { |k,v| v[:login] }

user_ids
end
Expand Down