-
Notifications
You must be signed in to change notification settings - Fork 115
Optimization #121
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
base: master
Are you sure you want to change the base?
Optimization #121
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
/tmp | ||
/log | ||
/public | ||
/.idea |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
--require spec_helper |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
3.4.1 | ||
3.3.1 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,6 @@ class TripsController < ApplicationController | |
def index | ||
@from = City.find_by_name!(params[:from]) | ||
@to = City.find_by_name!(params[:to]) | ||
@trips = Trip.where(from: @from, to: @to).order(:start_time) | ||
@trips = Trip.preload(:from, :to, bus: :services).where(from: @from, to: @to) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. а мы тут сортировку не потеряли? |
||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
class Reloader | ||
def self.call(file_name) | ||
new(file_name).call | ||
end | ||
|
||
def initialize(file_name) | ||
@file_name = file_name | ||
@json = JSON.parse(File.read(file_name)) | ||
end | ||
|
||
def call | ||
ActiveRecord::Base.transaction do | ||
clear_tables | ||
collect_and_create_data | ||
end | ||
end | ||
|
||
private | ||
|
||
attr_reader :file_name, :json | ||
|
||
def clear_tables | ||
City.delete_all | ||
Bus.delete_all | ||
Service.delete_all | ||
Trip.delete_all | ||
ActiveRecord::Base.connection.execute('DELETE FROM buses_services') | ||
end | ||
|
||
def collect_and_create_data | ||
collected_data = collect_data | ||
created_records = create_main_records(collected_data) | ||
create_associated_records(created_records) | ||
end | ||
|
||
def collect_data | ||
{ | ||
cities: Set.new, | ||
buses: [], | ||
services: Set.new | ||
}.tap do |data| | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. кстати вот в контексте профилирования и оптимизации не очень удобно работать с tap в отчётах профилировщиков это может выглядеть как - вся работа была в блоке tap (а что тормозило внутри этого блока, и какой именно это был tap в случае если их несколько - непонятно) |
||
json.each do |trip| | ||
data[:cities] << trip['from'] | ||
data[:cities] << trip['to'] | ||
data[:services].merge(trip['bus']['services']) | ||
data[:buses] << { | ||
number: trip['bus']['number'], | ||
model: trip['bus']['model'] | ||
} | ||
end | ||
end | ||
end | ||
|
||
def create_main_records(collected_data) | ||
{ | ||
cities: create_cities(collected_data[:cities]), | ||
services: create_services(collected_data[:services]), | ||
buses: create_buses(collected_data[:buses]) | ||
} | ||
end | ||
|
||
def create_cities(cities) | ||
City.insert_all( | ||
cities.map { |name| { name: name } }, | ||
returning: [:id, :name] | ||
).index_by { |city| city['name'] } | ||
end | ||
|
||
def create_services(services) | ||
Service.insert_all( | ||
services.map { |name| { name: name } }, | ||
returning: [:id, :name] | ||
).index_by { |service| service['name'] } | ||
end | ||
|
||
def create_buses(buses) | ||
Bus.insert_all( | ||
buses.uniq { |bus| bus[:number] }, | ||
returning: [:id, :number] | ||
).index_by { |bus| bus['number'] } | ||
end | ||
|
||
def create_associated_records(created_records) | ||
buses_services_data = [] | ||
trips_data = [] | ||
|
||
json.each do |trip| | ||
bus_id = created_records[:buses][trip['bus']['number']]['id'] | ||
|
||
collect_buses_services_data( | ||
buses_services_data, | ||
bus_id, | ||
trip['bus']['services'], | ||
created_records[:services] | ||
) | ||
|
||
collect_trips_data( | ||
trips_data, | ||
trip, | ||
bus_id, | ||
created_records[:cities] | ||
) | ||
end | ||
|
||
create_buses_services(buses_services_data) | ||
create_trips(trips_data) | ||
end | ||
|
||
def collect_buses_services_data(data, bus_id, services, services_records) | ||
services.each do |service_name| | ||
data << { | ||
bus_id: bus_id, | ||
service_id: services_records[service_name]['id'] | ||
} | ||
end | ||
end | ||
|
||
def collect_trips_data(data, trip, bus_id, cities) | ||
data << { | ||
from_id: cities[trip['from']]['id'], | ||
to_id: cities[trip['to']]['id'], | ||
bus_id: bus_id, | ||
start_time: trip['start_time'], | ||
duration_minutes: trip['duration_minutes'], | ||
price_cents: trip['price_cents'] | ||
} | ||
end | ||
|
||
def create_buses_services(data) | ||
return if data.empty? | ||
|
||
values = data.uniq.map { |r| "(#{r[:bus_id]}, #{r[:service_id]})" }.join(', ') | ||
ActiveRecord::Base.connection.execute( | ||
"INSERT INTO buses_services (bus_id, service_id) VALUES #{values}" | ||
) | ||
end | ||
|
||
def create_trips(data) | ||
Trip.insert_all!(data) | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,11 @@ | ||
<li><%= "Отправление: #{trip.start_time}" %></li> | ||
<li><%= "Прибытие: #{(Time.parse(trip.start_time) + trip.duration_minutes.minutes).strftime('%H:%M')}" %></li> | ||
<li><%= "В пути: #{trip.duration_minutes / 60}ч. #{trip.duration_minutes % 60}мин." %></li> | ||
<li><%= "Цена: #{trip.price_cents / 100}р. #{trip.price_cents % 100}коп." %></li> | ||
<li><%= "Автобус: #{trip.bus.model} №#{trip.bus.number}" %></li> | ||
<ul class="trip-details" data-bus-id="<%= trip.bus.id %>"> | ||
<li><%= "Отправление: #{trip.start_time}" %></li> | ||
<li><%= "Прибытие: #{(Time.parse(trip.start_time) + trip.duration_minutes.minutes).strftime('%H:%M')}" %></li> | ||
<li><%= "В пути: #{trip.duration_minutes / 60}ч. #{trip.duration_minutes % 60}мин." %></li> | ||
<li><%= "Цена: #{trip.price_cents / 100}р. #{trip.price_cents % 100}коп." %></li> | ||
<li><%= "Автобус: #{trip.bus.model} №#{trip.bus.number}" %></li> | ||
|
||
<% if trip.bus.services.present? %> | ||
<%= render "services", services: trip.bus.services %> | ||
<% end %> | ||
</ul> |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,12 +5,6 @@ | |
<%= "В расписании #{@trips.count} рейсов" %> | ||
</h2> | ||
|
||
<% @trips.each do |trip| %> | ||
<ul> | ||
<%= render "trip", trip: trip %> | ||
<% if trip.bus.services.present? %> | ||
<%= render "services", services: trip.bus.services %> | ||
<% end %> | ||
</ul> | ||
<%= render "delimiter" %> | ||
<% end %> | ||
<div class="trips-container"> | ||
<%= render partial: "trip", collection: @trips, spacer_template: "delimiter" %> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
</div> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍