Skip to content
This repository has been archived by the owner on Nov 28, 2024. It is now read-only.

Move summary model to PostgreSQL #605

Draft
wants to merge 1 commit into
base: big-postgres-refactor
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 0 additions & 48 deletions app/models/legacy/summary.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,52 +25,4 @@ class Legacy::Summary

embeds_one :level_summary
embeds_one :category_summary

def self.generate
summary = Legacy::Summary.create

validations = Legacy::Validation.where(:url.ne => nil).order_by(:created_at.desc)
# retrieve validations from Mongo Datastore, ordered in reverse by date created

summary.sources = validations.length
summary.states = {"warnings"=>0, "valid"=>0, "not_found"=>0, "invalid"=>0}
summary.hosts = Hash.new 0
summary.create_level_summary( errors_breakdown: Hash.new(0), warnings_breakdown: Hash.new(0), info_messages_breakdown: Hash.new(0) )
summary.create_category_summary( structure_breakdown: Hash.new(0), schema_breakdown: Hash.new(0), context_breakdown: Hash.new(0) )

validations.each do |validation|
summary.states[validation.state] += 1
host = source_host(validation.url)
summary.hosts[host] += 1 unless host.nil?
validator = validation.validator
messages = []
[:errors, :warnings, :info_messages].each do |level|
unless validator.send(level).nil?
messages = messages + validator.send(level)
validator.send(level).uniq { |m| m.type }.each do |msg|
summary.level_summary.send("#{level}_breakdown".to_sym)[ msg.type ] += 1
end
end
end
[:structure, :schema, :context].each do |category|
messages.reject {|m| m.category != category }.uniq { |m| m.type }.each do |msg|
summary.category_summary.send("#{category}_breakdown".to_sym)[ msg.type ] += 1
end
end
end
summary.save
summary
end

private

def self.source_host(url)
host = URI.parse(url.to_s).host
return if host.nil?
host.downcase!
host = host.start_with?('www.') ? host[4..-1] : host
#TODO better option?
host.gsub(".", "\uff0e")
end

end
52 changes: 52 additions & 0 deletions app/services/summary_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
class SummaryGenerator

def initialize()
end

def call
summary = Legacy::Summary.create

validations = Legacy::Validation.where(:url.ne => nil).order_by(:created_at.desc)
# retrieve validations from Mongo Datastore, ordered in reverse by date created

summary.sources = validations.length
summary.states = {"warnings"=>0, "valid"=>0, "not_found"=>0, "invalid"=>0}
summary.hosts = Hash.new 0
summary.create_level_summary( errors_breakdown: Hash.new(0), warnings_breakdown: Hash.new(0), info_messages_breakdown: Hash.new(0) )
summary.create_category_summary( structure_breakdown: Hash.new(0), schema_breakdown: Hash.new(0), context_breakdown: Hash.new(0) )

validations.each do |validation|
summary.states[validation.state] += 1
host = source_host(validation.url)
summary.hosts[host] += 1 unless host.nil?
validator = validation.validator
messages = []
[:errors, :warnings, :info_messages].each do |level|
unless validator.send(level).nil?
messages = messages + validator.send(level)
validator.send(level).uniq { |m| m.type }.each do |msg|
summary.level_summary.send("#{level}_breakdown".to_sym)[ msg.type ] += 1
end
end
end
[:structure, :schema, :context].each do |category|
messages.reject {|m| m.category != category }.uniq { |m| m.type }.each do |msg|
summary.category_summary.send("#{category}_breakdown".to_sym)[ msg.type ] += 1
end
end
end
summary.save!
end

private

def source_host(url)
host = URI.parse(url.to_s).host
return if host.nil?
host.downcase!
host = host.start_with?('www.') ? host[4..-1] : host
#TODO better option?
host.gsub(".", "\uff0e")
end

end
9 changes: 1 addition & 8 deletions lib/tasks/summary.rake
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
namespace :summary do
desc "Generate validation summary"
task :generate => :environment do
GenerateSummary.perform
end
end

class GenerateSummary
def self.perform
summary = Summary.generate
summary.save!
SummaryGenerator.new.call
end
end