-
Notifications
You must be signed in to change notification settings - Fork 115
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
Comimt task 3 #5
Closed
Closed
Changes from all commits
Commits
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 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 +1 @@ | ||
2.6.3 | ||
2.6.1 | ||
This file contains 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,26 +1,32 @@ | ||
# frozen_string_literal: true | ||
|
||
source 'https://rubygems.org' | ||
git_source(:github) { |repo| "https://github.com/#{repo}.git" } | ||
|
||
ruby '2.6.3' | ||
ruby '2.6.1' | ||
|
||
gem 'rails', '~> 5.2.3' | ||
gem 'bootsnap', '>= 1.1.0', require: false | ||
gem 'pg', '>= 0.18', '< 2.0' | ||
gem 'puma', '~> 3.11' | ||
gem 'bootsnap', '>= 1.1.0', require: false | ||
gem 'rails', '~> 5.2.3' | ||
|
||
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 'byebug', platforms: %i[mri mingw x64_mingw] | ||
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 'web-console', '>= 3.3.0' | ||
end | ||
|
||
group :test do | ||
end | ||
|
||
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem | ||
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby] | ||
gem 'tzinfo-data', platforms: %i[mingw mswin x64_mingw jruby] | ||
|
||
gem 'activerecord-import' | ||
gem 'oj' | ||
gem 'ruby-prof', '~> 0.18' |
This file contains 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 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 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 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,2 @@ | ||
class BusesService < ApplicationRecord | ||
end |
This file contains 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 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,34 +1,114 @@ | ||
# frozen_string_literal: true | ||
|
||
# Наивная загрузка данных из json-файла в БД | ||
# rake reload_json[fixtures/small.json] | ||
|
||
require 'benchmark' | ||
|
||
@cities = {} | ||
@services = {} | ||
@buses = {} | ||
|
||
def import_trip(trip) | ||
from_id = @cities[trip['from']] | ||
unless from_id | ||
from_id = @cities.size + 1 | ||
@cities[trip['from']] = from_id | ||
end | ||
|
||
to_id = @cities[trip['to']] | ||
unless to_id | ||
to_id = @cities.size + 1 | ||
@cities[trip['to']] = to_id | ||
end | ||
|
||
trip['bus']['services'].each do |service| | ||
service_id = @services[service] | ||
unless service_id | ||
service_id = @services.size + 1 | ||
@services[service] = service_id | ||
end | ||
end | ||
|
||
bus_id = @buses[trip['bus']['number']].try(:[], :id) | ||
unless bus_id | ||
bus_id = @buses.size + 1 | ||
services = trip['bus']['services'].map do |s| | ||
@services[s] | ||
end | ||
@buses[trip['bus']['number']] = { id: bus_id, model: trip['bus']['model'], services: services } | ||
end | ||
|
||
bus_id = @buses[trip['bus']['number']][:id] | ||
ActiveRecord::Base.connection.raw_connection.put_copy_data("#{@cities[trip['from']]};#{@cities[trip['to']]};#{trip['start_time']};#{trip['duration_minutes']};#{trip['price_cents']};#{bus_id}\n") | ||
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']) | ||
time = Benchmark.realtime do | ||
ActiveRecord::Base.transaction do | ||
trips_command = "copy trips (from_id, to_id, start_time, duration_minutes, price_cents, bus_id) from stdin with csv delimiter ';'" | ||
|
||
City.delete_all | ||
Bus.delete_all | ||
Service.delete_all | ||
Trip.delete_all | ||
ActiveRecord::Base.connection.execute('delete from buses_services;') | ||
ActiveRecord::Base.connection.reset_pk_sequence!('cities') | ||
ActiveRecord::Base.connection.reset_pk_sequence!('buses') | ||
ActiveRecord::Base.connection.reset_pk_sequence!('services') | ||
|
||
ActiveRecord::Base.connection.raw_connection.copy_data trips_command do | ||
File.open(args.file_name) do |ff| | ||
nesting = 0 | ||
str = +'' | ||
|
||
until ff.eof? | ||
ch = ff.read(1) # читаем по одному символу | ||
if ch == '{' # начинается объект, повышается вложенность | ||
nesting += 1 | ||
str << ch | ||
elsif ch == '}' # заканчивается объект, понижается вложенность | ||
nesting -= 1 | ||
str << ch | ||
if nesting == 0 # если закончился объкет уровня trip, парсим и импортируем его | ||
trip = Oj.load(str) | ||
|
||
import_trip(trip) | ||
|
||
str = +'' | ||
end | ||
elsif nesting >= 1 | ||
str << ch | ||
end | ||
end | ||
end | ||
end | ||
|
||
cities = [] | ||
@cities.each do |city| | ||
cities << City.new(name: city[0]) | ||
end | ||
|
||
buses = [] | ||
buses_services = [] | ||
@buses.each do |value| | ||
buses << Bus.new(number: value[0], model: value[1][:model]) | ||
value[1][:services].map do |s| | ||
buses_services << BusesService.new(bus_id: value[1][:id], service_id: s) | ||
end | ||
end | ||
|
||
services = [] | ||
trip['bus']['services'].each do |service| | ||
s = Service.find_or_create_by(name: service) | ||
services << s | ||
@services.each do |value| | ||
services << Service.new(name: value[0]) | ||
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'], | ||
) | ||
|
||
City.import cities | ||
Service.import services | ||
Bus.import buses | ||
BusesService.import buses_services | ||
end | ||
end | ||
|
||
puts "Finish in #{time.round(2)}" | ||
end |
This file contains 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,11 @@ | ||
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html | ||
|
||
# This model initially had no columns defined. If you add columns to the | ||
# model remove the '{}' from the fixture names and add the columns immediately | ||
# below each fixture, per the syntax in the comments below | ||
# | ||
one: {} | ||
# column: value | ||
# | ||
two: {} | ||
# column: value |
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.
сбросил версию потому что инета не хватает чтобы закачать все гемы, на мобилке пока сижу.