Metricky
Display metrics about your database in your application. Depends on the awesome Chartkick and Groupdate.
Demo (with code!)
Usage
Make this in Ruby:
Generate it
rails g metricky:metric TotalUsersMetric --scope User --type :count --period :day
Display it
In your view where you want to display the metric:
render_metric :total_users
Customize it
In app/metrics/total_users_metric.rb
class TotalUsersMetric < ApplicationMetric
def scope
User
end
def type
:count
end
def period
:day
end
endInstallation
Add this line to your application's Gemfile:
gem 'metricky'And then execute:
$ bundleOr install it yourself as:
$ gem install metrickyThen drop in Chartkick into your application.js (or similar):
//= require chartkickCustomizing
Blatantly ripped from Super-inspired by Laravel Nova's metric system.
Sending more data into the metric
Need to pass current_user?
render_metric :users, user: current_user, account: current_account
user and account will be attr_reader-ized.
Value to calculate
In your metric, define columns:
def columns
:id
endGrouping the value
To User.all.group(:color).count
def group
:color
endGrouping by period (day, month, year, quarter, etc.)
In your metric, define what period:
def period
:day
endThis can be any one of Groupdate::PERIODS
Define what column should be used:
def period_column
:created_at
endValue type
In your metric, define what type:
def type
:count
endThis can be any one of :min, :max, :sum, :count, :average
Ranges
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
endDefaults are all, today, 24 hours, 3 days, 7 days, 10 days, 14 days, 30 days, 3 months, 6 months, 12 months
Creating a new range
In your metric, define what ranges as a class method:
class TotalUsersMetric < ApplicationMetric
register_range '15h', label: "15 hours" do
15.hours.ago
end
endRemoving a range
class TotalUsersMetric < ApplicationMetric
remove_ranges '24h', '7d' # an array
remove_range '3d' # individual
endRemoving all ranges
class TotalUsersMetric < ApplicationMetric
reset_ranges!
endSetting the default range
class TotalUsersMetric < ApplicationMetric
default_range '24h'
endCustomizing the label
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
Partial
In your metric, define the partial path:
def to_partial_path
"shared/metric"
endTake a look at app/views/metricky/_metric.html.erb for implementation.
Contributing
Add stuff.
License
The gem is available as open source under the terms of the MIT License.