norikra-client 0.0.4 → 0.0.5

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
  SHA1:
3
- metadata.gz: 837ff9e8704a6bf0e6eba8053e8166ff324f92c3
4
- data.tar.gz: 20b6cd31a7f90479dbe52d7563e66798dfd7166f
3
+ metadata.gz: 2061eec9c108858f789bd02c34d351e191afaff6
4
+ data.tar.gz: ac1166580e048a93f253364fc1532c25fdcd6a94
5
5
  SHA512:
6
- metadata.gz: 4d484d1fa474ce71593d64ac998242368356e9314a0e984d8491ced0c70239f0db27698d0ddd41b4b4dc43333addea7eaa8b266a7a7877a1731c4a4db63c1037
7
- data.tar.gz: c9f90e87a56d2313c271fa73836af141b3f5602527890eace1102eb2c98942f45bcfb262c765c01476eb52b58cbd34b817fe519c87a132dbb32570ae83eb355d
6
+ metadata.gz: f47eb1dd0bedd20ce04aa9cbe11dc4df14d6493acfdf04101805a82815d8626f7d7f69dda2c9e22cd7ed42abe6027b6914ad4ad49d9b441ce0500467803a1f33
7
+ data.tar.gz: 88f19de2390530f8a6c48a33999af0a84779e6714b5743c6fade09c842d45455dfa3401770161be53e3e833d787b3dbb9968846c255fdfb0624485fb7927ec5f
@@ -3,6 +3,11 @@ require "norikra/client/version"
3
3
  require 'msgpack-rpc-over-http'
4
4
 
5
5
  module Norikra
6
+ module RPC
7
+ class ClientError < MessagePack::RPCOverHTTP::RemoteError; end
8
+ class ServerError < MessagePack::RPCOverHTTP::RemoteError; end
9
+ end
10
+
6
11
  class Client
7
12
  RPC_DEFAULT_PORT = 26571
8
13
  TIMEOUT_OPTIONS = [:connect_timeout, :send_timeout, :receive_timeout]
@@ -9,6 +9,16 @@ class Norikra::Client
9
9
  def client(options)
10
10
  Norikra::Client.new(options[:host], options[:port])
11
11
  end
12
+ def wrap
13
+ begin
14
+ yield
15
+ rescue Norikra::RPC::ClientError => e
16
+ puts "Failed: " + e.message
17
+ rescue Norikra::RPC::ServerError => e
18
+ puts "ERROR on norikra server: " + e.message
19
+ puts " For more details, see norikra server's logs"
20
+ end
21
+ end
12
22
  end
13
23
 
14
24
  class Target < Thor
@@ -17,30 +27,36 @@ class Norikra::Client
17
27
  desc "list", "show list of targets"
18
28
  option :simple, :type => :boolean, :default => false, :desc => "suppress header/footer", :aliases => "-s"
19
29
  def list
20
- puts "TARGET" unless options[:simple]
21
- targets = client(parent_options).targets
22
- targets.each do |t|
23
- puts t
30
+ wrap do
31
+ puts "TARGET" unless options[:simple]
32
+ targets = client(parent_options).targets
33
+ targets.each do |t|
34
+ puts t
35
+ end
36
+ puts "#{targets.size} targets found." unless options[:simple]
24
37
  end
25
- puts "#{targets.size} targets found." unless options[:simple]
26
38
  end
27
39
 
28
40
  desc "open TARGET [fieldname1:type1 [fieldname2:type2 [fieldname3:type3] ...]]", "create new target (and define its fields)"
29
41
  def open(target, *field_defs)
30
- fields = nil
31
- if field_defs.size > 0
32
- fields = {}
33
- field_defs.each do |str|
34
- fname,ftype = str.split(':')
35
- fields[fname] = ftype
42
+ wrap do
43
+ fields = nil
44
+ if field_defs.size > 0
45
+ fields = {}
46
+ field_defs.each do |str|
47
+ fname,ftype = str.split(':')
48
+ fields[fname] = ftype
49
+ end
36
50
  end
51
+ client(parent_options).open(target, fields)
37
52
  end
38
- client(parent_options).open(target, fields)
39
53
  end
40
54
 
41
55
  desc "close TARGET", "close existing target and all its queries"
42
56
  def close(target)
43
- client(parent_options).close(target)
57
+ wrap do
58
+ client(parent_options).close(target)
59
+ end
44
60
  end
45
61
  end
46
62
 
@@ -50,22 +66,28 @@ class Norikra::Client
50
66
  desc "list", "show list of queries"
51
67
  option :simple, :type => :boolean, :default => false, :desc => "suppress header/footer", :aliases => "-s"
52
68
  def list
53
- puts "QUERY_NAME\tTARGETS\tQUERY" unless options[:simple]
54
- queries = client(parent_options).queries
55
- queries.sort{|a,b| (a['targets'].first <=> b['targets'].first).nonzero? || a['name'] <=> b['name']}.each do |q|
56
- puts "#{q['name']}\t#{q['targets'].join(',')}\t#{q['expression']}"
69
+ wrap do
70
+ puts "QUERY_NAME\tTARGETS\tQUERY" unless options[:simple]
71
+ queries = client(parent_options).queries
72
+ queries.sort{|a,b| (a['targets'].first <=> b['targets'].first).nonzero? || a['name'] <=> b['name']}.each do |q|
73
+ puts "#{q['name']}\t#{q['targets'].join(',')}\t#{q['expression']}"
74
+ end
75
+ puts "#{queries.size} queries found." unless options[:simple]
57
76
  end
58
- puts "#{queries.size} queries found." unless options[:simple]
59
77
  end
60
78
 
61
79
  desc "add QUERY_NAME QUERY_EXPRESSION", "register a query"
62
80
  def add(query_name, expression)
63
- client(parent_options).register(query_name, expression)
81
+ wrap do
82
+ client(parent_options).register(query_name, expression)
83
+ end
64
84
  end
65
85
 
66
86
  desc "remove QUERY_NAME", "deregister a query"
67
87
  def remove(query_name)
68
- client(parent_options).deregister(query_name)
88
+ wrap do
89
+ client(parent_options).deregister(query_name)
90
+ end
69
91
  end
70
92
  end
71
93
 
@@ -75,17 +97,21 @@ class Norikra::Client
75
97
  desc "list TARGET", "show list of field definitions of specified target"
76
98
  option :simple, :type => :boolean, :default => false, :desc => "suppress header/footer", :aliases => "-s"
77
99
  def list(target)
78
- puts "FIELD\tTYPE\tOPTIONAL" unless options[:simple]
79
- fields = client(parent_options).fields(target)
80
- fields.each do |f|
81
- puts "#{f['name']}\t#{f['type']}\t#{f['optional']}"
100
+ wrap do
101
+ puts "FIELD\tTYPE\tOPTIONAL" unless options[:simple]
102
+ fields = client(parent_options).fields(target)
103
+ fields.each do |f|
104
+ puts "#{f['name']}\t#{f['type']}\t#{f['optional']}"
105
+ end
106
+ puts "#{fields.size} fields found." unless options[:simple]
82
107
  end
83
- puts "#{fields.size} fields found." unless options[:simple]
84
108
  end
85
109
 
86
110
  desc "add TARGET FIELDNAME TYPE", "reserve fieldname and its type of target"
87
111
  def add(target, field, type)
88
- client(parent_options).reserve(target, field, type)
112
+ wrap do
113
+ client(parent_options).reserve(target, field, type)
114
+ end
89
115
  end
90
116
  end
91
117
 
@@ -96,17 +122,19 @@ class Norikra::Client
96
122
  option :format, :type => :string, :default => 'json', :desc => "format of input data per line of stdin [json(default), ltsv]"
97
123
  option :batch_size, :type => :numeric, :default => 10000, :desc => "records sent in once transferring (default: 10000)"
98
124
  def send(target)
99
- client = client(parent_options)
100
- parser = parser(options[:format])
101
- buffer = []
102
- $stdin.each_line do |line|
103
- buffer.push(parser.parse(line))
104
- if buffer.size >= options[:batch_size]
105
- client.send(target, buffer)
106
- buffer = []
125
+ wrap do
126
+ client = client(parent_options)
127
+ parser = parser(options[:format])
128
+ buffer = []
129
+ $stdin.each_line do |line|
130
+ buffer.push(parser.parse(line))
131
+ if buffer.size >= options[:batch_size]
132
+ client.send(target, buffer)
133
+ buffer = []
134
+ end
107
135
  end
136
+ client.send(target, buffer) if buffer.size > 0
108
137
  end
109
- client.send(target, buffer) if buffer.size > 0
110
138
  end
111
139
 
112
140
  desc "fetch QUERY_NAME", "fetch events from specified query"
@@ -114,12 +142,14 @@ class Norikra::Client
114
142
  option :time_key, :type => :string, :default => 'time', :desc => "output key name for event time (default: time)"
115
143
  option :time_format, :type => :string, :default => '%Y/%m/%d %H:%M:%S', :desc => "output time format (default: '2013/05/14 17:57:59')"
116
144
  def fetch(query_name)
117
- formatter = formatter(options[:format])
118
- time_formatter = lambda{|t| Time.at(t).strftime(options[:time_format])}
145
+ wrap do
146
+ formatter = formatter(options[:format])
147
+ time_formatter = lambda{|t| Time.at(t).strftime(options[:time_format])}
119
148
 
120
- client(parent_options).event(query_name).each do |time,event|
121
- event = {options[:time_key] => Time.at(time).strftime(options[:time_format])}.merge(event)
122
- puts formatter.format(event)
149
+ client(parent_options).event(query_name).each do |time,event|
150
+ event = {options[:time_key] => Time.at(time).strftime(options[:time_format])}.merge(event)
151
+ puts formatter.format(event)
152
+ end
123
153
  end
124
154
  end
125
155
 
@@ -129,19 +159,21 @@ class Norikra::Client
129
159
  option :time_key, :type => :string, :default => 'time', :desc => "output key name for event time (default: time)"
130
160
  option :time_format, :type => :string, :default => '%Y/%m/%d %H:%M:%S', :desc => "output time format (default: '2013/05/14 17:57:59')"
131
161
  def sweep
132
- formatter = formatter(options[:format])
133
- time_formatter = lambda{|t| Time.at(t).strftime(options[:time_format])}
134
-
135
- data = client(parent_options).sweep
136
-
137
- data.keys.sort.each do |queryname|
138
- events = data[queryname]
139
- events.each do |time,event|
140
- event = {
141
- options[:time_key] => Time.at(time).strftime(options[:time_format]),
142
- options[:query_name_key] => queryname,
143
- }.merge(event)
144
- puts formatter.format(event)
162
+ wrap do
163
+ formatter = formatter(options[:format])
164
+ time_formatter = lambda{|t| Time.at(t).strftime(options[:time_format])}
165
+
166
+ data = client(parent_options).sweep
167
+
168
+ data.keys.sort.each do |queryname|
169
+ events = data[queryname]
170
+ events.each do |time,event|
171
+ event = {
172
+ options[:time_key] => Time.at(time).strftime(options[:time_format]),
173
+ options[:query_name_key] => queryname,
174
+ }.merge(event)
175
+ puts formatter.format(event)
176
+ end
145
177
  end
146
178
  end
147
179
  end
@@ -1,5 +1,5 @@
1
1
  module Norikra
2
2
  class Client
3
- VERSION = "0.0.4"
3
+ VERSION = "0.0.5"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: norikra-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - TAGOMORI Satoshi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-06-17 00:00:00.000000000 Z
11
+ date: 2013-08-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: msgpack-rpc-over-http
@@ -126,3 +126,4 @@ signing_key:
126
126
  specification_version: 4
127
127
  summary: Client commands and libraries for Norikra
128
128
  test_files: []
129
+ has_rdoc: