rails-server-monitor 0.1.7 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a4c02a410440fe656a5775b8fcbf634cde598ccfbdc931ea6df0c100fd52b566
4
- data.tar.gz: d9f857b9f0cfa8da45543c2f9b5a7fdb8aa39388bb31a6020dfe66c9c90984e6
3
+ metadata.gz: 033a9d13d063db61dac341da383d08f124f71b1a4a7dcb6c3b971d33c8fb5b7b
4
+ data.tar.gz: 9a3cba2a858b60e041783670aedcc2350646aeaee0a8edb5b83980d0ae9303ce
5
5
  SHA512:
6
- metadata.gz: 68a29e610bb3bb98ba4381c930301fd39c74e9195eeaf173d8803f58dec0152f9076a2897f88f917119602ef6ee0567f53ae53aace1edf0b2cc905c15e14e62f
7
- data.tar.gz: b17b84868d09d28008763b049601124eac946b34d42d3b3f20aa73e2e38bf0a3bb3f7f983c01db6a8ca19645ef12494c898250f8f7cec5c5c9d06c561b9db46e
6
+ metadata.gz: da14da9f1a6edbea7dfbadeebba08f0166e4febbe2b8762efa1915d7b703f6c68a9294d2250f9b7e30eb26174182eb15bd530a0b8c6019bf6992515b8e411304
7
+ data.tar.gz: a7c82165c201bf319a4a3f31f626f7093c93d42f0a81774dd3513bb3659c3479d0e6e04ea1052e870102b4814a054b2ce3c89253be8d40a2813e76b80375fe60
data/README.md CHANGED
@@ -1,28 +1,130 @@
1
- # Rails::Server::Monitor
2
- Short description and motivation.
1
+ # Rails Server Monitor
2
+ A performance dashboard for rails applications which work with one or multiple servers at once.
3
+ Stats can be collected from a Rack application or Sidekiq.
3
4
 
4
- ## Usage
5
- How to use my plugin.
5
+ [See it in action](https://rails-sever-monitor.herokuapp.com)
6
6
 
7
- ## Installation
8
- Add this line to your application's Gemfile:
7
+ ![demo](https://github.com/personal-social-media/rails-server-monitor/raw/master/resources/screenshot-dashboard.jpg)
8
+ ![demo](https://github.com/personal-social-media/rails-server-monitor/raw/master/resources/screenshot1.jpg)
9
9
 
10
+ ## Usage
11
+ #### 1)
10
12
  ```ruby
11
- gem 'rails-server-monitor'
13
+ gem 'rails-server-monitor', require: "rails_server_monitor"
12
14
  ```
13
15
 
14
- And then execute:
16
+ #### 2) generate Rails Server Monitor tables
15
17
  ```bash
16
- $ bundle
18
+ rails g rails_server_monitor:install
19
+ rails db:migrate
17
20
  ```
18
21
 
19
- Or install it yourself as:
20
- ```bash
21
- $ gem install rails-server-monitor
22
+ #### 3) hook Rails Server Monitor's rack middleware
23
+ ```ruby
24
+ # inside config/application.rb
25
+ config.middleware.use RailsServerMonitor::RackMiddleware
26
+ ```
27
+
28
+ #### 4) compile Rails Server Monitor assets locally
29
+ if you aren't using webpacker already
30
+ ```shell
31
+ # you don't need to do this in production, it's prepended to rake assets:precompile
32
+ # but you need to do it again after you after you update the gem locally
33
+ rake webpacker:compile
34
+ ```
35
+
36
+ or if you are using
37
+
38
+ ```ruby
39
+ # inside bin/webpack-dev-server
40
+ require "rails_server_monitor/compile_locally"
41
+ RailsServerMonitor::CompileLocally.compile
42
+
43
+ # insert before this before
44
+ # Dir.chdir(APP_ROOT) do
45
+ # Webpacker::DevServerRunner.run(ARGV)
46
+ # end
47
+ ```
48
+
49
+ #### 5) (Optional) configure gem
50
+
51
+ ```ruby
52
+ # inside config/initializers/rails_server_monitor
53
+
54
+ RailsServerMonitor.config do |c|
55
+ c.update_server_interval = 1.hour
56
+ c.snapshot_server_interval = 15.minutes
57
+ c.cleanup_snapshots_after = 90.days
58
+ c.ignore_workers = %w(MyIgnoredWorker)
59
+ c.ignore_urls = ["/", /ignored-url/]
60
+
61
+ c.high_cpu_usage_threshold = 95
62
+ c.low_memory_threshold = 20
63
+ c.low_free_disk_disk_threshold = 30
64
+ c.hostname = -> do
65
+ `hostname`
66
+ end # how to retrieve server hostname, heroku generates a hostname each time your app reboots
67
+ end
68
+ ```
69
+
70
+ #### 6) Mount engine to routes
71
+
72
+ ```ruby
73
+ # inside config/routes.rb
74
+ constraints LoggedInAsAdmin do
75
+ mount RailsServerMonitor::Engine => "/system-information"
76
+ end
22
77
  ```
23
78
 
79
+ Your should define a constraint to protect against unauthorized access
80
+
81
+ ```ruby
82
+ # example LoggedInAsAdmin, this is a basic implementation
83
+
84
+ class LoggedInAsAdmin
85
+ def self.matches?(request)
86
+ AdminUser.find_by(id: request.session[:admin_id]).present?
87
+ end
88
+ end
89
+
90
+
91
+ ```
92
+
93
+ #### 7) (Optional) Add sidekiq middleware
94
+
95
+ ```ruby
96
+ # append to config/sidekiq.rb
97
+
98
+ Sidekiq.configure_server do |config|
99
+ config.server_middleware do |chain|
100
+ chain.add RailsServerMonitor::SidekiqMiddleware
101
+ end
102
+ end
103
+ ```
104
+
105
+ ### 8) Result
106
+
107
+ you should now go to http://localhost:3000/system-information
108
+
24
109
  ## Contributing
25
- Contribution directions go here.
110
+
111
+ Everyone is encouraged to help improve this project. Here are a few ways you can help:
112
+
113
+ - [Report bugs](https://github.com/personal-social-media/rails-server-monitor/issues)
114
+ - Fix bugs and [submit pull requests](https://github.com/personal-social-media/rails-server-monitor/pulls)
115
+ - Write, clarify, or fix documentation
116
+ - Suggest or add new features
117
+
118
+ ### Development
119
+
120
+ ```shell
121
+ # fork project, clone
122
+ bundle install
123
+ yarn install
124
+
125
+ bin/start # to start local server
126
+ bin/rspec # to run specs
127
+ ```
26
128
 
27
129
  ## License
28
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
130
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -6,11 +6,20 @@ module RailsServerMonitor
6
6
 
7
7
  def show
8
8
  @server = current_server
9
- @chart = RailsServerMonitor::ChartForServer.new(@server)
9
+ @chart = RailsServerMonitor::ChartForServer.new(@server, timeline: params[:timeline])
10
10
 
11
11
  @title = @server.display_name
12
12
  end
13
13
 
14
+ def update
15
+ update_params = params.require(:server).permit(:custom_name, :custom_description)
16
+ current_server.update!(update_params)
17
+ redirect_to server_path(current_server)
18
+
19
+ rescue ActiveRecord::RecordInvalid => e
20
+ render json: { error: e.message }, status: 422
21
+ end
22
+
14
23
  def current_server
15
24
  @current_server ||= RailsServerMonitor::Server.find_by(id: params[:id])
16
25
  end
@@ -25,5 +25,9 @@ module RailsServerMonitor
25
25
 
26
26
  stylesheet_pack_tag(name, **options)
27
27
  end
28
+
29
+ def server_version
30
+ RailsServerMonitor::VERSION
31
+ end
28
32
  end
29
33
  end
@@ -2,16 +2,18 @@
2
2
 
3
3
  module RailsServerMonitor
4
4
  class SidekiqMiddleware
5
- def call(worker, *opts)
6
- return if ignore_worker?(worker)
5
+ def call(worker, *_)
6
+ return yield if ignore_worker?(worker)
7
7
 
8
- server = RailsServerMonitor::ServerSetup.new.call
9
- snapshot = RailsServerMonitor::TakeSnapshot.new(server)
8
+ yield.tap do
9
+ server = RailsServerMonitor::ServerSetup.new.call
10
+ snapshot = RailsServerMonitor::TakeSnapshot.new(server)
10
11
 
11
- if snapshot.can_take_snapshot?
12
- snapshot.call
12
+ if snapshot.can_take_snapshot?
13
+ snapshot.call
13
14
 
14
- RailsServerMonitor::Cleanup.new.call
15
+ RailsServerMonitor::Cleanup.new.call
16
+ end
15
17
  end
16
18
  end
17
19
 
@@ -20,8 +20,14 @@ module RailsServerMonitor
20
20
  foreign_key: :rails_server_monitor_server_id,
21
21
  dependent: :delete_all
22
22
 
23
+ before_save :squish_text
24
+
23
25
  def display_name
24
26
  @display_name ||= custom_name.present? ? custom_name : hostname
25
27
  end
28
+
29
+ def squish_text
30
+ self.custom_name = self.custom_name&.squish
31
+ end
26
32
  end
27
33
  end
@@ -2,10 +2,11 @@
2
2
 
3
3
  module RailsServerMonitor
4
4
  class ChartForServer
5
+ AVAILABLE_TIMELINES = %w(today week month all)
5
6
  attr_reader :server, :timeline
6
- def initialize(server, timeline: :today)
7
+ def initialize(server, timeline:)
7
8
  @server = server
8
- @timeline = timeline
9
+ @timeline = timeline.blank? ? "today" : timeline
9
10
  end
10
11
 
11
12
  def render_chart
@@ -23,10 +24,9 @@ module RailsServerMonitor
23
24
  end
24
25
 
25
26
  def today?
26
- timeline == :today
27
+ timeline == "today"
27
28
  end
28
29
 
29
-
30
30
  def last_record
31
31
  @last_record = scope.last
32
32
  end
@@ -36,6 +36,12 @@ module RailsServerMonitor
36
36
  query = server.server_snapshots.order(id: :asc)
37
37
  if today?
38
38
  query = query.where("created_at > ?", 1.day.ago)
39
+ elsif timeline == "week"
40
+ query = query.where("created_at > ?", 7.day.ago)
41
+ elsif timeline == "month"
42
+ query = query.where("created_at > ?", 30.day.ago)
43
+ else
44
+ query = query.all
39
45
  end
40
46
  @scope = query.to_a
41
47
  end
@@ -19,7 +19,7 @@ module RailsServerMonitor
19
19
 
20
20
  private
21
21
  def hostname
22
- @hostname ||= `hostname`.squish
22
+ @hostname ||= config.hostname.call.squish
23
23
  end
24
24
 
25
25
  def update_server
@@ -9,17 +9,22 @@
9
9
  <%= safe_render_css "rails-server-application", 'data-turbolinks-track': "reload" %>
10
10
  </head>
11
11
  <body>
12
+ <div class="flex min-h-screen flex-col justify-between">
13
+ <div class="flex">
14
+ <div class="overflow-y-auto fixed top-0 left-0 bottom-0 bg-white w-1/5">
15
+ <%= render RailsServerMonitor::LeftbarComponent.new(ctx: self) %>
16
+ </div>
17
+ <div class="w-1/5 mr-4"></div>
12
18
 
13
- <div class="flex">
14
- <div class="overflow-y-auto fixed top-0 left-0 bottom-0 bg-white w-1/5">
15
- <%= render RailsServerMonitor::LeftbarComponent.new(ctx: self) %>
16
- </div>
17
- <div class="w-1/5 mr-4"></div>
19
+ <div class="flex-1">
20
+ <%= yield %>
21
+ </div>
18
22
 
19
- <div class="flex-1">
20
- <%= yield %>
23
+ </div>
24
+ <div class="p-4 flex">
25
+ <div class="w-1/5 mr-4"></div>
26
+ <%= render "rails_server_monitor/page/footer" %>
21
27
  </div>
22
28
  </div>
23
-
24
29
  </body>
25
30
  </html>
@@ -0,0 +1,8 @@
1
+ <div class="flex-1">
2
+ <a href="https://github.com/personal-social-media/rails-server-monitor" target="_blank" class="flex items-center mx-auto w-1/4">
3
+ <i class="fab fa-github fa-3x"></i>
4
+ <span class="ml-2">
5
+ Rails Server Monitor V<%= server_version %>
6
+ </span>
7
+ </a>
8
+ </div>
@@ -3,11 +3,8 @@
3
3
  <%= @server.display_name %>
4
4
  </div>
5
5
 
6
-
7
6
  <div class="flex mt-4">
8
- <%= content_tag :div, class: "#{@chart.today? ? "w-1/2" : "w-full"}" do %>
9
- <%= line_chart @chart.render_chart %>
10
- <% end %>
7
+ <%= render(partial: "rails_server_monitor/servers/show/chart") %>
11
8
 
12
9
  <% if @chart.today? && @chart.last_record.present? %>
13
10
  <div class="w-1/2 p-2">
@@ -49,7 +46,7 @@
49
46
  <% end %>
50
47
  </div>
51
48
 
52
- <div class="mt-20">
49
+ <%= form_with model: @server, html: { class: "mt-20" } do |f| %>
53
50
  <div class="mb-2 text-xl">
54
51
  Sever information:
55
52
  </div>
@@ -64,13 +61,13 @@
64
61
  Custom name:
65
62
  </div>
66
63
  <div class="p-1">
67
- <%= @server.custom_name %>
64
+ <%= f.text_field :custom_name, class: "border border-solid border-gray-200 p-2 focus:border-gray-300 w-full" %>
68
65
  </div>
69
66
  <div class="bg-blue-300 text-gray-800 p-1">
70
67
  Custom description:
71
68
  </div>
72
69
  <div class="p-1">
73
- <%= @server.custom_description %>
70
+ <%= f.text_area :custom_description, class: "border border-solid border-gray-200 p-2 focus:border-gray-300 w-full" %>
74
71
  </div>
75
72
  <div class="mb-2 text-xl">
76
73
  OS information
@@ -110,7 +107,7 @@
110
107
  )) %>
111
108
 
112
109
  <%= render(RailsServerMonitor::ServerTableRow.new(
113
- title: "CPU Codes",
110
+ title: "CPU Cores",
114
111
  value: @server.system_cpu_cores
115
112
  )) %>
116
113
 
@@ -129,5 +126,9 @@
129
126
  value: "#{@server.system_hdd_available_in_gb} GB"
130
127
  )) %>
131
128
  </div>
132
- </div>
129
+
130
+ <div class="mt-20 w-1/2">
131
+ <%= f.submit "Save custom information", class: "p-2 w-full bg-blue-500 text-white" %>
132
+ </div>
133
+ <% end %>
133
134
  </div>
@@ -0,0 +1,10 @@
1
+ <%= content_tag :div, class: "#{@chart.today? ? "w-1/2" : "w-full"}" do %>
2
+ <%= line_chart @chart.render_chart %>
3
+
4
+ <div class="flex justify-between">
5
+ <% RailsServerMonitor::ChartForServer::AVAILABLE_TIMELINES.each do |timeline| %>
6
+ <%= link_to timeline.humanize, server_path(@server, timeline: timeline),
7
+ class: "#{@chart.timeline == timeline ? "bg-blue-500 text-white" : "bg-gray-300 text-gray-800" } p-2 rounded hover:bg-blue-400" %>
8
+ <% end %>
9
+ </div>
10
+ <% end %>
data/bin/rails ADDED
@@ -0,0 +1,24 @@
1
+ #!/usr/bin/env ruby
2
+ # This command will automatically be run when you run "rails" with Rails gems
3
+ # installed from the root of your application.
4
+
5
+ ENGINE_ROOT = File.expand_path('..', __dir__)
6
+ ENGINE_PATH = File.expand_path('../lib/rails/server/monitor/engine', __dir__)
7
+
8
+ # Set up gems listed in the Gemfile.
9
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__)
10
+ require "bundler/setup" if File.exist?(ENV["BUNDLE_GEMFILE"])
11
+
12
+ require "rails"
13
+ # Pick the frameworks you want:
14
+ require "active_model/railtie"
15
+ # require "active_job/railtie"
16
+ require "active_record/railtie"
17
+ # require "active_storage/engine"
18
+ require "action_controller/railtie"
19
+ # require "action_mailer/railtie"
20
+ require "action_view/railtie"
21
+ # require "action_cable/engine"
22
+ # require "sprockets/railtie"
23
+ # require "rails/test_unit/railtie"
24
+ require "rails/engine/commands"
data/bin/rspec ADDED
@@ -0,0 +1,5 @@
1
+ #!/bin/bash
2
+
3
+ cd dummy
4
+
5
+ bin/rspec
data/bin/start ADDED
@@ -0,0 +1,4 @@
1
+ #!/bin/bash
2
+
3
+ cd dummy
4
+ bundle exec foreman start
data/bin/webpack ADDED
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ ENV["RAILS_ENV"] ||= ENV["RACK_ENV"] || "development"
4
+ ENV["NODE_ENV"] ||= "development"
5
+
6
+ require "pathname"
7
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
8
+ Pathname.new(__FILE__).realpath)
9
+
10
+ require "bundler/setup"
11
+
12
+ require "webpacker"
13
+ require "webpacker/webpack_runner"
14
+
15
+ APP_ROOT = File.expand_path("..", __dir__)
16
+ Dir.chdir(APP_ROOT) do
17
+ Webpacker::WebpackRunner.run(ARGV)
18
+ end