From 5f9a4c0da650c8e2857cd58f13e11f34133330b2 Mon Sep 17 00:00:00 2001 From: Dmitry <124861781+ada-dmitry@users.noreply.github.com> Date: Wed, 10 Dec 2025 15:02:39 +0300 Subject: [PATCH] =?UTF-8?q?SU2-7=20|=20=D0=94=D0=BE=D0=B1=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BC=D0=BE=D0=B4=D0=B5=D0=BB?= =?UTF-8?q?=D0=B8=20Report=20=D1=81=20=D0=BF=D0=BE=D0=B4=D0=B4=D0=B5=D1=80?= =?UTF-8?q?=D0=B6=D0=BA=D0=BE=D0=B9=20=D0=B7=D0=B0=D0=B3=D1=80=D1=83=D0=B7?= =?UTF-8?q?=D0=BA=D0=B8=20=D1=84=D0=B0=D0=B9=D0=BB=D0=BE=D0=B2,=20=D0=BE?= =?UTF-8?q?=D0=B1=D0=BD=D0=BE=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BA?= =?UTF-8?q?=D0=BE=D0=BD=D1=82=D1=80=D0=BE=D0=BB=D0=BB=D0=B5=D1=80=D0=B0=20?= =?UTF-8?q?=D0=B8=20=D0=BF=D1=80=D0=B5=D0=B4=D1=81=D1=82=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=B4=D0=BB=D1=8F=20=D0=BE=D0=B1?= =?UTF-8?q?=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=BA=D0=B8=20=D0=BE=D1=82=D1=87?= =?UTF-8?q?=D0=B5=D1=82=D0=BE=D0=B2,=20=D0=B0=20=D1=82=D0=B0=D0=BA=D0=B6?= =?UTF-8?q?=D0=B5=20=D1=81=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD=D0=B8=D0=B5=20?= =?UTF-8?q?=D0=BC=D0=B8=D0=B3=D1=80=D0=B0=D1=86=D0=B8=D0=B9=20=D0=B4=D0=BB?= =?UTF-8?q?=D1=8F=20=D1=82=D0=B0=D0=B1=D0=BB=D0=B8=D1=86=D1=8B=20=D0=BE?= =?UTF-8?q?=D1=82=D1=87=D0=B5=D1=82=D0=BE=D0=B2=20=D0=B8=20=D0=B0=D0=BA?= =?UTF-8?q?=D1=82=D0=B8=D0=B2=D0=BD=D0=BE=D0=B3=D0=BE=20=D1=85=D1=80=D0=B0?= =?UTF-8?q?=D0=BD=D0=B8=D0=BB=D0=B8=D1=89=D0=B0.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/reports_controller.rb | 6 ++-- app/models/report.rb | 5 +++ app/sidekiq/file_processor_job.rb | 25 ++++++++++++-- app/views/reports/index.html.erb | 34 ++++++++++++------- db/migrate/20251209142627_create_reports.rb | 10 ++++++ ...e_active_storage_tables.active_storage.rb} | 0 db/schema.rb | 9 ++++- spec/models/report_spec.rb | 5 +++ 8 files changed, 74 insertions(+), 20 deletions(-) create mode 100644 app/models/report.rb create mode 100644 db/migrate/20251209142627_create_reports.rb rename db/migrate/{20250805090922_create_active_storage_tables.active_storage.rb => 20251209142642_create_active_storage_tables.active_storage.rb} (100%) create mode 100644 spec/models/report_spec.rb diff --git a/app/controllers/reports_controller.rb b/app/controllers/reports_controller.rb index 9d76023..accc7a7 100644 --- a/app/controllers/reports_controller.rb +++ b/app/controllers/reports_controller.rb @@ -1,13 +1,13 @@ class ReportsController < ApplicationController def index + @reports = Report.order(created_at: :desc).limit(10) end def create inn = params[:inn] - output_path = Rails.root.join('tmp', 'reports', inn) - FileUtils.mkdir_p(output_path) + report = Report.create!(inn: inn, status: 'processing') - FileProcessorJob.perform_async(inn, output_path.to_s) + FileProcessorJob.perform_async(inn, report.id) redirect_to reports_path, notice: 'Отчёт загружается в фоне' end diff --git a/app/models/report.rb b/app/models/report.rb new file mode 100644 index 0000000..b9ab0a8 --- /dev/null +++ b/app/models/report.rb @@ -0,0 +1,5 @@ +class Report < ApplicationRecord + has_many_attached :files + + validates :inn, presence: true +end diff --git a/app/sidekiq/file_processor_job.rb b/app/sidekiq/file_processor_job.rb index 8f36cec..a331ac5 100644 --- a/app/sidekiq/file_processor_job.rb +++ b/app/sidekiq/file_processor_job.rb @@ -5,16 +5,35 @@ require 'stringio' class FileProcessorJob include Sidekiq::Job - def perform(inn, output_path) + def perform(inn, report_id) + report = Report.find(report_id) Rails.logger.info "=== Начало загрузки для ИНН: #{inn} ===" - download_inn(inn, output_path) + + # Временная папка + temp_dir = Rails.root.join('tmp', 'reports', inn) + FileUtils.mkdir_p(temp_dir) + + download_inn(inn, temp_dir.to_s) + + Dir.glob("#{temp_dir}/**/*").select { |f| File.file?(f) }.each do |file_path| + report.files.attach( + io: File.open(file_path), + filename: File.basename(file_path) + ) + end + + # Удали временные файлы + FileUtils.rm_rf(temp_dir) + + report.update!(status: 'completed') Rails.logger.info "=== Завершено для ИНН: #{inn} ===" rescue => e + report.update!(status: 'failed') if report Rails.logger.error "=== ОШИБКА: #{e.message} ===" - Rails.logger.error e.backtrace.first(5).join("\n") raise end + private def get_content(url, json_need = false) diff --git a/app/views/reports/index.html.erb b/app/views/reports/index.html.erb index ea83847..577c6e5 100644 --- a/app/views/reports/index.html.erb +++ b/app/views/reports/index.html.erb @@ -1,17 +1,25 @@ -
-

Загрузка отчётов по ИНН

+

Загрузка отчётов по ИНН

- <% if notice %> -

<%= notice %>

- <% end %> +<% if notice %> +

<%= notice %>

+<% end %> - <%= form_with url: reports_path, method: :post, local: true do |f| %> -
- <%= f.label :inn, "Введите ИНН:" %> - <%= f.text_field :inn, placeholder: "1234567890", required: true, style: "width: 100%; padding: 8px;" %> -
+<%= form_with url: reports_path, method: :post, local: true do |f| %> + <%= f.label :inn, "Введите ИНН:" %> + <%= f.text_field :inn, required: true %> + <%= f.submit "Загрузить отчёт" %> +<% end %> - <%= f.submit "Загрузить отчёт", style: "padding: 10px 20px; cursor: pointer;" %> - <% end %> -
\ No newline at end of file +

История загрузок

+<% @reports.each do |report| %> +
+

ИНН: <%= report.inn %> | Статус: <%= report.status %>

+ <% if report.files.attached? %> +

Файлы:

+ <% report.files.each do |file| %> + <%= link_to file.filename, rails_blob_path(file, disposition: "attachment") %> + <% end %> + <% end %> +
+<% end %> \ No newline at end of file diff --git a/db/migrate/20251209142627_create_reports.rb b/db/migrate/20251209142627_create_reports.rb new file mode 100644 index 0000000..00ab5a8 --- /dev/null +++ b/db/migrate/20251209142627_create_reports.rb @@ -0,0 +1,10 @@ +class CreateReports < ActiveRecord::Migration[7.1] + def change + create_table :reports do |t| + t.string :inn + t.string :status + + t.timestamps + end + end +end diff --git a/db/migrate/20250805090922_create_active_storage_tables.active_storage.rb b/db/migrate/20251209142642_create_active_storage_tables.active_storage.rb similarity index 100% rename from db/migrate/20250805090922_create_active_storage_tables.active_storage.rb rename to db/migrate/20251209142642_create_active_storage_tables.active_storage.rb diff --git a/db/schema.rb b/db/schema.rb index 2ab424b..6967052 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2025_08_05_090922) do +ActiveRecord::Schema[7.1].define(version: 2025_12_09_142642) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -42,6 +42,13 @@ ActiveRecord::Schema[7.1].define(version: 2025_08_05_090922) do t.index ["blob_id", "variation_digest"], name: "index_active_storage_variant_records_uniqueness", unique: true end + create_table "reports", force: :cascade do |t| + t.string "inn" + t.string "status" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + create_table "sessions", force: :cascade do |t| t.string "session_id", null: false t.string "cas_ticket" diff --git a/spec/models/report_spec.rb b/spec/models/report_spec.rb new file mode 100644 index 0000000..92088ee --- /dev/null +++ b/spec/models/report_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe Report, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end