mdash 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f04e567a5a7a3c4f5c350db0a238c0f1193b9a312b60069159c740a6c64a309b
4
+ data.tar.gz: 4e22e7a1d14be1045162856737f72e181ba86b7f7041e2241e59dc44c0d55c45
5
+ SHA512:
6
+ metadata.gz: 9ea1ac9cbc669d1673cfc5cec7b3eebd935c08ea2ae8a7703d926cf7aefa9f867ed47bf6b195bd26c52508f1fdf1f940a161c27ce2f4443cd1603c5df568fd4c
7
+ data.tar.gz: cdbe84f79f38cf298fd22bd0ef792025dfe152c57eeb6daa649bf9911ad927e40898b36bc1de8855293188dac4f9ec7756907aad7822161c13ffb350c207e1bc
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright Tim Marks
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,48 @@
1
+ # Mdash
2
+ Mdash is the easiest, most secure and most efficient way to setup dashboards for your Rails apps.
3
+ Since we use client-side apps instead of bulky server-side embeds the overhead on your app and the surface area is kept to a minimum.
4
+
5
+ ## Installation
6
+ Add this line to your application's Gemfile:
7
+
8
+ ```ruby
9
+ gem "mdash"
10
+ ```
11
+
12
+ And then execute:
13
+ ```bash
14
+ $ bundle
15
+ ```
16
+
17
+ Or install it yourself as:
18
+ ```bash
19
+ $ gem install mdash
20
+ ```
21
+
22
+ #### Run the installer
23
+
24
+ To setup the Mdash config file in your rails project run
25
+
26
+ ```bash
27
+ rails generate mdash:install
28
+ ```
29
+
30
+ ## Usage
31
+
32
+ Mount the Mdash engine in your routes file
33
+
34
+ ```ruby
35
+ mount Mdash::Engine => "/mdash"
36
+ ```
37
+
38
+ ## Contributing
39
+
40
+ Bug reports and pull requests are welcome on GitHub at https://github.com/imothee/mdash. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/imothee/policygen/blob/main/CODE_OF_CONDUCT.md).
41
+
42
+ ## License
43
+
44
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
45
+
46
+ ## Code of Conduct
47
+
48
+ Everyone interacting in the Mdash project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/imothee/policygen/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/setup"
2
+
3
+ APP_RAKEFILE = File.expand_path("test/dummy/Rakefile", __dir__)
4
+ load "rails/tasks/engine.rake"
5
+
6
+ load "rails/tasks/statistics.rake"
7
+
8
+ require "bundler/gem_tasks"
@@ -0,0 +1,18 @@
1
+ module Mdash
2
+ class AnnounceController < ApplicationController
3
+ def index
4
+ available = Mdash.config.metrics.each_with_object({}) do |metric, hash|
5
+ hash[metric.id] = {
6
+ model: metric.model,
7
+ aggregation: metric.aggregation,
8
+ aggregation_column: metric.aggregation_column,
9
+ period: metric.period,
10
+ periods: metric.periods,
11
+ modifier: metric.modifier
12
+ }
13
+ end
14
+
15
+ render json: available.as_json
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,21 @@
1
+ module Mdash
2
+ class ApplicationController < ActionController::Base
3
+ before_action :authenticate_token!
4
+
5
+ private
6
+
7
+ def authenticate_token!
8
+ unless Mdash.config.secret.present?
9
+ return render json: { error: "Mdash secret is not set" }, status: 500
10
+ end
11
+
12
+ unless Mdash.config.secret.length > 10
13
+ return render json: { error: "Mdash secret is too short" }, status: 500
14
+ end
15
+
16
+ unless request.headers["X-Mdash-Token"] == Mdash.config.secret
17
+ return render json: { error: "Unauthorized" }, status: 401
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,8 @@
1
+ module Mdash
2
+ class StatsController < ApplicationController
3
+ def index
4
+ @stats = Mdash.stats
5
+ render json: @stats
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,4 @@
1
+ module Mdash
2
+ module ApplicationHelper
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module Mdash
2
+ class ApplicationJob < ActiveJob::Base
3
+ end
4
+ end
@@ -0,0 +1,6 @@
1
+ module Mdash
2
+ class ApplicationMailer < ActionMailer::Base
3
+ default from: "from@example.com"
4
+ layout "mailer"
5
+ end
6
+ end
@@ -0,0 +1,5 @@
1
+ module Mdash
2
+ class ApplicationRecord < ActiveRecord::Base
3
+ self.abstract_class = true
4
+ end
5
+ end
@@ -0,0 +1,93 @@
1
+ module Mdash
2
+ class Metric
3
+ include ActiveModel::Model
4
+
5
+ # Valid aggregations
6
+ AGGREGATIONS = %i[sum average count].freeze
7
+ # Valid periods
8
+ PERIODS = %i[hour day week month year].freeze
9
+
10
+ attr_reader :id, :model, :aggregation, :aggregation_column, :period, :periods, :modifier
11
+
12
+ def self.all(configuration: nil)
13
+ configuration ||= Mdash.config
14
+ configuration.metrics
15
+ end
16
+
17
+ def self.find(id, configuration: nil)
18
+ all(configuration:).find { |m| m.id == id.to_sym }.tap do |m|
19
+ raise ActiveRecord::RecordNotFound unless m
20
+ end
21
+ end
22
+
23
+ def initialize(id:, model:, aggregation:, aggregation_column: :id, period: nil, periods: nil, modifier: nil)
24
+ @id = id.to_sym
25
+ @model = model.to_sym
26
+ @aggregation = aggregation.to_sym
27
+ @aggregation_column = aggregation_column.to_sym
28
+ @period = period&.to_sym
29
+ @periods = periods
30
+ @modifier = modifier
31
+ end
32
+
33
+ def valid?
34
+ return false unless AGGREGATIONS.include?(@aggregation)
35
+ return false unless PERIODS.include?(@period) if @period.present?
36
+
37
+ true
38
+ end
39
+
40
+ def data
41
+ clazz = @model.to_s.classify.constantize
42
+ data = clazz.all
43
+ data = data.send(@modifier) if @modifier.present?
44
+ data = period_query(data) if @period.present? and @periods.nil?
45
+ data = periods_query(data) if @period.present? and @periods.present? and @periods.positive?
46
+ data = aggregation_query(data)
47
+ data
48
+ end
49
+
50
+ private
51
+
52
+ def period_query(query)
53
+ case @period
54
+ when :hour
55
+ query.where(created_at: 1.hour.ago..Time.now)
56
+ when :day
57
+ query.where(created_at: 1.day.ago..Time.now)
58
+ when :week
59
+ query.where(created_at: 1.week.ago..Time.now)
60
+ when :month
61
+ query.where(created_at: 1.month.ago..Time.now)
62
+ when :year
63
+ query.where(created_at: 1.year.ago..Time.now)
64
+ end
65
+ end
66
+
67
+ def periods_query(query)
68
+ case @period
69
+ when :hour
70
+ query.where(created_at: @periods.hours.ago..Time.now).group_by_hour(:created_at)
71
+ when :day
72
+ query.where(created_at: @periods.days.ago..Time.now).group_by_day(:created_at)
73
+ when :week
74
+ query.where(created_at: @periods.weeks.ago..Time.now).group_by_week(:created_at)
75
+ when :month
76
+ query.where(created_at: @periods.months.ago..Time.now).group_by_month(:created_at)
77
+ when :year
78
+ query.where(created_at: @periods.years.ago..Time.now).group_by_year(:created_at)
79
+ end
80
+ end
81
+
82
+ def aggregation_query(query)
83
+ case @aggregation
84
+ when :sum
85
+ query.sum(@aggregation_column)
86
+ when :avg
87
+ query.average(@aggregation_column)
88
+ when :count
89
+ query.count
90
+ end
91
+ end
92
+ end
93
+ end
data/config/routes.rb ADDED
@@ -0,0 +1,4 @@
1
+ Mdash::Engine.routes.draw do
2
+ get "announce" => "announce#index"
3
+ get "stats" => "stats#index"
4
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mdash
4
+ module Generators
5
+ class InstallGenerator < Rails::Generators::Base
6
+ desc "Copy Mdash default files"
7
+ source_root File.expand_path("templates", __dir__)
8
+
9
+ def copy_config
10
+ template "config/initializers/mdash.rb.tt", "config/initializers/mdash.rb"
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,6 @@
1
+ # Use this setup block to configure all options available in Mdash
2
+ Mdash.configure do |config|
3
+ config.secret = "<%= SecureRandom.hex(32) %>"
4
+
5
+ config.exported_metrics = {}
6
+ end
@@ -0,0 +1,21 @@
1
+ module Mdash
2
+ class Configuration
3
+ attr_accessor :secret
4
+ attr_accessor :exported_metrics
5
+
6
+ def metrics
7
+ @metrics ||= exported_metrics.flat_map do |prefix, params|
8
+ model = params[:model]
9
+ params[:metrics].map do |k, metric_params|
10
+ metric = Metric.new(id: "#{prefix}_#{k}", model: model, **metric_params)
11
+ # Check if the metric is valid
12
+ unless metric.valid?
13
+ Rails.logger.warn("Invalid metric: #{metric.id}")
14
+ return nil
15
+ end
16
+ metric
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,5 @@
1
+ module Mdash
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace Mdash
4
+ end
5
+ end
@@ -0,0 +1,3 @@
1
+ module Mdash
2
+ VERSION = "0.1.0"
3
+ end
data/lib/mdash.rb ADDED
@@ -0,0 +1,32 @@
1
+ require "mdash/version"
2
+ require "mdash/engine"
3
+ require "mdash/configuration"
4
+ require "groupdate"
5
+
6
+ module Mdash
7
+ class Error < StandardError; end
8
+
9
+ class << self
10
+ def configure
11
+ yield config
12
+ end
13
+
14
+ def config
15
+ @config ||= Configuration.new
16
+ end
17
+
18
+ def stats
19
+ # Check the last time stats were updated
20
+ # If it's been more than 5 minutes, update the stats
21
+ if @last_updated.nil? || @last_updated < 5.minutes.ago
22
+ @stats = config.metrics.reduce({}) do |stats, metric|
23
+ stats[metric.id] = metric.data
24
+ stats
25
+ end
26
+ @last_updated = Time.now
27
+ end
28
+ @stats
29
+ end
30
+ end
31
+ end
32
+
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :mdash do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mdash
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Tim Marks
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 1980-01-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 7.0.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 7.0.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: groupdate
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '6.5'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '6.5'
41
+ description: mdash-rails exposes metrics for your Rails app to be consumed by the
42
+ mdash app.
43
+ email:
44
+ - t@imothee.com
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - MIT-LICENSE
50
+ - README.md
51
+ - Rakefile
52
+ - app/controllers/mdash/announce_controller.rb
53
+ - app/controllers/mdash/application_controller.rb
54
+ - app/controllers/mdash/stats_controller.rb
55
+ - app/helpers/mdash/application_helper.rb
56
+ - app/jobs/mdash/application_job.rb
57
+ - app/mailers/mdash/application_mailer.rb
58
+ - app/models/mdash/application_record.rb
59
+ - app/models/mdash/metric.rb
60
+ - config/routes.rb
61
+ - lib/generators/mdash/install_generator.rb
62
+ - lib/generators/mdash/templates/config/initializers/mdash.rb.tt
63
+ - lib/mdash.rb
64
+ - lib/mdash/configuration.rb
65
+ - lib/mdash/engine.rb
66
+ - lib/mdash/version.rb
67
+ - lib/tasks/mdash_tasks.rake
68
+ homepage: https://mdash.app
69
+ licenses:
70
+ - MIT
71
+ metadata:
72
+ allowed_push_host: https://rubygems.org
73
+ homepage_uri: https://mdash.app
74
+ source_code_uri: https://github.com/imothee/mdash-rails
75
+ changelog_uri: https://github.com/imothee/mdash-rails/CHANGELOG.md
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: 3.0.0
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubygems_version: 3.5.22
92
+ signing_key:
93
+ specification_version: 4
94
+ summary: mdash-rails exposes metrics for your Rails app to be consumed by the mdash
95
+ app.
96
+ test_files: []