added 260907 version of worker toolkit

This commit is contained in:
2026-09-08 20:30:51 -04:00
parent cbd3f0f8ca
commit 97aca37663
1283 changed files with 142951 additions and 0 deletions

View File

@@ -0,0 +1,32 @@
class MergeTrackables::CheckinTrackables
include Sidekiq::Worker
def perform(trackable_type, parent_id, rest_ids)
if Checkin::FIELD_TYPE.include? trackable_type
update_health_factors(trackable_type, parent_id, rest_ids)
else
update_regular_trackables(trackable_type, parent_id, rest_ids)
end
p "PERFORM MergeTrackables::PostTrackables"
MergeTrackables::PatternIncludes.perform_async(trackable_type, parent_id, rest_ids)
end
def update_health_factors(trackable_type, parent_id, rest_ids)
Checkin.where("#{trackable_type}_ids" => {"$in" => rest_ids}).map do |checkin|
updated_ids = checkin.send("#{trackable_type}_ids") - rest_ids + [parent_id]
checkin.update("#{trackable_type}_ids".to_sym => updated_ids.uniq)
end
end
def update_regular_trackables(trackable_type, parent_id, rest_ids)
klass = "Checkin::#{trackable_type.capitalize}".constantize
return unless klass
klass.where("#{trackable_type}_id".to_sym.in => rest_ids).map do |checkin|
checkin.update("#{trackable_type}_id".to_sym => parent_id)
end
end
end

View File

@@ -0,0 +1,50 @@
class MergeTrackables::Dispatcher
include Sidekiq::Worker
def perform(trackable_type, translation = nil)
trackable_class = trackable_type.capitalize.constantize
searchable_attr = (trackable_class.name == "Food") ? "long_desc" : "name"
return find_duplicates(trackable_type, trackable_class, translation, searchable_attr) if translation.present?
trackable_class::Translation.find_each do |trackable_translation|
translation = trackable_translation.send(searchable_attr.to_s)
next unless translation
find_duplicates(trackable_type, trackable_class, translation, searchable_attr)
end
end
def find_duplicates(trackable_type, trackable_class, translation, searchable_attr = "name")
begin
escaped_translation = Regexp.escape(translation.squish).split(" ").join("s+")
regex = "^\\s*#{escaped_translation}\\s*$"
same_translations = trackable_class::Translation.where("#{searchable_attr} ~* ?", regex)
return if same_translations.length <= 1 # Next step if origin found only
same_trackables = trackable_class
.where(id: same_translations.where(locale: "en").select("#{trackable_type}_id".to_sym))
.order(trackable_usages_count: :desc, id: :asc)
parent, *rest = same_trackables
return if parent.nil? || rest.length.zero?
p "DUPLICATES FOUND..."
p "#{trackable_type.capitalize} ids: #{same_trackables.pluck(:id)}"
p "#{trackable_type.capitalize} translations: }"
p same_trackables.map(&:"#{searchable_attr}")
rest_ids = rest.map(&:id)
# Remove duplicates from translations
same_translations.where("#{trackable_type}_id": rest_ids).map(&:destroy)
rescue ActiveRecord::StatementInvalid
return
end
p "PERFORM MergeTrackables::UserTrackableAssociation"
MergeTrackables::UserTrackableAssociation.perform_async(trackable_type, parent.id, rest_ids)
end
end

View File

@@ -0,0 +1,22 @@
class MergeTrackables::PatternIncludes
include Sidekiq::Worker
def perform(trackable_type, parent_id, rest_ids)
parent = trackable_type.capitalize.constantize.find_by(id: parent_id)
return unless parent
Pattern.where("includes.id" => {"$in" => rest_ids}).map do |item|
item.includes
.select { |hash| hash[:category] == trackable_type.tableize && rest_ids.include?(hash[:id]) }
.map do |selected_hash|
selected_hash[:id] = parent_id
selected_hash[:label] = parent.name
end
item.includes = item.includes.uniq
item.save
end
MergeTrackables::PostTrackables.perform_async(trackable_type, parent_id, rest_ids)
end
end

View File

@@ -0,0 +1,17 @@
class MergeTrackables::PostTrackables
include Sidekiq::Worker
def perform(trackable_type, parent_id, rest_ids)
trackable_key = "#{trackable_type}_ids"
Post.where(trackable_key.to_sym.in => rest_ids).map do |post|
updated_ids = post.send(trackable_key) - rest_ids + [parent_id]
post.update(trackable_key.to_sym => updated_ids.uniq)
end
p "PERFORM MergeTrackables::RemoveDuplicates"
MergeTrackables::RemoveDuplicates.perform_async(trackable_type, rest_ids)
end
end

View File

@@ -0,0 +1,11 @@
class MergeTrackables::RemoveDuplicates
include Sidekiq::Worker
def perform(trackable_type, rest_ids)
trackable_class = trackable_type.capitalize.constantize
return unless trackable_class
trackable_class.where(id: rest_ids).map(&:destroy)
p "FINISH!!!!"
end
end

View File

@@ -0,0 +1,17 @@
class MergeTrackables::TopicFollowing
include Sidekiq::Worker
def perform(trackable_type, parent_id, rest_ids)
trackable_key = "#{trackable_type}_ids"
TopicFollowing.all(trackable_key.to_sym.in => rest_ids).map do |tf|
updated_ids = tf.send(trackable_key) - rest_ids + [parent_id]
tf.update(trackable_key.to_sym => updated_ids.uniq)
end
p "PERFORM MergeTrackables::CheckinTrackables"
MergeTrackables::CheckinTrackables.perform_async(trackable_type, parent_id, rest_ids)
end
end

View File

@@ -0,0 +1,31 @@
class MergeTrackables::TrackableUsages
include Sidekiq::Worker
def perform(trackable_type, parent_id, rest_ids)
trackable_class = trackable_type.capitalize.constantize
return unless trackable_class
parent = trackable_class.find_by(id: parent_id)
rest = trackable_class.where(id: rest_ids)
return if parent.nil? && rest.length.zero?
TrackableUsage.where(trackable: rest).map do |tr_usage|
parent_usage = TrackableUsage.find_by(trackable: parent, user_id: tr_usage.user_id)
if parent_usage
parent_usage.count += tr_usage.count
parent_usage.save
tr_usage.destroy
else
tr_usage.update(trackable_id: parent.id)
parent.increment!(:trackable_usages_count)
end
end
p "PERFORM MergeTrackables::Trackings"
MergeTrackables::Trackings.perform_async(trackable_type, parent_id, rest_ids)
end
end

View File

@@ -0,0 +1,13 @@
class MergeTrackables::Trackings
include Sidekiq::Worker
def perform(trackable_type, parent_id, rest_ids)
Tracking.where(trackable_id: rest_ids, trackable_type: trackable_type.camelcase).map do |tracking|
tracking.update(trackable_id: parent_id)
end
p "PERFORM MergeTrackables::TopicFollowing"
MergeTrackables::TopicFollowing.perform_async(trackable_type, parent_id, rest_ids)
end
end

View File

@@ -0,0 +1,27 @@
class MergeTrackables::UserTrackableAssociation
include Sidekiq::Worker
def perform(trackable_type, parent_id, rest_ids)
trackable_class = trackable_type.capitalize.constantize
klass = "User#{trackable_type.capitalize}".constantize
return unless trackable_class && klass
parent = trackable_class.find_by(id: parent_id)
rest = trackable_class.where(id: rest_ids)
return if parent.nil? && rest.length.zero?
klass.where(trackable_type.to_sym => rest).map do |user_trackable_type|
parent_user_trackable = klass.find_by(trackable_type.to_sym => parent,
:user_id => user_trackable_type.user_id)
if parent_user_trackable.present?
user_trackable_type.destroy
else
user_trackable_type.update_columns("#{trackable_type}_id".to_sym => parent.id)
end
end
p "PERFORM MergeTrackables::TrackableUsages"
MergeTrackables::TrackableUsages.perform_async(trackable_type, parent_id, rest_ids)
end
end