Skip to content

Commit

Permalink
Add task to return statistics for future changes to a given path.
Browse files Browse the repository at this point in the history
  • Loading branch information
KludgeKML committed Aug 2, 2023
1 parent 6f2e152 commit e534076
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 39 deletions.
19 changes: 19 additions & 0 deletions lib/reports/concerns/notification_stats.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module Reports::Concerns::NotificationStats
def list_names_array(lists)
lists.collect(&:title)
end

def list_stats_array(lists)
total_subs = lists.sum { |l| l.subscriptions.active.count }
immediately_subs = lists.sum { |l| l.subscriptions.active.immediately.count }
daily_subs = lists.sum { |l| l.subscriptions.active.daily.count }
weekly_subs = lists.sum { |l| l.subscriptions.active.weekly.count }

[
"notified immediately: #{immediately_subs}",
"notified next day: #{daily_subs}",
"notified at weekend: #{weekly_subs}",
"notified total: #{total_subs}",
]
end
end
35 changes: 0 additions & 35 deletions lib/reports/content_change_statistics_report.rb

This file was deleted.

98 changes: 98 additions & 0 deletions lib/reports/future_content_change_statistics_report.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
require "reports/concerns/notification_stats"

class Reports::FutureContentChangeStatisticsReport
include Reports::Concerns::NotificationStats

attr_reader :govuk_path, :draft

def initialize(govuk_path, draft)
@govuk_path = govuk_path
@draft = draft
end

def call
content_item = content_store_client.content_item(govuk_path).to_hash

lists = SubscriberListQuery.new(
content_id: content_item["content_id"],
tags: tags_from_content_item(content_item),
links: links_from_content_item(content_item),
document_type: content_item["document_type"],
email_document_supertype: supertypes(content_item)["email_document_supertype"],
government_document_supertype: supertypes(content_item)["government_document_supertype"],
).lists

output_string = change_messages.join

list_names_array(lists).each { |ln| output_string += " - #{ln}\n" }

output_string += "\nResulting in:\n"

list_stats_array(lists).each { |ls| output_string += " - #{ls}\n" }

output_string
end

def change_messages
if draft
[
"Publishing the drafted changes to #{govuk_path} will trigger alerts on these lists:\n",
"(NB: publishing as a minor change will not trigger alerts)",
]
else
[
"Publishing major changes to the information on #{govuk_path} will trigger alerts on these lists:\n",
"(NB: If major changes involve changes to the taxons/links/etc these lists will change)\n",
]
end
end

def content_store_client
return GdsApi.content_store unless draft

# We don't appear to need a token for the #content_item method?
GdsApi::ContentStore.new(Plek.find("draft-content-store"), bearer_token: "")
end

def supertypes(content_item)
GovukDocumentTypes.supertypes(document_type: content_item["document_type"])
end

def tags_from_content_item(content_item)
content_item["details"]["tags"].merge(additional_items(content_item))
end

def links_from_content_item(content_item)
compressed_links(content_item).merge(additional_items(content_item).merge("taxon_tree" => taxon_tree(content_item)))
end

def compressed_links(content_item)
keys = content_item["links"].keys - %w[available_translations taxons]
compressed_links = {}
keys.each do |k|
compressed_links[k] = content_item["links"][k].collect { |i| i["content_id"] }
end
compressed_links
end

def additional_items(content_item)
{ "content_store_document_type" => content_item["document_type"] }.merge(supertypes(content_item))
end

def taxon_tree(content_item)
[content_item["links"]["taxons"].first["content_id"]] + get_parent_links(content_item["links"]["taxons"].first)
end

def get_parent_links(taxon_struct)
return [] unless taxon_struct["links"].key?("parent_taxons")
return [] unless taxon_struct["links"]["parent_taxons"].any?

tree = []
taxon_struct["links"]["parent_taxons"].each do |parent_taxon|
tree += [parent_taxon["content_id"]]
tree += get_parent_links(parent_taxon)
end

tree
end
end
31 changes: 31 additions & 0 deletions lib/reports/historical_content_change_statistics_report.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require "reports/concerns/notification_stats"

class Reports::HistoricalContentChangeStatisticsReport
include Reports::Concerns::NotificationStats

attr_reader :govuk_path

def initialize(govuk_path)
@govuk_path = govuk_path
end

def call
content_changes = ContentChange.where(base_path: govuk_path).order(:public_updated_at)

if content_changes.any?
output_string = "#{content_changes.count} content changes registered for #{govuk_path}.\n\n"

content_changes.each do |cc|
output_string += "Content change on #{cc.public_updated_at}:\n"

lists = SubscriberListQuery.new(content_id: cc.content_id, tags: cc.tags, links: cc.links, document_type: cc.document_type, email_document_supertype: cc.email_document_supertype, government_document_supertype: cc.government_document_supertype).lists

list_stats_array(lists).each { |ls| output_string += " - #{ls}\n" }
end

output_string
else
"No content changes registered for path: #{govuk_path}"
end
end
end
16 changes: 12 additions & 4 deletions lib/tasks/report.rake
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,18 @@ namespace :report do
puts Reports::SinglePageNotificationsReport.new(args[:limit] || 25).call.join("\n")
end

desc "Output content-change information for a page URL"
task :content_change_statistics, %i[url] => :environment do |_t, args|
puts Reports::ContentChangeStatisticsReport.new(
args.fetch(:url),
desc "Output how many people were notified for recent content changes to the page at the given path"
task :historical_content_change_statistics, %i[govuk_path] => :environment do |_t, args|
puts Reports::HistoricalContentChangeStatisticsReport.new(
args.fetch(:govuk_path),
).call
end

desc "Output how many people would be notified if a major change were published at the given path"
task :future_content_change_statistics, %i[govuk_path draft] => :environment do |_t, args|
puts Reports::FutureContentChangeStatisticsReport.new(
args.fetch(:govuk_path),
args.fetch(:draft).downcase == "true",
).call
end
end

0 comments on commit e534076

Please sign in to comment.