-
Notifications
You must be signed in to change notification settings - Fork 12
Data import and trips#index optimizations #3
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
28a55cf
a388cf3
adc775e
3479b6f
1f0b884
20fc703
0209a28
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 |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| --require rails_helper | ||
| --profile 10 | ||
| --color | ||
|
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. 👍 |
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| 2.6.1 | ||
| 2.6.1-railsexpress |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,5 @@ | ||
| module ApplicationHelper | ||
| def render_delimiter | ||
|
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,16 @@ | ||
| module TripsHelper | ||
| def render_service(service) | ||
| "<li>#{service.name}</li>".html_safe | ||
| end | ||
|
|
||
| def render_services(services) | ||
| services.map do |service| | ||
| <<~HTML | ||
| <li>Сервисы в автобусе:</li> | ||
| <ul> | ||
| #{render_service(service)} | ||
| </ul> | ||
| HTML | ||
| end.join('').html_safe | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,7 +13,8 @@ class Bus < ApplicationRecord | |
| ].freeze | ||
|
|
||
| has_many :trips | ||
| has_and_belongs_to_many :services, join_table: :buses_services | ||
| has_many :bus_services | ||
| has_many :services, through: :bus_services | ||
|
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. 👍 |
||
|
|
||
| validates :number, presence: true, uniqueness: true | ||
| validates :model, inclusion: { in: MODELS } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| class BusService < ApplicationRecord | ||
| self.table_name = 'buses_services' | ||
|
|
||
| belongs_to :bus | ||
| belongs_to :service | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| class DbPopulator | ||
|
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. 👍 |
||
| class << self | ||
| def populate(file_name) | ||
| json = JSON.parse(File.read(file_name)) | ||
|
|
||
| all_services = {} | ||
| all_buses = {} | ||
| all_cities = {} | ||
|
|
||
| ActiveRecord::Base.transaction do | ||
| City.delete_all | ||
| BusService.delete_all | ||
| Bus.delete_all | ||
| Service.delete_all | ||
| Trip.delete_all | ||
|
|
||
| Trip.bulk_insert do |trip_worker| | ||
| BusService.bulk_insert do |bus_service_worker| | ||
| json.each do |trip| | ||
| from = (all_cities[trip['from']] ||= City.create!(name: trip['from'])) | ||
| to = (all_cities[trip['to']] ||= City.create!(name: trip['to'])) | ||
|
|
||
| services = [] | ||
| trip['bus']['services'].each do |service| | ||
| s = (all_services[service] ||= Service.create!(name: service)) | ||
| services << s | ||
| end | ||
|
|
||
| number = trip['bus']['number'] | ||
| bus = (all_buses[number] ||= begin | ||
| Bus.create!(number: number, model: trip['bus']['model']).tap do |bus| | ||
| services.each do |service| | ||
| bus_service_worker.add(bus_id: bus.id, service_id: service.id) | ||
| end | ||
| end | ||
| end) | ||
|
|
||
| trip_worker.add( | ||
| from_id: from.id, | ||
| to_id: to.id, | ||
| bus_id: bus.id, | ||
| start_time: trip['start_time'], | ||
| duration_minutes: trip['duration_minutes'], | ||
| price_cents: trip['price_cents'], | ||
| ) | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
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,9 @@ | ||
| class AddIndexes < ActiveRecord::Migration[5.2] | ||
| def change | ||
| add_index :buses_services, :bus_id | ||
| add_index :buses_services, :service_id | ||
| add_index :cities, :name | ||
| add_index :trips, :bus_id | ||
| add_index :trips, [:from_id, :to_id] | ||
| end | ||
| 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.
👍