Simplify homepage modeling code a bit

I have some other changes planned too, but these are some easy ones. I
also turn back on this stuff in development, in hopes that my changes
can make these queries fast enough to not be a big deal anymore!
This commit is contained in:
Emi Matchu 2024-10-02 17:21:37 -07:00
parent 26add4577c
commit 7ba68c52d4
2 changed files with 27 additions and 52 deletions

View file

@ -51,25 +51,23 @@ class OutfitsController < ApplicationController
@species = Species.alphabetical
# HACK: Skip this in development, because it's slow!
unless Rails.env.development?
newest_items = Item.newest.
select(:id, :name, :updated_at, :thumbnail_url, :rarity_index, :is_manually_nc)
.limit(18)
@newest_modeled_items, @newest_unmodeled_items =
newest_items.partition(&:predicted_fully_modeled?)
newest_items = Item.newest.
select(:id, :name, :updated_at, :thumbnail_url, :rarity_index, :is_manually_nc)
.limit(18)
@newest_modeled_items, @newest_unmodeled_items =
newest_items.partition(&:predicted_fully_modeled?)
@newest_unmodeled_items_predicted_missing_species_by_color = {}
@newest_unmodeled_items_predicted_modeled_ratio = {}
@newest_unmodeled_items.each do |item|
h = item.predicted_missing_nonstandard_body_ids_by_species_by_color
standard_body_ids_by_species = item.
predicted_missing_standard_body_ids_by_species
if standard_body_ids_by_species.present?
h[:standard] = standard_body_ids_by_species
end
@newest_unmodeled_items_predicted_missing_species_by_color[item] = h
@newest_unmodeled_items_predicted_modeled_ratio[item] = item.predicted_modeled_ratio
@newest_unmodeled_items_predicted_missing_species_by_color = {}
@newest_unmodeled_items_predicted_modeled_ratio = {}
@newest_unmodeled_items.each do |item|
h = item.predicted_missing_nonstandard_body_ids_by_species_by_color
standard_body_ids_by_species = item.
predicted_missing_standard_body_ids_by_species
if standard_body_ids_by_species.present?
h[:standard] = standard_body_ids_by_species
end
@newest_unmodeled_items_predicted_missing_species_by_color[item] = h
@newest_unmodeled_items_predicted_modeled_ratio[item] = item.predicted_modeled_ratio
end
@species_count = Species.count

View file

@ -273,58 +273,35 @@ class Item < ApplicationRecord
write_attribute('species_support_ids', replacement)
end
def support_species?(species)
species_support_ids.blank? || species_support_ids.include?(species.id)
end
def modeled_body_ids
@modeled_body_ids ||= swf_assets.select('DISTINCT body_id').map(&:body_id)
end
def modeled_color_ids
# Might be empty if modeled_body_ids is 0. But it's currently not called
# in that scenario, so, whatever.
@modeled_color_ids ||= PetType.select('DISTINCT color_id').
where(body_id: modeled_body_ids).
map(&:color_id)
end
def basic_body_ids
@basic_body_ids ||= begin
basic_color_ids ||= Color.select([:id]).basic.map(&:id)
PetType.select('DISTINCT body_id').
where(color_id: basic_color_ids).map(&:body_id)
end
end
def predicted_body_ids
@predicted_body_ids ||= if modeled_body_ids.include?(0)
@predicted_body_ids ||= if compatible_body_ids.include?(0)
# Oh, look, it's already known to fit everybody! Sweet. We're done. (This
# isn't folded into the case below, in case this item somehow got a
# body-specific and non-body-specific asset. In all the cases I've seen
# it, that indicates a glitched item, but this method chooses to reflect
# behavior elsewhere in the app by saying that we can put this item on
# anybody. (Heh. Any body.))
modeled_body_ids
elsif modeled_body_ids.size == 1
compatible_body_ids
elsif compatible_body_ids.size == 1
# This might just be a species-specific item. Let's be conservative in
# our prediction, though we'll revise it if we see another body ID.
modeled_body_ids
compatible_body_ids
else
# If an item is worn by more than one body, then it must be wearable by
# all bodies of the same color. (To my knowledge, anyway. I'm not aware
# of any exceptions.) So, let's find those bodies by first finding those
# colors.
basic_modeled_body_ids, nonbasic_modeled_body_ids = modeled_body_ids.
partition { |bi| basic_body_ids.include?(bi) }
basic_body_ids = PetType.basic.distinct.pluck(:body_id)
basic_compatible_body_ids, nonbasic_compatible_body_ids =
compatible_body_ids.partition { |bi| basic_body_ids.include?(bi) }
output = []
if basic_modeled_body_ids.present?
if basic_compatible_body_ids.present?
output += basic_body_ids
end
if nonbasic_modeled_body_ids.present?
if nonbasic_compatible_body_ids.present?
nonbasic_modeled_color_ids = PetType.select('DISTINCT color_id').
where(body_id: nonbasic_modeled_body_ids).
where(body_id: nonbasic_compatible_body_ids).
map(&:color_id)
output += PetType.select('DISTINCT body_id').
where(color_id: nonbasic_modeled_color_ids).
@ -335,7 +312,7 @@ class Item < ApplicationRecord
end
def predicted_missing_body_ids
@predicted_missing_body_ids ||= predicted_body_ids - modeled_body_ids
@predicted_missing_body_ids ||= predicted_body_ids - compatible_body_ids
end
def predicted_missing_standard_body_ids_by_species_id
@ -387,7 +364,7 @@ class Item < ApplicationRecord
end
def predicted_modeled_ratio
modeled_body_ids.size.to_f / predicted_body_ids.size
compatible_body_ids.size.to_f / predicted_body_ids.size
end
def as_json(options={})