Skip to content

Commit

Permalink
Merge pull request #2496 from tvdeyen/usage-task
Browse files Browse the repository at this point in the history
Add task(s) to visualize element and page usage
  • Loading branch information
tvdeyen authored Jul 3, 2023
2 parents 38af3ff + 825c66b commit c239df1
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 0 deletions.
33 changes: 33 additions & 0 deletions lib/alchemy/tasks/usage.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# frozen_string_literal: true

module Alchemy
module Tasks
module Usage
extend self

def elements_count_by_name
res = Alchemy::Element.all
.select("name, COUNT(*) AS count")
.group(:name)
.order("count DESC, name ASC")
.map { |e| {"name" => e.name, "count" => e.count} }
Alchemy::Element.definitions.reject { |definition| res.map { |e| e["name"] }.include? definition["name"] }.each do |definition|
res << {"name" => definition["name"], "count" => 0}
end
res
end

def pages_count_by_type
res = Alchemy::Page.all
.select("page_layout, COUNT(*) AS count")
.group(:page_layout)
.order("count DESC, page_layout ASC")
.map { |p| {"page_layout" => p.page_layout, "count" => p.count} }
Alchemy::PageLayout.all.reject { |page_layout| res.map { |p| p["page_layout"] }.include? page_layout["name"] }.each do |page_layout|
res << {"page_layout" => page_layout["name"], "count" => 0}
end
res
end
end
end
end
40 changes: 40 additions & 0 deletions lib/tasks/alchemy/usage.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
require "alchemy/tasks/usage"

namespace :alchemy do
desc "List Alchemy elements and pages usage"
task usage: [:page_usage, :element_usage]

desc "List Alchemy elements usage"
task page_usage: :environment do
include ActionView::Helpers::NumberHelper
puts "\n Alchemy pages usage"
results = Alchemy::Tasks::Usage.pages_count_by_type
if results.any?
puts " ----------------------"
puts "\n"
results.each do |row|
puts " #{number_with_delimiter(row["count"])} 𝗑 #{row["page_layout"]}"
end
puts "\n = #{number_with_delimiter(Alchemy::Page.count)} pages in total."
else
puts " > No pages found!"
end
end

desc "List Alchemy elements usage"
task element_usage: :environment do
include ActionView::Helpers::NumberHelper
puts "\n Alchemy elements usage"
results = Alchemy::Tasks::Usage.elements_count_by_name
if results.any?
puts " ----------------------"
puts "\n"
results.each do |row|
puts " #{number_with_delimiter(row["count"])} 𝗑 #{row["name"]}"
end
puts "\n = #{number_with_delimiter(Alchemy::Element.count)} elements in total."
else
puts " > No elements found!"
end
end
end
65 changes: 65 additions & 0 deletions spec/libraries/alchemy/tasks/usage_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
require "rails_helper"
require "alchemy/tasks/usage"

RSpec.describe Alchemy::Tasks::Usage do
describe ".elements_count_by_name" do
subject { described_class.elements_count_by_name }

before do
create_list(:alchemy_element, 3, name: "headline")
create_list(:alchemy_element, 2, name: "image")
create(:alchemy_element, name: "text")
end

it "returns the elements count by name" do
expect(subject).to eq [
{"name" => "headline", "count" => 3},
{"name" => "image", "count" => 2},
{"name" => "text", "count" => 1},
{"name" => "header", "count" => 0},
{"name" => "article", "count" => 0},
{"name" => "search", "count" => 0},
{"name" => "news", "count" => 0},
{"name" => "download", "count" => 0},
{"name" => "bild", "count" => 0},
{"name" => "contactform", "count" => 0},
{"name" => "all_you_can_eat", "count" => 0},
{"name" => "erb_element", "count" => 0},
{"name" => "slide", "count" => 0},
{"name" => "slider", "count" => 0},
{"name" => "gallery", "count" => 0},
{"name" => "gallery_picture", "count" => 0},
{"name" => "right_column", "count" => 0},
{"name" => "left_column", "count" => 0},
{"name" => "erb_cell", "count" => 0},
{"name" => "menu", "count" => 0},
{"name" => "old", "count" => 0},
{"name" => "element_with_ingredient_groups", "count" => 0},
{"name" => "element_with_warning", "count" => 0}
]
end
end

describe ".pages_count_by_type" do
subject { described_class.pages_count_by_type }

before do
create_list(:alchemy_page, 2, page_layout: "standard")
create(:alchemy_page, page_layout: "home")
end

it "returns the pages count by type" do
expect(subject).to eq [
{"page_layout" => "standard", "count" => 2},
{"page_layout" => "home", "count" => 1},
{"page_layout" => "index", "count" => 1},
{"page_layout" => "readonly", "count" => 0},
{"page_layout" => "everything", "count" => 0},
{"page_layout" => "news", "count" => 0},
{"page_layout" => "contact", "count" => 0},
{"page_layout" => "footer", "count" => 0},
{"page_layout" => "erb_layout", "count" => 0}
]
end
end
end

0 comments on commit c239df1

Please sign in to comment.