-
Notifications
You must be signed in to change notification settings - Fork 12
Optimization #7
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
foxy-eyed
wants to merge
7
commits into
spajic:master
Choose a base branch
from
foxy-eyed:optimization
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+341
−75
Open
Optimization #7
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d2aa2c8
Move logic from rake task to service
foxy-eyed beb3bf7
Add rubocop and fix offenses
foxy-eyed 604e10c
Add unit test for db_importer service
foxy-eyed 0f7985d
Add rake task for benchmark import
foxy-eyed 27a580d
Import optimization (wip)
foxy-eyed a71cf18
Optimize import with activerecord-import gem
foxy-eyed 2d90db8
Optimize page load
foxy-eyed File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,3 +25,5 @@ | |
|
|
||
| # Ignore master key for decrypting credentials and more. | ||
| /config/master.key | ||
|
|
||
| .env | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| AllCops: | ||
| TargetRubyVersion: 2.6.1 | ||
| Exclude: | ||
| - "config/**/*" | ||
| - "db/**/*" | ||
| - "bin/**" | ||
| - "*Gemfile*" | ||
| - "tmp/**/*" | ||
|
|
||
| Metrics/AbcSize: | ||
| Enabled: false | ||
|
|
||
| Metrics/LineLength: | ||
| Max: 120 | ||
|
|
||
| Metrics/MethodLength: | ||
| Max: 15 | ||
|
|
||
| Style/ClassAndModuleChildren: | ||
| Enabled: false | ||
|
|
||
| Style/Documentation: | ||
| Enabled: false | ||
|
|
||
| Style/FrozenStringLiteralComment: | ||
| Enabled: false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,9 +8,18 @@ gem 'pg', '>= 0.18', '< 2.0' | |
| gem 'puma', '~> 3.11' | ||
| gem 'bootsnap', '>= 1.1.0', require: false | ||
|
|
||
| gem 'activerecord-import' | ||
| gem 'dotenv-rails' | ||
| gem 'newrelic_rpm' | ||
| gem 'oj' | ||
| gem 'pghero' | ||
| gem 'strong_migrations' | ||
|
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 'rubocop' | ||
| end | ||
|
|
||
| group :development do | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,5 @@ | ||
| module ApplicationHelper | ||
| def trips_delimiter | ||
| '===================================================='.freeze | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| require 'oj' | ||
|
|
||
| class DbImporter | ||
|
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. 👍 |
||
| def initialize | ||
| @cities = {} | ||
| @services = {} | ||
| @buses = {} | ||
| end | ||
|
|
||
| def call(source:) | ||
| json = Oj.load_file(source) | ||
|
|
||
| ActiveRecord::Base.transaction do | ||
| clear_db! | ||
| create_from_json!(json) | ||
| end | ||
| end | ||
|
|
||
| private | ||
|
|
||
| attr_reader :cities, :services, :buses | ||
|
|
||
| def clear_db! | ||
| City.delete_all | ||
| Bus.delete_all | ||
| Service.delete_all | ||
| Trip.delete_all | ||
| ActiveRecord::Base.connection.execute('delete from buses_services;') | ||
| end | ||
|
|
||
| def create_from_json!(json) | ||
| import_cities_and_services(json) | ||
| import_buses(json) | ||
| import_trips(json) | ||
| end | ||
|
|
||
| def import_cities_and_services(json) | ||
| json.each do |trip| | ||
| cities[trip['from']] ||= City.new(name: trip['from']) | ||
| cities[trip['to']] ||= City.new(name: trip['to']) | ||
|
|
||
| trip['bus']['services'].each do |service| | ||
| services[service] ||= Service.new(name: service) | ||
| end | ||
| end | ||
|
|
||
| City.import %i[name], cities.values, syncronize: true, raise_error: true | ||
| Service.import %i[name], services.values, syncronize: true, raise_error: true | ||
| end | ||
|
|
||
| def import_buses(json) | ||
| json.each do |trip| | ||
| bus = buses[trip['bus']['number']] || Bus.new(number: trip['bus']['number']) | ||
| bus.model = trip['bus']['model'] | ||
| bus.services = services.values_at(*trip['bus']['services']) | ||
| buses[trip['bus']['number']] = bus | ||
| end | ||
|
|
||
| Bus.import buses.values, recursive: true, syncronize: true, raise_error: true | ||
| end | ||
|
|
||
| def import_trips(json) | ||
| trips = [] | ||
| json.each do |trip| | ||
| from = cities[trip['from']] | ||
| to = cities[trip['to']] | ||
|
|
||
| trips << Trip.new( | ||
| from: from, | ||
| to: to, | ||
| bus: buses[trip['bus']['number']], | ||
| start_time: trip['start_time'], | ||
| duration_minutes: trip['duration_minutes'], | ||
| price_cents: trip['price_cents'] | ||
| ) | ||
| end | ||
|
|
||
| Trip.import trips, raise_error: true | ||
| end | ||
| end | ||
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,16 @@ | ||
| <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? %> | ||
| <li>Сервисы в автобусе:</li> | ||
| <ul> | ||
| <% trip.bus.services.each do |service| %> | ||
| <li><%= "#{service.name}" %></li> | ||
| <% end %> | ||
| </ul> | ||
| <% end %> | ||
| </ul> | ||
| <%= trips_delimiter %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
👍