Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion day_2/tn_rails_concurrent/app/controllers/orders_controller.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion day_2/tn_rails_concurrent/app/models/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddIndexToOrders < ActiveRecord::Migration[7.1]
disable_ddl_transaction!
def change
add_index :orders, :product_id, algorithm: :concurrently
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddOrdersCountToProduct < ActiveRecord::Migration[7.1]
def change
add_column :products, :orders_count, :integer, default: 0, null: false
end
end
Original file line number Diff line number Diff line change
@@ -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
27 changes: 26 additions & 1 deletion day_2/tn_rails_concurrent/db/structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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: -
--
Expand Down Expand Up @@ -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
);


Expand Down Expand Up @@ -157,13 +165,30 @@ 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
--

SET search_path TO "$user", public;

INSERT INTO "schema_migrations" (version) VALUES
('20241227012439'),
('20241226024546'),
('20241226023934'),
('20241216120643'),
('20241216120641');