Skip to content

Task 3 #119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/tmp
/log
/public
/profile
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--require spec_helper
15 changes: 15 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,21 @@ gem 'puma'
gem 'listen'
gem 'bootsnap'
gem 'rack-mini-profiler'
gem 'rubocop', require: false
gem 'pghero'
gem 'benchmark'
gem 'ruby-prof'
gem 'stackprof'
gem 'oj'
gem 'strong_migrations'

group :development, :test do
gem 'rspec-rails', '~> 7.1.1'
gem 'factory_bot_rails', '~> 6.4.4'
gem 'rails-controller-testing'
gem 'rspec-rake'
gem 'rspec-benchmark'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
92 changes: 92 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,12 @@ GEM
securerandom (>= 0.3)
tzinfo (~> 2.0, >= 2.0.5)
uri (>= 0.13.1)
ast (2.4.2)
base64 (0.2.0)
benchmark (0.4.0)
benchmark-malloc (0.2.0)
benchmark-perf (0.6.0)
benchmark-trend (0.4.0)
bigdecimal (3.1.9)
bootsnap (1.18.4)
msgpack (~> 1.2)
Expand All @@ -82,8 +86,14 @@ GEM
connection_pool (2.5.0)
crass (1.0.6)
date (3.4.1)
diff-lcs (1.6.0)
drb (2.2.1)
erubi (1.13.1)
factory_bot (6.5.1)
activesupport (>= 6.1.0)
factory_bot_rails (6.4.4)
factory_bot (~> 6.5)
railties (>= 5.0.0)
ffi (1.17.1-arm64-darwin)
globalid (1.2.1)
activesupport (>= 6.1)
Expand All @@ -94,6 +104,9 @@ GEM
pp (>= 0.6.0)
rdoc (>= 4.0.0)
reline (>= 0.4.2)
json (2.10.1)
language_server-protocol (3.17.0.4)
lint_roller (1.1.0)
listen (3.9.0)
rb-fsevent (~> 0.10, >= 0.10.3)
rb-inotify (~> 0.9, >= 0.9.10)
Expand Down Expand Up @@ -122,7 +135,17 @@ GEM
nio4r (2.7.4)
nokogiri (1.18.2-arm64-darwin)
racc (~> 1.4)
oj (3.16.9)
bigdecimal (>= 3.0)
ostruct (>= 0.2)
ostruct (0.6.1)
parallel (1.26.3)
parser (3.3.7.1)
ast (~> 2.4.1)
racc
pg (1.5.9)
pghero (3.6.1)
activerecord (>= 6.1)
pp (0.6.2)
prettyprint
prettyprint (0.2.0)
Expand Down Expand Up @@ -156,6 +179,10 @@ GEM
activesupport (= 8.0.1)
bundler (>= 1.15.0)
railties (= 8.0.1)
rails-controller-testing (1.0.5)
actionpack (>= 5.0.1.rc1)
actionview (>= 5.0.1.rc1)
activesupport (>= 5.0.1.rc1)
rails-dom-testing (2.2.0)
activesupport (>= 5.0.0)
minitest
Expand All @@ -171,20 +198,72 @@ GEM
rake (>= 12.2)
thor (~> 1.0, >= 1.2.2)
zeitwerk (~> 2.6)
rainbow (3.1.1)
rake (13.2.1)
rb-fsevent (0.11.2)
rb-inotify (0.11.1)
ffi (~> 1.0)
rdoc (6.12.0)
psych (>= 4.0.0)
regexp_parser (2.10.0)
reline (0.6.0)
io-console (~> 0.5)
rspec (3.13.0)
rspec-core (~> 3.13.0)
rspec-expectations (~> 3.13.0)
rspec-mocks (~> 3.13.0)
rspec-benchmark (0.6.0)
benchmark-malloc (~> 0.2)
benchmark-perf (~> 0.6)
benchmark-trend (~> 0.4)
rspec (>= 3.0)
rspec-core (3.13.3)
rspec-support (~> 3.13.0)
rspec-expectations (3.13.3)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
rspec-mocks (3.13.2)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
rspec-rails (7.1.1)
actionpack (>= 7.0)
activesupport (>= 7.0)
railties (>= 7.0)
rspec-core (~> 3.13)
rspec-expectations (~> 3.13)
rspec-mocks (~> 3.13)
rspec-support (~> 3.13)
rspec-rake (0.0.4)
rake
rspec-core
rspec-support (3.13.2)
rubocop (1.72.1)
json (~> 2.3)
language_server-protocol (~> 3.17.0.2)
lint_roller (~> 1.1.0)
parallel (~> 1.10)
parser (>= 3.3.0.2)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 2.9.3, < 3.0)
rubocop-ast (>= 1.38.0, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 2.4.0, < 4.0)
rubocop-ast (1.38.0)
parser (>= 3.3.1.0)
ruby-prof (1.7.1)
ruby-progressbar (1.13.0)
securerandom (0.4.1)
stackprof (0.2.27)
stringio (3.1.2)
strong_migrations (2.2.0)
activerecord (>= 7)
thor (1.3.2)
timeout (0.4.3)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
unicode-display_width (3.1.4)
unicode-emoji (~> 4.0, >= 4.0.4)
unicode-emoji (4.0.4)
uri (1.0.2)
useragent (0.16.11)
websocket-driver (0.7.7)
Expand All @@ -194,15 +273,28 @@ GEM
zeitwerk (2.7.1)

PLATFORMS
arm64-darwin-21
arm64-darwin-24

DEPENDENCIES
benchmark
bootsnap
factory_bot_rails (~> 6.4.4)
listen
oj
pg
pghero
puma
rack-mini-profiler
rails (~> 8.0.1)
rails-controller-testing
rspec-benchmark
rspec-rails (~> 7.1.1)
rspec-rake
rubocop
ruby-prof
stackprof
strong_migrations
tzinfo-data

RUBY VERSION
Expand Down
18 changes: 17 additions & 1 deletion app/controllers/trips_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
class TripsController < ApplicationController
before_action :set_pagination_params, :set_cities, only: :index

def index
@total_count = Trip.where(from: @from, to: @to).count
@trips = Trip.where(from: @from, to: @to)
.order(:start_time)
.limit(@per).offset(@per * (@page - 1))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

пагинация limit offset в целом имеет для меня плохую репутацию (вроде бы про это что-то было в лекциях)

Trip.where(from: @from, to: @to).count при большом кол-ве трипов может начать жёстко тормозить, что негативно скажется на времени загрузки страницы (у нас тут их так много нет, но сам паттерн такой пагинации потенциально проблемный)

.preload(bus: [:services])
end

private

def set_pagination_params
@page = params[:page].present? ? params[:page].to_i : 1
@per = params[:per].present? ? params[:per].to_i : 100
end

def set_cities
@from = City.find_by_name!(params[:from])
@to = City.find_by_name!(params[:to])
@trips = Trip.where(from: @from, to: @to).order(:start_time)
end
end
3 changes: 2 additions & 1 deletion app/models/bus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ class Bus < ApplicationRecord
].freeze

has_many :trips
has_and_belongs_to_many :services, join_table: :buses_services
has_many :buses_services
has_many :services, through: :buses_services

validates :number, presence: true, uniqueness: true
validates :model, inclusion: { in: MODELS }
Expand Down
4 changes: 4 additions & 0 deletions app/models/buses_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class BusesService < ApplicationRecord
belongs_to :bus
belongs_to :service
end
3 changes: 2 additions & 1 deletion app/models/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ class Service < ApplicationRecord
'Можно не печатать билет',
].freeze

has_and_belongs_to_many :buses, join_table: :buses_services
has_many :buses_services
has_many :buses, through: :buses_services

validates :name, presence: true
validates :name, inclusion: { in: SERVICES }
Expand Down
105 changes: 105 additions & 0 deletions app/services/data_loader.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# frozen_string_literal: true

class DataLoader
TRIPS_COMMAND =
"copy trips (from_id, to_id, start_time, duration_minutes, price_cents, bus_id) from stdin with csv delimiter ';'"
CITIES_COMMAND = "copy cities (id, name) from stdin with csv delimiter ';'"
BUSES_COMMAND = "copy buses (id, number, model) from stdin with csv delimiter ';'"
SERVICES_COMMAND = "copy services (id, name) from stdin with csv delimiter ';'"
BUSES_SERVICES_COMMAND = "copy buses_services (bus_id, service_id) from stdin with csv delimiter ';'"

def self.load(filename)
new(filename).load
end

def initialize(filename)
@stream = JsonStreamer.stream(filename)
@cities = {}
@services = {}
@buses = {}
@buses_services = []
end

def load
ActiveRecord::Base.transaction do
clean_database
connection = ActiveRecord::Base.connection.raw_connection

ActiveRecord::Base.connection.raw_connection.copy_data TRIPS_COMMAND do
stream.each do |trip|
from = city(trip['from'])
to = city(trip['to'])

if buses[trip['bus']['number']].nil?
bus_services = []
trip['bus']['services'].each do |name|
bus_services << service(name)
end

create_bus(trip['bus']['number'], trip['bus']['model'])
create_bus_services(buses[trip['bus']['number']], bus_services)
end

bus = buses[trip['bus']['number']]

connection.put_copy_data("#{from[:id]};#{to[:id]};#{trip['start_time']};#{trip['duration_minutes']};#{trip['price_cents']};#{bus[:id]}\n")
end
end

ActiveRecord::Base.connection.raw_connection.copy_data CITIES_COMMAND do
cities.each do |name, attrs|
connection.put_copy_data("#{attrs[:id]};#{name}\n")
end
end

ActiveRecord::Base.connection.raw_connection.copy_data BUSES_COMMAND do
buses.each do |number, attrs|
connection.put_copy_data("#{attrs[:id]};#{number};#{attrs[:model]}\n")
end
end

ActiveRecord::Base.connection.raw_connection.copy_data SERVICES_COMMAND do
services.each do |name, attrs|
connection.put_copy_data("#{attrs[:id]};#{name}\n")
end
end

ActiveRecord::Base.connection.raw_connection.copy_data BUSES_SERVICES_COMMAND do
buses_services.each do |bus_id, service_id|
connection.put_copy_data("#{bus_id};#{service_id}\n")
end
end
end
end

private

attr_reader :stream
attr_accessor :cities, :services, :buses, :buses_services

def clean_database
City.delete_all
Bus.delete_all
Service.delete_all
Trip.delete_all
ActiveRecord::Base.connection.execute('delete from buses_services;')
end

def city(name)
cities[name] ||= { id: cities.size + 1 }
end

def service(name)
services[name] ||= { id: services.size + 1 }
end

def create_bus(number, model)
buses[number] = { id: buses.size + 1, model: }
end

def create_bus_services(bus, services)
services.map do |service|
@buses_services << [bus[:id], service[:id]]
end
end
end
44 changes: 44 additions & 0 deletions app/services/json_streamer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# frozen_string_literal: true

class JsonStreamer
def self.stream(filename)
new(filename).stream
end

def initialize(filename)
@file = File.open(filename, 'r:UTF-8')
end

def stream
Enumerator.new do |yielder|
loop do
yielder << Oj.load(object)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 ого, найс

rescue Oj::ParseError, TypeError => _e
break
end
end
end

private

attr_reader :file

def object
nesting = 0
str = +''

while nesting > 0 || str.empty?
ch = file.getc

return if file.eof?

nesting += 1 if ch == '{'
str << ch if nesting >= 1
nesting -= 1 if ch == '}'

str
end

str
end
end
Loading