Module: GoogleOtg

Includes:
HTTParty
Defined in:
lib/google_otg.rb

Constant Summary collapse

@@DEFAULT_INCREMENT =

1 day

1

Instance Method Summary collapse

Instance Method Details

#date_rangeObject



11
12
13
14
# File 'lib/google_otg.rb', line 11

def date_range
  tr = self.setup_time_range
  tr[:lower_bound].strftime("%m/%d/%Y") + " - " + tr[:upper_bound].strftime("%m/%d/%Y")
end

#grouped_query(class_to_query, args = []) ⇒ Object



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
# File 'lib/google_otg.rb', line 82

def grouped_query(class_to_query, args = [])

  date_field = args.has_key?(:date_field) ? args[:date_field] : "created_at"
  where_args = args.has_key?(:conditions) ? args[:conditions] : ["TRUE"]
  where_query = where_args.shift

  tr = self.setup_time_range
  return class_to_query.find_by_sql(["
        SELECT DAYOFYEAR(TIMESTAMPADD(SECOND, ?, #{date_field})) as d,
            DATE(TIMESTAMPADD(SECOND, ?, #{date_field})) as created_at,
            count(*) as count
        FROM #{class_to_query.table_name}
        WHERE #{where_query}

        AND #{date_field} >= TIMESTAMPADD(SECOND, -1 * ?, DATE(?))
        AND #{date_field} <= TIMESTAMPADD(SECOND, -1 * ?, DATE(?)) + INTERVAL 1 DAY

        GROUP BY d
        ORDER BY created_at 
    ", tr[:time_zone].utc_offset, 
       tr[:time_zone].utc_offset, 
       where_args, 
       tr[:time_zone].utc_offset, 
       tr[:lower_bound].strftime("%Y-%m-%d"), 
       tr[:time_zone].utc_offset, 
       tr[:upper_bound].strftime("%Y-%m-%d")].flatten)      
end

#over_time_graph(hits, args = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/google_otg.rb', line 16

def over_time_graph(hits, args = {})
    tr = self.setup_time_range
    args[:time_zone] = tr[:time_zone] unless args.has_key?(:time_zone)
    args[:range] = tr[:range] unless args.has_key?(:range)      

    height = args.has_key?(:height) ? args[:height] : 125
    src = args.has_key?(:src) ? args[:src] : "http://www.google.com/analytics/static/flash/OverTimeGraph.swf"

    if hits.is_a?(Array) and hits[0].is_a?(Array)
        range = hits.map{|h| hits_to_otg_range(h, args) }
    else
        range = [hits_to_otg_range(hits, args)]
    end
    vars = range_to_flashvars(range)

    html = <<-eos
    <embed width="100%" height="#{height}"
    wmode="opaque" salign="tl" scale="noScale" quality="high" bgcolor="#FFFFFF"
    flashvars="input=#{vars}" 
    pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" 
    src="#{src}"/>
    eos

    return html

end

#over_time_graph_download(results, type, title, legend) ⇒ Object



43
44
45
46
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
# File 'lib/google_otg.rb', line 43

def over_time_graph_download(results, type, title, legend)
  tr = self.setup_time_range

  if type == :csv
    csv_data = data_to_csv(results,
            :legend => legend,
            :x_label_format => "%m/%d/%Y",
            :time_zone => tr[:time_zone],
            :range => tr[:range])

    time_label = tr[:time_zone].now.strftime("%Y-%m-%d")
    file_data_type = legend.join(" ").downcase.gsub(" ", "_")
    outfile = "#{request.env['HTTP_HOST']}-#{file_data_type}_#{time_label}.csv"

    send_data csv_data,
        :type => 'text/csv; charset=iso-8859-1; header=present',
        :disposition => "attachment; filename=#{outfile}"        
  else
    graph_url = google_line_graph(
        results ,
        :x_label_format => "%a, %b %d",
        :time_zone => tr[:time_zone],
        :title => title,
        :range => tr[:range],
        :max_x_label_count => 4,
        :legend => legend)

    if params.has_key?(:send_file)
      send_data HTTParty::get(graph_url), :filename => "graph.png"        

    else
      url = url_for({:send_file => 1, 
        :controller => controller_name,
        :action => action_name}.merge(params))
      render :inline => "<img src='#{url}'/>"
    end
  end
end