Class: Stats::SourceReport

Inherits:
EventReport show all
Defined in:
lib/stats/source_report.rb

Constant Summary

Constants inherited from EventReport

EventReport::AMMAP_COUNTRIES, EventReport::MinimumMapBubbleSize, EventReport::SUPPRESSED_LABELS

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from EventReport

calculate_bubble_size, #get_start_date, get_url, make_title_value, #set_date, to_ammap_country_data, to_ammap_metro_data, zoom_levels

Methods inherited from Report

#==, #[], array_to_csv, array_to_xml, csv_labels, format_csv_value, pad_reports, set_attributes, set_numeric_attributes, strip_label

Constructor Details

#initialize(options = {}) ⇒ SourceReport

Returns a new instance of SourceReport.



7
8
9
10
11
12
13
14
# File 'lib/stats/source_report.rb', line 7

def initialize(options = {})
  @label           = options[:label]
  @publish_type    = options[:publish_type]
  @publishes       = options[:publishes] || 0
  @query_time      = nil
  @duration        = options[:duration].to_f || 0
  @duration_min    = ('%.2f' % (@duration / 60.seconds.to_f)).to_f
end

Class Method Details

.csv_keysObject



16
17
18
# File 'lib/stats/source_report.rb', line 16

def self.csv_keys
  [ :publish_type, :publishes, :duration ]
end

.to_amcharts(reports, show_date, show_date_range, options = {}) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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
100
101
102
103
104
105
# File 'lib/stats/source_report.rb', line 47

def self.to_amcharts(reports, show_date, show_date_range, options = {})
  complete_labels = []
  durations       = []
  guids           = []
  labels          = []
  publishes       = []
  reports         = pad_reports(reports, options) if options["pad"]
  reports         = reports.reverse if options[:reverse]

  reports.each do |report|
    label = report.publish_type
    label = '<blank>' if 'blank' == label

    complete_labels << label
    labels          << label.previewize((options[:max_label_length] || 15).to_i)
    durations       << report.duration_min
    publishes       << report.publishes
    guids           << report.id
  end

  show_screenshots = reports.length <= 10

  max_labels = 20
  # frequency == 1 if we can display all labels, or a number greater than 1 if we can't
  frequency = (labels.length / max_labels.to_f).ceil

  xml = options[:builder] ||= Builder::XmlMarkup.new(:indent => 2)
  xml.instruct! :xml, :version=>"1.0", :encoding=>"UTF-8"
  xml.chart do
    xml.series do
      labels.each_with_index do |value, index|
        # only display every nth value where n depends the total
        # number of labels and the max labels we are allowed to
        # display, otherwise clear out the value
        value = "" if index % frequency != 0
        xml.value value, :xid => index
      end
    end

    xml.graphs do
      #the gid is used in the settings file to set different settings just for this graph
      unless options[:show_uploads] == 'false'
        xml.graph :gid => 'uploads' do
          publishes.each_with_index do |value, index|
            xml.value value, :xid => index
          end
        end
      end

      unless options[:show_duration_uploaded] == 'false'
        xml.graph :gid => 'duration_uploaded' do
          durations.each_with_index do |value, index|
            xml.value value, :xid => index
          end
        end
      end
    end
  end
end

Instance Method Details

#to_csv(options = {}) ⇒ Object



20
21
22
23
24
25
# File 'lib/stats/source_report.rb', line 20

def to_csv(options = {})
  the_csv_keys = strip_keys_by_label(self.class.csv_keys, @label)
  the_csv_keys.map do |k| 
    self.class.format_csv_value(send(k))
  end.join(',')
end

#to_json(options = {}) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/stats/source_report.rb', line 38

def to_json(options = {})
  report                 = HashWithIndifferentAccess.new
  report['publish_type'] = @publish_type
  report['publishes']    = @publishes
  report['duration']     = @duration

  report.to_json
end

#to_xml(options = {}) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/stats/source_report.rb', line 27

def to_xml options = {}
  xml = options[:builder] ||= Builder::XmlMarkup.new(:indent => options[:indent])
  xml.instruct! unless options[:skip_instruct]

  xml.__send__(@label) do
    xml.publish_type @publish_type
    xml.publishes    @publishes
    xml.duration     @duration
  end
end