* Update gitignore There is a mystery postgres folder and we are going to ignore it * Add migrations * Update preview state default to pending * Adds indexes * Adds PgHero and PgStatements * Update .gitignore * Update Gemfile Production: - `newrelic_rpm` Development: - `pg_query` - `prosopite` * Configure Sidekiq Create job for cleaning up party previews * Configure Prosopite and remove CacheFreeLogger * Enable query logging * Update api_controller.rb Add N+1 detectioin via Prosopite in development/test environments * Refactor canonical object blueprints * Refactor grid object blueprints * Remove N+1 from grid object models Reimplementing `character` `summon` and `weapon` was making N+1s which made queries really slow * Add counter caches to party * Add preview generation helpers The Party model can respond to queries about its preview state with the following models: - `schedule_preview_generation` - `preview_content_changed?` - `preview_expired?` - `should_generate_preview?` - `ready_for_preview?` - `needs_preview_generation?` - `preview_relevant_attributes` Removes the following methods: - `schedule_preview_regeneration` - `preview_relevant_changes?` * Add cache to is_favorited(user) * Refactored PartyBlueprint to minimize N+1s * Remove preview expiry constants These are defined in the Coordinator instead * Add method comments * Create parties_controller.rbs * Update logic and logs * Updates excluded methods and calculate_count * Use `includes` instead of `joins` * Use a less-insane way of counting * Adds a helper method for party privacy * Update filter condition helpers Just minor refactoring * Fix old view name in PartyBlueprint * Refactor parties#create * Remove redundant return * Update parties_controller.rbs * Update parties#index * Update parties_controller.rb Updates apply_includes and apply_excludes, along with modifying id_to_table and build_query * Update parties_controller.rb Adds the rest of the changes, too tired to write them all out. Some preview generation, some filtering * Refactor parties#index and parties#favorites These are mostly the same methods, so we remove common code into build_parties_query and render_paginated_parties * Alias table name to object to maintain API consistency * Maintain API consistency with raid blueprint * Optimize party loading by adding eager loading to `set_from_slug` - Refactored `set_from_slug` to use `includes` for eager loading associated models: - `user`, `job`, `raid` (with `group`) - `characters` (with `character` and `awakening`) - `weapons` (with `weapon`, `awakenings`, `weapon_key1`, `weapon_key2`, `weapon_key3`) - `summons` (with `summon`) - `guidebooks` (`guidebook1`, `guidebook2`, `guidebook3`) - `source_party`, `remixes`, `skills`, and `accessory` - This change improves query efficiency by reducing N+1 queries and ensures all relevant associations are preloaded. - Removed redundant favorite check as it was not necessary in this context. * Refactor grid blueprints - **GridCharacterBlueprint:** - Removed `:minimal` view restriction on `party` association. - Improved nil checks for `ring1`, `ring2`, and `earring` to prevent errors. - Converted string values in `awakening_level`, `over_mastery`, and `aetherial_mastery` fields to integers for consistency. - Ensured `over_mastery` and `aetherial_mastery` only include valid entries, filtering out blank or zero-modifier values. - **GridWeaponBlueprint:** - Removed `:minimal` view restriction on `party` association. - Ensured `weapon` association exists before accessing `ax`, `series`, or `awakening`. - Improved conditional checks for `weapon_keys` to prevent errors when `weapon` or `series` is nil. - Converted `awakening_level` field to integer for consistency. - **GridCharacterBlueprint:** - Removed `:minimal` view restriction on `party` association. * Update raid blueprints - Show flat representation of raid group in RaidBlueprint's nested view - Show nested representation of raid in RaidGroupBlueprint's full view * Move n+1 detection to around_action hook * Improve handling mastery bonuses - Improved handling of nested attributes: - Replaced old mastery structure with new `rings` and `awakening` assignments. - Added `new_rings` and `new_awakening` virtual attributes for easier updates. - Updated `assign_attributes` to exclude `rings` and `awakening` to prevent conflicts. - Enhanced parameter transformation: - Introduced `transform_character_params` to process `rings`, `awakening`, and `earring` more reliably. - Ensured proper type conversion (`to_i`) for numeric values in `uncap_level`, `transcendence_step`, and `awakening_level`. - Improved error handling for missing values by setting defaults where needed. - Optimized database queries: - Added `.includes(:awakening)` to `set` to prevent N+1 query issues. - Updated strong parameters: - Changed `rings` from individual keys (`ring1`, `ring2`, etc.) to a structured array format. - Refactored permitted attributes to align with the new nested structure. * Eager-load jobs when querying job skills * Eager load raids/groups when querying * Update users_controller.rb More efficient way of denoting favorited parties. * Update awakening.rb - Removes explicitly defined associations and adds ActiveRecord associations instead * Update party.rb - Removes favorited accessor - Renames derivative_parties to remixes and adds in-built sort * Update weapon_awakening.rb - Removes redefined explicit associations * Update grid_character.rb - Adds code transforming incoming ring and awakening values into something the db understands * Update character.rb Add explicit Awakenings enum * Update coordinator.rb Adds 'queued' as a state for generation
246 lines
6 KiB
Ruby
246 lines
6 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
class Party < ApplicationRecord
|
|
##### ActiveRecord Associations
|
|
belongs_to :source_party,
|
|
class_name: 'Party',
|
|
foreign_key: :source_party_id,
|
|
optional: true
|
|
|
|
has_many :remixes, -> { order(created_at: :desc) },
|
|
class_name: 'Party',
|
|
foreign_key: 'source_party_id',
|
|
inverse_of: :source_party,
|
|
dependent: :nullify
|
|
|
|
belongs_to :user, optional: true
|
|
belongs_to :raid, optional: true
|
|
belongs_to :job, optional: true
|
|
|
|
belongs_to :accessory,
|
|
foreign_key: 'accessory_id',
|
|
class_name: 'JobAccessory',
|
|
optional: true
|
|
|
|
belongs_to :skill0,
|
|
foreign_key: 'skill0_id',
|
|
class_name: 'JobSkill',
|
|
optional: true
|
|
|
|
belongs_to :skill1,
|
|
foreign_key: 'skill1_id',
|
|
class_name: 'JobSkill',
|
|
optional: true
|
|
|
|
belongs_to :skill2,
|
|
foreign_key: 'skill2_id',
|
|
class_name: 'JobSkill',
|
|
optional: true
|
|
|
|
belongs_to :skill3,
|
|
foreign_key: 'skill3_id',
|
|
class_name: 'JobSkill',
|
|
optional: true
|
|
|
|
belongs_to :guidebook1,
|
|
foreign_key: 'guidebook1_id',
|
|
class_name: 'Guidebook',
|
|
optional: true
|
|
|
|
belongs_to :guidebook2,
|
|
foreign_key: 'guidebook2_id',
|
|
class_name: 'Guidebook',
|
|
optional: true
|
|
|
|
belongs_to :guidebook3,
|
|
foreign_key: 'guidebook3_id',
|
|
class_name: 'Guidebook',
|
|
optional: true
|
|
|
|
has_many :characters,
|
|
foreign_key: 'party_id',
|
|
class_name: 'GridCharacter',
|
|
counter_cache: true,
|
|
dependent: :destroy,
|
|
inverse_of: :party
|
|
|
|
has_many :weapons,
|
|
foreign_key: 'party_id',
|
|
class_name: 'GridWeapon',
|
|
counter_cache: true,
|
|
dependent: :destroy,
|
|
inverse_of: :party
|
|
|
|
has_many :summons,
|
|
foreign_key: 'party_id',
|
|
class_name: 'GridSummon',
|
|
counter_cache: true,
|
|
dependent: :destroy,
|
|
inverse_of: :party
|
|
|
|
has_many :favorites, dependent: :destroy
|
|
|
|
accepts_nested_attributes_for :characters
|
|
accepts_nested_attributes_for :summons
|
|
accepts_nested_attributes_for :weapons
|
|
|
|
before_create :set_shortcode
|
|
before_create :set_edit_key
|
|
|
|
##### Amoeba configuration
|
|
amoeba do
|
|
set weapons_count: 0
|
|
set characters_count: 0
|
|
set summons_count: 0
|
|
|
|
nullify :description
|
|
nullify :shortcode
|
|
nullify :edit_key
|
|
|
|
include_association :characters
|
|
include_association :weapons
|
|
include_association :summons
|
|
end
|
|
|
|
##### ActiveRecord Validations
|
|
validate :skills_are_unique
|
|
validate :guidebooks_are_unique
|
|
|
|
self.enum :preview_state, {
|
|
pending: 0,
|
|
queued: 1,
|
|
in_progress: 2,
|
|
generated: 3,
|
|
failed: 4
|
|
}
|
|
|
|
after_commit :schedule_preview_generation, if: :should_generate_preview?
|
|
|
|
def is_remix
|
|
!source_party.nil?
|
|
end
|
|
|
|
def remixes
|
|
Party.where(source_party_id: id)
|
|
end
|
|
|
|
def blueprint
|
|
PartyBlueprint
|
|
end
|
|
|
|
def public?
|
|
visibility == 1
|
|
end
|
|
|
|
def unlisted?
|
|
visibility == 2
|
|
end
|
|
|
|
def private?
|
|
visibility == 3
|
|
end
|
|
|
|
def is_favorited(user)
|
|
return false unless user
|
|
|
|
Rails.cache.fetch("party_#{id}_favorited_by_#{user.id}", expires_in: 1.hour) do
|
|
user.favorite_parties.include?(self)
|
|
end
|
|
end
|
|
|
|
def ready_for_preview?
|
|
return false if weapons_count < 1 # At least 1 weapon
|
|
return false if characters_count < 1 # At least 1 character
|
|
return false if summons_count < 1 # At least 1 summon
|
|
true
|
|
end
|
|
|
|
def should_generate_preview?
|
|
return false unless ready_for_preview?
|
|
|
|
# Always generate if no preview exists
|
|
return true if preview_state.nil? || preview_state == 'pending'
|
|
|
|
# Generate if failed and enough time has passed for conditions to change
|
|
return true if preview_state == 'failed' && preview_generated_at < 5.minutes.ago
|
|
|
|
# Generate if preview is old
|
|
return true if preview_state == 'generated' && preview_expired?
|
|
|
|
# Only regenerate on content changes if the last generation was > 5 minutes ago
|
|
# This prevents rapid regeneration during party building
|
|
if preview_content_changed?
|
|
return true if preview_generated_at.nil? || preview_generated_at < 5.minutes.ago
|
|
end
|
|
|
|
false
|
|
end
|
|
|
|
def preview_expired?
|
|
preview_generated_at.nil? ||
|
|
preview_generated_at < PreviewService::Coordinator::PREVIEW_EXPIRY.ago
|
|
end
|
|
|
|
def preview_content_changed?
|
|
saved_changes.keys.any? { |attr| preview_relevant_attributes.include?(attr) }
|
|
end
|
|
|
|
def schedule_preview_generation
|
|
return if preview_state == 'queued' || preview_state == 'in_progress'
|
|
|
|
update_column(:preview_state, 'queued')
|
|
GeneratePartyPreviewJob.perform_later(id)
|
|
end
|
|
|
|
private
|
|
|
|
def set_shortcode
|
|
self.shortcode = random_string
|
|
end
|
|
|
|
def set_edit_key
|
|
return if user
|
|
|
|
self.edit_key = Digest::SHA1.hexdigest([Time.now, rand].join)
|
|
end
|
|
|
|
def random_string
|
|
num_chars = 6
|
|
o = [('a'..'z'), ('A'..'Z'), (0..9)].map(&:to_a).flatten
|
|
(0...num_chars).map { o[rand(o.length)] }.join
|
|
end
|
|
|
|
def skills_are_unique
|
|
skills = [skill0, skill1, skill2, skill3].compact
|
|
|
|
return if skills.uniq.length == skills.length
|
|
|
|
skills.each_with_index do |skill, index|
|
|
next if index.zero?
|
|
|
|
errors.add(:"skill#{index + 1}", 'must be unique') if skills[0...index].include?(skill)
|
|
end
|
|
|
|
errors.add(:job_skills, 'must be unique')
|
|
end
|
|
|
|
def guidebooks_are_unique
|
|
guidebooks = [guidebook1, guidebook2, guidebook3].compact
|
|
return if guidebooks.uniq.length == guidebooks.length
|
|
|
|
guidebooks.each_with_index do |book, index|
|
|
next if index.zero?
|
|
|
|
errors.add(:"guidebook#{index + 1}", 'must be unique') if guidebooks[0...index].include?(book)
|
|
end
|
|
|
|
errors.add(:guidebooks, 'must be unique')
|
|
end
|
|
|
|
def preview_relevant_attributes
|
|
%w[
|
|
name job_id element weapons_count characters_count summons_count
|
|
full_auto auto_guard charge_attack clear_time
|
|
]
|
|
end
|
|
end
|