refactor: clean up city model and adjust worker timing
- Implement caching methods for last weather fetch and image generation - Adjust sleep duration in BatchGenerateWeatherArtsWorker from 10 seconds to 3 seconds - Remove unused fields `last_weather_fetch` and `last_image_generation` from the cities table - Add index on the weather_arts table for optimized querying This refactor improves data retrieval performance for weather data associated with cities. Caching reduces database load while the worker modification allows for faster iterations in generating weather arts without significantly impacting performance.
This commit is contained in:
parent
2cd23a6047
commit
06a861c639
@ -48,6 +48,20 @@ class City < ApplicationRecord
|
|||||||
[ "active", "country", "created_at", "id", "id_value", "last_image_generation", "last_weather_fetch", "latitude", "longitude", "name", "priority", "region", "slug", "timezone", "updated_at" ]
|
[ "active", "country", "created_at", "id", "id_value", "last_image_generation", "last_weather_fetch", "latitude", "longitude", "name", "priority", "region", "slug", "timezone", "updated_at" ]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def last_weather_fetch
|
||||||
|
# latest_weather_art&.created_at
|
||||||
|
Rails.cache.fetch("city/#{id}/last_weather_fetch", expires_in: 1.hour) do
|
||||||
|
latest_weather_art&.created_at
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def last_image_generation
|
||||||
|
# latest_weather_art&.image&.created_at
|
||||||
|
Rails.cache.fetch("city/#{id}/last_image_generation", expires_in: 1.hour) do
|
||||||
|
latest_weather_art&.image&.created_at
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def latest_weather_art
|
def latest_weather_art
|
||||||
weather_arts.order(weather_date: :desc).first
|
weather_arts.order(weather_date: :desc).first
|
||||||
end
|
end
|
||||||
|
@ -12,7 +12,7 @@ class BatchGenerateWeatherArtsWorker
|
|||||||
|
|
||||||
# GenerateWeatherArtJob.perform_now(city)
|
# GenerateWeatherArtJob.perform_now(city)
|
||||||
GenerateWeatherArtWorker.perform_async(city.id)
|
GenerateWeatherArtWorker.perform_async(city.id)
|
||||||
sleep 10.seconds
|
sleep 3.seconds
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -25,10 +25,4 @@ class BatchGenerateWeatherArtsWorker
|
|||||||
# .select { |city| early_morning_in_timezone?(city.timezone) }
|
# .select { |city| early_morning_in_timezone?(city.timezone) }
|
||||||
end
|
end
|
||||||
|
|
||||||
# def early_morning_in_timezone?(timezone)
|
|
||||||
# return false if timezone.blank?
|
|
||||||
|
|
||||||
# time = Time.current.in_time_zone(timezone)
|
|
||||||
# time.hour == 2
|
|
||||||
# end
|
|
||||||
end
|
end
|
||||||
|
@ -0,0 +1,8 @@
|
|||||||
|
class RemoveLastFetchFieldsFromCities < ActiveRecord::Migration[8.0]
|
||||||
|
def change
|
||||||
|
remove_column :cities, :last_weather_fetch
|
||||||
|
remove_column :cities, :last_image_generation
|
||||||
|
|
||||||
|
add_index :weather_arts, [:city_id, :weather_date]
|
||||||
|
end
|
||||||
|
end
|
5
db/schema.rb
generated
5
db/schema.rb
generated
@ -10,7 +10,7 @@
|
|||||||
#
|
#
|
||||||
# It's strongly recommended that you check this file into your version control system.
|
# It's strongly recommended that you check this file into your version control system.
|
||||||
|
|
||||||
ActiveRecord::Schema[8.0].define(version: 2025_01_22_053220) do
|
ActiveRecord::Schema[8.0].define(version: 2025_01_23_155234) do
|
||||||
create_table "active_admin_comments", force: :cascade do |t|
|
create_table "active_admin_comments", force: :cascade do |t|
|
||||||
t.string "namespace"
|
t.string "namespace"
|
||||||
t.text "body"
|
t.text "body"
|
||||||
@ -72,8 +72,6 @@ ActiveRecord::Schema[8.0].define(version: 2025_01_22_053220) do
|
|||||||
t.boolean "active"
|
t.boolean "active"
|
||||||
t.integer "priority"
|
t.integer "priority"
|
||||||
t.string "timezone"
|
t.string "timezone"
|
||||||
t.datetime "last_weather_fetch"
|
|
||||||
t.datetime "last_image_generation"
|
|
||||||
t.datetime "created_at", null: false
|
t.datetime "created_at", null: false
|
||||||
t.datetime "updated_at", null: false
|
t.datetime "updated_at", null: false
|
||||||
t.string "slug"
|
t.string "slug"
|
||||||
@ -132,6 +130,7 @@ ActiveRecord::Schema[8.0].define(version: 2025_01_22_053220) do
|
|||||||
t.datetime "created_at", null: false
|
t.datetime "created_at", null: false
|
||||||
t.datetime "updated_at", null: false
|
t.datetime "updated_at", null: false
|
||||||
t.string "slug"
|
t.string "slug"
|
||||||
|
t.index ["city_id", "weather_date"], name: "index_weather_arts_on_city_id_and_weather_date"
|
||||||
t.index ["city_id"], name: "index_weather_arts_on_city_id"
|
t.index ["city_id"], name: "index_weather_arts_on_city_id"
|
||||||
t.index ["slug"], name: "index_weather_arts_on_slug", unique: true
|
t.index ["slug"], name: "index_weather_arts_on_slug", unique: true
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user