class SiteDiff::Report

SiteDiff Report Helper.

Constants

DIFFS_DIR

Directory where diffs will be generated.

FAILURES_FILE

Name of file containing a list of pages with diffs.

REPORT_BUILD_DIR

Name of directory in which to build the portable report.

REPORT_DIR

Name of the portable report directory.

REPORT_FILE_HTML

Name of file containing HTML report of diffs.

REPORT_FILE_JSON

Name of file containing JSON report of diffs.

REPORT_FILE_TAR

Name of file containing exported file archive.

SETTINGS_FILE

Path to settings used for report.

Attributes

cache[R]
results[R]

Public Class Methods

css() click to toggle source

Returns CSS for HTML report.

# File lib/sitediff/report.rb, line 224
def self.css
  output = ''
  output += File.read(File.join(SiteDiff::FILES_DIR, 'normalize.css'))
  output += File.read(File.join(SiteDiff::FILES_DIR, 'sitediff.css'))
  output
end
js() click to toggle source

Returns JS for HTML report.

# File lib/sitediff/report.rb, line 233
def self.js
  output = ''
  output += File.read(File.join(SiteDiff::FILES_DIR, 'jquery.min.js'))
  output += File.read(File.join(SiteDiff::FILES_DIR, 'sitediff.js'))
  output
end
new(config, cache, results) click to toggle source

Creates a Reporter object.

@param [Config] config. @param [Cache] cache. @param [Array] results.

# File lib/sitediff/report.rb, line 54
def initialize(config, cache, results)
  @config = config
  @cache = cache
  @results = results
end

Public Instance Methods

generate_html( dir, report_before = nil, report_after = nil ) click to toggle source

Generates an HTML report.

@param [String] dir

The directory in which the report is to be generated.
# File lib/sitediff/report.rb, line 65
def generate_html(
  dir,
  report_before = nil,
  report_after = nil
)
  report_before ||= @config.before_url
  report_after ||= @config.after_url
  @config.before_time = get_timestamp(:before)
  @config.after_time = get_timestamp(:after)

  dir = SiteDiff.ensure_dir dir

  write_diffs dir
  write_failures dir

  # Prepare report.
  report = Diff.generate_html(
    @results,
    report_before,
    report_after,
    @cache,
    @config
  )

  # Write report.
  report_file = dir + REPORT_FILE_HTML
  report_file.unlink if report_file.file?
  report_file.open('w') { |f| f.write(report) }

  write_settings dir, report_before, report_after

  if @config.export
    package_report(dir)
  else
    SiteDiff.log 'Report generated to ' + report_file.expand_path.to_s
  end
end
generate_json(dir) click to toggle source

Generates a JSON report.

@param dir

The directory in which the report is to be generated.
# File lib/sitediff/report.rb, line 108
def generate_json(dir)
  dir = SiteDiff.ensure_dir dir
  write_diffs dir
  write_failures dir

  # Prepare report.
  report = {
    paths_compared: @results.length,
    paths_diffs: 0,
    paths: {}
  }
  @results.each do |item|
    report[:paths_diffs] += 1 unless item.success?

    item_report = {
      path: item.path,
      status: item.status,
      message: item.error
    }
    report[:paths][item.path] = item_report
  end
  report = JSON report

  # Write report.
  report_file = dir + REPORT_FILE_JSON
  report_file.unlink if report_file.file?
  report_file.open('w') { |f| f.write(report) }

  write_settings dir

  SiteDiff.log 'Report generated to ' + report_file.expand_path.to_s
end
package_report(dir) click to toggle source

Package report for export.

# File lib/sitediff/report.rb, line 143
def package_report(dir)
  # Create temporaryreport directories.
  temp_path = dir + REPORT_BUILD_DIR
  temp_path.rmtree if temp_path.directory?
  temp_path.mkpath
  report_path = temp_path + REPORT_DIR
  report_path.mkpath
  files_path = report_path + 'files'
  files_path.mkpath
  diffs_path = dir + DIFFS_DIR

  # Move files to place.
  FileUtils.move(dir + REPORT_FILE_HTML, report_path)
  FileUtils.move(diffs_path, files_path) if diffs_path.directory?

  # Make tar file.
  Dir.chdir(temp_path) do
    Minitar.pack(
      REPORT_DIR,
      Zlib::GzipWriter.new(File.open(REPORT_FILE_TAR, 'wb'))
    )
  end
  FileUtils.move(temp_path + REPORT_FILE_TAR, dir)
  temp_path.rmtree
  SiteDiff.log 'Archived report generated to ' + dir.join(REPORT_FILE_TAR).to_s
end
write_diffs(dir) click to toggle source

Creates diff files in a directory named “diffs”.

If “dir” is /foo/bar, then diffs will be placed in /foo/bar/diffs.

@param [Pathname] dir

The directory in which a "diffs" directory is to be generated.
# File lib/sitediff/report.rb, line 177
def write_diffs(dir)
  raise Exception 'dir must be a Pathname' unless dir.is_a? Pathname

  # Delete existing "diffs" dir, if exists.
  diff_dir = dir + DIFFS_DIR
  diff_dir.rmtree if diff_dir.exist?

  # Write diffs to the diff directory.
  @results.each { |r| r.dump(dir, @config.export) if r.status == Result::STATUS_FAILURE }
  SiteDiff.log "All diff files written to #{diff_dir.expand_path}" unless @config.export
end
write_failures(dir) click to toggle source

Writes paths with diffs into a file.

@param [Pathname] dir

The directory in which the report is to be generated.
# File lib/sitediff/report.rb, line 194
def write_failures(dir)
  raise Exception 'dir must be a Pathname' unless dir.is_a? Pathname

  failures = dir + FAILURES_FILE
  SiteDiff.log "All failures written to #{failures.expand_path}"
  failures.open('w') do |f|
    @results.each { |r| f.puts r.path unless r.success? }
  end
end
write_settings(dir, report_before = nil, report_after = nil) click to toggle source

Creates report settings.yaml file.

TODO: Find a way to avoid having to create this file.

@param [Pathname] dir

The directory in which the report is to be generated.
# File lib/sitediff/report.rb, line 211
def write_settings(dir, report_before = nil, report_after = nil)
  raise Exception 'dir must be a Pathname' unless dir.is_a? Pathname

  settings = {
    'before' => report_before,
    'after' => report_after,
    'cached' => %w[before after]
  }
  dir.+(SETTINGS_FILE).open('w') { |f| YAML.dump(settings, f) }
end

Private Instance Methods

get_timestamp(tag) click to toggle source

Get crawl timestamps

# File lib/sitediff/report.rb, line 243
def get_timestamp(tag)
  timestamp_file = File.join(@config.directory, 'snapshot', tag.to_s, SiteDiff::Cache::TIMESTAMP_FILE)
  if File.exist? timestamp_file
    file = File::Stat.new(timestamp_file)
    time = file.mtime
    time.class == Time ? time.strftime('%Y-%m-%d %H:%M') : ''
  else
    'unknown'
  end
end