floodlight_rails 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +52 -0
- data/Rakefile +32 -0
- data/app/controllers/floodlight/api/health_check_controller.rb +7 -0
- data/app/controllers/floodlight/api/metrics_controller.rb +7 -0
- data/app/controllers/floodlight/api/statistics_controller.rb +7 -0
- data/app/controllers/floodlight/api_controller.rb +12 -0
- data/bin/floodlight +5 -0
- data/config/routes.rb +7 -0
- data/lib/floodlight.rb +45 -0
- data/lib/floodlight/configuration.rb +5 -0
- data/lib/floodlight/engine.rb +18 -0
- data/lib/floodlight/installer.rb +24 -0
- data/lib/floodlight/lazy_metric.rb +27 -0
- data/lib/floodlight/runner.rb +12 -0
- data/lib/floodlight/version.rb +3 -0
- data/lib/tasks/floodlight_tasks.rake +0 -0
- data/spec/configuration_spec.rb +26 -0
- data/spec/dummy/Rakefile +6 -0
- data/spec/dummy/app/assets/javascripts/application.js +13 -0
- data/spec/dummy/app/assets/javascripts/cable.js +13 -0
- data/spec/dummy/app/assets/stylesheets/application.css +15 -0
- data/spec/dummy/app/channels/application_cable/channel.rb +4 -0
- data/spec/dummy/app/channels/application_cable/connection.rb +4 -0
- data/spec/dummy/app/controllers/application_controller.rb +3 -0
- data/spec/dummy/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/app/jobs/application_job.rb +2 -0
- data/spec/dummy/app/mailers/application_mailer.rb +4 -0
- data/spec/dummy/app/models/application_record.rb +3 -0
- data/spec/dummy/app/views/layouts/application.html.erb +14 -0
- data/spec/dummy/app/views/layouts/mailer.html.erb +13 -0
- data/spec/dummy/app/views/layouts/mailer.text.erb +1 -0
- data/spec/dummy/bin/bundle +3 -0
- data/spec/dummy/bin/rails +3 -0
- data/spec/dummy/bin/rake +4 -0
- data/spec/dummy/bin/setup +38 -0
- data/spec/dummy/bin/update +29 -0
- data/spec/dummy/bin/yarn +11 -0
- data/spec/dummy/config.ru +5 -0
- data/spec/dummy/config/application.rb +18 -0
- data/spec/dummy/config/boot.rb +5 -0
- data/spec/dummy/config/cable.yml +10 -0
- data/spec/dummy/config/database.yml +25 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +54 -0
- data/spec/dummy/config/environments/production.rb +91 -0
- data/spec/dummy/config/environments/test.rb +42 -0
- data/spec/dummy/config/initializers/application_controller_renderer.rb +6 -0
- data/spec/dummy/config/initializers/assets.rb +14 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/config/initializers/cookies_serializer.rb +5 -0
- data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/spec/dummy/config/initializers/inflections.rb +16 -0
- data/spec/dummy/config/initializers/mime_types.rb +4 -0
- data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/spec/dummy/config/locales/en.yml +33 -0
- data/spec/dummy/config/puma.rb +56 -0
- data/spec/dummy/config/routes.rb +2 -0
- data/spec/dummy/config/secrets.yml +32 -0
- data/spec/dummy/config/spring.rb +6 -0
- data/spec/dummy/db/development.sqlite3 +0 -0
- data/spec/dummy/db/schema.rb +15 -0
- data/spec/dummy/db/test.sqlite3 +0 -0
- data/spec/dummy/log/development.log +8 -0
- data/spec/dummy/log/test.log +2426 -0
- data/spec/dummy/package.json +5 -0
- data/spec/dummy/public/404.html +67 -0
- data/spec/dummy/public/422.html +67 -0
- data/spec/dummy/public/500.html +66 -0
- data/spec/dummy/public/apple-touch-icon-precomposed.png +0 -0
- data/spec/dummy/public/apple-touch-icon.png +0 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/installer_spec.rb +19 -0
- data/spec/requests/api/health_check_spec.rb +27 -0
- data/spec/requests/api/metrics_spec.rb +39 -0
- data/spec/requests/api/statistics_spec.rb +52 -0
- data/spec/spec_helper.rb +23 -0
- data/spec/support/stub_access_token.rb +17 -0
- 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,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
data/config/routes.rb
ADDED
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,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
|
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
|
data/spec/dummy/Rakefile
ADDED
@@ -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
|
+
*/
|