rails-server-monitor 0.1.8 → 0.1.9

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 42a5e4efd2fa04d41325825215360f65ae66c648a9a3b117f32b3bec8c59d6b7
4
- data.tar.gz: 5ec3f5a55ea2c9fd59b4c667688c16cd8f3a9fd415b37a96bfa4c4a5c1dad6da
3
+ metadata.gz: 3e47312d01c09679dc3df5b1b4d042bd7a7d7d0290d55902d7fe4ef8dd6e7a22
4
+ data.tar.gz: b30f2db395c5c25eeae2ffd1fa639aa8f277c35d25d0fd6a3fbeacdeda662dcc
5
5
  SHA512:
6
- metadata.gz: 3ef74d34f33f862cdccf698e1f5153aff9f6176a19121b4c3b087e9539ee39a46a1d6648de72315f1c152df6f6dd0767b8168af0787b47262d6ce876394d3e65
7
- data.tar.gz: f0a805d4f4573b09fb7f65e03dd0c2f872ee9055acc34a42d1db32fec2c164c844436ac83f3d0a5bccf7692f68a55319daa989198ab2218ee447fc35aa90693e
6
+ metadata.gz: e2b2ad6e4df19eb305bfe9f02e4d1d4034519acdd70d0558960e2d7bcc55e16acccb8ff12dd51639d76927e2ae16cde848f76d62031581145061a556304672cb
7
+ data.tar.gz: 446a671e8cd3918173c10f82ad5f8f63364224fb05559e860e4fdbcfc758352ef44c28f6ea2edcb8a26e60b761f22b4c9a424a8269d40e94b7678130bcb411b0
data/README.md CHANGED
@@ -1,28 +1,129 @@
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.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
+ ```shell
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
+ or if you are using
36
+
37
+ ```ruby
38
+ # inside bin/webpack-dev-server
39
+ require "rails_server_monitor/compile_locally"
40
+ RailsServerMonitor::CompileLocally.compile
41
+
42
+ # insert before this before
43
+ # Dir.chdir(APP_ROOT) do
44
+ # Webpacker::DevServerRunner.run(ARGV)
45
+ # end
46
+ ```
47
+
48
+ #### 5) (Optional) configure gem
49
+
50
+ ```ruby
51
+ # inside config/initializers/rails_server_monitor
52
+
53
+ RailsServerMonitor.config do |c|
54
+ c.update_server_interval = 1.hour
55
+ c.snapshot_server_interval = 15.minutes
56
+ c.cleanup_snapshots_after = 90.days
57
+ c.ignore_workers = %w(MyIgnoredWorker)
58
+ c.ignore_urls = ["/", /ignored-url/]
59
+
60
+ c.high_cpu_usage_threshold = 95
61
+ c.low_memory_threshold = 20
62
+ c.low_free_disk_disk_threshold = 30
63
+ c.hostname = -> do
64
+ `hostname`
65
+ end # how to retrieve server hostname, heroku generates a hostname each time your app reboots
66
+ end
67
+ ```
68
+
69
+ #### 6) Mount engine to routes
70
+
71
+ ```ruby
72
+ # inside config/routes.rb
73
+ constraints LoggedInAsAdmin do
74
+ mount RailsServerMonitor::Engine => "/system-information"
75
+ end
76
+ ```
77
+
78
+ Your should define a constraint to protect against unauthorized access
79
+
80
+ ```ruby
81
+ # example LoggedInAsAdmin, this is a basic implementation
82
+
83
+ class LoggedInAsAdmin
84
+ def self.matches?(request)
85
+ AdminUser.find_by(id: request.session[:admin_id]).present?
86
+ end
87
+ end
88
+
89
+
22
90
  ```
23
91
 
92
+ #### 7) (Optional) Add sidekiq middleware
93
+
94
+ ```ruby
95
+ # append to config/sidekiq.rb
96
+
97
+ Sidekiq.configure_server do |config|
98
+ config.server_middleware do |chain|
99
+ chain.add RailsServerMonitor::SidekiqMiddleware
100
+ end
101
+ end
102
+ ```
103
+
104
+ ### 8) Result
105
+
106
+ you should now go to http://localhost:3000/system-information
107
+
24
108
  ## Contributing
25
- Contribution directions go here.
109
+
110
+ Everyone is encouraged to help improve this project. Here are a few ways you can help:
111
+
112
+ - [Report bugs](https://github.com/personal-social-media/rails-server-monitor/issues)
113
+ - Fix bugs and [submit pull requests](https://github.com/personal-social-media/rails-server-monitor/pulls)
114
+ - Write, clarify, or fix documentation
115
+ - Suggest or add new features
116
+
117
+ ### Development
118
+
119
+ ```shell
120
+ # fork project, clone
121
+ bundle install
122
+ yarn install
123
+
124
+ bin/start # to start local server
125
+ bin/rspec # to run specs
126
+ ```
26
127
 
27
128
  ## License
28
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
129
+ 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
@@ -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 %>
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails"
4
+ require_relative "./engine"
5
+ module RailsServerMonitor
6
+ class CompileLocally
7
+ class << self
8
+ def compile
9
+ new.compile(skip_check: true)
10
+ end
11
+
12
+ def compile_force
13
+ new.compile(skip_check: false)
14
+ end
15
+ end
16
+
17
+ def compile(skip_check:)
18
+ unless skip_check
19
+ return unless Rails.env.development? || Rails.env.test?
20
+ end
21
+ return if Dir.exist?(RailsServerMonitor::Engine.root.join("public", "rails-server-monitor-packs"))
22
+
23
+ RailsServerMonitor.webpacker.commands.compile
24
+ end
25
+ end
26
+ end
@@ -4,7 +4,7 @@ module RailsServerMonitor
4
4
  class Configuration
5
5
  attr_writer :update_server_interval, :snapshot_server_interval, :ignore_urls,
6
6
  :cleanup_snapshots_after, :ignore_workers, :high_cpu_usage_threshold,
7
- :low_memory_threshold, :low_free_disk_disk_threshold
7
+ :low_memory_threshold, :low_free_disk_disk_threshold, :hostname
8
8
 
9
9
  def update_server_interval
10
10
  @update_server_interval || 1.hour
@@ -15,7 +15,7 @@ module RailsServerMonitor
15
15
  end
16
16
 
17
17
  def cleanup_snapshots_after
18
- @cleanup_snapshots_after || 30.days
18
+ @cleanup_snapshots_after || 90.days
19
19
  end
20
20
 
21
21
  def ignore_urls
@@ -37,5 +37,9 @@ module RailsServerMonitor
37
37
  def low_free_disk_disk_threshold
38
38
  @low_free_disk_disk_threshold || 30
39
39
  end
40
+
41
+ def hostname
42
+ -> { `hostname` }
43
+ end
40
44
  end
41
45
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RailsServerMonitor
4
- VERSION = "0.1.8"
4
+ VERSION = "0.1.9"
5
5
  end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ Gem.post_install do
4
+ puts "post_install called for gem"
5
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails-server-monitor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.8
4
+ version: 0.1.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Personal Social Media
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-06-20 00:00:00.000000000 Z
11
+ date: 2021-06-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec-rails
@@ -246,7 +246,9 @@ files:
246
246
  - app/views/layouts/rails_server_monitor/application.html.erb
247
247
  - app/views/layouts/rails_server_monitor/application_record.rb
248
248
  - app/views/rails_server_monitor/home/index.html.erb
249
+ - app/views/rails_server_monitor/page/_footer.html.erb
249
250
  - app/views/rails_server_monitor/servers/show.html.erb
251
+ - app/views/rails_server_monitor/servers/show/_chart.html.erb
250
252
  - app/views/rails_server_monitor/snapshots/show.html.erb
251
253
  - babel.config.js
252
254
  - bin/rails
@@ -264,9 +266,11 @@ files:
264
266
  - lib/generators/rails_server_monitor/templates/install.rb.tt
265
267
  - lib/rails/server/monitor/engine.rb
266
268
  - lib/rails_server_monitor.rb
269
+ - lib/rails_server_monitor/compile_locally.rb
267
270
  - lib/rails_server_monitor/configuration.rb
268
271
  - lib/rails_server_monitor/engine.rb
269
272
  - lib/rails_server_monitor/version.rb
273
+ - lib/rubygems_plugin.rb
270
274
  - lib/tasks/rails_server_monitor_tasks.rake
271
275
  - package.json
272
276
  - postcss.config.js