metrify 0.3.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -116,10 +116,6 @@ Example Controller:
116
116
  private
117
117
  def metrified_class
118
118
  @metrified_class || @metrified_class = MetricsClass.new
119
- end
120
-
121
- def set_classname
122
- @classname || @classname = Dashboard.name.downcase
123
119
  end
124
120
 
125
121
  end
@@ -131,6 +127,33 @@ Example View for graph:
131
127
 
132
128
  </style>
133
129
  <%= render :partial => "metrify/graph" %>
130
+
131
+ Depending on the jscript framework you are using, you will also want to define an new instance of Breakdown, defining the Ajax method appropriately:
132
+
133
+ Prototype:
134
+ <script type="text/javascript" charset="utf-8">
135
+ window.breakdown = new Breakdown(function(url, complete_fn){
136
+
137
+ new Ajax.Request(url, {evalJSON : 'force', onComplete : function(text){
138
+ complete_fn(text.responseJSON);
139
+ }})
140
+ });
141
+
142
+ document.observe("dom:loaded", function(){window.breakdown.create_new_chart();});
143
+
144
+ </script>
145
+
146
+ Mootools:
147
+
148
+ <script type="text/javascript" charset="utf-8">
149
+ window.breakdown = new Breakdown(function(url, complete_fn){
150
+ new Request({url : url, onSuccess : function(text){
151
+ complete_fn(text);
152
+ }}).get();
153
+ });
154
+
155
+ window.addEvent('domready', function(){window.breakdown.create_new_chart();});
156
+ </script>
134
157
 
135
158
  Example View for chart:
136
159
 
@@ -31,13 +31,17 @@ module MetrifyController
31
31
  def graph_stats
32
32
  prepare_for_graph
33
33
 
34
- @stat_names = params[:stat_names] || metrified_class.stat_names
34
+ @stat_names = parsed_stat_names
35
35
  end
36
36
 
37
+ def parsed_stat_names
38
+ !params[:stat_names].blank? ? params[:stat_names].split(',') : metrified_class.stat_names
39
+ end
40
+
37
41
  # chart_data.json?filters[type][]=letters&filters[type][]=animals&filters[furriness][]=not_furry
38
42
  def chart_data
39
43
  @metrify = metrified_class
40
- @stat_names = metrified_class.stat_names(params[:filters])
44
+ @stat_names = metrified_class.stat_names(parsed_stat_names, params[:filters])
41
45
  @unit = params[:unit] || unit
42
46
 
43
47
  @number_of_stats = params[:number_of_stats] || number_of_stats
@@ -25,7 +25,7 @@ module MetrifyHelper
25
25
 
26
26
  def print_stat_value(stat, stat_name, metrify, previous_stat = nil)
27
27
  val = stat.send(stat_name)
28
- val = number_with_precision(val, :precision => metrify.value_precision(stat_name)) if (metrify.value_precision(stat_name))
28
+ val = number_with_precision(val, :precision => metrify.value_precision(stat_name)) if metrify.value_precision(stat_name)
29
29
  if metrify.value_type(stat_name) == "currency"
30
30
  str = number_to_currency(val)
31
31
  else
@@ -1,41 +1,69 @@
1
1
  <script type="text/javascript" charset="utf-8">
2
- Breakdown = Class.create({
3
- initialize : function(series_data){
4
- this.get_chart(series_data);
5
- },
6
- get_chart : function(data){
7
- if(this.chart){ return this.chart; }
8
- this.chart = new Highcharts.Chart({
9
- chart: {
10
- renderTo: "stat_chart"
11
- },
12
- title: {
13
- text: "All Stats"
14
- },
15
- xAxis: {
16
- type: "datetime"
17
- },
18
- yAxis: {
2
+ Breakdown = function(ajax_fn){
3
+ var ajax_fn = ajax_fn;
4
+
5
+ return {
6
+ get_chart : function(data){
7
+ this.chart = new Highcharts.Chart({
8
+ chart: {
9
+ renderTo: "stat_chart",
10
+ zoomType : 'x'
11
+ },
19
12
  title: {
20
- text: "Value"
21
- }
22
- },
23
- series: data
24
- });
25
- },
26
- create_new_chart : function(stat_names){
27
- var filters = get_filter_values();
28
- // var params = $H({names : stat_names || [], filters : filters || []}).toQueryString();
29
- params = filters;
30
- var url = ["/<%= @classname %>/chart_data.json", params].join('?');
31
- new Ajax.Request(url, {onSuccess : function(trans){
32
- this.chart = null;
33
- this.get_chart(eval(trans.responseText));
34
- }.bind(this)})
35
- }
36
- });
13
+ text: "All Stats"
14
+ },
15
+ xAxis: {
16
+ type: "datetime",
17
+ title : {
18
+ text : 'Date'
19
+ }
20
+ },
21
+ yAxis: {
22
+ title: {
23
+ text: "Value"
24
+ }
25
+ },
26
+ series: data
27
+ });
28
+ },
29
+ get_filter_values : function(){
30
+ var paramStrings = "";
31
+ <% @metrify.filters.each_pair do |filter, subfilters| %>
32
+ <% subfilters.each_pair do |subfilter, attributes| %>
33
+ var param = "<%= 'filters[' + filter.to_s + '[' + subfilter.to_s + ']]' %>";
34
+ e = document.getElementById(param);
35
+ var value = e.value;
36
+ var k = e.name;
37
+ if (value != null && value != '') {
38
+ if (paramStrings != "") {
39
+ paramStrings = paramStrings + '&';
40
+ }
41
+ paramStrings = paramStrings + 'filters[<%= filter.to_s %>][]=' + k;
42
+ }
43
+ <% end %>
44
+ <% end %>
45
+ <% if @unit %>
46
+ paramStrings = paramStrings + '&unit=<%= @unit %>';
47
+ <% end %>
48
+ <% if @stat_names %>
49
+ paramStrings = paramStrings + '&stat_names=<%= (@stat_names || []).join(",") %>'
50
+ <% end %>
51
+ return paramStrings;
52
+ },
53
+ create_new_chart : function(){
54
+ var filters = this.get_filter_values();
55
+ params = filters;
56
+ var url = '<%= url_for(:action => 'chart_data', :format => 'json') %>' + '&' + params;
57
+ var self = this;
58
+ console.log("ajax function is " + ajax_fn);
59
+ ajax_fn.apply(this, [url, function(response_text){
60
+ //self.chart = null;
61
+ self.get_chart(eval(response_text));
62
+ }]);
63
+ }
64
+ };
65
+ };
37
66
  </script>
38
-
39
67
  <%= render :partial => 'metrify/time_links', :locals => {:unit => @unit, :action => 'graph_stats', :stat_names => @stat_names} %>
40
68
  <%= link_to "Metrics Home", 'index' %>
41
69
  <% showDateRangeOptions = ""
@@ -47,36 +75,4 @@ showDateRangeOptions += "$(\'dateRangeOptions\').style.display = \'block\';\n" %
47
75
  <input type="checkbox" id="<%= 'filters[' + filter.to_s + '[' + subfilter.to_s + ']]' %>" name="<%= subfilter.to_s %>" onClick="breakdown.create_new_chart();"/><%= subfilter.to_s %><br>
48
76
  <% end %>
49
77
  <% end %>
50
- <input type="button" value="Click Here" onUnclick="dashboard.create_new_chart();">
51
- <!-- this to controller and replace this with ajax also -->
52
- <script type="text/javascript" charset="utf-8">
53
- var data = <%= @stat_names.map{|s| {:name => pretty_col_name(s, @metrify),
54
- :pointInterval => (1.send(@unit) * 1000),
55
- :pointStart => (@number_of_stats.send(@unit).ago.to_i * 1000),
56
- :data => get_stat_arr(s)}}.to_json %>;
57
- window.breakdown = new Breakdown(data);
58
-
59
- function get_filter_values()
60
- {
61
- var paramStrings = "";
62
- <% @metrify.filters.each_pair do |filter, subfilters| %>
63
- <% subfilters.each_pair do |subfilter, attributes| %>
64
- var param = "<%= 'filters[' + filter.to_s + '[' + subfilter.to_s + ']]' %>";
65
- e = document.getElementById(param);
66
- var value = e.value;
67
- var k = e.name;
68
- if (value != null && value != '') {
69
- if (paramStrings != "") {
70
- paramStrings = paramStrings + '&';
71
- }
72
- paramStrings = paramStrings + 'filters[<%= filter.to_s %>][]=' + k;
73
- }
74
- <% end %>
75
- <% end %>
76
- <% if @unit %>
77
- paramStrings = paramStrings + '&unit=<%= @unit %>';
78
- <% end %>
79
- return paramStrings;
80
- }
81
-
82
- </script>
78
+ <input type="button" value="Click Here" onUnclick="breakdown.create_new_chart();">
@@ -45,24 +45,31 @@ module Metrify
45
45
 
46
46
  # currency
47
47
  def value_type(stat_name)
48
- metrify_data['stats'][stat_name]['value_type']
48
+ config_val(stat_name, 'value_type')
49
49
  end
50
50
 
51
51
  # int
52
52
  def value_precision(stat_name)
53
- metrify_data['stats'][stat_name]['precision']
53
+ config_val(stat_name, 'precision')
54
54
  end
55
55
 
56
56
  # if stat should also have % +/- over previous time period
57
57
  def show_variance(stat_name)
58
- metrify_data['stats'][stat_name]['show_variance']
58
+ config_val(stat_name, 'show_variance')
59
59
  end
60
60
 
61
+ def display_name(stat_name)
62
+ config_val(stat_name, 'display_name')
63
+ end
64
+
65
+ def config_val(stat_name, val)
66
+ metrify_data['stats'][stat_name] == nil ? nil : metrify_data['stats'][stat_name][val]
67
+ end
68
+
61
69
  def method_missing(method, *args, &block)
62
70
  stat_names.each do |name|
63
71
  if (name + NAME == method.to_s)
64
- obj = metrify_data['stats'][name]
65
- return obj['display_name']
72
+ return display_name(name)
66
73
  elsif (CALC + name == method.to_s)
67
74
  new_meth = method.to_s[CALC.length,method.to_s.length]
68
75
  raise MetrifyInclusionError, "Base class must implement method: #{new_meth}." if !self.class.respond_to?(new_meth)
@@ -91,30 +98,29 @@ module Metrify
91
98
  end
92
99
  end
93
100
 
94
- def stat_names(my_filters = nil)
101
+ def stat_names(names = nil, my_filters = nil)
95
102
  #filters = ['type' => ['numbers', 'letters'], 'furriness' => ['furry', 'not_furry']]
96
103
  if my_filters
97
- col_names = metrify_data['stats'].keys
98
- final_col_names = metrify_data['stats'].keys
99
-
100
- my_filters.each do |filter_type, filter_set|
104
+ col_names = names || metrify_data['stats'].keys
105
+ final_col_names = col_names.clone
106
+ my_filters.each do |filter_type, filter_subtypes|
101
107
  filter_col_names = []
102
108
  filters.keys.each do |filter_type_from_config|
103
109
  if (filter_type_from_config == filter_type)
104
- filter_set.each do |filter|
105
- filters[filter_type_from_config][filter]['set'].each do |col|
110
+ filter_subtypes.each do |subtype|
111
+ filters[filter_type_from_config][subtype]['set'].each do |col|
106
112
  filter_col_names << col
107
113
  end
108
114
  end
109
115
  end
110
116
  end
111
- col_names.each do |col|
112
- final_col_names.delete(col) if !filter_col_names.include?(col)
117
+ col_names.each do |ycol|
118
+ final_col_names.delete(ycol.to_s) if !filter_col_names.include?(ycol)
113
119
  end
114
120
  end
115
121
  final_col_names
116
122
  else
117
- metrify_data['stats'].keys
123
+ names || metrify_data['stats'].keys
118
124
  end
119
125
 
120
126
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: metrify
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
4
+ hash: 17
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 3
9
- - 0
10
- version: 0.3.0
9
+ - 1
10
+ version: 0.3.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Stephen Abrams
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-11-15 00:00:00 -05:00
18
+ date: 2010-11-18 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency