mirror of
https://github.com/SrIzan10/hc-harbor.git
synced 2026-05-01 10:45:21 +00:00
Add sailors log remote database
This commit is contained in:
21
app/jobs/one_time/import_from_sailors_log_job.rb
Normal file
21
app/jobs/one_time/import_from_sailors_log_job.rb
Normal file
@@ -0,0 +1,21 @@
|
||||
class SailorsLog::SlackNotificationPreference < ::ApplicationRecord
|
||||
self.abstract_class = true
|
||||
connects_to database: { reading: :sailors_log, writing: :sailors_log }
|
||||
self.table_name = "SlackNotificationPreference"
|
||||
|
||||
scope :enabled, -> { where(enabled: true) }
|
||||
end
|
||||
|
||||
class OneTime::ImportFromSailorsLogJob < ApplicationJob
|
||||
queue_as :default
|
||||
|
||||
def perform
|
||||
# Import from SailorsLog
|
||||
SailorsLog::SlackNotificationPreference.enabled.each do |preference|
|
||||
slnp = ::SailorsLogNotificationPreference.find_or_create_by(
|
||||
slack_uid: preference.slack_user_id,
|
||||
slack_channel_id: preference.slack_channel_id
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -2,7 +2,7 @@ class SailorsLog < ApplicationRecord
|
||||
validates :slack_uid, presence: true, uniqueness: true
|
||||
validates :projects_summary, presence: true
|
||||
|
||||
after_create :initialize_projects_summary
|
||||
before_create :initialize_projects_summary
|
||||
|
||||
has_many :notification_preferences,
|
||||
class_name: "SailorsLogNotificationPreference",
|
||||
@@ -17,10 +17,9 @@ class SailorsLog < ApplicationRecord
|
||||
private
|
||||
|
||||
def initialize_projects_summary
|
||||
self.projects_summary = {}
|
||||
return unless projects_summary.blank?
|
||||
Heartbeat.where(user_id: slack_uid).distinct.pluck(:project).each do |project|
|
||||
self.projects_summary[project] = Heartbeat.where(user_id: slack_uid, project: project).duration_seconds
|
||||
end
|
||||
save! if changed?
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,14 +1,23 @@
|
||||
class SailorsLogNotificationPreference < ApplicationRecord
|
||||
after_create :ensure_sailors_log_exists
|
||||
before_validation :ensure_sailors_log_exists
|
||||
|
||||
belongs_to :sailors_log,
|
||||
class_name: "SailorsLog",
|
||||
foreign_key: :slack_uid,
|
||||
primary_key: :slack_uid,
|
||||
optional: true
|
||||
primary_key: :slack_uid
|
||||
|
||||
validates :slack_uid, uniqueness: {
|
||||
scope: :slack_channel_id,
|
||||
message: "already has a notification preference for this channel"
|
||||
}
|
||||
|
||||
private
|
||||
|
||||
def ensure_sailors_log_exists
|
||||
SailorsLog.find_or_create_by(slack_uid: slack_uid)
|
||||
return if sailors_log.present?
|
||||
|
||||
sailors_log = SailorsLog.find_or_create_by(slack_uid: slack_uid)
|
||||
self.sailors_log = sailors_log
|
||||
sailors_log.send(:initialize_projects_summary)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -18,6 +18,9 @@ development:
|
||||
wakatime:
|
||||
url: <%= ENV['WAKATIME_DATABASE_URL'] %>
|
||||
replica: true
|
||||
sailors_log:
|
||||
url: <%= ENV['SAILORS_LOG_DATABASE_URL'] %>
|
||||
replica: true
|
||||
|
||||
# Warning: The database defined as "test" will be erased and
|
||||
# re-generated from your development database when you run "rake".
|
||||
@@ -43,6 +46,9 @@ production:
|
||||
wakatime:
|
||||
url: <%= ENV['WAKATIME_DATABASE_URL'] %>
|
||||
replica: true
|
||||
sailors_log:
|
||||
url: <%= ENV['SAILORS_LOG_DATABASE_URL'] %>
|
||||
replica: true
|
||||
cache:
|
||||
<<: *default
|
||||
database: storage/production_cache.sqlite3
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
class AddUniqueIndexToSailorsLogNotificationPreferences < ActiveRecord::Migration[8.0]
|
||||
def change
|
||||
add_index :sailors_log_notification_preferences,
|
||||
[ :slack_uid, :slack_channel_id ],
|
||||
unique: true,
|
||||
name: 'idx_sailors_log_notification_preferences_unique_user_channel'
|
||||
end
|
||||
end
|
||||
3
db/schema.rb
generated
3
db/schema.rb
generated
@@ -10,7 +10,7 @@
|
||||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema[8.0].define(version: 2025_02_22_032930) do
|
||||
ActiveRecord::Schema[8.0].define(version: 2025_02_22_082500) do
|
||||
# These are extensions that must be enabled in order to support this database
|
||||
enable_extension "pg_catalog.plpgsql"
|
||||
|
||||
@@ -128,6 +128,7 @@ ActiveRecord::Schema[8.0].define(version: 2025_02_22_032930) do
|
||||
t.boolean "enabled", default: true, null: false
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.index ["slack_uid", "slack_channel_id"], name: "idx_sailors_log_notification_preferences_unique_user_channel", unique: true
|
||||
end
|
||||
|
||||
create_table "sailors_log_slack_notifications", force: :cascade do |t|
|
||||
|
||||
Reference in New Issue
Block a user