gfspark 0.0.1 → 0.1.1

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.
data/.gitignore CHANGED
@@ -15,3 +15,5 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
+ vendor/
19
+ .gfspark
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Growth Forecast on Terminal.
4
4
 
5
- gfspark is CLI graph viewer for Growth Forecast.
5
+ gfspark is a CLI graph viewer for Growth Forecast.
6
6
 
7
7
  ## Installation
8
8
 
@@ -18,8 +18,14 @@ Or install it yourself as:
18
18
 
19
19
  $ gem install gfspark
20
20
 
21
+ ## ScreenShots
22
+
23
+ <img src='https://github.com/yuroyoro/gfspark/raw/master/images/gfspark_screenshot.png' width='600'/>
24
+
21
25
  ## Usage
22
26
 
27
+ gfspark : Growth Forecast on Terminal
28
+
23
29
  usage: gfspark <url|path|service_name> [section_name] [graph_name]
24
30
 
25
31
  Examples:
@@ -32,15 +38,53 @@ Or install it yourself as:
32
38
  -u, --user=USER
33
39
  -p, --pass=PASS
34
40
  -t=VALUE Range of Graph
35
- --from=VALUE Start date of graph.(2011/12/08 12:10:00) required if t=c or sc
36
- --to=VALUE End date of graph.(2011/12/08 12:10:00) required if t=c or sc
37
- -h, --height=VALUE graph height.(default 10
38
- -w, --width=VALUE graph width.(default is deteced from $COLUMNS
39
- -c, --color=VALUE Color of graph bar
41
+ --gmode=VALUE graph mode: gauge or subtract (default is gauge)
42
+ --from=VALUE Start date of graph (2011/12/08 12:10:00) required if t=c or sc
43
+ --to=VALUE End date of graph (2011/12/08 12:10:00) required if t=c or sc
44
+ -h, --height=VALUE graph height (default 10
45
+ -w, --width=VALUE graph width (default is deteced from $COLUMNS)
46
+ -c, --color=VALUE Color of graph bar (black/red/green/yellow/blue/magenta/cyan/white)
47
+ -n, --non-fullwidth-font Show bar symbol as fullwidth
40
48
  --sslnoverify don't verify SSL
41
49
  --sslcacert=v SSL CA CERT
42
50
  --debug debug print
43
51
 
52
+ -t option detail:
53
+ y : Year (1day avg)
54
+ m : Month (2hour avg)
55
+ w : Week (30min avg)
56
+ 3d : 3 Days (5min avg)
57
+ s3d : 3 Days (5min avg)
58
+ d : Day (5min avg)
59
+ sd : Day (1min avg)
60
+ 8h : 8 Hours (5min avg)
61
+ s8h : 8 Hours (1min avg)
62
+ 4h : 4 Hours (5min avg)
63
+ s4h : 4 Hours (1min avg)
64
+ h : Hour (5min avg),
65
+ sh : Hour (1min avg)
66
+ n : Half Day (5min avg)
67
+ sn : Half Day (1min avg)
68
+ c : Custom (5min avg)
69
+ sc : Custom (1min avg)
70
+
71
+ gfspark does not supports "COMPLEX GRAPH".
72
+ if graph output is broken, try `--non-fullwidth-font` option.
73
+ For example, your Terminal font is "Rickty", then it'll be fix graph output problem.
74
+
75
+ ## Configuration
76
+
77
+ gfspark load default settings from `.gfspark` file.
78
+ This configuration file is searched from current dirctory to pararents, or your home directory(~/.gfspark).
79
+
80
+ .gfspark is YAML file. It's contents is like bellow
81
+
82
+ url: http://your.growthforecast.com/
83
+ color: red
84
+ username: "your basic auth username"
85
+ password: "your basic auth password"
86
+ non_fullwidth_font: true
87
+
44
88
  ## Contributing
45
89
 
46
90
  1. Fork it
Binary file
data/lib/gfspark.rb CHANGED
@@ -27,10 +27,16 @@ module Gfspark
27
27
  app = nil
28
28
  begin
29
29
  app = Gfspark::App.new(argv)
30
- if app.valid
30
+ if app && app.debug
31
+ puts "App Instance Variables ----"
32
+ pp app
33
+ puts "---------------------------"
34
+ end
35
+
36
+ if app && app.valid
31
37
  status = app.execute
32
38
  else
33
- app.help
39
+ app.help if app
34
40
  return false
35
41
  end
36
42
  rescue => e
data/lib/gfspark/app.rb CHANGED
@@ -1,3 +1,4 @@
1
+ # -*- coding: utf-8 -*-
1
2
  class Gfspark::App
2
3
  include Term::ANSIColor
3
4
  include ::Gfspark::Config
@@ -17,12 +18,18 @@ class Gfspark::App
17
18
  @options = load_default_settings
18
19
  try_url(args) || try_path(args) || try_default(args)
19
20
 
20
- unless @url && @service && @section && @graph
21
- puts "Invalid Arguments"
21
+ required_args = [:url, :service, :section, :graph]
22
+ unless required_args.map(&:to_s).map(&"@".method(:+)).map(&method(:instance_variable_get)).inject(true, :&)
23
+ puts "Invalid Arguments: check arguments or your .gfspark file"
24
+ required_args.each do |n|
25
+ puts " #{n} is required" unless instance_variable_get("@#{n}")
26
+ end
27
+ puts ""
22
28
  @valid = false
23
29
  return
24
30
  end
25
31
 
32
+ @valid = true
26
33
  detect_width_and_height!
27
34
  set_ssl_options!
28
35
  end
@@ -31,17 +38,30 @@ class Gfspark::App
31
38
  json = fetch(:xport)
32
39
  summary = fetch(:summary)
33
40
 
34
- render(json, summary)
41
+ url = to_url(:view_graph)
42
+ render(json, summary, url)
35
43
  true
36
44
  end
37
45
 
46
+ def to_url(api)
47
+ url = "#{@url}/#{api.to_s}/#{@service}/#{@section}/#{@graph}"
48
+ queries = {}
49
+ queries[:t] = @options[:t] || "d"
50
+ queries[:width] = @width
51
+ queries[:gmode] = @options[:gmode] if @options[:gmode]
52
+ queries[:from] = @options[:from] if @options[:from]
53
+ queries[:to] = @options[:to] if @options[:to]
54
+ "#{url}?#{queries.map{|k,v| "#{k}=#{v}"}.join("&")}"
55
+ end
56
+
38
57
  def fetch(api)
39
58
  url = "#{@url}/#{api.to_s}/#{@service}/#{@section}/#{@graph}"
40
59
  queries = {}
41
60
  queries[:t] = @options[:t] || "d"
42
61
  queries[:width] = @width
43
- queries[:from] = @options[:from] if @options[:from].nil?
44
- queries[:to] = @options[:to] if @options[:to].nil?
62
+ queries[:gmode] = @options[:gmode] if @options[:gmode]
63
+ queries[:from] = @options[:from] if @options[:from].nil?
64
+ queries[:to] = @options[:to] if @options[:to].nil?
45
65
 
46
66
  json = fetch_json(url, {}, queries)
47
67
  end
@@ -1,3 +1,4 @@
1
+ # -*- coding: utf-8 -*-
1
2
  module Gfspark::Config
2
3
 
3
4
  def try_url(args)
@@ -60,6 +61,11 @@ module Gfspark::Config
60
61
  MSG
61
62
  puts " Options:"
62
63
  puts @opt_parse_obj.summarize
64
+ puts <<-MSG
65
+
66
+ -t option detail:
67
+ #{Gfspark::Graph::RANGE_DEFS.map{|k,v| sprintf(" %3s : %s", k, v)}.join("\n")}
68
+ MSG
63
69
  end
64
70
 
65
71
  def parse_options(args)
@@ -102,6 +108,7 @@ module Gfspark::Config
102
108
  return YAML.load_file filename if File.exists? filename
103
109
  end
104
110
 
111
+ COLORS = [ :black, :red, :green, :yellow, :blue, :magenta, :cyan, :white]
105
112
 
106
113
  def opt_parser
107
114
  OptionParser.new{|opts|
@@ -110,11 +117,13 @@ module Gfspark::Config
110
117
  opts.on('-u=USER', '--user=USER'){|v| @options[:username] = v }
111
118
  opts.on('-p=PASS', '--pass=PASS'){|v| @options[:password] = v }
112
119
  opts.on("-t=VALUE", "Range of Graph"){|v| @options[:t] = v}
113
- opts.on("--from=VALUE", "Start date of graph.(2011/12/08 12:10:00) required if t=c or sc"){|v| @options[:from] = v}
114
- opts.on("--to=VALUE", "End date of graph.(2011/12/08 12:10:00) required if t=c or sc"){|v| @options[:to] = v}
115
- opts.on("-h=VALUE", "--height=VALUE", "graph height.(default 10"){|v| @options[:height] = v}
116
- opts.on("-w=VALUE", "--width=VALUE", "graph width.(default is deteced from $COLUMNS"){|v| @options[:width] = v}
117
- opts.on("-c=VALUE", "--color=VALUE", "Color of graph bar"){|v| @options[:color] = v}
120
+ opts.on("--gmode=VALUE", "graph mode: gauge or subtract (default is gauge)"){|v| @options[:gmode] = v}
121
+ opts.on("--from=VALUE", "Start date of graph (2011/12/08 12:10:00) required if t=c or sc"){|v| @options[:from] = v}
122
+ opts.on("--to=VALUE", "End date of graph (2011/12/08 12:10:00) required if t=c or sc"){|v| @options[:to] = v}
123
+ opts.on("-h=VALUE", "--height=VALUE", "graph height (default 10"){|v| @options[:height] = v}
124
+ opts.on("-w=VALUE", "--width=VALUE", "graph width (default is deteced from $COLUMNS)"){|v| @options[:width] = v}
125
+ opts.on("-c=VALUE", "--color=VALUE", "Color of graph bar (#{COLORS.join("/")})"){|v| @options[:color] = v if COLORS.include?(v.to_sym)}
126
+ opts.on("-n", "--non-fullwidth-font", "Show bar symbol as fullwidth"){ @options[:non_fullwidth_font] = true }
118
127
  opts.on("--sslnoverify", "don't verify SSL"){|v| @options[:sslNoVerify] = true}
119
128
  opts.on("--sslcacert=v", "SSL CA CERT"){|v| @options[:sslCaCert] = v}
120
129
  opts.on("--debug", "debug print"){@debug= true }
@@ -1,3 +1,4 @@
1
+ # -*- coding: utf-8 -*-
1
2
  module Gfspark::Connection
2
3
  @ssl_options = {}
3
4
 
@@ -14,7 +15,7 @@ module Gfspark::Connection
14
15
 
15
16
  def fetch_json(url, options = {}, params = {})
16
17
  response = send_request(url, {},options, params, :get)
17
- raise response.code unless response_success?(response)
18
+ raise "#{response.code} #{response.msg}" unless response_success?(response)
18
19
  json = JSON.parse(response.body)
19
20
 
20
21
  if @debug
data/lib/gfspark/graph.rb CHANGED
@@ -1,16 +1,27 @@
1
+ # -*- coding: utf-8 -*-
1
2
  module Gfspark::Graph
2
3
  @height = 20
3
4
 
4
- BARS = [" ", "▁", "▂", "▃", "▄", "▅", "▆", "▇", "█" ]
5
- def bar(val, unit)
5
+ BARS = [" ", "▁", "▂", "▃", "▄", "▅", "▆", "▇", "█" ]
6
+ def bar(val, unit, non_fullwidth_font = false)
6
7
  n = (val.to_f/unit).ceil
7
8
  @height.times.map{|i|
8
9
  x = n - (i * 8)
9
- (x > 0) ? (x < 8) ? BARS[x] : BARS.last : " "
10
+ if x <= 0
11
+ BARS.first
12
+ else
13
+ bar_symbol = if x < 8
14
+ BARS[x]
15
+ else
16
+ BARS.last
17
+ end
18
+ bar_symbol += " " if non_fullwidth_font
19
+ bar_symbol
20
+ end
10
21
  }
11
22
  end
12
23
 
13
- def render(json, summary)
24
+ def render(json, summary, url = nil)
14
25
  rows = json['rows']
15
26
  max = rows.flatten.compact.max
16
27
  max_val = (max / 8).ceil * 8
@@ -20,12 +31,14 @@ module Gfspark::Graph
20
31
  e = Time.at(json["end_timestamp"].to_i).strftime("%Y-%m-%d %H:%M:%S")
21
32
 
22
33
  puts " #{blue(json["column_names"].first)}"
34
+ puts " #{yellow(url)}"
23
35
  puts ""
24
36
  puts " #{period_title} #{s} - #{e}"
25
37
  puts ""
26
38
 
27
39
  result = []
28
- rows.flatten.map{|row| bar(row, unit)}.transpose.reverse.each_with_index do |row, i|
40
+
41
+ rows.flatten.map{|row| bar(row, unit, @options[:non_fullwidth_font])}.transpose.reverse.each_with_index do |row, i|
29
42
  i = (@height- i)
30
43
  label = i.even? ? sprintf("%.1f", unit * i * 8) : ""
31
44
  line = row.join
@@ -43,30 +56,31 @@ module Gfspark::Graph
43
56
 
44
57
  def period_title
45
58
  case @options[:t]
46
- when "c", "sc" then sprintf(RANGES[@options[:t]], @options[:from], @options[:to])
59
+ when "c", "sc" then sprintf("%s to %s", @options[:from], @options[:to])
47
60
  else RANGES[@options[:t]]
48
61
  end
49
62
  end
50
63
 
51
- RANGES = {
52
- "y" => 'Year (1day avg)',
53
- "m" => 'Month (2hour avg)',
54
- "w" => 'Week (30min avg)',
55
- "3d" => '3 Days (5min avg)',
56
- "s3d" => '3 Days (5min avg)',
57
- "d" => 'Day (5min avg)',
58
- "sd" => 'Day (1min avg)' ,
59
- "8h" => '8 Hours (5min avg)',
60
- "s8h" => '8 Hours (1min avg)' ,
61
- "4h" => '4 Hours (5min avg)',
62
- "s4h" => '4 Hours (1min avg)' ,
63
- "h" => 'Hour (5min avg), ',
64
- "sh" => 'Hour (1min avg)' ,
65
- "n" => 'Half Day (5min avg)',
66
- "sn" => 'Half Day (1min avg)',
67
- "c" => "%s to $s",
68
- "sc" => "%s to $s"
69
- }
64
+ RANGE_DEFS = [
65
+ ["y" , 'Year (1day avg)'],
66
+ ["m" , 'Month (2hour avg)'],
67
+ ["w" , 'Week (30min avg)'],
68
+ ["3d" , '3 Days (5min avg)'],
69
+ ["s3d" , '3 Days (5min avg)'],
70
+ ["d" , 'Day (5min avg)'],
71
+ ["sd" , 'Day (1min avg)' ],
72
+ ["8h" , '8 Hours (5min avg)'],
73
+ ["s8h" , '8 Hours (1min avg)' ],
74
+ ["4h" , '4 Hours (5min avg)'],
75
+ ["s4h" , '4 Hours (1min avg)' ],
76
+ ["h" , 'Hour (5min avg), '],
77
+ ["sh" , 'Hour (1min avg)' ],
78
+ ["n" , 'Half Day (5min avg)'],
79
+ ["sn" , 'Half Day (1min avg)'],
80
+ ["c" , "Custom (5min avg)"],
81
+ ["sc" , "Custom (1min avg)"]
82
+ ]
83
+ RANGES = Hash[*RANGE_DEFS.flatten]
70
84
 
71
85
  def range_arg?(t)
72
86
  RANGES.keys.include? t
@@ -1,3 +1,4 @@
1
+ # -*- coding: utf-8 -*-
1
2
  module Gfspark
2
- VERSION = "0.0.1"
3
+ VERSION = "0.1.1"
3
4
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gfspark
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 25
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 0
9
8
  - 1
10
- version: 0.0.1
9
+ - 1
10
+ version: 0.1.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Tomohito Ozaki
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2013-05-28 00:00:00 +09:00
18
+ date: 2013-05-29 00:00:00 +09:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -77,6 +77,7 @@ files:
77
77
  - Rakefile
78
78
  - bin/gfspark
79
79
  - gfspark.gemspec
80
+ - images/gfspark_screenshot.png
80
81
  - lib/gfspark.rb
81
82
  - lib/gfspark/app.rb
82
83
  - lib/gfspark/config.rb