Skip to content

Commit

Permalink
[fix][cms] generation report routing not to bind to task (#5141)
Browse files Browse the repository at this point in the history
task is volatile.
  • Loading branch information
sunny4381 committed Dec 27, 2023
1 parent 070aaf3 commit fe6546e
Show file tree
Hide file tree
Showing 15 changed files with 70 additions and 77 deletions.
19 changes: 0 additions & 19 deletions app/controllers/cms/generation_report/nodes_controller.rb

This file was deleted.

19 changes: 0 additions & 19 deletions app/controllers/cms/generation_report/pages_controller.rb

This file was deleted.

40 changes: 31 additions & 9 deletions app/controllers/cms/generation_report/titles_controller.rb
Expand Up @@ -6,36 +6,53 @@ class Cms::GenerationReport::TitlesController < ApplicationController

model Cms::GenerationReport::Title

helper_method :task
helper_method :latest_task

private

def task
@task ||= Cms::Task.site(@cur_site).find(params[:task])
end

def set_items
@items ||= begin
items = @model.site(@cur_site)
items = items.where(task_id: task.id)
items = items.where(generation_type: params[:type])
items = items.allow(:read, @cur_user, site: @cur_site)
items.order(created: -1)
end
end

def latest_task
@latest_task ||= begin
criteria = Cms::Task.all
criteria = criteria.site(@cur_site)
case params[:type].to_s
when "pages"
task_name = Cms::Page::GenerateJob.task_name
else
task_name = Cms::Node::GenerateJob.task_name
end
criteria = criteria.where(name: task_name, node_id: nil)
criteria = criteria.reorder(updated: -1)
criteria.first
end
end

def latest_title
@latest_title ||= begin
criteria = Cms::GenerationReport::Title.all
criteria = criteria.site(@cur_site)
criteria = criteria.where(task_id: task.id)
criteria = criteria.where(sha256_hash: Cms::GenerationReport.sha256_hash(task.perf_log_file_path))
criteria = criteria.where(generation_type: params[:type])
criteria = criteria.where(sha256_hash: Cms::GenerationReport.sha256_hash(latest_task.perf_log_file_path))
criteria.first
end
end

public

def new
if latest_task.blank? || !::File.exist?(latest_task.perf_log_file_path) || ::File.size(latest_task.perf_log_file_path).zero?
notice = t("mongoid.errors.models.cms/generation_report/title.generate_#{params[:type]}_is_not_done")
redirect_to url_for(action: :index), notice: notice
return
end
if latest_title.present?
notice = t("mongoid.errors.models.cms/generation_report/title.latest_report_is_already_existed")
redirect_to url_for(action: :index), notice: notice
Expand All @@ -46,14 +63,19 @@ def new
end

def create
if latest_task.blank? || !::File.exist?(latest_task.perf_log_file_path) || ::File.size(latest_task.perf_log_file_path).zero?
notice = t("mongoid.errors.models.cms/generation_report/title.generate_#{params[:type]}_is_not_done")
redirect_to url_for(action: :index), notice: notice
return
end
if latest_title.present?
notice = t("mongoid.errors.models.cms/generation_report/title.latest_report_is_already_existed")
redirect_to url_for(action: :index), notice: notice
return
end

job_class = Cms::GenerationReportCreateJob.bind(site_id: @cur_site.id, user_id: @cur_user.id)
job_class.perform_later(task.id)
job_class.perform_later(latest_task.id)

redirect_to url_for(action: :index), notice: t("cms.notices.generation_report_jos_is_started")
end
Expand Down
17 changes: 9 additions & 8 deletions app/jobs/cms/generation_report_create_job.rb
Expand Up @@ -41,9 +41,9 @@ def generation_type
@generation_type ||= begin
case generation_task.name
when "cms:generate_pages"
:page
:pages
when "cms:generate_nodes"
:node
:nodes
end
end
end
Expand Down Expand Up @@ -102,16 +102,17 @@ def create_title!
end
end
unless title_name
title_name = generation_task.started ? "Performance Report at #{I18n.l(generation_task.started, format: :long)}" : ""
if generation_type
title_name = "Pages Generation #{title_name}".strip
else
title_name = "Nodes Generation #{title_name}".strip
title_name = generation_task.started ? "performance log at #{generation_task.started.iso8601}" : "performance log"
case generation_type
when :pages
title_name = "generate page #{title_name}".strip
when :nodes
title_name = "generate node #{title_name}".strip
end
end

title = Cms::GenerationReport::Title.new(
cur_site: site, name: title_name, task: generation_task, sha256_hash: digest)
cur_site: site, name: title_name, task: generation_task, sha256_hash: digest, generation_type: generation_type)
title.save!
title
end
Expand Down
2 changes: 2 additions & 0 deletions app/models/cms/generation_report/title.rb
Expand Up @@ -11,6 +11,8 @@ class Cms::GenerationReport::Title
belongs_to :task, polymorphic: true
field :sha256_hash, type: String

field :generation_type, type: String

after_destroy :destroy_all_histories
after_destroy :destroy_all_aggregations

Expand Down
4 changes: 0 additions & 4 deletions app/views/cms/generation_report/nodes/index.ja.html

This file was deleted.

4 changes: 0 additions & 4 deletions app/views/cms/generation_report/pages/index.ja.html

This file was deleted.

2 changes: 1 addition & 1 deletion app/views/cms/generation_report/titles/_new.ja.html.erb
@@ -1,3 +1,3 @@
<div class="main-box">
<%= task.name.include?("page") ? t("cms.generate_page") : t("cms.generate_node") %>から書き出し性能レポートを作成します。
<%= latest_task.name.include?("page") ? t("cms.generate_page") : t("cms.generate_node") %>から書き出し性能レポートを作成します。
</div>
2 changes: 2 additions & 0 deletions config/locales/cms/ja.yml
Expand Up @@ -941,6 +941,8 @@ ja:
url:
youtube_id_can_not_get: からYouTubeIDが取得できませんでした。
cms/generation_report/title:
generate_nodes_is_not_done: フォルダー書き出しが一度も実行されていません。まずはフォルダー書き出しを実行してください。
generate_pages_is_not_done: ページ書き出しが一度も実行されていません。まずはページ書き出しを実行してください。
latest_report_is_already_existed: 最新の書き出し性能レポートがすでに存在します。
attributes:
cms/content:
Expand Down
8 changes: 4 additions & 4 deletions config/routes/cms/routes_end.rb
Expand Up @@ -260,11 +260,11 @@
get "generate_pages/download_logs" => "generate_pages#download_logs"
post "generate_pages/segment/:segment" => "generate_pages#run"
namespace "generation_report", path: "generation_report" do
get "/" => redirect { |p, req| "#{req.path}/nodes" }, as: :main
resources :nodes, only: %i[index]
resources :pages, only: %i[index]
get "/" => redirect { |p, req| "#{req.path}/nodes/titles" }, as: :main
get "/nodes" => redirect { |p, req| "#{req.path}/titles" }, as: :nodes
get "/pages" => redirect { |p, req| "#{req.path}/titles" }, as: :pages
end
namespace "generation_report", path: "generation_report/:type/:task" do
namespace "generation_report", path: "generation_report/:type" do
resources :titles, only: %i[index new create destroy], concerns: :deletion
resources :histories, path: "titles/:title/histories", only: %i[index show] do
match :download_all, on: :collection, via: %i[get post]
Expand Down
11 changes: 7 additions & 4 deletions spec/features/cms/generation_report/nodes/basic_spec.rb
@@ -1,17 +1,20 @@
require 'spec_helper'

describe Cms::GenerationReport::NodesController, type: :feature, dbscope: :example do
describe Cms::GenerationReport::TitlesController, type: :feature, dbscope: :example do
let!(:site) { cms_site }

before { login_cms_user }

context "without task 'cms:generate_nodes'" do
it do
visit cms_generation_report_main_path(site: site)
expect(page).to have_content("フォルダー書き出しが一度も実行されていません。")
click_on I18n.t("ss.links.new")
notice = I18n.t("mongoid.errors.models.cms/generation_report/title.generate_nodes_is_not_done")
expect(page).to have_css("#notice", text: notice)

visit cms_generation_report_nodes_path(site: site)
expect(page).to have_content("フォルダー書き出しが一度も実行されていません。")
click_on I18n.t("ss.links.new")
expect(page).to have_css("#notice", text: notice)
end
end

Expand Down Expand Up @@ -43,7 +46,7 @@
end

context "with title" do
let!(:title) { create :cms_generation_report_title, cur_site: site, task: task }
let!(:title) { create :cms_generation_report_title, cur_site: site, task: task, generation_type: "nodes" }
let!(:content) { create :article_node_page, cur_site: site }
let(:history_type1) { "node" }
let(:history1_db) { rand }
Expand Down
8 changes: 5 additions & 3 deletions spec/features/cms/generation_report/pages/basic_spec.rb
@@ -1,14 +1,16 @@
require 'spec_helper'

describe Cms::GenerationReport::PagesController, type: :feature, dbscope: :example do
describe Cms::GenerationReport::TitlesController, type: :feature, dbscope: :example do
let!(:site) { cms_site }

before { login_cms_user }

context "without task 'cms:generate_pages'" do
it do
visit cms_generation_report_pages_path(site: site)
expect(page).to have_content("ページ書き出しが一度も実行されていません。")
click_on I18n.t("ss.links.new")
notice = I18n.t("mongoid.errors.models.cms/generation_report/title.generate_pages_is_not_done")
expect(page).to have_css("#notice", text: notice)
end
end

Expand Down Expand Up @@ -40,7 +42,7 @@
end

context "with title" do
let!(:title) { create :cms_generation_report_title, cur_site: site, task: task }
let!(:title) { create :cms_generation_report_title, cur_site: site, task: task, generation_type: "pages" }
let!(:content) { create :article_node_page, cur_site: site }
let(:history_type1) { "page" }
let(:history1_db) { rand }
Expand Down
Expand Up @@ -61,9 +61,10 @@
expect(Cms::GenerationReport::Title.all.count).to eq 1
title = Cms::GenerationReport::Title.all.first
expect(title.site_id).to eq site.id
expect(title.name).to be_present
expect(title.name).to include("generate node performance")
expect(title.task_id).to eq task.id
expect(title.sha256_hash).to be_present
expect(title.generation_type).to eq "nodes"

expect(Cms::GenerationReport::History[title].all.count).to eq 3
Cms::GenerationReport::History[title].all.to_a.tap do |histories|
Expand Down
3 changes: 2 additions & 1 deletion spec/jobs/cms/node/generate_job/article_spec.rb
Expand Up @@ -57,9 +57,10 @@
expect(Cms::GenerationReport::Title.all.count).to eq 1
title = Cms::GenerationReport::Title.all.first
expect(title.site_id).to eq site.id
expect(title.name).to be_present
expect(title.name).to include("generate node performance")
expect(title.task_id).to eq task.id
expect(title.sha256_hash).to be_present
expect(title.generation_type).to eq "nodes"

expect(Cms::GenerationReport::History[title].all.count).to eq 3
Cms::GenerationReport::History[title].all.to_a.tap do |histories|
Expand Down
5 changes: 5 additions & 0 deletions spec/jobs/cms/page/generate_job/article_spec.rb
Expand Up @@ -111,6 +111,11 @@

expect(Cms::GenerationReport::Title.all.count).to eq 1
title = Cms::GenerationReport::Title.all.first
expect(title.site_id).to eq site.id
expect(title.name).to include("generate page performance log")
expect(title.task_id).to eq task.id
expect(title.sha256_hash).to be_present
expect(title.generation_type).to eq "pages"

expect(Cms::GenerationReport::History[title].all.count).to eq 5
expect(Cms::GenerationReport::Aggregation[title].all.count).to eq 1
Expand Down

0 comments on commit fe6546e

Please sign in to comment.