-
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
task3. #30
base: master
Are you sure you want to change the base?
task3. #30
Changes from all commits
984ad5f
ac385e6
a1202fc
09de722
8806e81
0d1279f
dd6800d
735a6ce
5654499
242518b
dcf7a85
b0a0ae8
9e05daf
12f4e16
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
FROM ruby:2.6.3-alpine | ||
|
||
ENV APP_PATH /var/www/schedule_app | ||
ENV TZ Europe/Moscow | ||
ENV LANG ru_RU.UTF-8 | ||
ENV LANGUAGE ru_RU.UTF-8 | ||
ENV LC_ALL ru_RU.UTF-8 | ||
|
||
RUN apk update && apk add --no-cache yarn \ | ||
build-base curl-dev git postgresql-dev \ | ||
yaml-dev zlib-dev nodejs gcc g++ make busybox ctags \ | ||
tzdata | ||
|
||
RUN cp /usr/share/zoneinfo/Europe/Moscow /etc/localtime && \ | ||
echo "Europe/Moscow" > /etc/timezone | ||
|
||
RUN mkdir -p $APP_PATH | ||
|
||
WORKDIR $APP_PATH | ||
|
||
COPY Gemfile $APP_PATH/Gemfile | ||
COPY Gemfile.lock $APP_PATH/Gemfile.lock | ||
COPY package.json $APP_PATH/package.json | ||
|
||
RUN yarn global add node-sass | ||
RUN yarn | ||
RUN gem install bundler:2.0.2 | ||
RUN bundle install | ||
|
||
COPY . $APP_PATH | ||
|
||
EXPOSE 3000/tcp | ||
#CMD sh -c "bin/setup" | ||
#CMD sh -c "bundle exec rails server -b 0.0.0.0 -p 3000" |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,6 @@ class TripsController < ApplicationController | |
def index | ||
@from = City.find_by_name!(params[:from]) | ||
@to = City.find_by_name!(params[:to]) | ||
@trips = Trip.where(from: @from, to: @to).order(:start_time) | ||
@trips = Trip.where(from: @from, to: @to).order(:start_time).includes(:bus).includes(bus: :services) | ||
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.
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 |
---|---|---|
|
@@ -12,8 +12,10 @@ class Bus < ApplicationRecord | |
'Газель', | ||
].freeze | ||
|
||
has_many :trips | ||
has_and_belongs_to_many :services, join_table: :buses_services | ||
self.primary_keys = :number, :model | ||
|
||
has_many :trips, :foreign_key => [:number, :model] | ||
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. 👍 Интересное решение, вы первый так сделали 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. В задании было написано |
||
has_and_belongs_to_many :services, join_table: :buses_services, :foreign_key => [:number, :model] | ||
|
||
validates :number, presence: true, uniqueness: true | ||
validates :model, inclusion: { in: MODELS } | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
require 'active_record' | ||
require 'activerecord-import' | ||
|
||
# frozen_string_literal: true | ||
STrip = Struct.new(:from, :to, :start_time, :duration_minutes, :price_cents, :number, :model) | ||
SBus = Struct.new(:number, :model, :services) | ||
|
||
class ScheduleLoader | ||
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 self.call(file_name) | ||
@cities = {} | ||
@services = {} | ||
@buses = Set.new | ||
|
||
@@services = {} | ||
Service.pluck(:name, :id).map{|sr| @@services[sr[0].to_sym] = sr[1]} | ||
|
||
# https://koshigoe.github.io/postgresql/ruby/2018/10/31/bulk-insert-vs-copy-in-postgres.html | ||
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. 👍 |
||
@base_connection = ActiveRecord::Base.connection | ||
@raw_conn = ActiveRecord::Base.connection.raw_connection | ||
@encoder = PG::TextEncoder::CopyRow.new | ||
|
||
@trips_command_raw = "copy trips (from_id, to_id, start_time, duration_minutes, price_cents, model, number) from stdin" | ||
@buses_services_command_raw = "copy buses_services (service_id, model, number) from stdin" | ||
@bus_command_raw = "copy buses (model, number) from stdin" | ||
|
||
@nr2 = Array.new(2) | ||
@nr3 = Array.new(3) | ||
@nr7 = Array.new(7) | ||
|
||
ActiveRecord::Base.transaction do | ||
@raw_conn.exec('delete from buses_services;') | ||
@raw_conn.exec('delete from trips;') | ||
@raw_conn.exec('delete from cities;') | ||
@raw_conn.exec('delete from buses;') | ||
@base_connection.reset_pk_sequence!('cities') | ||
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. 👍 |
||
@base_connection.reset_pk_sequence!('buses') | ||
|
||
File.open(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 | ||
City.import @cities.map {|cc| ({id: cc[1], name: cc[0]})} | ||
end | ||
end | ||
|
||
def self.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 | ||
|
||
bus_number = trip['bus']['number'] | ||
bus_model = trip['bus']['model'] | ||
|
||
bus_key = "#{bus_number}-#{bus_model}" | ||
|
||
unless @buses.include?(bus_key) | ||
@raw_conn.copy_data @bus_command_raw, @encoder do | ||
@nr2 = bus_number, bus_model | ||
@raw_conn.put_copy_data(@nr2) | ||
end | ||
@buses << bus_key | ||
|
||
@raw_conn.copy_data @buses_services_command_raw, @encoder do | ||
trip["bus"]["services"].map do |srv| | ||
@nr3 = @@services[srv.to_sym], bus_number, bus_model | ||
@raw_conn.put_copy_data(@nr3) | ||
end | ||
end | ||
end | ||
|
||
@raw_conn.copy_data @trips_command_raw, @encoder do | ||
@nr7 = from_id,to_id, trip['start_time'],trip['duration_minutes'],trip['price_cents'],bus_number,bus_model | ||
@raw_conn.put_copy_data(@nr7) | ||
end | ||
end | ||
end |
This file was deleted.
This file was deleted.
This file was deleted.
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.
👍