rollup_engine 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: bd7fb01c9476db83d64067639000119055f3780bdb32cc8e3eeeed3133597aa9
4
+ data.tar.gz: 8c41a6577ae4fcaf573662fdbc3dc4aca6dc4005c6f6617f8c86c3d0a298153b
5
+ SHA512:
6
+ metadata.gz: f4746be9f68a87b3dc5fd16a9887da4d01ef8e57a1671cc10546e400a118abe1207fe8d102683afe2a8ded39447b628221d2ddeaf22a7c7c7f5de852f3ed378e
7
+ data.tar.gz: e48e39aef77e91b4fde1a1a967ce71badff3cc28912a8ae6fc509bdf1012b256471e90b71e1e73bfc95f9d7b9e5acf6556e4d07fdcfa0a1382490a6529924c11
data/CHANGELOG.md ADDED
@@ -0,0 +1,11 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project are documented here, following
4
+ [Keep a Changelog](https://keepachangelog.com/en/1.1.0/) and
5
+ [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
+
7
+ ## [Unreleased]
8
+
9
+ ### Added
10
+ - Initial gem scaffold: mountable `RollupEngine` Rails engine (source-agnostic; no event
11
+ source dependency).
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright tylercschneider
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,64 @@
1
+ # RollupEngine
2
+
3
+ A **source-agnostic aggregation engine**. Feed it facts — rows with dimensions and
4
+ measurable values — and it turns them into numbers: declared **measures**, rolled up
5
+ by **time grain** and **dimension**, recomputed idempotently.
6
+
7
+ RollupEngine depends on no particular data source. It doesn't know about events, an outbox,
8
+ or any specific producer — anything that can hand it facts can use it. (In the
9
+ EventEngine stack, an adapter feeds `event_engine-store` events in as facts; but
10
+ that adapter lives elsewhere, not here.)
11
+
12
+ ## Installation
13
+
14
+ ```ruby
15
+ gem "rollup_engine"
16
+ ```
17
+
18
+ ```bash
19
+ $ bundle
20
+ ```
21
+
22
+ ## Status
23
+
24
+ Early development. The first rollup works — `RollupEngine::Rollup.count` / `.sum` aggregate
25
+ facts by **time grain** and optional **dimensions**:
26
+
27
+ ```ruby
28
+ RollupEngine::Rollup.count(facts, grain: :day, time: :occurred_at)
29
+ RollupEngine::Rollup.sum(facts, :amount, grain: :day, time: :occurred_at, by: [:channel])
30
+ ```
31
+
32
+ Declare named **measures** once and compute by name:
33
+
34
+ ```ruby
35
+ RollupEngine.register_measure(:revenue, aggregation: :sum, field: :amount)
36
+ RollupEngine::Rollup.compute(facts, measure: :revenue, grain: :day, time: :occurred_at)
37
+ ```
38
+
39
+ Persist rollups idempotently into the `rollup_engine_datapoints` table:
40
+
41
+ ```ruby
42
+ RollupEngine.recompute(:revenue, facts, grain: :day, time: :occurred_at, by: [:channel])
43
+ # upserts one RollupEngine::Datapoint per (measure, grain, period_start, dimensions);
44
+ # re-running updates values in place — no duplicates.
45
+ ```
46
+
47
+ `time`, `field`, and dimensions accept a **method symbol or a callable**, so facts
48
+ with nested data roll up without a wrapper — e.g. an event's JSON `payload`:
49
+
50
+ ```ruby
51
+ RollupEngine.recompute(:revenue, stored_events,
52
+ grain: :day,
53
+ time: :occurred_at,
54
+ field: ->(e) { e.payload["amount"] },
55
+ by: [ ->(e) { e.payload["channel"] } ])
56
+ ```
57
+
58
+ This is what lets `event_engine-store` events feed `rollup_engine` without coupling the two.
59
+
60
+ Next: a recompute job + window scoping, then sketches/cohorts.
61
+
62
+ ## License
63
+
64
+ Available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require "bundler/setup"
2
+
3
+ APP_RAKEFILE = File.expand_path("test/dummy/Rakefile", __dir__)
4
+ load "rails/tasks/engine.rake"
5
+
6
+ require "bundler/gem_tasks"
7
+
8
+ task test: "app:test"
9
+ task default: :test
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
10
+ * files in this directory. Styles in this file should be added after the last require_* statement.
11
+ * It is generally better to create a new file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,4 @@
1
+ module RollupEngine
2
+ class ApplicationController < ActionController::Base
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module RollupEngine
2
+ module ApplicationHelper
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module RollupEngine
2
+ class ApplicationJob < ActiveJob::Base
3
+ end
4
+ end
@@ -0,0 +1,6 @@
1
+ module RollupEngine
2
+ class ApplicationMailer < ActionMailer::Base
3
+ default from: "from@example.com"
4
+ layout "mailer"
5
+ end
6
+ end
@@ -0,0 +1,5 @@
1
+ module RollupEngine
2
+ class ApplicationRecord < ActiveRecord::Base
3
+ self.abstract_class = true
4
+ end
5
+ end
@@ -0,0 +1,28 @@
1
+ require "json"
2
+
3
+ module RollupEngine
4
+ class Datapoint < ApplicationRecord
5
+ self.table_name = "rollup_engine_datapoints"
6
+
7
+ scope :in_period, ->(range) { where(period_start: range) }
8
+
9
+ before_validation :assign_dimensions_key
10
+
11
+ # A canonical, order-independent string for a dimensions hash, used to match
12
+ # and uniquely index rows. Avoids relying on JSON column equality, which
13
+ # Postgres's `json` type does not support.
14
+ def self.dimensions_key_for(dimensions)
15
+ (dimensions || {}).to_a.sort.to_h.to_json
16
+ end
17
+
18
+ def self.series(measure, range)
19
+ where(measure: measure.to_s).in_period(range).group(:period_start).order(:period_start).sum(:value).to_a
20
+ end
21
+
22
+ private
23
+
24
+ def assign_dimensions_key
25
+ self.dimensions_key = self.class.dimensions_key_for(dimensions)
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,17 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>RollupEngine</title>
5
+ <%= csrf_meta_tags %>
6
+ <%= csp_meta_tag %>
7
+
8
+ <%= yield :head %>
9
+
10
+ <%= stylesheet_link_tag "rollup_engine/application", media: "all" %>
11
+ </head>
12
+ <body>
13
+
14
+ <%= yield %>
15
+
16
+ </body>
17
+ </html>
data/config/routes.rb ADDED
@@ -0,0 +1,2 @@
1
+ RollupEngine::Engine.routes.draw do
2
+ end
@@ -0,0 +1,17 @@
1
+ class CreateTallyDatapoints < ActiveRecord::Migration[8.1]
2
+ def change
3
+ create_table :tally_datapoints do |t|
4
+ t.string :measure, null: false
5
+ t.string :grain, null: false
6
+ t.datetime :period_start, null: false
7
+ t.json :dimensions, null: false, default: {}
8
+ t.decimal :value, null: false
9
+ t.datetime :recomputed_at
10
+
11
+ t.timestamps
12
+ end
13
+
14
+ add_index :tally_datapoints, [ :measure, :grain, :period_start ],
15
+ name: "index_tally_datapoints_on_measure_grain_period"
16
+ end
17
+ end
@@ -0,0 +1,9 @@
1
+ class AddDimensionsKeyToTallyDatapoints < ActiveRecord::Migration[8.1]
2
+ def change
3
+ add_column :tally_datapoints, :dimensions_key, :string, null: false, default: ""
4
+
5
+ remove_index :tally_datapoints, name: "index_tally_datapoints_on_measure_grain_period"
6
+ add_index :tally_datapoints, %i[measure grain period_start dimensions_key],
7
+ unique: true, name: "index_tally_datapoints_unique"
8
+ end
9
+ end
@@ -0,0 +1,8 @@
1
+ class RenameTallyTablesToRollupEngine < ActiveRecord::Migration[8.1]
2
+ def change
3
+ rename_table :tally_datapoints, :rollup_engine_datapoints
4
+ rename_index :rollup_engine_datapoints,
5
+ "index_tally_datapoints_unique",
6
+ "index_rollup_engine_datapoints_unique"
7
+ end
8
+ end
@@ -0,0 +1,5 @@
1
+ module RollupEngine
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace RollupEngine
4
+ end
5
+ end
@@ -0,0 +1,3 @@
1
+ module RollupEngine
2
+ Measure = Struct.new(:name, :aggregation, :field, keyword_init: true)
3
+ end
@@ -0,0 +1,25 @@
1
+ module RollupEngine
2
+ module Recompute
3
+ def self.call(measure_name, facts, grain:, time:, by: [])
4
+ result = Rollup.compute(facts, measure: measure_name, grain: grain, time: time, by: by)
5
+
6
+ result.each do |key, value|
7
+ period_start, dimensions = decompose(key, by)
8
+ datapoint = Datapoint.find_or_initialize_by(
9
+ measure: measure_name.to_s,
10
+ grain: grain.to_s,
11
+ period_start: period_start,
12
+ dimensions_key: Datapoint.dimensions_key_for(dimensions)
13
+ )
14
+ datapoint.update!(dimensions: dimensions, value: value, recomputed_at: Time.current)
15
+ end
16
+ end
17
+
18
+ def self.decompose(key, by)
19
+ return [ key, {} ] if by.empty?
20
+
21
+ period_start, *dimension_values = key
22
+ [ period_start, by.map(&:to_s).zip(dimension_values).to_h ]
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,42 @@
1
+ module RollupEngine
2
+ module Rollup
3
+ def self.compute(facts, measure:, grain:, time:, by: [])
4
+ definition = RollupEngine.measure(measure)
5
+
6
+ case definition.aggregation
7
+ when :count then count(facts, grain: grain, time: time, by: by)
8
+ when :sum then sum(facts, definition.field, grain: grain, time: time, by: by)
9
+ else raise ArgumentError, "Unknown aggregation: #{definition.aggregation.inspect}"
10
+ end
11
+ end
12
+
13
+ def self.count(facts, grain:, time:, by: [])
14
+ grouped(facts, grain, time, by).transform_values(&:size)
15
+ end
16
+
17
+ def self.sum(facts, field, grain:, time:, by: [])
18
+ grouped(facts, grain, time, by).transform_values do |group|
19
+ group.sum { |fact| extract(fact, field) }
20
+ end
21
+ end
22
+
23
+ def self.grouped(facts, grain, time, by)
24
+ facts.group_by { |fact| key(fact, grain, time, by) }
25
+ end
26
+
27
+ def self.key(fact, grain, time, by)
28
+ bucket = bucket(extract(fact, time), grain)
29
+ return bucket if by.empty?
30
+
31
+ [ bucket, *by.map { |dimension| extract(fact, dimension) } ]
32
+ end
33
+
34
+ def self.extract(fact, accessor)
35
+ accessor.respond_to?(:call) ? accessor.call(fact) : fact.public_send(accessor)
36
+ end
37
+
38
+ def self.bucket(moment, grain)
39
+ moment.public_send("beginning_of_#{grain}")
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,3 @@
1
+ module RollupEngine
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,29 @@
1
+ require "rollup_engine/version"
2
+ require "rollup_engine/engine"
3
+ require "rollup_engine/measure"
4
+ require "rollup_engine/rollup"
5
+ require "rollup_engine/recompute"
6
+
7
+ module RollupEngine
8
+ class << self
9
+ def measures
10
+ @measures ||= {}
11
+ end
12
+
13
+ def recompute(measure_name, facts, grain:, time:, by: [])
14
+ Recompute.call(measure_name, facts, grain: grain, time: time, by: by)
15
+ end
16
+
17
+ def register_measure(name, aggregation:, field: nil)
18
+ measures[name] = Measure.new(name: name, aggregation: aggregation, field: field)
19
+ end
20
+
21
+ def measure(name)
22
+ measures.fetch(name)
23
+ end
24
+
25
+ def reset_measures!
26
+ measures.clear
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :rollup_engine do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rollup_engine
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - tylercschneider
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: json
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "<"
17
+ - !ruby/object:Gem::Version
18
+ version: '3'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - "<"
24
+ - !ruby/object:Gem::Version
25
+ version: '3'
26
+ - !ruby/object:Gem::Dependency
27
+ name: rails
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 7.1.6
33
+ - - "<"
34
+ - !ruby/object:Gem::Version
35
+ version: '9'
36
+ type: :runtime
37
+ prerelease: false
38
+ version_requirements: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 7.1.6
43
+ - - "<"
44
+ - !ruby/object:Gem::Version
45
+ version: '9'
46
+ description: Turn facts into numbers. RollupEngine declares measures, rolls them up
47
+ by time grain and dimension, and recomputes idempotently. Source-agnostic — feed
48
+ it any facts (dimensions + measures); it does not depend on any particular event
49
+ source.
50
+ email:
51
+ - tylercschneider@gmail.com
52
+ executables: []
53
+ extensions: []
54
+ extra_rdoc_files: []
55
+ files:
56
+ - CHANGELOG.md
57
+ - MIT-LICENSE
58
+ - README.md
59
+ - Rakefile
60
+ - app/assets/stylesheets/rollup_engine/application.css
61
+ - app/controllers/rollup_engine/application_controller.rb
62
+ - app/helpers/rollup_engine/application_helper.rb
63
+ - app/jobs/rollup_engine/application_job.rb
64
+ - app/mailers/rollup_engine/application_mailer.rb
65
+ - app/models/rollup_engine/application_record.rb
66
+ - app/models/rollup_engine/datapoint.rb
67
+ - app/views/layouts/rollup_engine/application.html.erb
68
+ - config/routes.rb
69
+ - db/migrate/20260605000001_create_tally_datapoints.rb
70
+ - db/migrate/20260605160001_add_dimensions_key_to_tally_datapoints.rb
71
+ - db/migrate/20260921120000_rename_tally_tables_to_rollup_engine.rb
72
+ - lib/rollup_engine.rb
73
+ - lib/rollup_engine/engine.rb
74
+ - lib/rollup_engine/measure.rb
75
+ - lib/rollup_engine/recompute.rb
76
+ - lib/rollup_engine/rollup.rb
77
+ - lib/rollup_engine/version.rb
78
+ - lib/tasks/rollup_engine_tasks.rake
79
+ homepage: https://github.com/DYB-Development/rollup_engine
80
+ licenses:
81
+ - MIT
82
+ metadata:
83
+ allowed_push_host: https://rubygems.org
84
+ homepage_uri: https://github.com/DYB-Development/rollup_engine
85
+ source_code_uri: https://github.com/DYB-Development/rollup_engine
86
+ changelog_uri: https://github.com/DYB-Development/rollup_engine/blob/main/CHANGELOG.md
87
+ bug_tracker_uri: https://github.com/DYB-Development/rollup_engine/issues
88
+ documentation_uri: https://github.com/DYB-Development/rollup_engine#readme
89
+ rdoc_options: []
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: 3.2.0
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ requirements: []
103
+ rubygems_version: 4.0.20
104
+ specification_version: 4
105
+ summary: 'Source-agnostic aggregation engine: measures, rollups, sketches'
106
+ test_files: []