How It Works
Let's say we have the following chart:
config/reports_kit/reports/my_posts.yml
measure: post
dimensions:
- created_at
In this YAML, measure: post
tells ReportsKit to count the number of Post
records, and dimensions: ['created_at']
tells it to group by the week of the created_at
column. Since created_at
is a datetime
column, ReportsKit knows that it should group the counts by week (the granularity is configurable), sort them chronologically, and add in zeros for any missing weeks.
ReportsKit infers sensible defaults from your ActiveRecord model configurations. Since the Post
model also has a belongs_to :author
association, if you use dimensions: ['author']
, then ReportsKit would count posts grouped by the author_id
column and show author names on the x-axis.
If you need more customization (e.g. custom filters, custom dimensions, custom aggregation functions, custom orders, aggregations of aggregations, etc), ReportsKit is very flexible and powerful and supports all of these with a simple syntax. It lets you use SQL, too.
To create the chart above, simply add the following YAML file and call render_report
in any view:
YAML
config/reports_kit/reports/how_it_works.yml
measure: post dimensions: - created_at
View
app/views/my_controller/my_view.html.haml
= render_report 'how_it_works'
Model
app/models/post.rb
class Post < ApplicationRecord belongs_to :author has_many :post_views, dependent: :destroy has_many :posts_tags, dependent: :destroy has_many :tags, through: :posts_tags include ReportsKit::Model reports_kit do aggregation :average_time_to_publish, [:average, 'posts.published_at - posts.created_at'] contextual_filter :for_author, ->(relation, context_params) { relation.where(author_id: context_params[:author_id]) } dimension :approximate_views_count, group: 'ROUND(posts.views_count, -1)' filter :is_published, :boolean, conditions: ->(relation) { relation.where(status: 'published') } end STATUSES = %w(draft private published).freeze def to_s title end end
Model's Columns
id integer author_id integer title string status string published_at datetime is_featured boolean views_count integer created_at datetime updated_at datetime