Skip to content

Commit

Permalink
Merge pull request #17 from glaszig/replace-google-charts
Browse files Browse the repository at this point in the history
Replace Google Charts
  • Loading branch information
benschwarz authored May 30, 2022
2 parents 46d90b9 + 9691c98 commit 3890027
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.5.8
2.5.9
13 changes: 0 additions & 13 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -90,19 +90,6 @@ on heroku, like this:

heroku config:add AMNESIA_CREDS=ben:schwarz

### Content Security Policy

Amnesia uses Google Charts to display pie charts. In case you're employing
a CSP header you'll need to add the Google Chart servers as a valid image source, e.g.:

Content-Security-Policy: img-src 'self' data: https: https://chart.apis.google.com;

Within a Rack app you can use [Rack::Protection](http://www.sinatrarb.com/protection):

```rb
use Rack::Protection::ContentSecurityPolicy, img-src: "'self' data: https: https://chart.apis.google.com"
```

## Potential issues

* Hosts are listed as "Inactive" or "Not Responding"
Expand Down
3 changes: 1 addition & 2 deletions amnesia.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = "amnesia"
s.version = "1.0.2"
s.version = "1.1.0"
s.platform = Gem::Platform::RUBY
s.authors = ["Ben Schwarz"]
s.email = ["ben.schwarz@gmail.com"]
Expand All @@ -12,7 +12,6 @@ Gem::Specification.new do |s|

s.add_dependency "sinatra"
s.add_dependency "dalli"
s.add_dependency "googlecharts", "~> 1.6.8"
s.add_dependency "haml"

s.add_development_dependency "rspec", "~> 3.9"
Expand Down
1 change: 0 additions & 1 deletion lib/amnesia.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require 'sinatra/base'
require 'googlecharts'
require 'haml'
require 'amnesia/authentication'
require 'amnesia/helpers'
Expand Down
2 changes: 1 addition & 1 deletion lib/amnesia/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module HelperMethods
SIZE_UNITS = %w[ Bytes KB MB GB TB PB EB ]

def graph_url(*data)
Gchart.pie(data: data, size: '115x115', bg: 'ffffff00', use_ssl: true)
url "/pie?d="+data.join(',')
end

# https://github.com/rails/rails/blob/fbe335cfe09bf0949edfdf0c4b251f4d081bd5d7/activesupport/lib/active_support/number_helper/number_to_human_size_converter.rb
Expand Down
3 changes: 2 additions & 1 deletion lib/amnesia/host.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ module Amnesia
class Host
FLOAT_STATS = %w[ rusage_user rusage_system ]
STRING_STATS = %w[ version libevent ]
DEFAULT_PORT = 11_211

def self.normalize_address address
return "#{address}:#{Dalli::Server::DEFAULT_PORT}" unless address.include? ":"
return "#{address}:#{DEFAULT_PORT}" unless address.include? ":"

address
end
Expand Down
3 changes: 1 addition & 2 deletions lib/amnesia/public/css/application.css
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,9 @@ header { margin: 1em 0; }
align-items: center;
margin: 2em 0;
}
.stats-graph img { margin-left: -10px; }
.stats-text {
flex: 1 1;
margin-left: 1em;
margin-left: 1.5em;
}

.graph-indicator { color: #ff9900; }
Expand Down
21 changes: 21 additions & 0 deletions lib/amnesia/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,27 @@ def self.included app
haml :index
end

get '/pie' do
data = params[:d].to_s.split(",").map(&:to_i)

r = 25
c = (2*Math::PI*r).ceil
v = data.first.to_f / data.last * 100 * c / 100
v = v.nan? ? 0 : v.ceil

content_type "image/svg+xml"
<<~BODY
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"
style="transform:rotate(-90deg);">
<circle r="#{r*2}" cx="50" cy="50" fill="#FFEBCC" />
<circle r="#{r}" cx="50" cy="50"
fill="transparent" stroke="#FF9901"
stroke-width="#{r*2}" stroke-dasharray="#{v} #{c}"
/>
</svg>
BODY
end

get '/:address' do
@host = find_host params[:address]
@host ? haml(:host) : halt(404)
Expand Down
6 changes: 3 additions & 3 deletions lib/amnesia/views/host.haml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

%article.stats
%section.stats-graph
%img{src: graph_url(@host.bytes, @host.limit_maxbytes) }
%img{src: graph_url(@host.bytes, @host.limit_maxbytes), width: 90}
%section.stats-text
%h3
%span.graph-indicator Used (#{number_to_human_size(@host.bytes)})
Expand All @@ -15,7 +15,7 @@

%article.stats
%section.stats-graph
%img{src: graph_url(@host.get_hits, @host.get_misses) }
%img{src: graph_url(@host.get_hits, @host.get_misses), width: 90}
%section.stats-text
%h3
%span.graph-indicator Hit (#{@host.get_hits})
Expand All @@ -24,7 +24,7 @@

%section.stats
%section.stats-graph
%img{src: graph_url(@host.cmd_get, @host.cmd_set) }
%img{src: graph_url(@host.cmd_get, @host.cmd_set), width: 90}
%section.stats-text
%h3
%span.graph-indicator Read (#{@host.cmd_get})
Expand Down
6 changes: 3 additions & 3 deletions lib/amnesia/views/index.haml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
- if alive_hosts.any?
%article.stats
%section.stats-graph
%img{src: graph_url(bytes_sum, limit_maxbytes_sum)}
%img{src: graph_url(bytes_sum, limit_maxbytes_sum), width: 90}
%section.stats-text
%h3
%span.graph-indicator Used (#{number_to_human_size(bytes_sum)})
Expand All @@ -10,7 +10,7 @@

%article.stats
%section.stats-graph
%img{src: graph_url(get_hits_sum, get_misses_sum)}
%img{src: graph_url(get_hits_sum, get_misses_sum), width: 90}
%section.stats-text
%h3
%span.graph-indicator Hit (#{get_hits_sum})
Expand All @@ -19,7 +19,7 @@

%article.stats
%section.stats-graph
%img{src: graph_url(cmd_get_sum, cmd_set_sum)}
%img{src: graph_url(cmd_get_sum, cmd_set_sum), width: 90}
%section.stats-text
%h3
%span.graph-indicator Read (#{cmd_get_sum})
Expand Down

0 comments on commit 3890027

Please sign in to comment.