floodlight_rails 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (80) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.md +52 -0
  4. data/Rakefile +32 -0
  5. data/app/controllers/floodlight/api/health_check_controller.rb +7 -0
  6. data/app/controllers/floodlight/api/metrics_controller.rb +7 -0
  7. data/app/controllers/floodlight/api/statistics_controller.rb +7 -0
  8. data/app/controllers/floodlight/api_controller.rb +12 -0
  9. data/bin/floodlight +5 -0
  10. data/config/routes.rb +7 -0
  11. data/lib/floodlight.rb +45 -0
  12. data/lib/floodlight/configuration.rb +5 -0
  13. data/lib/floodlight/engine.rb +18 -0
  14. data/lib/floodlight/installer.rb +24 -0
  15. data/lib/floodlight/lazy_metric.rb +27 -0
  16. data/lib/floodlight/runner.rb +12 -0
  17. data/lib/floodlight/version.rb +3 -0
  18. data/lib/tasks/floodlight_tasks.rake +0 -0
  19. data/spec/configuration_spec.rb +26 -0
  20. data/spec/dummy/Rakefile +6 -0
  21. data/spec/dummy/app/assets/javascripts/application.js +13 -0
  22. data/spec/dummy/app/assets/javascripts/cable.js +13 -0
  23. data/spec/dummy/app/assets/stylesheets/application.css +15 -0
  24. data/spec/dummy/app/channels/application_cable/channel.rb +4 -0
  25. data/spec/dummy/app/channels/application_cable/connection.rb +4 -0
  26. data/spec/dummy/app/controllers/application_controller.rb +3 -0
  27. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  28. data/spec/dummy/app/jobs/application_job.rb +2 -0
  29. data/spec/dummy/app/mailers/application_mailer.rb +4 -0
  30. data/spec/dummy/app/models/application_record.rb +3 -0
  31. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  32. data/spec/dummy/app/views/layouts/mailer.html.erb +13 -0
  33. data/spec/dummy/app/views/layouts/mailer.text.erb +1 -0
  34. data/spec/dummy/bin/bundle +3 -0
  35. data/spec/dummy/bin/rails +3 -0
  36. data/spec/dummy/bin/rake +4 -0
  37. data/spec/dummy/bin/setup +38 -0
  38. data/spec/dummy/bin/update +29 -0
  39. data/spec/dummy/bin/yarn +11 -0
  40. data/spec/dummy/config.ru +5 -0
  41. data/spec/dummy/config/application.rb +18 -0
  42. data/spec/dummy/config/boot.rb +5 -0
  43. data/spec/dummy/config/cable.yml +10 -0
  44. data/spec/dummy/config/database.yml +25 -0
  45. data/spec/dummy/config/environment.rb +5 -0
  46. data/spec/dummy/config/environments/development.rb +54 -0
  47. data/spec/dummy/config/environments/production.rb +91 -0
  48. data/spec/dummy/config/environments/test.rb +42 -0
  49. data/spec/dummy/config/initializers/application_controller_renderer.rb +6 -0
  50. data/spec/dummy/config/initializers/assets.rb +14 -0
  51. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  52. data/spec/dummy/config/initializers/cookies_serializer.rb +5 -0
  53. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  54. data/spec/dummy/config/initializers/inflections.rb +16 -0
  55. data/spec/dummy/config/initializers/mime_types.rb +4 -0
  56. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  57. data/spec/dummy/config/locales/en.yml +33 -0
  58. data/spec/dummy/config/puma.rb +56 -0
  59. data/spec/dummy/config/routes.rb +2 -0
  60. data/spec/dummy/config/secrets.yml +32 -0
  61. data/spec/dummy/config/spring.rb +6 -0
  62. data/spec/dummy/db/development.sqlite3 +0 -0
  63. data/spec/dummy/db/schema.rb +15 -0
  64. data/spec/dummy/db/test.sqlite3 +0 -0
  65. data/spec/dummy/log/development.log +8 -0
  66. data/spec/dummy/log/test.log +2426 -0
  67. data/spec/dummy/package.json +5 -0
  68. data/spec/dummy/public/404.html +67 -0
  69. data/spec/dummy/public/422.html +67 -0
  70. data/spec/dummy/public/500.html +66 -0
  71. data/spec/dummy/public/apple-touch-icon-precomposed.png +0 -0
  72. data/spec/dummy/public/apple-touch-icon.png +0 -0
  73. data/spec/dummy/public/favicon.ico +0 -0
  74. data/spec/installer_spec.rb +19 -0
  75. data/spec/requests/api/health_check_spec.rb +27 -0
  76. data/spec/requests/api/metrics_spec.rb +39 -0
  77. data/spec/requests/api/statistics_spec.rb +52 -0
  78. data/spec/spec_helper.rb +23 -0
  79. data/spec/support/stub_access_token.rb +17 -0
  80. metadata +254 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 472ab736a0ece36c2e1c38b489c5e0cc9d153a78
4
+ data.tar.gz: 0b166aa6e26adf29fdf014d5a55fd656cbb22397
5
+ SHA512:
6
+ metadata.gz: ad778a8afb3e7ee66652d413b886d16038a8b1608a3d49646c176c26e4bbf83a3681149ddf014bbb61a04b642f82f224421174f500285d9d69ea77b6007b0828
7
+ data.tar.gz: 365456a030d5a968a007cbaca01535d6838fe7e8d30676b3e77c6e114bcca9847a18ce222cba0f0465c643d43f886c45950eca7728fe75938635ca9b3affa375
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2017 Chuck Callebs
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,52 @@
1
+ # Floodlight for Rails
2
+
3
+ [Floodlight](https://www.floodlight.io) helps you stop bugs in their tracks. We provide an easy way to gain insight into your data and notify you if we see problems.
4
+
5
+ This gem allows your Rails application to freely provide pre-configured daily data to Floodlight. It does this by mounting a set of protected API endpoints in your app. Only Floodlight can access these endpoints.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'floodlight_rails', require: 'floodlight'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ You can then install the necessary configuration by executing:
18
+
19
+ $ floodlight install
20
+
21
+ This will create a configuration file:
22
+
23
+ # config/initializers/floodlight.rb
24
+ Floodlight.configure do |config|
25
+ config.access_token = ENV['FLOODLIGHT_TOKEN']
26
+ end
27
+
28
+ # Sample usage:
29
+ # Floodlight.add_daily :confirmed_users, -> { User.confirmed }
30
+ # Floodlight.add_daily :unpaid_orders, -> { Order.unpaid }
31
+ # Floodlight.add_cumulative :total_users, -> { User }
32
+
33
+ You will need to check your application dashboard for your access token. By default, we advise storing it in an environment variable in your production environment.
34
+
35
+ ## Usage
36
+
37
+ For an example of how to set up various metrics to track, check out the configuration file. The `add_daily` and `add_cumulative` methods will expose additional metrics to the Floodlight application. The metrics will automatically be picked up by our servers.
38
+
39
+ These method take two parameters -- a descriptive symbol and a lambda containing an ActiveRecord scope. Be aware that if you change the symbol name, it will track it as a separate metric.
40
+
41
+
42
+ ## Contributing
43
+
44
+ Bug reports and pull requests are welcome on GitHub at https://github.com/FloodlightTeam/floodlight_rails. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.
45
+
46
+ ## License
47
+
48
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
49
+
50
+ ## Code of Conduct
51
+
52
+ Everyone interacting in the FloodlightTeam project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/FloodlightTeam/floodlight_rails/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,32 @@
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 = 'Floodlight'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
18
+ load 'rails/tasks/engine.rake'
19
+
20
+
21
+ load 'rails/tasks/statistics.rake'
22
+
23
+
24
+ require 'bundler/gem_tasks'
25
+
26
+ require 'rspec/core'
27
+ require 'rspec/core/rake_task'
28
+
29
+ desc "Run all specs in spec directory (excluding plugin specs)"
30
+ RSpec::Core::RakeTask.new(:spec => 'app:db:test:prepare')
31
+
32
+ task :default => :spec
@@ -0,0 +1,7 @@
1
+ module Floodlight
2
+ class Api::HealthCheckController < Floodlight::ApiController
3
+ def index
4
+ render json: { success: true }
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module Floodlight
2
+ class Api::MetricsController < ApiController
3
+ def index
4
+ render json: { metrics: Floodlight.metrics.map { |m| [m.name, m.type]} }
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module Floodlight
2
+ class Api::StatisticsController < ApiController
3
+ def show
4
+ render json: { count: Floodlight.statistics_for(params[:id], days_ago: params[:days_ago] || 1) }
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,12 @@
1
+ module Floodlight
2
+ class ApiController < ActionController::Base
3
+ before_action :verify_access_token
4
+
5
+ def verify_access_token
6
+ if params[:access_token] != Floodlight.configuration.access_token
7
+ render json: { error: 'not authorized' }, status: 401
8
+ return
9
+ end
10
+ end
11
+ end
12
+ end
data/bin/floodlight ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require_relative '../lib/floodlight/runner'
4
+
5
+ Floodlight::Runner.invoke
data/config/routes.rb ADDED
@@ -0,0 +1,7 @@
1
+ Floodlight::Engine.routes.draw do
2
+ namespace :api do
3
+ resources :health_check, only: [:index]
4
+ resources :metrics, only: [:index]
5
+ resources :statistics, only: [:show]
6
+ end
7
+ end
data/lib/floodlight.rb ADDED
@@ -0,0 +1,45 @@
1
+ require "floodlight/engine"
2
+ require "floodlight/configuration"
3
+ require "floodlight/installer"
4
+ require "floodlight/lazy_metric"
5
+
6
+ module Floodlight
7
+ class << self
8
+ attr_writer :configuration
9
+ end
10
+
11
+ def self.configuration
12
+ @configuration ||= Configuration.new
13
+ end
14
+
15
+ def self.configure
16
+ yield(configuration)
17
+ end
18
+
19
+ def self.metrics
20
+ @metrics ||= []
21
+ end
22
+
23
+ def self.add_daily(name, scope)
24
+ add(name, scope: scope, type: 'daily')
25
+ end
26
+
27
+ def self.add_cumulative(name, scope)
28
+ add(name, scope: scope, type: 'cumulative')
29
+ end
30
+
31
+ def self.add(name, scope:, type: 'daily')
32
+ if metric = metrics.find { |m| m.name == name }
33
+ metric.scope = scope
34
+ else
35
+ metrics << LazyMetric.new(name, scope: scope, type: type)
36
+ end
37
+ end
38
+
39
+ def self.statistics_for(name, days_ago: 1)
40
+ metric = metrics.find { |m| m.name == name.to_sym }
41
+ metric.compute(days_ago: days_ago)
42
+ rescue
43
+ nil
44
+ end
45
+ end
@@ -0,0 +1,5 @@
1
+ module Floodlight
2
+ class Configuration
3
+ attr_accessor :access_token
4
+ end
5
+ end
@@ -0,0 +1,18 @@
1
+ module Floodlight
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace Floodlight
4
+
5
+ initializer 'floodlight.mount' do |app|
6
+ app.routes.append do
7
+ mount Floodlight::Engine => '/floodlight'
8
+ end
9
+ end
10
+
11
+ config.generators do |g|
12
+ g.test_framework :rspec, :fixture => false
13
+ g.fixture_replacement :factory_girl, :dir => 'spec/factories'
14
+ g.assets false
15
+ g.helper false
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,24 @@
1
+ module Floodlight
2
+ class Installer
3
+ def self.perform
4
+ if defined?(Rails)
5
+ file_path = Rails.root.join('config', 'initializers', 'floodlight.rb')
6
+ else
7
+ file_path = Dir.pwd + '/config/initializers/floodlight.rb'
8
+ end
9
+
10
+ file_contents = <<-FILE
11
+ Floodlight.configure do |config|
12
+ config.access_token = ENV['FLOODLIGHT_TOKEN']
13
+ end
14
+
15
+ # Sample usage:
16
+ # Floodlight.add_daily :confirmed_users, -> { User.confirmed }
17
+ # Floodlight.add_daily :unpaid_orders, -> { Order.unpaid }
18
+ # Floodlight.add_cumulative :total_users, -> { User }
19
+ FILE
20
+
21
+ File.write(file_path, file_contents)
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,27 @@
1
+ module Floodlight
2
+ class LazyMetric
3
+ class InvalidMetricTypeException < StandardError; end
4
+
5
+ attr_accessor :name, :scope, :type
6
+
7
+ def initialize(name, scope:, type:)
8
+ raise InvalidMetricTypeException unless ['daily', 'cumulative'].include?(type)
9
+
10
+ self.name = name
11
+ self.scope = scope
12
+ self.type = type
13
+ end
14
+
15
+ def compute(days_ago: 1)
16
+ computed_scope = scope.call
17
+
18
+ if type == 'daily'
19
+ computed_scope = computed_scope.where("date(created_at) = date(?)", days_ago.to_i.days.ago)
20
+ elsif type == 'cumulative'
21
+ computed_scope = computed_scope.where("date(created_at) <= date(?)", days_ago.to_i.days.ago)
22
+ end
23
+
24
+ computed_scope.count
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,12 @@
1
+ require 'floodlight/installer'
2
+
3
+ module Floodlight
4
+ class Runner
5
+ def self.invoke
6
+ case ARGV
7
+ when ["install"]
8
+ Floodlight::Installer.perform
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ module Floodlight
2
+ VERSION = '1.0.0'
3
+ end
File without changes
@@ -0,0 +1,26 @@
1
+ require "spec_helper"
2
+
3
+ module Floodlight
4
+ describe Configuration do
5
+ subject { Floodlight.configuration }
6
+
7
+ describe '#access_token' do
8
+ it 'sets value' do
9
+ subject.access_token = '1234'
10
+ expect(subject.access_token).to eq('1234')
11
+ end
12
+ end
13
+
14
+ describe '.configure' do
15
+ before do
16
+ Floodlight.configure do |config|
17
+ config.access_token = '4321'
18
+ end
19
+ end
20
+
21
+ it 'sets values' do
22
+ expect(subject.access_token).to eq('4321')
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,6 @@
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_relative 'config/application'
5
+
6
+ Rails.application.load_tasks
@@ -0,0 +1,13 @@
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. JavaScript code in this file should be added after the last require_* statement.
9
+ //
10
+ // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require_tree .
@@ -0,0 +1,13 @@
1
+ // Action Cable provides the framework to deal with WebSockets in Rails.
2
+ // You can generate new channels where WebSocket features live using the `rails generate channel` command.
3
+ //
4
+ //= require action_cable
5
+ //= require_self
6
+ //= require_tree ./channels
7
+
8
+ (function() {
9
+ this.App || (this.App = {});
10
+
11
+ App.cable = ActionCable.createConsumer();
12
+
13
+ }).call(this);
@@ -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 ApplicationCable
2
+ class Channel < ActionCable::Channel::Base
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module ApplicationCable
2
+ class Connection < ActionCable::Connection::Base
3
+ end
4
+ end
@@ -0,0 +1,3 @@
1
+ class ApplicationController < ActionController::Base
2
+ protect_from_forgery with: :exception
3
+ end
@@ -0,0 +1,2 @@
1
+ module ApplicationHelper
2
+ end
@@ -0,0 +1,2 @@
1
+ class ApplicationJob < ActiveJob::Base
2
+ end
@@ -0,0 +1,4 @@
1
+ class ApplicationMailer < ActionMailer::Base
2
+ default from: 'from@example.com'
3
+ layout 'mailer'
4
+ end
@@ -0,0 +1,3 @@
1
+ class ApplicationRecord < ActiveRecord::Base
2
+ self.abstract_class = true
3
+ end