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

v1.5.0 #82

Merged
merged 6 commits into from
Jun 6, 2021
Merged
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
4 changes: 4 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ TWEET_STORAGE_USERNAME=
TWEET_STORAGE_PASSWORD=

BUGSNAG_API_KEY=
ROLLBAR_TOKEN=
APP_NAME_FOR_LOGGER=
REMOTE_SYSLOG_LOGGER_HOST=
REMOTE_SYSLOG_LOGGER_PORT=

RECOMMENDED_QUOTES_WORKSHEET_ID=
RECOMMENDED_QUOTES_SHEET_NAME=
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ gem 'google-apis-sheets_v4'
gem 'paper_trail'
gem 'pg'
gem 'puma'
gem 'remote_syslog_logger'
gem 'rollbar'
gem 'twitter'

group :development, :test do
Expand Down
6 changes: 6 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,8 @@ GEM
rb-inotify (0.10.1)
ffi (~> 1.0)
regexp_parser (2.1.1)
remote_syslog_logger (1.0.4)
syslog_protocol
representable (3.1.1)
declarative (< 0.1.0)
trailblazer-option (>= 0.1.1, < 0.2.0)
Expand All @@ -223,6 +225,7 @@ GEM
rack (>= 1.4)
retriable (3.1.2)
rexml (3.2.5)
rollbar (3.2.0)
rspec-core (3.10.1)
rspec-support (~> 3.10.0)
rspec-expectations (3.10.1)
Expand Down Expand Up @@ -273,6 +276,7 @@ GEM
actionpack (>= 4.0)
activesupport (>= 4.0)
sprockets (>= 3.0.0)
syslog_protocol (0.9.2)
thor (1.1.0)
thread_safe (0.3.6)
trailblazer-option (0.1.1)
Expand Down Expand Up @@ -316,6 +320,8 @@ DEPENDENCIES
pry-rails
puma
rails
remote_syslog_logger
rollbar
rspec-rails
rspec_junit_formatter
rubocop-rails
Expand Down
Empty file removed app/controllers/concerns/.keep
Empty file.
40 changes: 40 additions & 0 deletions app/controllers/concerns/logger_methods.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# TODO: 命名を再考
module LoggerMethods
extend ActiveSupport::Concern

module ModuleMethods
def convert_hash_to_json(options={})
{
app_name: ENV['APP_NAME_FOR_LOGGER'] || options[:app_name] || '',
message: options[:message] || '',
time: Time.zone.now.to_s
}.merge(options).to_json
end

def convert_tweet_objects_to_array(tweets)
converted_array_from_tweet_objects = []

# 長すぎると Papertrail が記録してくれない
tweets.each do |tweet|
inserted_hash = {
id_number: tweet.id_number,
# user_name: tweet.user.name,
screen_name: tweet.user.screen_name,
# full_text: tweet.full_text,
# is_retweet: tweet.is_retweet,
url: tweet.url,
tweeted_at: tweet.tweeted_at
# media_exists: tweet.has_assets?,
# is_public: tweet.is_public?,
# is_mentioned_to_gensosenkyo_admin: tweet.is_mentioned_to_gensosenkyo_admin?
}

converted_array_from_tweet_objects << inserted_hash
end

converted_array_from_tweet_objects
end
end

extend ModuleMethods
end
65 changes: 48 additions & 17 deletions app/lib/google_sheet_api/write_to_response_api_sheet_by_hashtag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

module GoogleSheetApi
class WriteToResponseApiSheetByHashtag
include LoggerMethods

def initialize(spreadsheet_id: nil, sheet_name: nil)
@client = GoogleSheetApi::Client.new.create
@spreadsheet_id = spreadsheet_id || ENV['RECOMMENDED_QUOTES_WORKSHEET_ID']
Expand All @@ -16,13 +18,24 @@ def initialize(spreadsheet_id: nil, sheet_name: nil)
end

def execute(hashtag, options={})
update_data(target_tweets(hashtag, options))
update_target_tweets = target_tweets(hashtag, options)

Rails.logger.info(
LoggerMethods.convert_hash_to_json(
hashtag: hashtag,
message: 'update_target_tweets',
tweets: LoggerMethods.convert_tweet_objects_to_array(update_target_tweets)
)
)

update_data(update_target_tweets)
rescue StandardError => e
Rails.logger.fatal 'FATAL エラーです: GoogleSheetApi::GoogleSheetApi#execute'
Rails.logger.fatal e if e.present?
Rails.logger.fatal(LoggerMethods.convert_hash_to_json(message: 'FATAL エラーです: GoogleSheetApi::GoogleSheetApi#execute'))
Rails.logger.fatal(LoggerMethods.convert_hash_to_json(message: e)) if e.present?

bugsnag_error_message = "FATAL エラーです: GoogleSheetApi::GoogleSheetApi#execute / #{e}"
Bugsnag.notify(bugsnag_error_message)
Rollbar.error(e, 'FATAL エラーです: GoogleSheetApi::GoogleSheetApi#execute')
end

# 既存のデータを読み込み、新規のデータをその後ろにくっつけ、それを貼り付けている
Expand All @@ -44,26 +57,44 @@ def update_data(tweets)
)
end

# TODO: not_retweet だったり '2021-05-01' をハードコーディングしたりしてるのは微妙
# [options]
# Time: beginning_at, end_at
# Boolean: remove_rt, not_by_gensosenkyo
# Bigint: end_search_tweet_id_number
# rubocop:disable Style/ConditionalAssignment
def target_tweets(hashtag, options={})
# 初回操作対応
beginning_search_tweet_id_number = @max_tweet_id_number.present? ? @max_tweet_id_number + 1 : 1
beginning_at = options[:beginning_at] || '2021-05-01'
end_at = options[:end_at] || '2030-12-31'

if options[:raw_log] == true
Tweet.contains_hashtag(hashtag).where(
id_number: beginning_search_tweet_id_number..
).where(
tweeted_at: Time.zone.parse('2021-04-01')..
).order(tweeted_at: :asc)
if options[:beginning_search_tweet_id_number].present?
beginning_search_tweet_id_number = options[:beginning_search_tweet_id_number]
else
# 初回操作対応
beginning_search_tweet_id_number = @max_tweet_id_number.present? ? @max_tweet_id_number + 1 : 1
end
end_search_tweet_id_number = options[:end_search_tweet_id_number]

active_record_relation = Tweet.contains_hashtag(hashtag).where(
tweeted_at: beginning_at..end_at
)
active_record_relation = active_record_relation.not_retweet if options[:remove_rt] == true
active_record_relation = active_record_relation.not_by_gensosenkyo if options[:not_by_gensosenkyo] == true

if end_search_tweet_id_number.present?
active_record_relation = active_record_relation.where(
id_number: beginning_search_tweet_id_number..end_search_tweet_id_number
)
else
# not_retweet, contains_hashtag, not_by_gensosenkyo, tweeted_at
Tweet.not_retweet.contains_hashtag(hashtag).not_by_gensosenkyo.where(
active_record_relation = active_record_relation.where(
id_number: beginning_search_tweet_id_number..
).where(
tweeted_at: Time.zone.parse('2021-05-01')..
).order(tweeted_at: :asc)
)
end

active_record_relation.where(
id_number: beginning_search_tweet_id_number..
).order(tweeted_at: :asc)
end
# rubocop:enable Style/ConditionalAssignment

def set_basic_valiables
already_sheet_all_data = @client.get_spreadsheet_values(
Expand Down
10 changes: 10 additions & 0 deletions app/models/tweet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,14 @@ def has_assets?
def has_in_tweet_urls?
in_tweet_urls.present?
end

def is_mentioned_to_gensosenkyo_admin?
# gensosenkyo: 1471724029, sub_gensosenkyo: 1388758231825018881
gensosenkyo_admin_user_id_numbers = {
'gensosenkyo': 1471724029,
'sub_gensosenkyo': 1388758231825018881
}

mentions.any? { |mention| mention.user_id_number.in?(gensosenkyo_admin_user_id_numbers.values) }
end
end
2 changes: 2 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,7 @@ class Application < Rails::Application
config.load_defaults 6.1
config.time_zone = 'Tokyo'
config.api_only = true

config.logger = RemoteSyslogLogger.new(ENV['REMOTE_SYSLOG_LOGGER_HOST'], ENV['REMOTE_SYSLOG_LOGGER_PORT'])
end
end
3 changes: 1 addition & 2 deletions config/initializers/bugsnag.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
Bugsnag.configure do |config|
# TODO: 運用が落ち着いてきたら環境を絞る
if true | Rails.env.in?(['production', 'staging'])
if Rails.env.in?(['production', 'staging', 'development'])
config.api_key = ENV['BUGSNAG_API_KEY']
end
end
71 changes: 71 additions & 0 deletions config/initializers/rollbar.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
Rollbar.configure do |config|
# Without configuration, Rollbar is enabled in all environments.
# To disable in specific environments, set config.enabled=false.

config.access_token = ENV['ROLLBAR_TOKEN']

# Here we'll disable in 'test':
if Rails.env.test?
config.enabled = false
end

# By default, Rollbar will try to call the `current_user` controller method
# to fetch the logged-in user object, and then call that object's `id`
# method to fetch this property. To customize:
# config.person_method = "my_current_user"
# config.person_id_method = "my_id"

# Additionally, you may specify the following:
# config.person_username_method = "username"
# config.person_email_method = "email"

# If you want to attach custom data to all exception and message reports,
# provide a lambda like the following. It should return a hash.
# config.custom_data_method = lambda { {:some_key => "some_value" } }

# Add exception class names to the exception_level_filters hash to
# change the level that exception is reported at. Note that if an exception
# has already been reported and logged the level will need to be changed
# via the rollbar interface.
# Valid levels: 'critical', 'error', 'warning', 'info', 'debug', 'ignore'
# 'ignore' will cause the exception to not be reported at all.
# config.exception_level_filters.merge!('MyCriticalException' => 'critical')
#
# You can also specify a callable, which will be called with the exception instance.
# config.exception_level_filters.merge!('MyCriticalException' => lambda { |e| 'critical' })

# Enable asynchronous reporting (uses girl_friday or Threading if girl_friday
# is not installed)
# config.use_async = true
# Supply your own async handler:
# config.async_handler = Proc.new { |payload|
# Thread.new { Rollbar.process_from_async_handler(payload) }
# }

# Enable asynchronous reporting (using sucker_punch)
# config.use_sucker_punch

# Enable delayed reporting (using Sidekiq)
# config.use_sidekiq
# You can supply custom Sidekiq options:
# config.use_sidekiq 'queue' => 'default'

# If your application runs behind a proxy server, you can set proxy parameters here.
# If https_proxy is set in your environment, that will be used. Settings here have precedence.
# The :host key is mandatory and must include the URL scheme (e.g. 'http://'), all other fields
# are optional.
#
# config.proxy = {
# host: 'http://some.proxy.server',
# port: 80,
# user: 'username_if_auth_required',
# password: 'password_if_auth_required'
# }

# If you run your staging application instance in production environment then
# you'll want to override the environment reported by `Rails.env` with an
# environment variable like this: `ROLLBAR_ENV=staging`. This is a recommended
# setup for Heroku. See:
# https://devcenter.heroku.com/articles/deploying-to-a-custom-rails-environment
config.environment = ENV['ROLLBAR_ENV'].presence || Rails.env
end