-
Notifications
You must be signed in to change notification settings - Fork 12
Ускорение загрузки данных и отображения расписания #5
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 |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| --require spec_helper | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,15 +8,25 @@ gem 'pg', '>= 0.18', '< 2.0' | |
| gem 'puma', '~> 3.11' | ||
| gem 'bootsnap', '>= 1.1.0', require: false | ||
|
|
||
| gem 'pghero' | ||
|
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. 👍 |
||
| gem 'activerecord-import' | ||
|
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. 👍 |
||
|
|
||
| group :development, :test do | ||
| # Call 'byebug' anywhere in the code to stop execution and get a debugger console | ||
| gem 'byebug', platforms: [:mri, :mingw, :x64_mingw] | ||
| gem 'rspec-rails', '~> 3.8' | ||
| end | ||
|
|
||
| group :development do | ||
| # Access an interactive console on exception pages or by calling 'console' anywhere in the code. | ||
| gem 'web-console', '>= 3.3.0' | ||
| gem 'listen', '>= 3.0.5', '< 3.2' | ||
|
|
||
| gem 'rack-mini-profiler' | ||
|
|
||
| gem 'flamegraph' | ||
| gem "stackprof", "~> 0.2", require: false, platforms: :ruby | ||
| gem "memory_profiler", "~> 0.9" | ||
| end | ||
|
|
||
| group :test do | ||
|
|
||
| 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 :buses_services | ||
| has_many :services, through: :buses_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,4 @@ | ||
| class BusesService < ApplicationRecord | ||
| belongs_to :bus | ||
| belongs_to :service | ||
| end |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,4 @@ | ||
| <li>Сервисы в автобусе:</li> | ||
| <ul> | ||
| <% services.each do |service| %> | ||
| <%= render "service", service: service %> | ||
| <% end %> | ||
| <%= render partial: "trips/service", collection: services, cached: true %> | ||
| </ul> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,12 @@ | ||
| <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> | ||
| <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 |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| #!/usr/bin/env ruby | ||
| require 'fileutils' | ||
| include FileUtils | ||
| require 'benchmark' | ||
|
|
||
| # path to your application root. | ||
| APP_ROOT = File.expand_path('..', __dir__) | ||
|
|
||
| def system!(*args) | ||
| system(*args) || abort("\n== Command #{args} failed ==") | ||
| end | ||
|
|
||
|
|
||
|
|
||
| chdir APP_ROOT do | ||
| result = Benchmark.measure do | ||
| puts "\n== Loading data from fixtures/small.json ==" | ||
| system! 'bin/rake reload_json[fixtures/small.json]' | ||
| end | ||
|
|
||
| puts result | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| class AddIndexes < ActiveRecord::Migration[5.2] | ||
| def change | ||
| add_index :cities, :name, unique: true | ||
|
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. 👍 плюсик за уникальность |
||
| add_index :buses, :number, unique: true | ||
| add_index :services, :name, unique: true | ||
| add_index :buses_services, [:bus_id, :service_id], unique: true | ||
|
|
||
| change_column_null :trips, :from_id, false | ||
| change_column_null :trips, :to_id, false | ||
| change_column_null :trips, :bus_id, false | ||
| change_column_null :trips, :duration_minutes, false | ||
| change_column_null :trips, :price_cents, false | ||
|
|
||
| add_index :trips, [:from_id, :to_id] | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| # Наивная загрузка данных из json-файла в БД | ||
| # rake reload_json[fixtures/small.json] | ||
| task :reload_json_old, [: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 | ||
| 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'], | ||
| ) | ||
| end | ||
| 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.
👍