Display metrics about your database in your application. Depends on the awesome Chartkick and Groupdate.
Make this in Ruby:
rails g metricky:metric TotalUsersMetric --scope User --type :count --period :day
In your view where you want to display the metric:
render_metric :total_users
In app/metrics/total_users_metric.rb
class TotalUsersMetric < ApplicationMetric
def scope
User
end
def type
:count
end
def period
:day
end
end
Add this line to your application's Gemfile:
gem 'metricky'
And then execute:
$ bundle
Or install it yourself as:
$ gem install metricky
Then drop in Chartkick into your application.js
(or similar):
//= require chartkick
Blatantly ripped from Super-inspired by Laravel Nova's metric system.
Need to pass current_user
?
render_metric :users, user: current_user, account: current_account
user
and account
will be attr_reader
-ized.
In your metric, define columns:
def columns
:id
end
To User.all.group(:color).count
def group
:color
end
In your metric, define what period:
def period
:day
end
This can be any one of Groupdate::PERIODS
Define what column should be used:
def period_column
:created_at
end
In your metric, define what type:
def type
:count
end
This can be any one of :min, :max, :sum, :count, :average
Ranges are what values are available on the form (used to query the metric, if applicable) via the range_column
def range_column
:created_at
end
Defaults are all
, today
, 24 hours
, 3 days
, 7 days
, 10 days
, 14 days
, 30 days
, 3 months
, 6 months
, 12 months
In your metric, define what ranges as a class method:
class TotalUsersMetric < ApplicationMetric
register_range '15h', label: "15 hours" do
15.hours.ago
end
end
class TotalUsersMetric < ApplicationMetric
remove_ranges '24h', '7d' # an array
remove_range '3d' # individual
end
class TotalUsersMetric < ApplicationMetric
reset_ranges!
end
class TotalUsersMetric < ApplicationMetric
default_range '24h'
end
Use collection_label
class TotalUsersMetric < ApplicationMetric
def collection_label(range_thing)
"Born #{range_thing.value.call}"
end
end
Need helpers? h
class TotalUsersMetric < ApplicationMetric
def collection_label(range_thing)
"Born #{h.time_ago_in_words(range_thing.value.call)}"
end
end
In your metric, define the partial path:
def to_partial_path
"shared/metric"
end
Take a look at app/views/metricky/_metric.html.erb
for implementation.
Add stuff.
The gem is available as open source under the terms of the MIT License.