metricky 0.7.5 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 54becc08e11727ef77a77175dd2039f34451e53fbd21df65cd9f041ed1b3177e
4
- data.tar.gz: 8c99b91ae474c167102f58d7f080ad653abf65cb7f777fb0a00a2fe2ba03a731
3
+ metadata.gz: 362f2f40e07645963b83935211c9ab8c222db9cb9064c099dd8b466597be2b08
4
+ data.tar.gz: 5a882ce98adecc050b1f3398935b26b09d961d67d98db24b5a018a3f3c43d65f
5
5
  SHA512:
6
- metadata.gz: 10dfa8257b5fae1e6169182f30f53800df54faa933e3daf73bb39382c2207c6a8c2f810a10b27b2e88b30249f2bc8d9865713c1c9deb190e678ac2f5c3bd7556
7
- data.tar.gz: 2e203ea4e914c2aa3a6309a3e92bafa0604656dc8b9ebd47f299f1cc00d631d11b5f520e3d5da669d07e16392e1e1957b0112141f91737658d6fd357f96a14c4
6
+ metadata.gz: f3f72bad0f1523a606e27244780c1b43e739fd846e5b393c4ab1bac1a2c4a8b8b2ca63cd23a01f51a7ff7a710faa624b1236dff0b0d1cc613707459ccec3967e
7
+ data.tar.gz: 9fa0ad994b4c4558f95eecb24ba9bf05ee42f19fc2805771900716925433f1b818e528f6887e2cff0b0f8ba5ab884cb85fd10541fdd482e6937aa9ef0f59103c
data/README.md CHANGED
@@ -86,6 +86,16 @@ def columns
86
86
  end
87
87
  ```
88
88
 
89
+ ### Grouping the value
90
+
91
+ To `User.all.group(:color).count`
92
+
93
+ ```ruby
94
+ def group
95
+ :color
96
+ end
97
+ ```
98
+
89
99
  ### Grouping by period (day, month, year, quarter, etc.)
90
100
 
91
101
  In your metric, define what period:
@@ -150,6 +160,14 @@ class TotalUsersMetric < ApplicationMetric
150
160
  end
151
161
  ```
152
162
 
163
+ #### Removing all ranges
164
+
165
+ ```ruby
166
+ class TotalUsersMetric < ApplicationMetric
167
+ reset_ranges!
168
+ end
169
+ ```
170
+
153
171
  #### Setting the default range
154
172
 
155
173
  ```ruby
@@ -0,0 +1,11 @@
1
+ module Metricky
2
+ class MetricsController < ActionController::Base
3
+ def show
4
+ metric = "#{params[:name].classify}Metric".constantize.new(params[:query] || {}, params[:options] || {})
5
+ unless metric.results.is_a?(Hash) || metric.results.is_a?(Array)
6
+ raise RenderError, "results must be a hash or array"
7
+ end
8
+ render json: metric.to_json
9
+ end
10
+ end
11
+ end
@@ -12,7 +12,7 @@
12
12
  </div>
13
13
  <div class="media">
14
14
  <div class="media-body">
15
- <% if metric.results.is_a?(Hash) %>
15
+ <% if metric.chart? %>
16
16
  <%= metricky_chart(metric) %>
17
17
  <% else %>
18
18
  <h3 class="font-weight-semibold mb-0"><%= metric.results %></h3>
data/config/routes.rb ADDED
@@ -0,0 +1,3 @@
1
+ Metricky::Engine.routes.draw do
2
+ resources :metrics, only: [:show], param: :name
3
+ end
@@ -10,15 +10,14 @@ class <%= class_name %>Metric < ApplicationMetric
10
10
  def type
11
11
  <%= options[:type][0] == ':' ? options[:type] : ":#{options[:type]}" %>
12
12
  end
13
- <% if options[:period] %>
13
+ <% if options[:group] %>
14
14
  def period
15
- <%= options[:period][0] == ':' ? options[:period] : ":#{options[:period]}" %>
15
+ <%= options[:group][0] == ':' ? options[:group] : ":#{options[:group]}" %>
16
16
  end
17
17
  <% else %>
18
- # ==> Change the period grouping
19
- # Must be one of Groupdate::PERIODS
20
- # def period
21
- # :week
18
+ # ==> Change the grouping
19
+ # def group
20
+ # :color
22
21
  # end
23
22
  <% end %>
24
23
 
@@ -42,5 +41,11 @@ class <%= class_name %>Metric < ApplicationMetric
42
41
  # def form?
43
42
  # false
44
43
  # end
44
+
45
+ # ==> Render chart async
46
+ # Renders the chart asynchronously with an ajax request. There are some limitations. Read here.
47
+ # def form?
48
+ # false
49
+ # end
45
50
  end
46
51
  <% end -%>
data/lib/metricky/base.rb CHANGED
@@ -20,6 +20,10 @@ module Metricky
20
20
  :column_chart
21
21
  end
22
22
 
23
+ def chart_options
24
+ @chart_options ||= {}
25
+ end
26
+
23
27
  # What partial is rendered.
24
28
  def to_partial_path
25
29
  '/metricky/metric'
@@ -40,6 +44,10 @@ module Metricky
40
44
  assets
41
45
  end
42
46
 
47
+ def name
48
+ self.class.metric_name
49
+ end
50
+
43
51
  def self.metric_name
44
52
  name.demodulize.sub(/Metric$/, "")
45
53
  end
@@ -85,6 +93,26 @@ module Metricky
85
93
  ranges.any?
86
94
  end
87
95
 
96
+ def chart?
97
+ results.is_a?(Hash) || results.is_a?(Array)
98
+ end
99
+
100
+ def to_json
101
+ results.chart_json
102
+ end
103
+
104
+ def group
105
+ false
106
+ end
107
+
108
+ def group?
109
+ group.present?
110
+ end
111
+
112
+ def json?
113
+ false
114
+ end
115
+
88
116
  private
89
117
 
90
118
  def assets
@@ -95,6 +123,9 @@ module Metricky
95
123
  if period? && valid_period?
96
124
  @query = @query.group_by_period(period, period_column)
97
125
  end
126
+ if group?
127
+ @query = @query.group(group)
128
+ end
98
129
  if valid_type?
99
130
  @query = @query.send(type, *columns)
100
131
  else
@@ -108,6 +139,7 @@ module Metricky
108
139
  @query
109
140
  end
110
141
 
142
+
111
143
  def format_value(value)
112
144
  value
113
145
  end
@@ -6,6 +6,10 @@ module Metricky
6
6
  end
7
7
  end
8
8
 
9
+ def metricky_metric_path(metric, options = {})
10
+ metricky.metric_path(name: metric.name.underscore, query: request.query_parameters, options: options)
11
+ end
12
+
9
13
  def render_metric(metric_name, options = {})
10
14
  if metric_name.is_a?(String) || metric_name.is_a?(Symbol)
11
15
  metric_name = metric_name.to_s
@@ -23,8 +27,12 @@ module Metricky
23
27
  render metric, metric: metric
24
28
  end
25
29
 
26
- def metricky_chart(metric)
27
- send(metric.chart, metric.results)
30
+ def metricky_chart(metric, options = {})
31
+ if metric.json?
32
+ send(metric.chart, metricky_metric_path(metric, options))
33
+ else
34
+ send(metric.chart, metric.results, options)
35
+ end
28
36
  end
29
37
  end
30
38
  end
@@ -1,3 +1,3 @@
1
1
  module Metricky
2
- VERSION = '0.7.5'
2
+ VERSION = '0.8.0'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: metricky
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.5
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Josh Brody
@@ -118,8 +118,10 @@ files:
118
118
  - MIT-LICENSE
119
119
  - README.md
120
120
  - Rakefile
121
+ - app/controllers/metrics_controller.rb
121
122
  - app/metrics/application_metric.rb
122
123
  - app/views/metricky/_metric.html.erb
124
+ - config/routes.rb
123
125
  - lib/generators/metricky/USAGE
124
126
  - lib/generators/metricky/metric_generator.rb
125
127
  - lib/generators/metricky/templates/application_metric.rb.tt