From 4cc093fe92f600e2f85292925111133d2fba3586 Mon Sep 17 00:00:00 2001 From: Andrey Paderin Date: Fri, 27 Dec 2024 04:27:39 +0300 Subject: [PATCH] hw3 --- .../app/controllers/orders_controller.rb | 24 ++++++++++++++++- day_2/tn_rails_concurrent/app/models/order.rb | 2 +- .../20241226023934_add_index_to_orders.rb | 6 +++++ ...41226024546_add_orders_count_to_product.rb | 5 ++++ .../20241227012439_add_fn_index_to_orders.rb | 6 +++++ day_2/tn_rails_concurrent/db/structure.sql | 27 ++++++++++++++++++- 6 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 day_2/tn_rails_concurrent/db/migrate/20241226023934_add_index_to_orders.rb create mode 100644 day_2/tn_rails_concurrent/db/migrate/20241226024546_add_orders_count_to_product.rb create mode 100644 day_2/tn_rails_concurrent/db/migrate/20241227012439_add_fn_index_to_orders.rb diff --git a/day_2/tn_rails_concurrent/app/controllers/orders_controller.rb b/day_2/tn_rails_concurrent/app/controllers/orders_controller.rb index 80cd997..85ecae4 100644 --- a/day_2/tn_rails_concurrent/app/controllers/orders_controller.rb +++ b/day_2/tn_rails_concurrent/app/controllers/orders_controller.rb @@ -1,6 +1,28 @@ +# frozen_string_literal: true class OrdersController < ApplicationController def top_products_report - top_products = Order.all + top_products = Order + .with( + daily_orders: Order + .select('product_id, DATE(created_at) AS order_date, SUM(quantity) AS total_quantity') + .group('product_id, DATE(created_at)'), + ) + .from('daily_orders AS d') + .joins(<<~SQL.squish) + LEFT JOIN products AS p ON d.product_id = p.id + AND d.total_quantity = p.orders_count + SQL + .select('d.order_date, d.product_id, d.total_quantity') + .order(total_quantity: :desc ) + .limit(10) + .map do |row| + { + date: row.order_date, + product_id: row.product_id, + total_quantity: row.total_quantity, + } + end + render json: top_products end end diff --git a/day_2/tn_rails_concurrent/app/models/order.rb b/day_2/tn_rails_concurrent/app/models/order.rb index efe9965..3b2f06c 100644 --- a/day_2/tn_rails_concurrent/app/models/order.rb +++ b/day_2/tn_rails_concurrent/app/models/order.rb @@ -10,7 +10,7 @@ # updated_at :datetime not null # class Order < ApplicationRecord - belongs_to :product + belongs_to :product, counter_cache: true scope :pending, -> { where(current_status: :pending) } scope :processed, -> { where(current_status: :processed) } diff --git a/day_2/tn_rails_concurrent/db/migrate/20241226023934_add_index_to_orders.rb b/day_2/tn_rails_concurrent/db/migrate/20241226023934_add_index_to_orders.rb new file mode 100644 index 0000000..ac2f559 --- /dev/null +++ b/day_2/tn_rails_concurrent/db/migrate/20241226023934_add_index_to_orders.rb @@ -0,0 +1,6 @@ +class AddIndexToOrders < ActiveRecord::Migration[7.1] + disable_ddl_transaction! + def change + add_index :orders, :product_id, algorithm: :concurrently + end +end diff --git a/day_2/tn_rails_concurrent/db/migrate/20241226024546_add_orders_count_to_product.rb b/day_2/tn_rails_concurrent/db/migrate/20241226024546_add_orders_count_to_product.rb new file mode 100644 index 0000000..1735703 --- /dev/null +++ b/day_2/tn_rails_concurrent/db/migrate/20241226024546_add_orders_count_to_product.rb @@ -0,0 +1,5 @@ +class AddOrdersCountToProduct < ActiveRecord::Migration[7.1] + def change + add_column :products, :orders_count, :integer, default: 0, null: false + end +end diff --git a/day_2/tn_rails_concurrent/db/migrate/20241227012439_add_fn_index_to_orders.rb b/day_2/tn_rails_concurrent/db/migrate/20241227012439_add_fn_index_to_orders.rb new file mode 100644 index 0000000..dc19d00 --- /dev/null +++ b/day_2/tn_rails_concurrent/db/migrate/20241227012439_add_fn_index_to_orders.rb @@ -0,0 +1,6 @@ +class AddFnIndexToOrders < ActiveRecord::Migration[7.1] + disable_ddl_transaction! + def change + add_index :orders, 'DATE(created_at)', name: 'index_orders_on_created_at_date', algorithm: :concurrently + end +end diff --git a/day_2/tn_rails_concurrent/db/structure.sql b/day_2/tn_rails_concurrent/db/structure.sql index 3e25355..0fea3ec 100644 --- a/day_2/tn_rails_concurrent/db/structure.sql +++ b/day_2/tn_rails_concurrent/db/structure.sql @@ -9,6 +9,13 @@ SET xmloption = content; SET client_min_messages = warning; SET row_security = off; +-- +-- Name: public; Type: SCHEMA; Schema: -; Owner: - +-- + +-- *not* creating schema, since initdb creates it + + -- -- Name: order_status; Type: TYPE; Schema: public; Owner: - -- @@ -79,7 +86,8 @@ CREATE TABLE public.products ( stock integer, price numeric, created_at timestamp(6) without time zone NOT NULL, - updated_at timestamp(6) without time zone NOT NULL + updated_at timestamp(6) without time zone NOT NULL, + orders_count integer DEFAULT 0 NOT NULL ); @@ -157,6 +165,20 @@ ALTER TABLE ONLY public.schema_migrations ADD CONSTRAINT schema_migrations_pkey PRIMARY KEY (version); +-- +-- Name: index_orders_on_created_at_date; Type: INDEX; Schema: public; Owner: - +-- + +CREATE INDEX index_orders_on_created_at_date ON public.orders USING btree (date(created_at)); + + +-- +-- Name: index_orders_on_product_id; Type: INDEX; Schema: public; Owner: - +-- + +CREATE INDEX index_orders_on_product_id ON public.orders USING btree (product_id); + + -- -- PostgreSQL database dump complete -- @@ -164,6 +186,9 @@ ALTER TABLE ONLY public.schema_migrations SET search_path TO "$user", public; INSERT INTO "schema_migrations" (version) VALUES +('20241227012439'), +('20241226024546'), +('20241226023934'), ('20241216120643'), ('20241216120641');