mountain-goat 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (24) hide show
  1. data/lib/flotilla.rb +169 -0
  2. data/lib/mountain_goat/controllers/mountain_goat/mountain_goat_controller.rb +11 -1
  3. data/lib/mountain_goat/metric_tracking.rb +2 -1
  4. data/lib/mountain_goat/public/containerbg.png +0 -0
  5. data/lib/mountain_goat/public/dirtyred.png +0 -0
  6. data/lib/mountain_goat/public/dottedblack.png +0 -0
  7. data/lib/mountain_goat/public/dottedblue.png +0 -0
  8. data/lib/mountain_goat/public/jquery.flot.js +2615 -0
  9. data/lib/mountain_goat/public/mg.css +334 -0
  10. data/lib/mountain_goat/public/mg.png +0 -0
  11. data/lib/mountain_goat/public/raster.png +0 -0
  12. data/lib/mountain_goat/version.rb +1 -1
  13. data/lib/mountain_goat/views/mountain_goat/layouts/.tmp_mountain_goat.html.erb.2801~ +53 -0
  14. data/lib/mountain_goat/views/mountain_goat/layouts/mountain_goat.html.erb +16 -21
  15. data/lib/mountain_goat/views/mountain_goat/mountain_goat_converts/.tmp_index.html.erb.67861~ +33 -0
  16. data/lib/mountain_goat/views/mountain_goat/mountain_goat_converts/.tmp_index.html.erb.97796~ +49 -0
  17. data/lib/mountain_goat/views/mountain_goat/mountain_goat_converts/index.html.erb +37 -21
  18. data/lib/mountain_goat/views/mountain_goat/mountain_goat_metric_variants/.tmp_show.html.erb.24381~ +14 -0
  19. data/lib/mountain_goat/views/mountain_goat/mountain_goat_metrics/.tmp_show.html.erb.21427~ +59 -0
  20. data/lib/mountain_goat/views/mountain_goat/mountain_goat_metrics/.tmp_show.html.erb.65988~ +59 -0
  21. data/lib/mountain_goat/views/mountain_goat/mountain_goat_metrics/show.html.erb +49 -36
  22. data/mountain-goat-0.0.3.gem +0 -0
  23. data/mountain-goat-0.0.4.gem +0 -0
  24. metadata +21 -4
data/lib/flotilla.rb ADDED
@@ -0,0 +1,169 @@
1
+ begin
2
+ require 'json'
3
+ rescue LoadError
4
+ p "Flotilla will not work without the 'json' gem"
5
+ end
6
+
7
+ module ActionView
8
+ module Helpers
9
+ module ScriptaculousHelper
10
+
11
+ # Insert a flot chart into the page. <tt>placeholder</tt> should be the
12
+ # name of the div that will hold the chart, <tt>collections</tt> is a hash
13
+ # of legends (as strings) and datasets with options as hashes, <tt>options</tt>
14
+ # contains graph-wide options.
15
+ #
16
+ # Example usage:
17
+ #
18
+ # chart("graph_div", {
19
+ # "January" => { :collection => @january, :x => :day, :y => :sales, :options => { :lines => {:show =>true}} },
20
+ # "February" => { :collection => @february, :x => :day, :y => :sales, :options => { :points => {:show =>true} } },
21
+ # :grid => { :backgroundColor => "#fffaff" })
22
+ #
23
+ # Options:
24
+ # :js_includes - includes flot library inline
25
+ # :js_tags - wraps resulting javascript in javascript tags if true. Defaults to true.
26
+ # :placeholder_tag - appends a placeholder div for graph
27
+ # :placeholder_size - specifys the size of the placeholder div
28
+ def chart(placeholder, series, options = {}, html_options = {})
29
+ html_options.reverse_merge!({ :js_includes => true, :js_tags => true, :placeholder_tag => true, :placeholder_size => "800x300", :pie_hover => false, :pie_hover_absolute => false })
30
+ width, height = html_options[:placeholder_size].split("x") if html_options[:placeholder_size].respond_to?(:split)
31
+ additional_js = get_additional_js(placeholder, html_options)
32
+
33
+ data, x_is_date, y_is_date = series_to_json(series)
34
+ if x_is_date
35
+ options[:xaxis] ||= {}
36
+ options[:xaxis].merge!({ :mode => 'time' })
37
+ end
38
+ if y_is_date
39
+ options[:yaxis] ||= {}
40
+ options[:yaxis].merge!({ :mode => 'time' })
41
+ end
42
+
43
+ if html_options[:js_includes]
44
+ chart_js = <<-EOF
45
+ <script type="text/javascript">
46
+ (function () {
47
+ if (typeof(jQuery) == 'undefined') {
48
+ alert("Please include jQuery to view flot");
49
+ } else {
50
+ if (typeof(jQuery.plot) == 'undefined') {
51
+ $('##{placeholder}').html("<span class='flot-error'>Please include jQuery.plot to view flot</span>");
52
+ } else {
53
+ var plot = jQuery.plot($('##{placeholder}'), #{data}, #{options.to_json});
54
+ #{additional_js}
55
+ }
56
+ }
57
+ })();
58
+ </script>
59
+ EOF
60
+ else
61
+ chart_js = <<-EOF
62
+ (function () {
63
+ if (typeof(jQuery) == 'undefined') {
64
+ alert("Please include jQuery to view flot");
65
+ } else {
66
+ if (typeof(jQuery.plot) == 'undefined') {
67
+ $('##{placeholder}').html("<span class='flot-error'>Please include jQuery.plot to view flot</span>");
68
+ } else {
69
+ var plot = jQuery.plot($('##{placeholder}'), #{data}, #{options.to_json});
70
+ #{additional_js}
71
+ }
72
+ }
73
+ })();
74
+ EOF
75
+ end
76
+
77
+ html_options[:js_tags] ? javascript_tag(chart_js) : chart_js
78
+ output = html_options[:placeholder_tag] ? content_tag(:div, nil, :id => placeholder, :style => "width:#{width}px;height:#{height}px;", :class => "chart") + chart_js : chart_js
79
+ output.html_safe
80
+ end
81
+
82
+ private
83
+ def series_to_json(series)
84
+ data_sets = []
85
+ x_is_date, y_is_date = false, false
86
+ series.each do |name, values|
87
+ set, data = {}, []
88
+ set[:label] = name
89
+ first = values[:collection].first
90
+ logger.warn "Collection is: #{values[:collection].inspect}"
91
+ if first #&& !values[:collection].is_a?(Array)
92
+ if first.is_a?(Hash)
93
+ x_is_date = first[values[:x]].acts_like?(:date) || first[values[:x]].acts_like?(:time)
94
+ y_is_date = first[values[:y]].acts_like?(:date) || first[values[:y]].acts_like?(:time)
95
+ else
96
+ x_is_date = first.send(values[:x]).acts_like?(:date) || first.send(values[:x]).acts_like?(:time)
97
+ y_is_date = first.send(values[:y]).acts_like?(:date) || first.send(values[:y]).acts_like?(:time)
98
+ end
99
+ end
100
+ values[:collection].each do |object|
101
+ if values[:collection].is_a?(Hash) || object.is_a?(Hash)
102
+ x_value, y_value = object[values[:x]], object[values[:y]]
103
+ #logger.warn "A: Object is: #{object}, x,y: #{[x_value, y_value]} from #{[values[:x], values[:y]]}"
104
+ else
105
+ x_value, y_value = object.send(values[:x]), object.send(values[:y])
106
+ #logger.warn "B: Object is: #{object}, x,y: #{[x_value, y_value]}"
107
+ end
108
+ x = x_is_date ? x_value.to_time.to_i * 1000 : x_value.to_f
109
+ y = y_is_date ? y_value.to_time.to_i * 1000 : y_value.to_f
110
+ #logger.warn "Tally x,y: #{[x, y]}"
111
+ data << [x,y]
112
+ end
113
+ set[:data] = data
114
+ values[:options].each {|option, parameters| set[option] = parameters } if values[:options]
115
+ data_sets << set
116
+ end
117
+ return data_sets.to_json, x_is_date, y_is_date
118
+ end
119
+
120
+ def get_additional_js(placeholder, options)
121
+ res = ""
122
+ if options[:pie_hover]
123
+ res << <<-EOF
124
+ function showTooltip(x, y, contents) {
125
+ $('<div id="tooltip">' + contents + '</div>').css( {
126
+ position: 'absolute',
127
+ //display: 'none',
128
+ top: y + 5,
129
+ left: x + 5,
130
+ border: '1px solid #fdd',
131
+ padding: '2px',
132
+ 'background-color': '#fee',
133
+ opacity: 0.80
134
+ }).appendTo("body");//.fadeIn(200);
135
+ }
136
+
137
+ var previousPoint = null;
138
+ $('##{placeholder}').bind('mouseout', function() {
139
+ plot.unhighlight();
140
+ $("#tooltip").remove();
141
+ $(this).data('previous-post', -1);
142
+ });
143
+
144
+ $('##{placeholder}').bind('plothover', function(event, pos, item) {
145
+ if (item) {
146
+ if ($(this).data('previous-post') != item.seriesIndex) {
147
+ plot.unhighlight();
148
+ plot.highlight(item.series, item.datapoint);
149
+ $(this).data('previous-post', item.seriesIndex);
150
+ }
151
+ $("#tooltip").remove();
152
+ #{ options[:line_hover_absolute] ? "y = 'on ' + (new Date(item.datapoint[0])).toDateString() + ': ' + item.datapoint[1] + ' #{options[:item_title] || ''}'" : !options[:pie_hover_absolute] ? "y = item.datapoint[0].toFixed(0) + '%';" : "y = item.datapoint[1][0][1] + ' #{options[:item_title] || ''}'" }
153
+ showTooltip(pos.pageX, pos.pageY, item.series.label + " " + y);
154
+ } else {
155
+ //console.log('unhighlight (3)');
156
+ plot.unhighlight();
157
+ $("#tooltip").remove();
158
+ previousPost = $(this).data('previous-post', -1);
159
+ }
160
+ });
161
+ EOF
162
+ end
163
+
164
+ res
165
+ end
166
+ end
167
+ end
168
+ end
169
+
@@ -3,9 +3,19 @@ class MountainGoatController < ActionController::Base
3
3
 
4
4
  self.prepend_view_path File.join([File.dirname(__FILE__), '../../views/mountain_goat/'])
5
5
 
6
+
6
7
  def fetch
8
+ ct = { :png => 'image/png', :css => 'text/css', :html => 'text/html', :js => 'text/javascript' }
9
+
7
10
  Dir.open(File.join([File.dirname(__FILE__), '../../public/'])).each do |file|
8
- render :file => File.join([File.dirname(__FILE__), '../../public/', file]) and return if file == params[:file].gsub('_','.')
11
+ if file == params[:file].gsub('_','.')
12
+ if file =~ /[.]([a-z0-9]+)$/
13
+ response.headers['Content-Type'] = ct[$1.to_sym]
14
+ end
15
+ response.headers['Content-Disposition'] = 'inline'
16
+ render :text => open(File.join([File.dirname(__FILE__), '../../public/', file]), "rb").read
17
+ return
18
+ end
9
19
  end
10
20
 
11
21
  render :file => "#{Rails.root}/public/404.html", :status => :not_found
@@ -2,6 +2,7 @@ require File.join([File.dirname(__FILE__), 'switch_variant'])
2
2
 
3
3
  module MetricTracking
4
4
 
5
+ #def rand =
5
6
  #Metric Tracking routes
6
7
  class << ActionController::Routing::Routes;self;end.class_eval do
7
8
  define_method :clear!, lambda {}
@@ -13,7 +14,6 @@ module MetricTracking
13
14
  map.resources :mountain_goat_metric_variants
14
15
  map.resources :mountain_goat_converts, :has_many => :mountain_goat_metrics
15
16
  map.resources :mountain_goat_metrics, :has_many => :mountain_goat_metric_variants
16
- map.resources :mountain_goat_upgrade_orders
17
17
  map.fresh_metrics '/fresh-metrics', :controller => :mountain_goat_metrics, :action => :fresh_metrics
18
18
  map.connect '/mg/public/:file', :controller => :mountain_goat, :action => :fetch
19
19
  end
@@ -90,6 +90,7 @@ module MetricTracking
90
90
  #logger.warn "Value: #{metric_variant_sym} - #{variant_id}"
91
91
 
92
92
  if variant_id.blank? #the user just doesn't have this set
93
+ logger.error "No variant found for #{metric.title}"
93
94
  next
94
95
  end
95
96