百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
R

rollup

> 编程语言
开源

在 Rails 中展开时间序列数据

351 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

在 Rails 中展开时间序列数据

Rollup

:fire: Rollup time-series data in Rails

Works great with Ahoy and Searchjoy

Installation

Add this line to your application’s Gemfile:

ruby
gem "rollups"

And run:

bash
bundle install
rails generate rollups
rails db:migrate

Contents

  • Getting Started
  • Creating Rollups
  • Querying Rollups
  • Other Topics
  • Examples

Getting Started

Store the number of users created by day in the rollups table

ruby
User.rollup("New users")

Get the series

ruby
Rollup.series("New users")
# {
#   Thu, 01 Jan 2026 => 50,
#   Fri, 02 Jan 2026 => 100,
#   Sat, 03 Jan 2026 => 34
# }

Use a rake task or background job to create rollups on a regular basis. Don’t worry too much about naming - you can rename later if needed.

Creating Rollups

Time Column

Specify the time column - created_at by default

ruby
User.rollup("New users", column: :joined_at)

Change the default column for a model

ruby
class User 

For multiple series, set a name for each series before charting

ruby
series = Rollup.multi_series("Orders by platform")
series.each do |s|
  s[:name] = s[:dimensions]["platform"]
end

Other Topics

Naming

Use any naming convention you prefer. Some ideas are:

  • Human - New users
  • Underscore - new_users
  • Dots - new_users.count

Rename with:

ruby
Rollup.rename("Old name", "New name")

Date Storage

Rollup stores both dates and times in the time column depending on the interval. For date intervals (day, week, etc), it stores 00:00:00 for the time part. Cast the time column to a date when querying in SQL to get the correct value.

  • PostgreSQL: time::date
  • MySQL: CAST(time AS date)
  • SQLite: date(time)

Examples

  • Ahoy
  • Searchjoy

Ahoy

Set the default rollup column for your models

ruby
class Ahoy::Visit < ApplicationRecord
  self.rollup_column = :started_at
end

and

ruby
class Ahoy::Event < ApplicationRecord
  self.rollup_column = :time
end

Hourly visits

ruby
Ahoy::Visit.rollup("Visits", interval: "hour")

Visits by browser

ruby
Ahoy::Visit.group(:browser).rollup("Visits by browser")

Unique homepage views

ruby
Ahoy::Event.where(name: "Viewed homepage").joins(:visit).rollup("Homepage views") { |r| r.distinct.count(:visitor_token) }

Product views

ruby
Ahoy::Event.where(name: "Viewed product").group_prop(:product_id).rollup("Product views")

Searchjoy

Daily searches

ruby
Searchjoy::Search.rollup("Searches")

Searches by query

ruby
Searchjoy::Search.group(:normalized_query).rollup("Searches by query", dimension_names: ["query"])

Conversion rate

ruby
Searchjoy::Search.rollup("Search conversion rate") { |r| r.average("(converted_at IS NOT NULL)::int") }

History

View the changelog

Contributing

Everyone is encouraged to help improve this project. Here are a few ways you can help:

  • Report bugs
  • Fix bugs and submit pull requests
  • Write, clarify, or fix documentation
  • Suggest or add new features

To get started with development:

bash
git clone https://github.com/ankane/rollup.git
cd rollup
bundle install

# create databases
createdb rollup_test
mysqladmin create rollup_test

# run tests
bundle exec rake test

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Ruby

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言