Skip to content

Commit

Permalink
Fix for daily report logic, extract issue report in separate worker
Browse files Browse the repository at this point in the history
  • Loading branch information
arturtr committed Apr 8, 2016
1 parent 528b220 commit 5d82e74
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 22 deletions.
4 changes: 2 additions & 2 deletions app/workers/telegram_group_auto_close_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ def perform
need_to_notify_issues = Issue.open(false).joins(:telegram_group).
where('redmine_chat_telegram_telegram_groups.last_notification_at <= ?', 12.hours.ago)

need_to_notify_issues.each do |issue|
need_to_notify_issues.find_each do |issue|
TelegramGroupCloseNotificationWorker.perform_async(issue.id)
end

need_to_close_issues = Issue.open(false).joins(:telegram_group).
where('redmine_chat_telegram_telegram_groups.need_to_close_at <= ?', Time.now)

need_to_close_issues.each do |issue|
need_to_close_issues.find_each do |issue|
telegram_id = issue.telegram_group.telegram_id

issue.telegram_group.destroy
Expand Down
21 changes: 21 additions & 0 deletions app/workers/telegram_group_daily_report_cron_worker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class TelegramGroupDailyReportCronWorker
include Sidekiq::Worker

TELEGRAM_GROUP_DAILY_REPORT_CRON_LOG = Logger.new(Rails.root.join('log/chat_telegram',
'telegram-group-daily-report-cron.log'))

def perform
if Setting.plugin_redmine_chat_telegram['daily_report']
yesterday = 12.hours.ago
time_from = yesterday.beginning_of_day
time_to = yesterday.end_of_day

Issue.joins(:telegram_messages).where('telegram_messages.sent_at >= ? and telegram_messages.sent_at <= ?',
time_from, time_to).find_each do |issue|
TelegramGroupDailyReportWorker.perform_async(issue.id, yesterday.to_s)
end
end
rescue ActiveRecord::RecordNotFound => e
# ignore
end
end
49 changes: 30 additions & 19 deletions app/workers/telegram_group_daily_report_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,36 @@ class TelegramGroupDailyReportWorker

TELEGRAM_GROUP_DAILY_REPORT_LOG = Logger.new(Rails.root.join('log/chat_telegram', 'telegram-group-daily-report.log'))

def perform
if Setting.plugin_redmine_chat_telegram['daily_report']
yesterday = 12.hours.ago
time_from = yesterday.beginning_of_day
time_to = yesterday.end_of_day

Issue.joins(:telegram_messages).where('telegram_messages.sent_at >= ? and telegram_messages.sent_at <= ?',
time_from, time_to).find_each do |issue|
telegram_messages = issue.telegram_messages.
where('sent_at >= ? and sent_at <= ?', time_from, time_to).
where(is_system: false, bot_message: false)

date_string = format_date(yesterday)
user_names = telegram_messages.map(&:author_name).uniq
joined_user_names = user_names.join(', ')
journal_text = I18n.t('redmine_chat_telegram.journal.daily_report', date: date_string, users: joined_user_names, messages_count: telegram_messages.size, users_count: user_names.count)
issue.init_journal(User.current, "_#{ I18n.t 'redmine_chat_telegram.journal.from_telegram' }:_ \n\n#{journal_text}")
issue.save
end
def perform(issue_id, yesterday_string)
I18n.locale = Setting['default_language']

yesterday = Date.parse yesterday_string
time_from = yesterday.beginning_of_day
time_to = yesterday.end_of_day

issue = Issue.find(issue_id)
telegram_messages = issue.telegram_messages.
where('sent_at >= ? and sent_at <= ?', time_from, time_to).
where(is_system: false, bot_message: false)

date_string = format_date(yesterday)
user_names = telegram_messages.map(&:author_name).uniq
joined_user_names = user_names.join(', ')
journal_text = I18n.t('redmine_chat_telegram.journal.daily_report',
date: date_string,
users: joined_user_names,
messages_count: telegram_messages.size,
users_count: user_names.count)


begin
issue.init_journal(User.current,
"_#{ I18n.t 'redmine_chat_telegram.journal.from_telegram' }:_ \n\n#{journal_text}")

issue.save
rescue ActiveRecord::StaleObjectError
issue.reload
retry
end
rescue ActiveRecord::RecordNotFound => e
# ignore
Expand Down
2 changes: 1 addition & 1 deletion init.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

Sidekiq::Cron::Job.create(name: 'Telegram Group Daily Report - every day',
cron: '7 0 * * *',
class: 'TelegramGroupDailyReportWorker')
class: 'TelegramGroupDailyReportCronWorker')

Redmine::Plugin.register :redmine_chat_telegram do
name 'Redmine Chat Telegram plugin'
Expand Down

0 comments on commit 5d82e74

Please sign in to comment.