Class: Stats::PublishReport

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

Constant Summary

Constants inherited from EventReport

EventReport::AMMAP_COUNTRIES, EventReport::MinimumMapBubbleSize, EventReport::PRECISION_DURATION, EventReport::PRECISION_GB, EventReport::SUPPRESSED_LABELS

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from EventReport

calculate_bubble_size, format_duration, format_float, format_gb, #get_start_date, get_url, make_title_value, #set_date, to_ammap_country_data, to_ammap_metro_data, #to_csv, 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, #the_csv_keys, #to_csv

Constructor Details

#initialize(options = {}) ⇒ PublishReport

Returns a new instance of PublishReport.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/stats/publish_report.rb', line 7

def initialize(options = {})
  @start_date = options[:date]

  if 'day' == options[:group]
    @date     = options[:date]
  else
    @end_date = options[:date].advance(options[:advance_by] => 1).advance(:days => -1)
  end

  @label            = options[:label]

  @assets_uploaded    = options[:assets_uploaded].to_i
  @assets_encoded   = options[:assets_encoded].to_i

  @query_time       = nil

  @bytes_uploaded     = options[:bytes_uploaded].to_i
  @bytes_uploaded_gb  = self.class.format_gb(options[:bytes_uploaded].to_i / 1.gigabyte.to_f)

  @bytes_encoded    = options[:bytes_encoded].to_i
  @bytes_encoded_gb = self.class.format_gb(options[:bytes_encoded].to_i / 1.gigabyte.to_f)

  @duration_uploaded    = self.class.format_duration(options[:duration_uploaded].to_f)
  @duration_uploaded_min = self.class.format_duration(options[:duration_uploaded].to_f / 60.seconds.to_f)

  @duration_encoded    = self.class.format_duration(options[:duration_encoded].to_f)
  @duration_encoded_min = self.class.format_duration(options[:duration_encoded].to_f / 60.seconds.to_f)
end

Class Method Details

.csv_keysObject



36
37
38
# File 'lib/stats/publish_report.rb', line 36

def self.csv_keys
  [ :assets_encoded, :assets_uploaded, :bytes_encoded, :bytes_uploaded, :duration_uploaded, :duration_encoded, :date, :start_date, :end_date ]
end

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



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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/stats/publish_report.rb', line 72

def self.to_amcharts(reports, show_date, show_date_range, options = {})
  assets_encoded   = []
  assets_uploaded    = []
  bytes_encoded    = []
  bytes_uploaded     = []
  complete_labels  = []
  duration_encoded = []
  duration_uploaded  = []
  guids            = []
  labels           = []
  urls             = []
  reports         = reports.reverse if options[:reverse]

  reports.each do |report|
    label = case options[:group]
            when 'daily'   ; "#{report.date.month}/#{report.date.day}"
            when 'weekly'  ; "#{report.start_date.month}/#{report.start_date.day}-#{report.end_date.month}/#{report.end_date.day}"
            when 'monthly' ; "#{report.start_date.strftime("%b \'%y")}"
            else raise "invalid group: #{options[:group]}"
            end

    complete_labels  << label
    labels           << label.previewize((options[:max_label_length] || 15).to_i)
    assets_uploaded    << report.assets_uploaded
    duration_uploaded  << report.duration_uploaded_min
    bytes_uploaded     << report.bytes_uploaded_gb
    assets_encoded   << report.assets_encoded
    duration_encoded << report.duration_encoded_min
    bytes_encoded    << report.bytes_encoded_gb
    guids            << report.id
  end

  show_screenshots = reports.length <= 10

  max_labels = 40
  # frequency == 1 if we can display all labels, or a number greater
  # than 1 if we can't
  frequency = (show_date || show_date_range) ? 1 : (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 => 'assets_uploaded' do
          assets_uploaded.each_with_index do |value, index|
            if value != 0 || show_date || show_date_range
              xml.value value, :xid => index
            end
          end
        end
      end

      unless options[:show_duration_uploaded] == 'false'
        xml.graph :gid => 'duration_uploaded' do
          duration_uploaded.each_with_index do |value, index|
            if value != 0 || show_date || show_date_range
              xml.value value, :xid => index
            end
          end
        end
      end

      unless options[:show_encodes] == 'false'
        xml.graph :gid => 'assets_encoded' do
          assets_encoded.each_with_index do |value, index|
            if value != 0 || show_date || show_date_range
              xml.value value, :xid => index
            end
          end
        end
      end
      
      unless options[:show_duration_encoded] == 'false'
        xml.graph :gid => 'duration_encoded' do
          duration_encoded.each_with_index do |value, index|
            if value != 0 || show_date || show_date_range
              xml.value value, :xid => index
            end
          end
        end
      end
    end
  end
end

Instance Method Details

#to_json(options = {}) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/stats/publish_report.rb', line 57

def to_json(options = {})
  report               = HashWithIndifferentAccess.new
  report['assets_encoded']   = @assets_encoded
  report['assets_uploaded']    = @assets_uploaded
  report['bytes_encoded']    = @bytes_encoded
  report['bytes_uploaded']     = @bytes_uploaded
  report['date']             = pretty_time(@date)       if @date
  report['duration_encoded'] = @duration_encoded
  report['duration_uploaded']  = @duration_uploaded
  report['end_date']         = pretty_time(@end_date)   if @end_date
  report['start_date']       = pretty_time(@start_date) if @start_date

  report.to_json
end

#to_xml(options = {}) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/stats/publish_report.rb', line 40

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

  xml.__send__(@label) do
    xml.assets_encoded @assets_encoded
    xml.assets_uploaded @assets_uploaded
    xml.bytes_encoded @bytes_encoded
    xml.bytes_uploaded  @bytes_uploaded
    xml.date(pretty_time(@date))             if @date
    xml.duration_encoded @duration_encoded
    xml.duration_uploaded  @duration_uploaded
    xml.end_date(pretty_time(@end_date))     if @end_date
    xml.start_date(pretty_time(@start_date)) if @start_date
  end
end