diff options
-rw-r--r-- | app/helpers/application_helper.rb | 4 | ||||
-rw-r--r-- | app/models/user.rb | 40 | ||||
-rw-r--r-- | lib/aclog/constants.rb | 4 | ||||
-rw-r--r-- | lib/unicorn_killer.rb | 54 | ||||
-rw-r--r-- | script/start.rb | 4 |
5 files changed, 22 insertions, 84 deletions
diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 64033e8..7251e80 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -53,11 +53,11 @@ module ApplicationHelper def title if @tweet - text = strip_tags(format_tweet_text(@tweet.text)) + text = CGI.unescapeHTML(strip_tags(format_tweet_text(@tweet.text))) @title = "\"#{text}\" from #{@user.screen_name}" end - CGI.unescapeHTML "#{@title || @caption} - aclog" + "#{@title || @caption} - aclog" end # utf8, form diff --git a/app/models/user.rb b/app/models/user.rb index 4c3cf7d..43696f8 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -17,7 +17,7 @@ class User < ActiveRecord::Base if orig["screen_name"] == user.screen_name && orig["name"] == user.name && - orig["profile_image_url"].split(//).reverse.take(36) == user.profile_image_url.split(//).reverse.take(36) && + orig["profile_image_url"][-44..-1] == user.profile_image_url[-44..-1] && orig["protected"] = user.protected? logger.debug("User not changed: #{user.id}") else @@ -71,41 +71,37 @@ class User < ActiveRecord::Base ret.retweeted_count = retweeted_counts.sum ret.average_favorited_count = favorited_counts.inject(:+).to_f / ret.tweets_count ret.average_retweeted_count = retweeted_counts.inject(:+).to_f / ret.tweets_count - ret.retweeted_count_str = ret.retweeted_count.to_s - ret.favorited_count_str = ret.favorited_count.to_s ret end end def count_discovered_by - merge_count_user(count_by(Favorite), count_by(Retweet)) - end - - def count_discovered_users - merge_count_user(count_to(Favorite), count_to(Retweet)) - end - - private - def count_by(klass) - klas = klass.arel_table tws = Tweet.arel_table - m = tws.project(tws[:id]).where(tws[:user_id].eq(self.id)).order(tws[:id].desc).take(100).as("m") - query = klas.project(klas[:user_id], klas[:user_id].count).join(m).on(klas[:tweet_id].eq(m[:id])).group(klas[:user_id]) - ActiveRecord::Base.connection.exec_query(query.to_sql).rows + f = -> model do + klas = model.arel_table + m = tws.project(tws[:id]).where(tws[:user_id].eq(self.id)).order(tws[:id].desc).take(100).as("m") + query = klas.project(klas[:user_id], klas[:user_id].count).join(m).on(klas[:tweet_id].eq(m[:id])).group(klas[:user_id]) + ActiveRecord::Base.connection.exec_query(query.to_sql).rows + end + merge_count_user(f.call(Favorite), f.call(Retweet)) end - def count_to(klass) - klas = klass.arel_table + def count_discovered_users tws = Tweet.arel_table - m = klas.project(klas[:tweet_id]).where(klas[:user_id].eq(self.id)).order(klas[:id].desc).take(500).as("m") - query = tws.project(tws[:user_id], tws[:user_id].count).join(m).on(tws[:id].eq(m[:tweet_id])).group(tws[:user_id]) - ActiveRecord::Base.connection.exec_query(query.to_sql).rows + f = -> model do + klas = model.arel_table + m = klas.project(klas[:tweet_id]).where(klas[:user_id].eq(self.id)).order(klas[:id].desc).take(500).as("m") + query = tws.project(tws[:user_id], tws[:user_id].count).join(m).on(tws[:id].eq(m[:tweet_id])).group(tws[:user_id]) + ActiveRecord::Base.connection.exec_query(query.to_sql).rows + end + merge_count_user(f.call(Favorite), f.call(Retweet)) end + private def merge_count_user(*args) ret = {} - args.map.each_with_index do |o, i| + args.each_with_index do |o, i| o.each do |user_id, count| ret[user_id] ||= Array.new(args.size, 0) ret[user_id][i] = count diff --git a/lib/aclog/constants.rb b/lib/aclog/constants.rb index d6cca67..8d07d46 100644 --- a/lib/aclog/constants.rb +++ b/lib/aclog/constants.rb @@ -4,9 +4,5 @@ module Aclog RESOLVED = 0 PENDING = 1 end - - module IssueType - TWEET_STOLEN = 0 - end end end diff --git a/lib/unicorn_killer.rb b/lib/unicorn_killer.rb deleted file mode 100644 index afec273..0000000 --- a/lib/unicorn_killer.rb +++ /dev/null @@ -1,54 +0,0 @@ -# https://gist.github.com/hotchpotch/1258681 -# # your config.ru -# require 'unicorn_killer' -# use UnicornKiller::MaxRequests, 1000 -# use UnicornKiller::Oom, 400 * 1024 - -module UnicornKiller - module Kill - def quit - sec = (Time.now - @process_start).to_i - warn "#{self.class} send SIGQUIT (pid: #{Process.pid})\talive: #{sec} sec" - Process.kill :QUIT, Process.pid - end - end - - class Oom - include Kill - - def initialize(app, memory_size= 512 * 1024, check_cycle = 10) - @app = app - @memory_size = memory_size - @check_cycle = check_cycle - @check_count = 0 - end - - def rss - `ps -o rss= -p #{Process.pid}`.to_i - end - - def call(env) - @process_start ||= Time.now - if (@check_count += 1) % @check_cycle == 0 - @check_count = 0 - quit if rss > @memory_size - end - @app.call env - end - end - - class MaxRequests - include Kill - - def initialize(app, max_requests = 1000) - @app = app - @max_requests = max_requests - end - - def call(env) - @process_start ||= Time.now - quit if (@max_requests -= 1) == 0 - @app.call env - end - end -end diff --git a/script/start.rb b/script/start.rb index 3d527a2..98ede1f 100644 --- a/script/start.rb +++ b/script/start.rb @@ -1,7 +1,7 @@ Aclog::Receiver::Worker.spawn!( working_dir: Rails.root, - pid_file: File.join(Rails.root, "tmp", "pids", "receiver.pid"), - log_file: File.join(Rails.root, "log", "receiver.log"), + pid_file: Rails.root.join("tmp", "pids", "receiver.pid").to_s, + log_file: Rails.root.join("log", "receiver.log").to_s, sync_log: true, singleton: true ) |