Class: Krikri::QAReport

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/krikri/qa_report.rb

Overview

Represents a QA Report, giving details about a set of records associated with a given provider. Reports are given as structured hashes, containing keys for each registered property in the DPLA::MAP::Aggregation structure.

Reports include two report types, a ‘#field_report` and a `#count_report`.

The ‘#field_report` includes values for each property with the URIs for the `ore:Aggregation` and `edm:isShownAt` (`edm:WebResource`) associated with that field.

The ‘#count_report` includes values for each field, with the count occurances for that value across all records for the provider.

‘QAReports` are saved to the database (with timestamp, etc… via ActiveRecord). More than one report can be associated with each provider.

Reports can also serialize themselves as ‘csv`, via `#field_csv` and `#count_csv`.

@example:

report = QAReport.create(provider: 'moomin_valley')
report.generate_field_report!
report.generate_count_report!

report.field_report['edm:aggregatedCHO->dc:alternative']
=> {"Stonewall Inn Graffiti"=>
   [{:aggregation=>"http://localhost:8983/marmotta/ldp/items/krikri_sample",
     :isShownAt=> "http://digitalcollections.nypl.org/items/12345"}]}

report.count_report['edm:aggregatedCHO->dc:alternative']
=> {"Stonewall Inn Graffiti"=>1}

See Also:

Instance Method Summary collapse

Instance Method Details

#build_providerKrikri::Provider

TODO:

figure out a better relations pattern between ActiveRecord objects and ActiveTriples

Retrieves the provider as an object



138
139
140
# File 'app/models/krikri/qa_report.rb', line 138

def build_provider
  Krikri::Provider.new(:rdf_subject => provider).agent
end

#count_csv(*include_fields) ⇒ CSV::Table



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'app/models/krikri/qa_report.rb', line 106

def count_csv(*include_fields)
  fields = count_report.keys
  fields.select! { |f| include_fields.include? f } unless
    include_fields.empty?

  variables = [:value, :count]
  headers = fields.product(variables).map { |header| header.join(' ') }

  table = CSV::Table.new([CSV::Row.new(headers, [], true)])
  return table if count_report.nil? || count_report.empty?

  rows = []

  count_report.each do |field, hash|
    hash.to_a.each_with_index do |value, i|
      rows[i] ||= CSV::Row.new(headers, [])
      rows[i]["#{field} value"] = value.first.to_s
      rows[i]["#{field} count"] = value.last
    end
  end

  rows.each { |r| table << r }
  table
end

#field_csv(*include_fields) ⇒ CSV::Table



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'app/models/krikri/qa_report.rb', line 73

def field_csv(*include_fields)
  fields = field_report.keys
  fields.select! { |f| include_fields.include? f } unless
    include_fields.empty?

  variables = [:value, :aggregation, :isShownAt]
  headers = fields.product(variables).map { |header| header.join(' ') }

  table = CSV::Table.new([CSV::Row.new(headers, [], true)])
  return table if field_report.nil? || field_report.empty?

  rows = []

  field_report.each do |field, values|
    values.each do |value, agg_list|
      agg_list.each_with_index do |agg_hash, i|
        rows[i] ||= CSV::Row.new(headers, [])
        rows[i]["#{field} value"] = value.to_s
        rows[i]["#{field} aggregation"] = agg_hash[:aggregation].to_s
        rows[i]["#{field} isShownAt"] = agg_hash[:isShownAt].to_s
      end
    end
  end

  rows.each { |r| table << r }
  table
end

#generate_count_report!Hash

Generates and saves the coun treport for the provider, sending SPARQL queries as necessary.



59
60
61
62
63
64
65
66
# File 'app/models/krikri/qa_report.rb', line 59

def generate_count_report!
  report = count_queries.inject({}) do |report_hash, (k, v)|
    report_hash[k] = solutions_to_counts(v.execute)
    report_hash
  end

  update(count_report: report)
end

#generate_field_report!Hash

Generates and saves the field report for the provider, sending SPARQL queries as necessary.



46
47
48
49
50
51
52
# File 'app/models/krikri/qa_report.rb', line 46

def generate_field_report!
  report = field_queries.inject({}) do |report_hash, (k, v)|
    report_hash[k] = solutions_to_hash(v.execute)
    report_hash
  end
  update(field_report: report)
end