-
Notifications
You must be signed in to change notification settings - Fork 12
Task 4 homework #8
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?
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 |
|---|---|---|
|
|
@@ -25,3 +25,4 @@ | |
|
|
||
| # Ignore master key for decrypting credentials and more. | ||
| /config/master.key | ||
| /config/skylight.yml | ||
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| class AddTripIndices < ActiveRecord::Migration[5.2] | ||
| disable_ddl_transaction! | ||
|
|
||
| def change | ||
| add_index :trips, [:from_id, :to_id], algorithm: :concurrently | ||
|
Owner
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,7 @@ | ||
| class AddBusIndex < ActiveRecord::Migration[5.2] | ||
| disable_ddl_transaction! | ||
|
|
||
| def change | ||
| add_index :buses, :number, algorithm: :concurrently | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| class AddBusServiceIndices < ActiveRecord::Migration[5.2] | ||
| disable_ddl_transaction! | ||
|
|
||
| def change | ||
| add_index :buses_services, [:bus_id, :service_id], algorithm: :concurrently | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,34 +1,77 @@ | ||
| # Наивная загрузка данных из json-файла в БД | ||
| # rake reload_json[fixtures/small.json] | ||
| require 'benchmark' | ||
|
|
||
| def create_services | ||
| new_services = [] | ||
| all_services = Service.const_get('SERVICES').each_with_object({}) do |name, result| | ||
|
Owner
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.
|
||
| new_services << result[name] = Service.new(name: name) | ||
| end | ||
| Service.import(new_services) | ||
| all_services | ||
| end | ||
|
|
||
| def create_cities(data) | ||
| uniq_cities = data.each_with_object([]) do |trip, result| | ||
| result << trip['from'] | ||
| result << trip['to'] | ||
| end.uniq | ||
| new_cities = [] | ||
| all_cities = uniq_cities.each_with_object({}) do |name, result| | ||
| new_cities << result[name] = City.new(name: name) | ||
| end | ||
| City.import(new_cities) | ||
| all_cities | ||
| end | ||
|
|
||
| def create_buses(data, all_services) | ||
| new_buses = [] | ||
| all_buses = data.each_with_object({}) do |trip, result| | ||
| next if result[trip['bus']['number']] | ||
|
|
||
| services = trip['bus']['services'].map {|service| all_services[service] } | ||
| new_buses << result[trip['bus']['number']] = Bus.new( | ||
| number: trip['bus']['number'], | ||
| model: trip['bus']['model'], | ||
| services: services | ||
| ) | ||
| end | ||
| #Bus.import(new_buses) | ||
| new_buses.each(&:save!) | ||
| all_buses | ||
| end | ||
|
|
||
| task :reload_json, [:file_name] => :environment do |_task, args| | ||
| json = JSON.parse(File.read(args.file_name)) | ||
|
|
||
| ActiveRecord::Base.transaction do | ||
| City.delete_all | ||
| Bus.delete_all | ||
| Service.delete_all | ||
| Trip.delete_all | ||
| ActiveRecord::Base.connection.execute('delete from buses_services;') | ||
|
|
||
| json.each do |trip| | ||
| from = City.find_or_create_by(name: trip['from']) | ||
| to = City.find_or_create_by(name: trip['to']) | ||
| services = [] | ||
| trip['bus']['services'].each do |service| | ||
| s = Service.find_or_create_by(name: service) | ||
| services << s | ||
| time = Benchmark.realtime do | ||
| json = JSON.parse(File.read(args.file_name)) | ||
|
|
||
| ActiveRecord::Base.transaction do | ||
| City.delete_all | ||
| Bus.delete_all | ||
| Service.delete_all | ||
| Trip.delete_all | ||
| ActiveRecord::Base.connection.execute('delete from buses_services;') | ||
|
|
||
| all_services = create_services | ||
| all_cities = create_cities(json) | ||
| all_buses = create_buses(json, all_services) | ||
|
|
||
| new_records = json.map do |trip| | ||
| from = all_cities[trip['from']] | ||
| to = all_cities[trip['to']] | ||
| bus = all_buses[trip['bus']['number']] | ||
|
|
||
| Trip.new( | ||
| from: from, | ||
| to: to, | ||
| bus: bus, | ||
| start_time: trip['start_time'], | ||
| duration_minutes: trip['duration_minutes'], | ||
| price_cents: trip['price_cents'], | ||
| ) | ||
| end | ||
| bus = Bus.find_or_create_by(number: trip['bus']['number']) | ||
| bus.update(model: trip['bus']['model'], services: services) | ||
|
|
||
| Trip.create!( | ||
| from: from, | ||
| to: to, | ||
| bus: bus, | ||
| start_time: trip['start_time'], | ||
| duration_minutes: trip['duration_minutes'], | ||
| price_cents: trip['price_cents'], | ||
| ) | ||
| Trip.import(new_records) | ||
| end | ||
| end | ||
| puts "Finish in #{time.round(2)}" | ||
| end | ||
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.
👍