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
5 changes: 4 additions & 1 deletion day_2/tn_rails_concurrent/Gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
source "https://rubygems.org"

ruby "3.2.2"
# ruby "3.4.0.preview1"
ruby "3.2.6"

gem "rails", "~> 7.1.4"
gem "sprockets-rails"
Expand All @@ -15,6 +16,8 @@ gem "bootsnap", require: false

group :development, :test do
gem "debug", platforms: %i[ mri windows ]
gem 'pry'
gem 'pry-rails'
end

group :development do
Expand Down
11 changes: 10 additions & 1 deletion day_2/tn_rails_concurrent/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ GEM
bootsnap (1.18.4)
msgpack (~> 1.2)
builder (3.3.0)
coderay (1.1.3)
concurrent-ruby (1.3.4)
connection_pool (2.4.1)
crass (1.0.6)
Expand Down Expand Up @@ -121,6 +122,7 @@ GEM
net-pop
net-smtp
marcel (1.0.4)
method_source (1.1.0)
mini_mime (1.1.5)
mini_portile2 (2.8.8)
minitest (5.25.4)
Expand All @@ -145,6 +147,11 @@ GEM
racc (~> 1.4)
parallel (1.26.3)
pg (1.5.8)
pry (0.15.2)
coderay (~> 1.1)
method_source (~> 1.0)
pry-rails (0.3.11)
pry (>= 0.13.0)
psych (5.2.1)
date
stringio
Expand Down Expand Up @@ -240,6 +247,8 @@ DEPENDENCIES
importmap-rails
parallel (~> 1.26.3)
pg
pry
pry-rails
puma (>= 5.0)
rails (~> 7.1.4)
sidekiq (~> 7.3.6)
Expand All @@ -250,7 +259,7 @@ DEPENDENCIES
web-console

RUBY VERSION
ruby 3.2.2p53
ruby 3.2.6p234

BUNDLED WITH
2.4.22
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
4 changes: 4 additions & 0 deletions day_2/tn_rails_concurrent/config/database.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
default: &default
adapter: postgresql
encoding: unicode
# host: localhost
# port: 5431
# username: postgres
# password: password
wait_timeout: 900 # 15 minutes.
timeout: 10000 # 5 seconds.
pool: <%= ENV.fetch("RAILS_MAX_THREADS", 5).to_i + 10 + ENV.fetch("SIDEKIQ_CONCURENNCY", 2).to_i %>
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
10 changes: 5 additions & 5 deletions day_2/tn_rails_concurrent/db/seeds.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Order.destroy_all
puts "Orders destroyed"
Product.destroy_all
puts "Products destroyed"
# Order.destroy_all
# puts "Orders destroyed"
# Product.destroy_all
# puts "Products destroyed"

Product.insert_all(Array.new(100) { { name: "Product #{rand(1..1000)}", stock: rand(1..50), price: rand(10..100) } })
puts "Products created: #{Product.count}"
Expand All @@ -10,7 +10,7 @@
statuses = ["pending", "processed", "cancelled"]
dates = (1..30).map { |n| n.days.ago }
ActiveRecord::Base.record_timestamps = false
Order.insert_all(Array.new(20_000) {
Order.insert_all(Array.new(200_000) {
{
product_id: product_ids.sample, quantity: rand(1..10),
current_status: statuses.sample,
Expand Down
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');

2 changes: 2 additions & 0 deletions day_2/tn_rails_concurrent/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ services:
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: password
ports:
- 5432:5432
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ namespace :orders do
end

def send_to_external_service
uri = URI.parse("https://yandex.ru")
uri = URI.parse("https://yandex.ru/")
sleep 0.1
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true if uri.scheme == 'https'
Expand Down