fix: correct logic for weather arts selection

- Update query to select weather arts with last generation time
  greater than the cutoff time.
- Adjust logging for clarity, including brackets around limits
  and counts for better readability.
- Change from appending to selected cities to concatenating selected
  cities, ensuring the full list is preserved.

This commit resolves an issue where the logic for filtering
recent weather arts was incorrect, potentially leading to missing
relevant entries. Additionally, it improves log output formatting
for better debugging.
This commit is contained in:
songtianlun 2025-02-17 13:29:31 +08:00
parent 4ea7f6c03c
commit df86a10f03

View File

@ -47,7 +47,7 @@ class BatchGenerateWeatherArtsWorker
FROM weather_arts
GROUP BY city_id
) latest_arts ON cities.id = latest_arts.city_id")
.where("latest_arts.last_generation_time < ?", cutoff_time)
.where("latest_arts.last_generation_time > ?", cutoff_time)
end
def calculate_remaining_slots
@ -69,8 +69,8 @@ class BatchGenerateWeatherArtsWorker
end
def select_cities(recent_city, countries, limit)
Rails.logger.debug "Select Cities with limit: #{limit}, in #{countries.size} countries."
Rails.logger.debug "Skip Cities(count: #{recent_city.count}) list: #{recent_city.map(&:name).join(', ')}."
Rails.logger.debug "Select Cities with limit: [#{limit}], in [#{countries.size}] countries."
Rails.logger.debug "Skip Cities(count: [#{recent_city.count}]) list: [#{recent_city.map(&:name).join(', ')}]."
# 第一阶段:筛选 active 城市, 排除最近生成过的城市
active_cities = City.where.not(id: recent_city.pluck(:id))
.where(active: true, country_id: countries.map(&:id)).limit(limit).to_a
@ -97,12 +97,12 @@ class BatchGenerateWeatherArtsWorker
.first(remaining_every_country_count)
if c.any? # 检查是否有有效的城市
Rails.logger.debug "== Selected city [#{c.first.name}] in country: [#{country.name}]"
selected << c
selected += c
else
Rails.logger.debug "== No valid cities found in country: [#{country.name}]"
end
end
Rails.logger.debug "==recent selected city list: #{selected.map(&:name).join(', ')}."
Rails.logger.debug "==recent selected city list: #{selected.map(&:name).join(', ')}." if selected.any?
Rails.logger.debug "Finished selected #{selected.size} cities."
selected
end