From 06a861c639f394c3e53a701f4f58b7cf3122f5cd Mon Sep 17 00:00:00 2001 From: songtianlun Date: Thu, 23 Jan 2025 23:59:48 +0800 Subject: [PATCH] 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. --- app/models/city.rb | 14 ++++++++++++++ app/workers/batch_generate_weather_arts_worker.rb | 8 +------- ...3155234_remove_last_fetch_fields_from_cities.rb | 8 ++++++++ db/schema.rb | 5 ++--- 4 files changed, 25 insertions(+), 10 deletions(-) create mode 100644 db/migrate/20250123155234_remove_last_fetch_fields_from_cities.rb diff --git a/app/models/city.rb b/app/models/city.rb index 23862ea..aa1d920 100644 --- a/app/models/city.rb +++ b/app/models/city.rb @@ -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" ] 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 weather_arts.order(weather_date: :desc).first end diff --git a/app/workers/batch_generate_weather_arts_worker.rb b/app/workers/batch_generate_weather_arts_worker.rb index f89f1bd..95948d2 100644 --- a/app/workers/batch_generate_weather_arts_worker.rb +++ b/app/workers/batch_generate_weather_arts_worker.rb @@ -12,7 +12,7 @@ class BatchGenerateWeatherArtsWorker # GenerateWeatherArtJob.perform_now(city) GenerateWeatherArtWorker.perform_async(city.id) - sleep 10.seconds + sleep 3.seconds end end @@ -25,10 +25,4 @@ class BatchGenerateWeatherArtsWorker # .select { |city| early_morning_in_timezone?(city.timezone) } end - # def early_morning_in_timezone?(timezone) - # return false if timezone.blank? - - # time = Time.current.in_time_zone(timezone) - # time.hour == 2 - # end end diff --git a/db/migrate/20250123155234_remove_last_fetch_fields_from_cities.rb b/db/migrate/20250123155234_remove_last_fetch_fields_from_cities.rb new file mode 100644 index 0000000..b06d7e3 --- /dev/null +++ b/db/migrate/20250123155234_remove_last_fetch_fields_from_cities.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index b1c482f..9a3025e 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # 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| t.string "namespace" t.text "body" @@ -72,8 +72,6 @@ ActiveRecord::Schema[8.0].define(version: 2025_01_22_053220) do t.boolean "active" t.integer "priority" t.string "timezone" - t.datetime "last_weather_fetch" - t.datetime "last_image_generation" t.datetime "created_at", null: false t.datetime "updated_at", null: false 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 "updated_at", null: false 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 ["slug"], name: "index_weather_arts_on_slug", unique: true end