├── .ruby-version
├── spec
├── dummy
│ ├── log
│ │ └── .keep
│ ├── app
│ │ ├── mailers
│ │ │ └── .keep
│ │ ├── models
│ │ │ ├── .keep
│ │ │ ├── concerns
│ │ │ │ └── .keep
│ │ │ ├── post.rb
│ │ │ ├── author.rb
│ │ │ ├── comment.rb
│ │ │ ├── post_report.rb
│ │ │ └── data_builder.rb
│ │ ├── assets
│ │ │ ├── images
│ │ │ │ └── .keep
│ │ │ ├── javascripts
│ │ │ │ └── application.js
│ │ │ └── stylesheets
│ │ │ │ └── application.css
│ │ ├── controllers
│ │ │ ├── concerns
│ │ │ │ └── .keep
│ │ │ ├── application_controller.rb
│ │ │ └── site_controller.rb
│ │ ├── helpers
│ │ │ └── application_helper.rb
│ │ └── views
│ │ │ ├── layouts
│ │ │ └── application.html.erb
│ │ │ └── site
│ │ │ └── report.html.erb
│ ├── lib
│ │ └── assets
│ │ │ └── .keep
│ ├── public
│ │ ├── favicon.ico
│ │ ├── 500.html
│ │ ├── 422.html
│ │ └── 404.html
│ ├── db
│ │ ├── seeds.rb
│ │ ├── migrate
│ │ │ └── 20150714202319_add_dummy_models.rb
│ │ └── schema.rb
│ ├── bin
│ │ ├── rake
│ │ ├── bundle
│ │ ├── rails
│ │ └── setup
│ ├── config.ru
│ ├── config
│ │ ├── initializers
│ │ │ ├── cookies_serializer.rb
│ │ │ ├── session_store.rb
│ │ │ ├── mime_types.rb
│ │ │ ├── filter_parameter_logging.rb
│ │ │ ├── backtrace_silencers.rb
│ │ │ ├── assets.rb
│ │ │ ├── wrap_parameters.rb
│ │ │ └── inflections.rb
│ │ ├── environment.rb
│ │ ├── boot.rb
│ │ ├── database.yml
│ │ ├── locales
│ │ │ └── en.yml
│ │ ├── secrets.yml
│ │ ├── application.rb
│ │ ├── environments
│ │ │ ├── development.rb
│ │ │ ├── test.rb
│ │ │ └── production.rb
│ │ └── routes.rb
│ ├── Rakefile
│ └── README.rdoc
├── factories
│ └── factories.rb
├── spec_helper.rb
├── repor
│ ├── dimensions
│ │ ├── bin_dimension
│ │ │ ├── bin_table_spec.rb
│ │ │ └── bin_spec.rb
│ │ ├── category_dimension_spec.rb
│ │ ├── number_dimension_spec.rb
│ │ ├── bin_dimension_spec.rb
│ │ ├── time_dimension_spec.rb
│ │ └── base_dimension_spec.rb
│ ├── serializers
│ │ ├── table_serializer_spec.rb
│ │ └── highcharts_serializer_spec.rb
│ ├── aggregator_spec.rb
│ └── report_spec.rb
└── acceptance
│ └── data_spec.rb
├── lib
├── repor
│ ├── version.rb
│ ├── invalid_params_error.rb
│ ├── aggregators
│ │ ├── avg_aggregator.rb
│ │ ├── max_aggregator.rb
│ │ ├── min_aggregator.rb
│ │ ├── sum_aggregator.rb
│ │ ├── count_aggregator.rb
│ │ ├── array_aggregator.rb
│ │ └── base_aggregator.rb
│ ├── serializers
│ │ ├── csv_serializer.rb
│ │ ├── table_serializer.rb
│ │ ├── base_serializer.rb
│ │ ├── form_field_serializer.rb
│ │ └── highcharts_serializer.rb
│ ├── dimensions
│ │ ├── category_dimension.rb
│ │ ├── number_dimension.rb
│ │ ├── bin_dimension
│ │ │ ├── bin_table.rb
│ │ │ └── bin.rb
│ │ ├── time_dimension.rb
│ │ ├── base_dimension.rb
│ │ └── bin_dimension.rb
│ └── report.rb
├── tasks
│ └── repor_tasks.rake
└── repor.rb
├── .gitignore
├── .travis.yml
├── Rakefile
├── Gemfile
├── MIT-LICENSE
├── repor.gemspec
├── Gemfile.lock
└── README.md
/.ruby-version:
--------------------------------------------------------------------------------
1 | 2.2.3
2 |
--------------------------------------------------------------------------------
/spec/dummy/log/.keep:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/spec/dummy/app/mailers/.keep:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/spec/dummy/app/models/.keep:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/spec/dummy/lib/assets/.keep:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/spec/dummy/public/favicon.ico:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/spec/dummy/app/assets/images/.keep:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/spec/dummy/app/models/concerns/.keep:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/spec/dummy/app/controllers/concerns/.keep:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/spec/dummy/db/seeds.rb:
--------------------------------------------------------------------------------
1 | DataBuilder.build!
2 |
--------------------------------------------------------------------------------
/lib/repor/version.rb:
--------------------------------------------------------------------------------
1 | module Repor
2 | VERSION = "0.1.0"
3 | end
4 |
--------------------------------------------------------------------------------
/spec/dummy/app/helpers/application_helper.rb:
--------------------------------------------------------------------------------
1 | module ApplicationHelper
2 | end
3 |
--------------------------------------------------------------------------------
/lib/repor/invalid_params_error.rb:
--------------------------------------------------------------------------------
1 | module Repor
2 | class InvalidParamsError < ArgumentError
3 | end
4 | end
5 |
--------------------------------------------------------------------------------
/spec/dummy/app/models/post.rb:
--------------------------------------------------------------------------------
1 | class Post < ActiveRecord::Base
2 | belongs_to :author
3 | has_many :comments
4 | end
5 |
--------------------------------------------------------------------------------
/lib/tasks/repor_tasks.rake:
--------------------------------------------------------------------------------
1 | # desc "Explaining what the task does"
2 | # task :repor do
3 | # # Task goes here
4 | # end
5 |
--------------------------------------------------------------------------------
/spec/dummy/app/models/author.rb:
--------------------------------------------------------------------------------
1 | class Author < ActiveRecord::Base
2 | has_many :posts
3 | has_many :comments
4 | end
5 |
--------------------------------------------------------------------------------
/spec/dummy/app/models/comment.rb:
--------------------------------------------------------------------------------
1 | class Comment < ActiveRecord::Base
2 | belongs_to :author
3 | belongs_to :post
4 | end
5 |
--------------------------------------------------------------------------------
/spec/dummy/bin/rake:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env ruby
2 | require_relative '../config/boot'
3 | require 'rake'
4 | Rake.application.run
5 |
--------------------------------------------------------------------------------
/spec/dummy/bin/bundle:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env ruby
2 | ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
3 | load Gem.bin_path('bundler', 'bundle')
4 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | .bundle/
2 | log/*.log
3 | pkg/
4 | spec/dummy/db/*.sqlite3
5 | spec/dummy/db/*.sqlite3-journal
6 | spec/dummy/log/*.log
7 | spec/dummy/tmp/
8 | spec/dummy/.sass-cache
9 |
--------------------------------------------------------------------------------
/spec/dummy/bin/rails:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env ruby
2 | APP_PATH = File.expand_path('../../config/application', __FILE__)
3 | require_relative '../config/boot'
4 | require 'rails/commands'
5 |
--------------------------------------------------------------------------------
/spec/dummy/config.ru:
--------------------------------------------------------------------------------
1 | # This file is used by Rack-based servers to start the application.
2 |
3 | require ::File.expand_path('../config/environment', __FILE__)
4 | run Rails.application
5 |
--------------------------------------------------------------------------------
/spec/dummy/config/initializers/cookies_serializer.rb:
--------------------------------------------------------------------------------
1 | # Be sure to restart your server when you modify this file.
2 |
3 | Rails.application.config.action_dispatch.cookies_serializer = :json
4 |
--------------------------------------------------------------------------------
/spec/dummy/config/initializers/session_store.rb:
--------------------------------------------------------------------------------
1 | # Be sure to restart your server when you modify this file.
2 |
3 | Rails.application.config.session_store :cookie_store, key: '_dummy_session'
4 |
--------------------------------------------------------------------------------
/spec/dummy/config/environment.rb:
--------------------------------------------------------------------------------
1 | # Load the Rails application.
2 | require File.expand_path('../application', __FILE__)
3 |
4 | # Initialize the Rails application.
5 | Rails.application.initialize!
6 |
--------------------------------------------------------------------------------
/spec/dummy/config/initializers/mime_types.rb:
--------------------------------------------------------------------------------
1 | # Be sure to restart your server when you modify this file.
2 |
3 | # Add new mime types for use in respond_to blocks:
4 | # Mime::Type.register "text/richtext", :rtf
5 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | script:
2 | - cd spec/dummy && RAILS_ENV=test bundle exec rake db:create db:schema:load && cd ../..
3 | - bundle exec rspec
4 | language: ruby
5 | rvm:
6 | - 2.2
7 | env:
8 | - DB=mysql
9 | - DB=postgres
10 | - DB=sqlite
11 |
--------------------------------------------------------------------------------
/spec/dummy/config/initializers/filter_parameter_logging.rb:
--------------------------------------------------------------------------------
1 | # Be sure to restart your server when you modify this file.
2 |
3 | # Configure sensitive parameters which will be filtered from the log file.
4 | Rails.application.config.filter_parameters += [:password]
5 |
--------------------------------------------------------------------------------
/spec/dummy/app/controllers/application_controller.rb:
--------------------------------------------------------------------------------
1 | class ApplicationController < ActionController::Base
2 | # Prevent CSRF attacks by raising an exception.
3 | # For APIs, you may want to use :null_session instead.
4 | protect_from_forgery with: :exception
5 | end
6 |
--------------------------------------------------------------------------------
/lib/repor/aggregators/avg_aggregator.rb:
--------------------------------------------------------------------------------
1 | module Repor
2 | module Aggregators
3 | class AvgAggregator < BaseAggregator
4 | def aggregation(groups)
5 | groups.select("AVG(#{expression}) AS #{sql_value_name}")
6 | end
7 | end
8 | end
9 | end
10 |
--------------------------------------------------------------------------------
/lib/repor/aggregators/max_aggregator.rb:
--------------------------------------------------------------------------------
1 | module Repor
2 | module Aggregators
3 | class MaxAggregator < BaseAggregator
4 | def aggregation(groups)
5 | groups.select("MAX(#{expression}) AS #{sql_value_name}")
6 | end
7 | end
8 | end
9 | end
10 |
--------------------------------------------------------------------------------
/lib/repor/aggregators/min_aggregator.rb:
--------------------------------------------------------------------------------
1 | module Repor
2 | module Aggregators
3 | class MinAggregator < BaseAggregator
4 | def aggregation(groups)
5 | groups.select("MIN(#{expression}) AS #{sql_value_name}")
6 | end
7 | end
8 | end
9 | end
10 |
--------------------------------------------------------------------------------
/lib/repor/aggregators/sum_aggregator.rb:
--------------------------------------------------------------------------------
1 | module Repor
2 | module Aggregators
3 | class SumAggregator < BaseAggregator
4 | def aggregation(groups)
5 | groups.select("SUM(#{expression}) AS #{sql_value_name}")
6 | end
7 | end
8 | end
9 | end
10 |
--------------------------------------------------------------------------------
/spec/dummy/config/boot.rb:
--------------------------------------------------------------------------------
1 | # Set up gems listed in the Gemfile.
2 | ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../../../Gemfile', __FILE__)
3 |
4 | require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE'])
5 | $LOAD_PATH.unshift File.expand_path('../../../../lib', __FILE__)
6 |
--------------------------------------------------------------------------------
/spec/dummy/Rakefile:
--------------------------------------------------------------------------------
1 | # Add your own tasks in files placed in lib/tasks ending in .rake,
2 | # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3 |
4 | require File.expand_path('../config/application', __FILE__)
5 |
6 | Rails.application.load_tasks
7 |
--------------------------------------------------------------------------------
/lib/repor/aggregators/count_aggregator.rb:
--------------------------------------------------------------------------------
1 | module Repor
2 | module Aggregators
3 | class CountAggregator < BaseAggregator
4 | def aggregation(groups)
5 | groups.select("COUNT(DISTINCT #{report.table_name}.id) AS #{sql_value_name}")
6 | end
7 | end
8 | end
9 | end
10 |
--------------------------------------------------------------------------------
/spec/dummy/app/controllers/site_controller.rb:
--------------------------------------------------------------------------------
1 | class SiteController < ApplicationController
2 | def report
3 | @report = PostReport.new(params.fetch(:post_report, {}))
4 | @csv = Repor::Serializers::CsvSerializer.new(@report)
5 |
6 | respond_to do |format|
7 | format.html
8 | format.csv { send_data(csv.csv_text, filename: csv.filename) }
9 | end
10 | end
11 | end
12 |
--------------------------------------------------------------------------------
/lib/repor/aggregators/array_aggregator.rb:
--------------------------------------------------------------------------------
1 | module Repor
2 | module Aggregators
3 | class ArrayAggregator < BaseAggregator
4 | def aggregation(groups)
5 | unless Repor.database_type == :postgres
6 | fail InvalidParamsError, "array agg is only supported in Postgres"
7 | end
8 |
9 | groups.select("ARRAY_AGG(#{expression}) AS #{sql_value_name}")
10 | end
11 | end
12 | end
13 | end
14 |
--------------------------------------------------------------------------------
/spec/dummy/config/initializers/backtrace_silencers.rb:
--------------------------------------------------------------------------------
1 | # Be sure to restart your server when you modify this file.
2 |
3 | # You can add backtrace silencers for libraries that you're using but don't wish to see in your backtraces.
4 | # Rails.backtrace_cleaner.add_silencer { |line| line =~ /my_noisy_library/ }
5 |
6 | # You can also remove all the silencers if you're trying to debug a problem that might stem from framework code.
7 | # Rails.backtrace_cleaner.remove_silencers!
8 |
--------------------------------------------------------------------------------
/spec/factories/factories.rb:
--------------------------------------------------------------------------------
1 | FactoryGirl.define do
2 | factory :author do
3 | name { Faker::Name.name }
4 | end
5 |
6 | [:post, :comment].each do |type|
7 | factory type do
8 | transient { author nil }
9 |
10 | before(:create) do |record, evaluator|
11 | if evaluator.author
12 | author = Author.find_or_create_by(name: evaluator.author)
13 | record.author_id = author.id
14 | end
15 | end
16 | end
17 | end
18 | end
19 |
--------------------------------------------------------------------------------
/lib/repor/serializers/csv_serializer.rb:
--------------------------------------------------------------------------------
1 | require 'csv'
2 |
3 | module Repor
4 | module Serializers
5 | class CsvSerializer < TableSerializer
6 | def csv_text
7 | CSV.generate do |csv|
8 | csv << headers
9 | each_row { |row| csv << row }
10 | end
11 | end
12 |
13 | def save(filename = self.filename)
14 | File.open(filename, 'w') { |f| f.write data }
15 | end
16 |
17 | def filename
18 | "#{caption.parameterize}.csv"
19 | end
20 | end
21 | end
22 | end
23 |
--------------------------------------------------------------------------------
/spec/dummy/config/database.yml:
--------------------------------------------------------------------------------
1 | sqlite: &sqlite
2 | adapter: sqlite3
3 |
4 | mysql: &mysql
5 | adapter: mysql2
6 |
7 | postgres: &postgres
8 | adapter: postgresql
9 |
10 | defaults: &defaults
11 | pool: 5
12 | timeout: 5000
13 | host: localhost
14 | <<: *<%= ENV['DB'] || 'postgres' %>
15 |
16 | development:
17 | database: <%= ENV['DB'] == 'sqlite' ? 'db/development.sqlite3' : 'repor_development' %>
18 | <<: *defaults
19 |
20 | test:
21 | database: <%= ENV['DB'] == 'sqlite' ? 'db/test.sqlite3' : 'repor_test' %>
22 | <<: *defaults
23 |
--------------------------------------------------------------------------------
/spec/dummy/config/initializers/assets.rb:
--------------------------------------------------------------------------------
1 | # Be sure to restart your server when you modify this file.
2 |
3 | # Version of your assets, change this if you want to expire all your assets.
4 | Rails.application.config.assets.version = '1.0'
5 |
6 | # Add additional assets to the asset load path
7 | # Rails.application.config.assets.paths << Emoji.images_path
8 |
9 | # Precompile additional assets.
10 | # application.js, application.css, and all non-JS/CSS in app/assets folder are already added.
11 | # Rails.application.config.assets.precompile += %w( search.js )
12 |
--------------------------------------------------------------------------------
/Rakefile:
--------------------------------------------------------------------------------
1 | begin
2 | require 'bundler/setup'
3 | rescue LoadError
4 | puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5 | end
6 |
7 | require 'rdoc/task'
8 |
9 | RDoc::Task.new(:rdoc) do |rdoc|
10 | rdoc.rdoc_dir = 'rdoc'
11 | rdoc.title = 'Repor'
12 | rdoc.options << '--line-numbers'
13 | rdoc.rdoc_files.include('README.rdoc')
14 | rdoc.rdoc_files.include('lib/**/*.rb')
15 | end
16 |
17 | Bundler::GemHelper.install_tasks
18 |
19 | require 'rspec/core/rake_task'
20 |
21 | RSpec::Core::RakeTask.new(:spec)
22 |
23 | task :default => :spec
24 |
--------------------------------------------------------------------------------
/spec/dummy/config/initializers/wrap_parameters.rb:
--------------------------------------------------------------------------------
1 | # Be sure to restart your server when you modify this file.
2 |
3 | # This file contains settings for ActionController::ParamsWrapper which
4 | # is enabled by default.
5 |
6 | # Enable parameter wrapping for JSON. You can disable this by setting :format to an empty array.
7 | ActiveSupport.on_load(:action_controller) do
8 | wrap_parameters format: [:json] if respond_to?(:wrap_parameters)
9 | end
10 |
11 | # To enable root element in JSON for ActiveRecord objects.
12 | # ActiveSupport.on_load(:active_record) do
13 | # self.include_root_in_json = true
14 | # end
15 |
--------------------------------------------------------------------------------
/spec/dummy/README.rdoc:
--------------------------------------------------------------------------------
1 | == README
2 |
3 | This README would normally document whatever steps are necessary to get the
4 | application up and running.
5 |
6 | Things you may want to cover:
7 |
8 | * Ruby version
9 |
10 | * System dependencies
11 |
12 | * Configuration
13 |
14 | * Database creation
15 |
16 | * Database initialization
17 |
18 | * How to run the test suite
19 |
20 | * Services (job queues, cache servers, search engines, etc.)
21 |
22 | * Deployment instructions
23 |
24 | * ...
25 |
26 |
27 | Please feel free to use a different markup language if you do not plan to run
28 | rake doc:app.
29 |
--------------------------------------------------------------------------------
/Gemfile:
--------------------------------------------------------------------------------
1 | source 'https://rubygems.org'
2 |
3 | # Declare your gem's dependencies in repor.gemspec.
4 | # Bundler will treat runtime dependencies like base dependencies, and
5 | # development dependencies will be added by default to the :development group.
6 | gemspec
7 |
8 | # Declare any dependencies that are still in development here instead of in
9 | # your gemspec. These might include edge Rails or gems from your path or
10 | # Git. Remember to move these dependencies to your gemspec before releasing
11 | # your gem to rubygems.org.
12 |
13 | # To use a debugger
14 | # gem 'byebug', group: [:development, :test]
15 |
16 |
--------------------------------------------------------------------------------
/spec/dummy/app/views/layouts/application.html.erb:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Dummy
5 |
6 | <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
7 | <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
8 |
9 |
10 | <%= csrf_meta_tags %>
11 |
12 |
13 |
14 | <%= yield %>
15 |
16 |
17 |
18 |
--------------------------------------------------------------------------------
/spec/dummy/db/migrate/20150714202319_add_dummy_models.rb:
--------------------------------------------------------------------------------
1 | class AddDummyModels < ActiveRecord::Migration
2 | def change
3 | create_table :posts, force: true do |t|
4 | t.timestamps
5 | t.string :title
6 | t.integer :author_id
7 | t.integer :likes, null: false, default: 0
8 | end
9 |
10 | create_table :comments, force: true do |t|
11 | t.timestamps
12 | t.integer :post_id
13 | t.integer :author_id
14 | t.integer :likes, null: false, default: 0
15 | end
16 |
17 | create_table :authors, force: true do |t|
18 | t.timestamps
19 | t.string :name
20 | end
21 | end
22 | end
23 |
--------------------------------------------------------------------------------
/lib/repor/serializers/table_serializer.rb:
--------------------------------------------------------------------------------
1 | module Repor
2 | module Serializers
3 | class TableSerializer < BaseSerializer
4 | def headers
5 | report.groupers.map(&method(:human_dimension_label)) + [human_aggregator_label(report.aggregator)]
6 | end
7 |
8 | def each_row
9 | return to_enum(__method__) unless block_given?
10 |
11 | report.flat_data.each do |xes, y|
12 | yield report.groupers.zip(xes).map { |d, v| human_dimension_value_label(d, v) } + [human_aggregator_value_label(report.aggregator, y)]
13 | end
14 | end
15 |
16 | def caption
17 | axis_summary
18 | end
19 | end
20 | end
21 | end
22 |
--------------------------------------------------------------------------------
/spec/dummy/app/assets/javascripts/application.js:
--------------------------------------------------------------------------------
1 | // This is a manifest file that'll be compiled into application.js, which will include all the files
2 | // listed below.
3 | //
4 | // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5 | // or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
6 | //
7 | // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8 | // compiled file.
9 | //
10 | // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
11 | // about supported directives.
12 | //
13 | //= require_tree .
14 |
--------------------------------------------------------------------------------
/spec/spec_helper.rb:
--------------------------------------------------------------------------------
1 | # Configure Rails Environment
2 | ENV["RAILS_ENV"] ||= "test"
3 |
4 | require File.expand_path("../../spec/dummy/config/environment.rb", __FILE__)
5 | ActiveRecord::Migrator.migrations_paths = [File.expand_path("../../spec/dummy/db/migrate", __FILE__)]
6 | require "rails/test_help"
7 |
8 | require 'rspec/rails'
9 | require 'factory_girl_rails'
10 | require 'pry'
11 |
12 | # Load support files
13 | Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
14 |
15 | ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)
16 |
17 | RSpec.configure do |config|
18 | config.use_transactional_fixtures = true
19 | config.include FactoryGirl::Syntax::Methods
20 | end
21 |
--------------------------------------------------------------------------------
/spec/dummy/config/initializers/inflections.rb:
--------------------------------------------------------------------------------
1 | # Be sure to restart your server when you modify this file.
2 |
3 | # Add new inflection rules using the following format. Inflections
4 | # are locale specific, and you may define rules for as many different
5 | # locales as you wish. All of these examples are active by default:
6 | # ActiveSupport::Inflector.inflections(:en) do |inflect|
7 | # inflect.plural /^(ox)$/i, '\1en'
8 | # inflect.singular /^(ox)en/i, '\1'
9 | # inflect.irregular 'person', 'people'
10 | # inflect.uncountable %w( fish sheep )
11 | # end
12 |
13 | # These inflection rules are supported but not enabled by default:
14 | # ActiveSupport::Inflector.inflections(:en) do |inflect|
15 | # inflect.acronym 'RESTful'
16 | # end
17 |
--------------------------------------------------------------------------------
/spec/dummy/config/locales/en.yml:
--------------------------------------------------------------------------------
1 | # Files in the config/locales directory are used for internationalization
2 | # and are automatically loaded by Rails. If you want to use locales other
3 | # than English, add the necessary files in this directory.
4 | #
5 | # To use the locales, use `I18n.t`:
6 | #
7 | # I18n.t 'hello'
8 | #
9 | # In views, this is aliased to just `t`:
10 | #
11 | # <%= t('hello') %>
12 | #
13 | # To use a different locale, set it with `I18n.locale`:
14 | #
15 | # I18n.locale = :es
16 | #
17 | # This would use the information in config/locales/es.yml.
18 | #
19 | # To learn more, please read the Rails Internationalization guide
20 | # available at http://guides.rubyonrails.org/i18n.html.
21 |
22 | en:
23 | hello: "Hello world"
24 |
--------------------------------------------------------------------------------
/spec/dummy/app/models/post_report.rb:
--------------------------------------------------------------------------------
1 | class PostReport < Repor::Report
2 | report_on :Post
3 | count_aggregator :count
4 | sum_aggregator :total_likes, expression: 'posts.likes'
5 | min_aggregator :min_likes, expression: 'posts.likes'
6 | min_aggregator :min_created_at, expression: 'posts.created_at'
7 | max_aggregator :max_likes, expression: 'posts.likes'
8 | max_aggregator :max_created_at, expression: 'posts.created_at'
9 | avg_aggregator :avg_likes, expression: 'posts.likes'
10 | array_aggregator :post_ids, expression: 'posts.id'
11 | category_dimension :author, expression: 'authors.name', relation: ->(r) { r.joins(
12 | "LEFT OUTER JOIN authors ON authors.id = posts.author_id"
13 | ) }
14 | number_dimension :likes
15 | time_dimension :created_at
16 | end
17 |
--------------------------------------------------------------------------------
/spec/dummy/bin/setup:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env ruby
2 | require 'pathname'
3 |
4 | # path to your application root.
5 | APP_ROOT = Pathname.new File.expand_path('../../', __FILE__)
6 |
7 | Dir.chdir APP_ROOT do
8 | # This script is a starting point to setup your application.
9 | # Add necessary setup steps to this file:
10 |
11 | puts "== Installing dependencies =="
12 | system "gem install bundler --conservative"
13 | system "bundle check || bundle install"
14 |
15 | # puts "\n== Copying sample files =="
16 | # unless File.exist?("config/database.yml")
17 | # system "cp config/database.yml.sample config/database.yml"
18 | # end
19 |
20 | puts "\n== Preparing database =="
21 | system "bin/rake db:setup"
22 |
23 | puts "\n== Removing old logs and tempfiles =="
24 | system "rm -f log/*"
25 | system "rm -rf tmp/cache"
26 |
27 | puts "\n== Restarting application server =="
28 | system "touch tmp/restart.txt"
29 | end
30 |
--------------------------------------------------------------------------------
/lib/repor/dimensions/category_dimension.rb:
--------------------------------------------------------------------------------
1 | module Repor
2 | module Dimensions
3 | class CategoryDimension < BaseDimension
4 | def filter(relation)
5 | values = filter_values
6 | query = "#{expression} IN (?)"
7 | query = "#{expression} IS NULL OR #{query}" if values.include?(nil)
8 | relation.where(query, values.compact)
9 | end
10 |
11 | def group(relation)
12 | order relation.select("#{expression} AS #{sql_value_name}").group(sql_value_name)
13 | end
14 |
15 | def group_values
16 | if filtering?
17 | filter_values
18 | else
19 | i = report.groupers.index(self)
20 | report.raw_data.map { |x, _y| x[i] }.uniq
21 | end
22 | end
23 |
24 | def all_values
25 | relate(report.base_relation).
26 | pluck("DISTINCT #{expression}").
27 | map(&method(:sanitize_sql_value))
28 | end
29 | end
30 | end
31 | end
32 |
--------------------------------------------------------------------------------
/spec/dummy/config/secrets.yml:
--------------------------------------------------------------------------------
1 | # Be sure to restart your server when you modify this file.
2 |
3 | # Your secret key is used for verifying the integrity of signed cookies.
4 | # If you change this key, all old signed cookies will become invalid!
5 |
6 | # Make sure the secret is at least 30 characters and all random,
7 | # no regular words or you'll be exposed to dictionary attacks.
8 | # You can use `rake secret` to generate a secure secret key.
9 |
10 | # Make sure the secrets in this file are kept private
11 | # if you're sharing your code publicly.
12 |
13 | development:
14 | secret_key_base: e9d1dadd7da70b2ad66f1ffc7a2a035a005105d365117baea034b96ac2d6efe0aa308b7895646311cbcf67f9a68fcc13ca4b4025e2ded24d32fd991b72b09a7c
15 |
16 | test:
17 | secret_key_base: 54514b1da835ea5d5e14a55d1eba636c5b72453780282f514995c43337e49789b6df3caa0f9ba5006fb8f71c496dcb0ea82c0fd2706de5749da175652ac44b4b
18 |
19 | # Do not keep production secrets in the repository,
20 | # instead read values from the environment.
21 | production:
22 | secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
23 |
--------------------------------------------------------------------------------
/MIT-LICENSE:
--------------------------------------------------------------------------------
1 | Copyright 2015 Andrew Ross
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.
21 |
--------------------------------------------------------------------------------
/spec/dummy/config/application.rb:
--------------------------------------------------------------------------------
1 | require File.expand_path('../boot', __FILE__)
2 |
3 | require 'rails/all'
4 |
5 | Bundler.require(*Rails.groups)
6 | require "repor"
7 |
8 | module Dummy
9 | class Application < Rails::Application
10 | # Settings in config/environments/* take precedence over those specified here.
11 | # Application configuration should go into files in config/initializers
12 | # -- all .rb files in that directory are automatically loaded.
13 |
14 | # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
15 | # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
16 | # config.time_zone = 'Central Time (US & Canada)'
17 |
18 | # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
19 | # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
20 | # config.i18n.default_locale = :de
21 |
22 | # Do not swallow errors in after_commit/after_rollback callbacks.
23 | config.active_record.raise_in_transactional_callbacks = true
24 | end
25 | end
26 |
27 |
--------------------------------------------------------------------------------
/spec/dummy/app/assets/stylesheets/application.css:
--------------------------------------------------------------------------------
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 styles
10 | * defined in the other CSS/SCSS files in this directory. It is generally better to create a new
11 | * file per style scope.
12 | *
13 | *= require_tree .
14 | *= require_self
15 | */
16 |
17 | body { margin: 2em; }
18 | fieldset { display: inline; }
19 | h2 { margin: 0; }
20 | form { margin-top: 1em; margin-bottom: 2em; }
21 | table { width: 100%; }
22 | select { margin-top: 2px; margin-bottom: 1px; }
23 | .repor-axis-fields { margin-bottom: 1em; }
24 | .repor-dimension-fields--number-dimension input { width: 5em; }
25 | .repor-dimension-fields--time-dimension input { width: 10em; }
26 | .repor-dimension-fields--time-dimension input:nth-child(4) { width: 5em; }
27 |
--------------------------------------------------------------------------------
/repor.gemspec:
--------------------------------------------------------------------------------
1 | $:.push File.expand_path("../lib", __FILE__)
2 |
3 | # Maintain your gem's version:
4 | require "repor/version"
5 |
6 | # Describe your gem and declare its dependencies:
7 | Gem::Specification.new do |s|
8 | s.name = "repor"
9 | s.version = Repor::VERSION
10 | s.authors = ["Andrew Ross"]
11 | s.email = ["andrewslavinross@gmail.com"]
12 | s.homepage = "http://github.com/asross/repor"
13 | s.summary = "Rails data aggregation framework"
14 | s.description = "Flexible but opinionated framework for defining and running reports on Rails models backed by SQL databases."
15 | s.license = "MIT"
16 |
17 | s.files = Dir["lib/**/*", "MIT-LICENSE", "Rakefile", "README.md"]
18 | s.test_files = Dir["spec/**/*"]
19 |
20 | s.add_dependency "rails", "~> 4"
21 |
22 | s.add_development_dependency "pg", "~> 0.18"
23 | s.add_development_dependency "sqlite3", "~> 1.3"
24 | s.add_development_dependency "mysql2", "~> 0.3"
25 | s.add_development_dependency "rspec-rails", "~> 3"
26 | s.add_development_dependency "factory_girl_rails", "~> 4.5"
27 | s.add_development_dependency "database_cleaner", "~> 1.4"
28 | s.add_development_dependency "pry", "~> 0.10"
29 | s.add_development_dependency "faker", "~> 1.6"
30 | end
31 |
--------------------------------------------------------------------------------
/lib/repor/dimensions/number_dimension.rb:
--------------------------------------------------------------------------------
1 | module Repor
2 | module Dimensions
3 | class NumberDimension < BinDimension
4 | def validate_params!
5 | super
6 |
7 | if params.key?(:bin_width)
8 | unless Repor.numeric?(params[:bin_width])
9 | invalid_param!(:bin_width, "must be numeric")
10 | end
11 |
12 | unless params[:bin_width].to_f > 0
13 | invalid_param!(:bin_width, "must be greater than 0")
14 | end
15 | end
16 | end
17 |
18 | def bin_width
19 | if params.key?(:bin_width)
20 | params[:bin_width].to_f
21 | elsif domain == 0
22 | 1
23 | elsif params.key?(:bin_count)
24 | domain / params[:bin_count].to_f
25 | else
26 | default_bin_width
27 | end
28 | end
29 |
30 | private
31 |
32 | def default_bin_width
33 | domain / default_bin_count.to_f
34 | end
35 |
36 | def default_bin_count
37 | 10
38 | end
39 |
40 | class Bin < BinDimension::Bin
41 | def parses?(value)
42 | Repor.numeric?(value)
43 | end
44 |
45 | def parse(value)
46 | value.to_f
47 | end
48 | end
49 | end
50 | end
51 | end
52 |
--------------------------------------------------------------------------------
/lib/repor/dimensions/bin_dimension/bin_table.rb:
--------------------------------------------------------------------------------
1 | module Repor
2 | module Dimensions
3 | class BinDimension
4 | class BinTable < Array
5 | def initialize(values)
6 | super(values.compact)
7 | end
8 |
9 | def filter(relation, expr)
10 | relation.where(any_contain(expr))
11 | end
12 |
13 | def group(relation, expr, value_name)
14 | name = "#{value_name}_bin_table"
15 |
16 | bin_join = <<-SQL
17 | INNER JOIN (
18 | #{rows.join(" UNION\n ")}
19 | ) AS #{name} ON (
20 | CASE
21 | WHEN #{name}.min IS NULL AND #{name}.max IS NULL THEN (#{expr} IS NULL)
22 | WHEN #{name}.min IS NULL THEN (#{expr} < #{name}.max)
23 | WHEN #{name}.max IS NULL THEN (#{expr} >= #{name}.min)
24 | ELSE ((#{expr} >= #{name}.min) AND (#{expr} < #{name}.max))
25 | END
26 | )
27 | SQL
28 |
29 | selection = "#{name}.bin_text AS #{value_name}"
30 |
31 | relation.
32 | joins(bin_join).
33 | select(selection).
34 | group(value_name)
35 | end
36 |
37 | def rows
38 | map(&:row_sql)
39 | end
40 |
41 | def any_contain(expr)
42 | map { |bin| bin.contains_sql(expr) }.join(' OR ')
43 | end
44 | end
45 | end
46 | end
47 | end
48 |
--------------------------------------------------------------------------------
/lib/repor.rb:
--------------------------------------------------------------------------------
1 | module Repor
2 | def self.database_type
3 | database_adapter_name = ActiveRecord::Base.connection_config[:adapter]
4 | case database_adapter_name
5 | when /postgres/ then :postgres
6 | when /mysql/ then :mysql
7 | when /sqlite/ then :sqlite
8 | else
9 | raise "unsupported database #{database_adapter_name}"
10 | end
11 | end
12 |
13 | def self.numeric?(value)
14 | value.is_a?(Numeric) || value.is_a?(String) && value =~ /\A\d+(?:\.\d+)?\z/
15 | end
16 | end
17 |
18 | require 'repor/invalid_params_error'
19 |
20 | require 'repor/aggregators/base_aggregator'
21 | require 'repor/aggregators/count_aggregator'
22 | require 'repor/aggregators/avg_aggregator'
23 | require 'repor/aggregators/sum_aggregator'
24 | require 'repor/aggregators/min_aggregator'
25 | require 'repor/aggregators/max_aggregator'
26 | require 'repor/aggregators/array_aggregator'
27 |
28 | require 'repor/dimensions/base_dimension'
29 | require 'repor/dimensions/bin_dimension'
30 | require 'repor/dimensions/bin_dimension/bin'
31 | require 'repor/dimensions/bin_dimension/bin_table'
32 | require 'repor/dimensions/time_dimension'
33 | require 'repor/dimensions/number_dimension'
34 | require 'repor/dimensions/category_dimension'
35 |
36 | require 'repor/serializers/base_serializer'
37 | require 'repor/serializers/table_serializer'
38 | require 'repor/serializers/csv_serializer'
39 | require 'repor/serializers/form_field_serializer'
40 | require 'repor/serializers/highcharts_serializer'
41 |
42 | require 'repor/report'
43 |
--------------------------------------------------------------------------------
/spec/dummy/db/schema.rb:
--------------------------------------------------------------------------------
1 | # encoding: UTF-8
2 | # This file is auto-generated from the current state of the database. Instead
3 | # of editing this file, please use the migrations feature of Active Record to
4 | # incrementally modify your database, and then regenerate this schema definition.
5 | #
6 | # Note that this schema.rb definition is the authoritative source for your
7 | # database schema. If you need to create the application database on another
8 | # system, you should be using db:schema:load, not running all the migrations
9 | # from scratch. The latter is a flawed and unsustainable approach (the more migrations
10 | # you'll amass, the slower it'll run and the greater likelihood for issues).
11 | #
12 | # It's strongly recommended that you check this file into your version control system.
13 |
14 | ActiveRecord::Schema.define(version: 20150714202319) do
15 |
16 | create_table "authors", force: :cascade do |t|
17 | t.datetime "created_at"
18 | t.datetime "updated_at"
19 | t.string "name"
20 | end
21 |
22 | create_table "comments", force: :cascade do |t|
23 | t.datetime "created_at"
24 | t.datetime "updated_at"
25 | t.integer "post_id"
26 | t.integer "author_id"
27 | t.integer "likes", default: 0, null: false
28 | end
29 |
30 | create_table "posts", force: :cascade do |t|
31 | t.datetime "created_at"
32 | t.datetime "updated_at"
33 | t.string "title"
34 | t.integer "author_id"
35 | t.integer "likes", default: 0, null: false
36 | end
37 |
38 | end
39 |
--------------------------------------------------------------------------------
/spec/dummy/public/500.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | We're sorry, but something went wrong (500)
5 |
6 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
We're sorry, but something went wrong.
62 |
63 |
If you are the application owner check the logs for more information.
64 |
65 |
66 |
67 |
--------------------------------------------------------------------------------
/spec/dummy/config/environments/development.rb:
--------------------------------------------------------------------------------
1 | Rails.application.configure do
2 | # Settings specified here will take precedence over those in config/application.rb.
3 |
4 | # In the development environment your application's code is reloaded on
5 | # every request. This slows down response time but is perfect for development
6 | # since you don't have to restart the web server when you make code changes.
7 | config.cache_classes = false
8 |
9 | # Do not eager load code on boot.
10 | config.eager_load = false
11 |
12 | # Show full error reports and disable caching.
13 | config.consider_all_requests_local = true
14 | config.action_controller.perform_caching = false
15 |
16 | # Don't care if the mailer can't send.
17 | config.action_mailer.raise_delivery_errors = false
18 |
19 | # Print deprecation notices to the Rails logger.
20 | config.active_support.deprecation = :log
21 |
22 | # Raise an error on page load if there are pending migrations.
23 | config.active_record.migration_error = :page_load
24 |
25 | # Debug mode disables concatenation and preprocessing of assets.
26 | # This option may cause significant delays in view rendering with a large
27 | # number of complex assets.
28 | config.assets.debug = true
29 |
30 | # Asset digests allow you to set far-future HTTP expiration dates on all assets,
31 | # yet still be able to expire them through the digest params.
32 | config.assets.digest = true
33 |
34 | # Adds additional error checking when serving assets at runtime.
35 | # Checks for improperly declared sprockets dependencies.
36 | # Raises helpful error messages.
37 | config.assets.raise_runtime_errors = true
38 |
39 | # Raises error for missing translations
40 | # config.action_view.raise_on_missing_translations = true
41 | end
42 |
--------------------------------------------------------------------------------
/spec/dummy/public/422.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | The change you wanted was rejected (422)
5 |
6 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
The change you wanted was rejected.
62 |
Maybe you tried to change something you didn't have access to.
63 |
64 |
If you are the application owner check the logs for more information.
65 |
66 |
67 |
68 |
--------------------------------------------------------------------------------
/spec/dummy/public/404.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | The page you were looking for doesn't exist (404)
5 |
6 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
The page you were looking for doesn't exist.
62 |
You may have mistyped the address or the page may have moved.
63 |
64 |
If you are the application owner check the logs for more information.
65 |
66 |
67 |
68 |
--------------------------------------------------------------------------------
/spec/dummy/config/routes.rb:
--------------------------------------------------------------------------------
1 | Rails.application.routes.draw do
2 | root 'site#report'
3 | # The priority is based upon order of creation: first created -> highest priority.
4 | # See how all your routes lay out with "rake routes".
5 |
6 | # You can have the root of your site routed with "root"
7 | # root 'welcome#index'
8 |
9 | # Example of regular route:
10 | # get 'products/:id' => 'catalog#view'
11 |
12 | # Example of named route that can be invoked with purchase_url(id: product.id)
13 | # get 'products/:id/purchase' => 'catalog#purchase', as: :purchase
14 |
15 | # Example resource route (maps HTTP verbs to controller actions automatically):
16 | # resources :products
17 |
18 | # Example resource route with options:
19 | # resources :products do
20 | # member do
21 | # get 'short'
22 | # post 'toggle'
23 | # end
24 | #
25 | # collection do
26 | # get 'sold'
27 | # end
28 | # end
29 |
30 | # Example resource route with sub-resources:
31 | # resources :products do
32 | # resources :comments, :sales
33 | # resource :seller
34 | # end
35 |
36 | # Example resource route with more complex sub-resources:
37 | # resources :products do
38 | # resources :comments
39 | # resources :sales do
40 | # get 'recent', on: :collection
41 | # end
42 | # end
43 |
44 | # Example resource route with concerns:
45 | # concern :toggleable do
46 | # post 'toggle'
47 | # end
48 | # resources :posts, concerns: :toggleable
49 | # resources :photos, concerns: :toggleable
50 |
51 | # Example resource route within a namespace:
52 | # namespace :admin do
53 | # # Directs /admin/products/* to Admin::ProductsController
54 | # # (app/controllers/admin/products_controller.rb)
55 | # resources :products
56 | # end
57 | end
58 |
--------------------------------------------------------------------------------
/spec/repor/dimensions/bin_dimension/bin_table_spec.rb:
--------------------------------------------------------------------------------
1 | require 'spec_helper'
2 |
3 | describe Repor::Dimensions::BinDimension::BinTable do
4 | let(:bin_class) { Repor::Dimensions::BinDimension::Bin }
5 |
6 | describe '#filter' do
7 | it 'ORs together predicates across bins' do
8 | table = described_class.new([
9 | bin_class.new(nil, nil),
10 | bin_class.new(0, nil),
11 | bin_class.new(nil, 10),
12 | bin_class.new(3, 5)
13 | ])
14 |
15 | sql = table.filter(Post, 'x').to_sql
16 |
17 | expect(sql).to include "WHERE (x IS NULL OR x >= 0 OR x < 10 OR (x >= 3 AND x < 5))"
18 | end
19 | end
20 |
21 | describe '#group' do
22 | it 'joins to a union of bin rows, then groups by the range' do
23 | table = described_class.new([
24 | bin_class.new(nil, nil),
25 | bin_class.new(0, nil),
26 | bin_class.new(nil, 10),
27 | bin_class.new(3, 5)
28 | ])
29 |
30 | sql = table.group(Post, 'likes', 'likes').to_sql
31 |
32 | expect(sql).to start_with "SELECT likes_bin_table.bin_text AS likes"
33 |
34 | if Repor.database_type == :mysql
35 | expect(sql).to include "SELECT NULL AS min, NULL AS max, ',' AS bin_text"
36 | expect(sql).to include "SELECT 0 AS min, NULL AS max, '0,' AS bin_text"
37 | expect(sql).to include "SELECT NULL AS min, 10 AS max, ',10' AS bin_text"
38 | expect(sql).to include "SELECT 3 AS min, 5 AS max, '3,5' AS bin_text"
39 | else
40 | expect(sql).to include "SELECT NULL AS min, NULL AS max, CAST(',' AS text) AS bin_text"
41 | expect(sql).to include "SELECT 0 AS min, NULL AS max, CAST('0,' AS text) AS bin_text"
42 | expect(sql).to include "SELECT NULL AS min, 10 AS max, CAST(',10' AS text) AS bin_text"
43 | expect(sql).to include "SELECT 3 AS min, 5 AS max, CAST('3,5' AS text) AS bin_text"
44 | end
45 | end
46 | end
47 | end
48 |
--------------------------------------------------------------------------------
/spec/dummy/config/environments/test.rb:
--------------------------------------------------------------------------------
1 | Rails.application.configure do
2 | # Settings specified here will take precedence over those in config/application.rb.
3 |
4 | # The test environment is used exclusively to run your application's
5 | # test suite. You never need to work with it otherwise. Remember that
6 | # your test database is "scratch space" for the test suite and is wiped
7 | # and recreated between test runs. Don't rely on the data there!
8 | config.cache_classes = true
9 |
10 | # Do not eager load code on boot. This avoids loading your whole application
11 | # just for the purpose of running a single test. If you are using a tool that
12 | # preloads Rails for running tests, you may have to set it to true.
13 | config.eager_load = false
14 |
15 | # Configure static file server for tests with Cache-Control for performance.
16 | config.serve_static_files = true
17 | config.static_cache_control = 'public, max-age=3600'
18 |
19 | # Show full error reports and disable caching.
20 | config.consider_all_requests_local = true
21 | config.action_controller.perform_caching = false
22 |
23 | # Raise exceptions instead of rendering exception templates.
24 | config.action_dispatch.show_exceptions = false
25 |
26 | # Disable request forgery protection in test environment.
27 | config.action_controller.allow_forgery_protection = false
28 |
29 | # Tell Action Mailer not to deliver emails to the real world.
30 | # The :test delivery method accumulates sent emails in the
31 | # ActionMailer::Base.deliveries array.
32 | config.action_mailer.delivery_method = :test
33 |
34 | # Randomize the order test cases are executed.
35 | config.active_support.test_order = :random
36 |
37 | # Print deprecation notices to the stderr.
38 | config.active_support.deprecation = :stderr
39 |
40 | # Raises error for missing translations
41 | # config.action_view.raise_on_missing_translations = true
42 | end
43 |
--------------------------------------------------------------------------------
/lib/repor/aggregators/base_aggregator.rb:
--------------------------------------------------------------------------------
1 | module Repor
2 | module Aggregators
3 | class BaseAggregator
4 | attr_reader :name, :report, :opts
5 |
6 | def initialize(name, report, opts={})
7 | @name = name
8 | @report = report
9 | @opts = opts
10 | end
11 |
12 | # This is the method called by Repor::Report. It should return a hash of
13 | # array keys (of grouper values) mapped to aggregation values.
14 | def aggregate(groups)
15 | query = aggregation(relate(groups))
16 | result = ActiveRecord::Base.connection.select_all(query)
17 | result.cast_values.each_with_object(Hash.new(default_y_value)) do |values, h|
18 | row = result.columns.zip(values).to_h
19 | h[x_value_of(row)] = y_value_of(row)
20 | end
21 | end
22 |
23 | private
24 |
25 | # This is the method any aggregator must implement. It should return a
26 | # relation with the aggregator value SELECTed as the `sql_value_name`.
27 | def aggregation(groups)
28 | raise NotImplementedError
29 | end
30 |
31 | def sql_value_name
32 | "_report_aggregator_#{name}"
33 | end
34 |
35 | def x_value_of(row)
36 | report.groupers.map { |g| g.extract_sql_value(row) }
37 | end
38 |
39 | def y_value_of(row)
40 | row[sql_value_name]
41 | end
42 |
43 | def relate(groups)
44 | relation.call(groups)
45 | end
46 |
47 | def relation
48 | opts.fetch(:relation, ->(r) { r })
49 | end
50 |
51 | def expression
52 | opts.fetch(:expression, "#{report.table_name}.#{name}")
53 | end
54 |
55 | # What value should be returned if there are no results for a certain key?
56 | # For count, that's clearly 0; for min/max, that may be less clear.
57 | def default_y_value
58 | opts.fetch(:default_value, 0)
59 | end
60 | end
61 | end
62 | end
63 |
--------------------------------------------------------------------------------
/spec/repor/serializers/table_serializer_spec.rb:
--------------------------------------------------------------------------------
1 | require 'spec_helper'
2 |
3 | describe Repor::Serializers::TableSerializer do
4 | let(:report_class) do
5 | Class.new(Repor::Report) do
6 | report_on :Post
7 | number_dimension :likes
8 | time_dimension :created_at
9 | category_dimension :title
10 | count_aggregator :post_count
11 | end
12 | end
13 |
14 | let(:report) do
15 | report_class.new(
16 | aggregator: :post_count,
17 | groupers: %i[created_at likes title],
18 | dimensions: {
19 | created_at: { bin_width: '1 day' },
20 | likes: { bin_width: 1 }
21 | }
22 | )
23 | end
24 |
25 | let(:table) do
26 | Repor::Serializers::TableSerializer.new(report)
27 | end
28 |
29 | before do
30 | create(:post, created_at: '2016-01-01', likes: 2, title: 'A')
31 | create(:post, created_at: '2016-01-01', likes: 2, title: 'A')
32 | create(:post, created_at: '2016-01-01', likes: 1, title: 'B')
33 | create(:post, created_at: '2016-01-02', likes: 1, title: 'A')
34 | end
35 |
36 | describe '#headers' do
37 | it 'is a formatted list of groupers and the aggregator' do
38 | expect(table.headers).to eq ['Created at', 'Likes', 'Title', 'Post count']
39 | end
40 | end
41 |
42 | describe '#caption' do
43 | it 'is a summary of the axes and the total record count' do
44 | expect(table.caption).to eq 'Post count by Created at, Likes, and Title for 4 Posts'
45 | end
46 | end
47 |
48 | describe '#each_row' do
49 | it 'iterates through arrays of formatted grouper values and the aggregator value' do
50 | expect(table.each_row.to_a).to eq [
51 | ['2016-01-01', '[1.0, 2.0)', 'A', 0],
52 | ['2016-01-01', '[1.0, 2.0)', 'B', 1],
53 | ['2016-01-01', '[2.0, 3.0)', 'A', 2],
54 | ['2016-01-01', '[2.0, 3.0)', 'B', 0],
55 | ['2016-01-02', '[1.0, 2.0)', 'A', 1],
56 | ['2016-01-02', '[1.0, 2.0)', 'B', 0],
57 | ['2016-01-02', '[2.0, 3.0)', 'A', 0],
58 | ['2016-01-02', '[2.0, 3.0)', 'B', 0]
59 | ]
60 | end
61 | end
62 | end
63 |
--------------------------------------------------------------------------------
/spec/repor/aggregator_spec.rb:
--------------------------------------------------------------------------------
1 | require 'spec_helper'
2 |
3 | describe Repor::Aggregators do
4 | let(:report_class) do
5 | Class.new(Repor::Report) do
6 | report_on :Post
7 | category_dimension :author, expression: 'authors.name', relation: ->(r) { r.joins(:author) }
8 | count_aggregator :count
9 | sum_aggregator :total_likes, expression: 'posts.likes'
10 | avg_aggregator :mean_likes, expression: 'posts.likes'
11 | min_aggregator :min_likes, expression: 'posts.likes'
12 | max_aggregator :max_likes, expression: 'posts.likes'
13 | array_aggregator :post_ids, expression: 'posts.id'
14 | end
15 | end
16 |
17 | before do
18 | @p1 = create(:post, likes: 3, author: 'Alice')
19 | @p2 = create(:post, likes: 2, author: 'Alice')
20 | @p3 = create(:post, likes: 4, author: 'Bob')
21 | @p4 = create(:post, likes: 1, author: 'Bob')
22 | @p5 = create(:post, likes: 5, author: 'Bob')
23 | @p6 = create(:post, likes: 10, author: 'Chester')
24 | end
25 |
26 | def data_for(aggregator_name)
27 | report = report_class.new(aggregator: aggregator_name, groupers: [:author])
28 | report.raw_data
29 | end
30 |
31 | specify 'array' do
32 | if Repor.database_type == :postgres
33 | expect(data_for(:post_ids)).to eq(
34 | %w(Alice) => [@p1.id, @p2.id],
35 | %w(Bob) => [@p3.id, @p4.id, @p5.id],
36 | %w(Chester) => [@p6.id]
37 | )
38 | else
39 | expect { data_for(:post_ids) }.to raise_error(Repor::InvalidParamsError)
40 | end
41 | end
42 |
43 | specify 'max' do
44 | expect(data_for(:max_likes)).to eq %w(Alice) => 3, %w(Bob) => 5, %w(Chester) => 10
45 | end
46 |
47 | specify 'min' do
48 | expect(data_for(:min_likes)).to eq %w(Alice) => 2, %w(Bob) => 1, %w(Chester) => 10
49 | end
50 |
51 | specify 'avg' do
52 | d = data_for(:mean_likes)
53 | expect(d[%w(Alice)]).to eq 2.5
54 | expect(d[%w(Bob)].round(2)).to eq 3.33
55 | expect(d[%w(Chester)]).to eq 10
56 | end
57 |
58 | specify 'sum' do
59 | expect(data_for(:total_likes)).to eq %w(Alice) => 5, %w(Bob) => 10, %w(Chester) => 10
60 | end
61 |
62 | specify 'count' do
63 | expect(data_for(:count)).to eq %w(Alice) => 2, %w(Bob) => 3, %w(Chester) => 1
64 | end
65 | end
66 |
--------------------------------------------------------------------------------
/spec/repor/dimensions/category_dimension_spec.rb:
--------------------------------------------------------------------------------
1 | require 'spec_helper'
2 |
3 | describe Repor::Dimensions::CategoryDimension do
4 | def author_dimension(report)
5 | described_class.new(:author, report, expression: 'authors.name', relation: ->(r) { r.joins(
6 | "LEFT OUTER JOIN authors ON authors.id = posts.author_id") })
7 | end
8 |
9 | describe '#filter' do
10 | it 'filters to rows matching at least one value' do
11 | p1 = create(:post, author: 'Alice')
12 | p2 = create(:post, author: 'Bob')
13 | p3 = create(:post, author: nil)
14 |
15 | def filter_by(author_values)
16 | report = OpenStruct.new(
17 | table_name: 'posts',
18 | params: { dimensions: { author: { only: author_values } } }
19 | )
20 | dimension = author_dimension(report)
21 | dimension.filter(dimension.relate(Post))
22 | end
23 |
24 | expect(filter_by(['Alice'])).to eq [p1]
25 | expect(filter_by([nil])).to eq [p3]
26 | expect(filter_by(['Alice', nil])).to eq [p1, p3]
27 | expect(filter_by(['Alice', 'Bob'])).to eq [p1, p2]
28 | expect(filter_by([])).to eq []
29 | end
30 | end
31 |
32 | describe '#group' do
33 | it 'groups the relation by the exact value of the SQL expression' do
34 | p1 = create(:post, author: 'Alice')
35 | p2 = create(:post, author: 'Alice')
36 | p3 = create(:post, author: nil)
37 | p4 = create(:post, author: 'Bob')
38 | p5 = create(:post, author: 'Bob')
39 | p6 = create(:post, author: 'Bob')
40 |
41 | report = OpenStruct.new(table_name: 'posts', params: {})
42 | dimension = author_dimension(report)
43 |
44 | results = dimension.group(dimension.relate(Post)).select("COUNT(*) AS count").map do |r|
45 | r.attributes.values_at(dimension.send(:sql_value_name), 'count')
46 | end
47 |
48 | expect(results).to eq [[nil, 1], ['Alice', 2], ['Bob', 3]]
49 | end
50 | end
51 |
52 | describe '#group_values' do
53 | it 'echoes filter_values if filtering' do
54 | dimension = author_dimension(OpenStruct.new(params: {
55 | dimensions: { author: { only: ['foo', 'bar'] } }
56 | }))
57 | expect(dimension.group_values).to eq %w(foo bar)
58 | end
59 | end
60 | end
61 |
--------------------------------------------------------------------------------
/spec/repor/dimensions/bin_dimension/bin_spec.rb:
--------------------------------------------------------------------------------
1 | require 'spec_helper'
2 |
3 | describe Repor::Dimensions::BinDimension::Bin do
4 | describe '.from_hash' do
5 | it 'builds a bin from a hash or nil' do
6 | bin = described_class.from_hash(min: 1, max: 2)
7 | expect(bin.min).to eq 1
8 | expect(bin.max).to eq 2
9 |
10 | bin = described_class.from_hash(nil)
11 | expect(bin.min).to eq nil
12 | expect(bin.max).to eq nil
13 | end
14 | end
15 |
16 | describe '.from_sql' do
17 | it 'builds a bin from a bin text string' do
18 | bin = described_class.from_sql("1,2")
19 | expect(bin.min).to eq '1'
20 | expect(bin.max).to eq '2'
21 |
22 | bin = described_class.from_sql("1,")
23 | expect(bin.min).to eq '1'
24 | expect(bin.max).to eq nil
25 |
26 | bin = described_class.from_sql(",2")
27 | expect(bin.min).to eq nil
28 | expect(bin.max).to eq '2'
29 |
30 | bin = described_class.from_sql(",")
31 | expect(bin.min).to eq nil
32 | expect(bin.max).to eq nil
33 | end
34 | end
35 |
36 | describe '#contains_sql' do
37 | it 'returns SQL checking if expr is in the bin' do
38 | bin = described_class.new(1, 2)
39 | expect(bin.contains_sql('foo')).to eq "(foo >= 1 AND foo < 2)"
40 |
41 | bin = described_class.new(1, nil)
42 | expect(bin.contains_sql('foo')).to eq "foo >= 1"
43 |
44 | bin = described_class.new(nil, 2)
45 | expect(bin.contains_sql('foo')).to eq "foo < 2"
46 |
47 | bin = described_class.new(nil, nil)
48 | expect(bin.contains_sql('foo')).to eq "foo IS NULL"
49 | end
50 | end
51 |
52 | describe '#to_json' do
53 | it 'reexpresses the bin as a hash' do
54 | bin = described_class.new(1, 2)
55 | json = { a: bin }.to_json
56 | expect(JSON.parse(json)).to eq('a' => { 'min' => 1, 'max' => 2 })
57 | end
58 | end
59 |
60 | describe 'hashing' do
61 | it 'works with hashes' do
62 | bin1 = described_class.new(1, 2)
63 | bin2 = described_class.new(1, 2)
64 | bin3 = { min: 1, max: 2 }
65 |
66 | h = { bin3 => 'foo' }
67 | expect(h[bin1]).to eq 'foo'
68 | expect(h[bin2]).to eq 'foo'
69 | expect(h[bin3]).to eq 'foo'
70 | end
71 |
72 | it 'works with nil' do
73 | bin1 = described_class.new(nil, nil)
74 | bin2 = described_class.new(nil, nil)
75 | bin3 = nil
76 |
77 | h = { bin3 => 'foo' }
78 | expect(h[bin1]).to eq 'foo'
79 | expect(h[bin2]).to eq 'foo'
80 | expect(h[bin3]).to eq 'foo'
81 | end
82 | end
83 | end
84 |
--------------------------------------------------------------------------------
/spec/repor/dimensions/number_dimension_spec.rb:
--------------------------------------------------------------------------------
1 | require 'spec_helper'
2 |
3 | describe Repor::Dimensions::NumberDimension do
4 | def new_dimension(dimension_params = {}, report_params = {}, opts = {})
5 | report_params[:dimensions] = { foo: dimension_params }
6 | Repor::Dimensions::NumberDimension.new(
7 | :foo,
8 | OpenStruct.new(params: report_params),
9 | opts
10 | )
11 | end
12 |
13 | def expect_error(&block)
14 | expect { yield }.to raise_error(Repor::InvalidParamsError)
15 | end
16 |
17 | describe 'param validation' do
18 | it 'yells unless :bin_width is numeric' do
19 | expect_error { new_dimension(bin_width: '') }
20 | expect_error { new_dimension(bin_width: '49er') }
21 | expect_error { new_dimension(bin_width: { seconds: 1 }) }
22 | expect(new_dimension(bin_width: 10.5).bin_width).to eq 10.5
23 | expect(new_dimension(bin_width: '10').bin_width).to eq 10.0
24 | end
25 | end
26 |
27 | describe '#bin_width' do
28 | it 'reads from params' do
29 | dimension = new_dimension(bin_width: 7)
30 | expect(dimension.bin_width).to eq 7
31 | end
32 |
33 | it 'can divide the domain into :bin_count bins' do
34 | dimension = new_dimension(bin_count: 5, only: { min: 0, max: 5 })
35 | expect(dimension.bin_width).to eq 1
36 | allow(dimension).to receive(:data_contains_nil?).and_return(false)
37 | expect(dimension.group_values).to eq [
38 | { min: 0, max: 1 },
39 | { min: 1, max: 2 },
40 | { min: 2, max: 3 },
41 | { min: 3, max: 4 },
42 | { min: 4, max: 5 }
43 | ]
44 | end
45 |
46 | it 'can include nils if they are present in the data' do
47 | dimension = new_dimension(bin_count: 3, only: { min: 0, max: 3 })
48 | allow(dimension).to receive(:data_contains_nil?).and_return(true)
49 | expect(dimension.group_values).to eq [
50 | { min: nil, max: nil },
51 | { min: 0, max: 1 },
52 | { min: 1, max: 2 },
53 | { min: 2, max: 3 }
54 | ]
55 |
56 | dimension = new_dimension(bin_count: 3, only: { min: 0, max: 3 }, nulls_last: true)
57 | allow(dimension).to receive(:data_contains_nil?).and_return(true)
58 | expect(dimension.group_values).to eq [
59 | { min: 0, max: 1 },
60 | { min: 1, max: 2 },
61 | { min: 2, max: 3 },
62 | { min: nil, max: nil }
63 | ]
64 | end
65 |
66 | it 'defaults to 10 equal bins' do
67 | dimension = new_dimension(only: { min: 0, max: 5 })
68 | expect(dimension.bin_width).to eq 0.5
69 | end
70 | end
71 | end
72 |
--------------------------------------------------------------------------------
/spec/dummy/app/views/site/report.html.erb:
--------------------------------------------------------------------------------
1 |