stat-count-client 0.0.1 → 0.0.2

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.
@@ -0,0 +1,263 @@
1
+ #encoding: UTF-8
2
+
3
+ require "thrift_client"
4
+ require "stat-count-client/thrift/collecter_types"
5
+ require "stat-count-client/thrift/remote_simple_count_collecter"
6
+ require "stat-count-client/stat_count_data"
7
+ require "stat-count-client/logger_factory"
8
+ require "singleton"
9
+
10
+ module Stat
11
+ module Count
12
+ module Client
13
+ class ThriftStatCountClient
14
+ include ConfigLoader
15
+ include Stat::Count::Thrift
16
+ @@logger = LoggerFactory.getLogger("StatCountThriftClient")
17
+
18
+ def initialize(servers=nil, config=nil)
19
+ servers ||= ENV['stat.count.domain'] || ConfigLoader::CONFIG['thrift.domain'] || "localhost:9090"
20
+ config ||= ConfigLoader::CONFIG['thrift.config']
21
+ if config.nil?
22
+ config[:client_class] = RemoteSimpleCountCollecter::Client
23
+ config[:application_exception_classes] = DataCollectionException
24
+ else
25
+ client_class = config['client_class'] || "RemoteSimpleCountCollecter::Client"
26
+ config[:client_class] = eval(client_class)
27
+ application_exception_classes_strs = config['application_exception_classes'] || ["DataCollectionException"]
28
+ application_exception_classe=application_exception_classes_strs.collect { |class_str|
29
+ eval(class_str)
30
+ }
31
+ config[:application_exception_classes] = application_exception_classe
32
+ init(servers, config)
33
+ end
34
+ end
35
+
36
+
37
+ def incrByCountWithDate(date_count)
38
+ thrift_date_count = to_date_count(date_count)
39
+ @client.incrByCountWithDate(thrift_date_count)
40
+ end
41
+
42
+ def incrByCount(count)
43
+ thrift_count = to_count(count)
44
+ count_result_hash = @client.incrByCount(thrift_count).countResults
45
+ count_result_hash = {}.tap{|hash|hash['countResults'] = count_result_hash}
46
+ Stat::Count::Data::CountResult.new(count_result_hash)
47
+ end
48
+
49
+ def incr(name, id, count=1, date=nil)
50
+ if(date.nil?)
51
+ count_result_hash = @client.incrBy(name,id, count)
52
+ else
53
+ @client.incrByWithDate(name, id, count, date)
54
+ end
55
+ end
56
+
57
+ def decr(name, id, count=1, date=nil)
58
+ if(date.nil?)
59
+ @client.decrBy(name, id, count)
60
+ else
61
+ @client.decrByWithDate(name, id, count, date)
62
+ end
63
+ end
64
+
65
+ def decrByCount(count)
66
+ thrift_count = to_count(count)
67
+ count_result_hash = @client.decrByCount(thrift_count).countResults
68
+ count_result_hash = {}.tap{|hash|hash['countResults'] = count_result_hash}
69
+ Stat::Count::Data::CountResult.new(count_result_hash)
70
+ end
71
+
72
+ def decrByCountWithDate(dateCount)
73
+ thrift_date_count = to_date_count(dateCount)
74
+ count_result_hash = @client.decrByCountWithDate(thrift_date_count).countResults
75
+ count_result_hash = {}.tap{|hash|hash['countResults'] = count_result_hash}
76
+ Stat::Count::Data::CountResult.new(count_result_hash)
77
+ end
78
+
79
+ def setByCountWithDate(dateCount)
80
+ thrift_date_count = to_date_count(dateCount)
81
+ @client.setByCountWithDate(thrift_date_count)
82
+ end
83
+
84
+ def setByCount(count)
85
+ thrift_count = to_count(count)
86
+ @client.setByCount(thrift_count)
87
+ end
88
+
89
+ def set(name,id, value, date=nil)
90
+ if(date.nil?)
91
+ @client.setValue(name, id, value)
92
+ else
93
+ @client.setWithDate(name,id, value,date)
94
+ end
95
+ end
96
+
97
+ def reset(name, id, limit=1)
98
+ @client.resetByLimit(name, id, limit)
99
+ end
100
+
101
+ def get(name, id, limit=1)
102
+ @client.getByLimit(name, id, limit)
103
+ end
104
+
105
+ def getByNames(name, id, limit=1)
106
+ @client.getByNamesAndLimit(name, id, limit)
107
+ end
108
+
109
+ def getByIds(name, ids, limit=1)
110
+ @client.getByIdsAndLimit(name, ids, limit)
111
+ end
112
+
113
+ def getByQuery(query)
114
+ thrift_query = to_query(query)
115
+ count_result_hash = @client.getByQuery(thrift_query).countResults
116
+ count_result_hash = {}.tap{|hash|hash['countResults'] = count_result_hash}
117
+ Stat::Count::Data::CountResult.new(count_result_hash)
118
+ end
119
+
120
+ def getByDateQuery(dateQuery)
121
+ thrift_query = to_date_query(query)
122
+ count_result_hash = @client.getByDateQuery(thrift_query).countResults
123
+ count_result_hash = {}.tap{|hash|hash['countResults'] = count_result_hash}
124
+ Stat::Count::Data::CountResult.new(count_result_hash)
125
+ end
126
+
127
+ def delByQuery(query)
128
+ thrift_query = to_query(query)
129
+ @client.delByQuery(thrift_query)
130
+ end
131
+
132
+ def delByDateQuery(dateQuery)
133
+ thrift_query = to_query(query)
134
+ @client.delByDateQuery(thrift_query)
135
+ end
136
+
137
+
138
+ private
139
+ def init(servers, config)
140
+ @@logger.info("Init Stat count client connect to thrift url: #{servers}, config: #{config}")
141
+ puts servers
142
+ puts config
143
+ begin
144
+ @client = ThriftClient.new(servers, config)
145
+
146
+ @client.add_callback(:on_exception) { |method_name, e|
147
+ @@logger.error("call #{method_name} fails! cause by: #{e.message}, " + "\n" + e.backtrace.join("\n"))
148
+ raise e
149
+ }
150
+ rescue => e
151
+ @@logger.error("init thrift client fails! cause by: #{e.message}, " + "\n" + e.backtrace.join("\n"))
152
+ raise e
153
+ end
154
+ end
155
+
156
+ def to_date_count(date_count)
157
+ if date_count.nil?
158
+ raise ArgumentError,"DateCount is nil"
159
+ end
160
+ thrift_counts = {}
161
+ counts = date_count.counts
162
+ counts.each_pair do |key, value|
163
+ name = key
164
+ count_unit_list = value
165
+ thrift_unit_list = count_unit_list.map do |count_unit|
166
+ thrift_count_unit = DateCountUnit.new
167
+ thrift_count_unit.id = count_unit.id
168
+ thrift_count_unit.count = count_unit.count
169
+ thrift_count_unit.timeMills = count_unit.date.to_f*1000
170
+ thrift_count_unit
171
+ end
172
+ thrift_counts[name] = thrift_unit_list
173
+ end
174
+ thrift_count = DateCount.new
175
+ thrift_count.counts = thrift_counts
176
+ thrift_count
177
+ end
178
+
179
+ def to_count(count)
180
+ if count.nil?
181
+ raise ArgumentError,"Count is nil"
182
+ end
183
+ thrift_counts = {}
184
+ counts = count.counts
185
+ counts.each_pair do |key, value|
186
+ name = key
187
+ count_unit_list = value
188
+ thrift_unit_list = count_unit_list.map do |count_unit|
189
+ thrift_count_unit = CountUnit.new
190
+ thrift_count_unit.id = count_unit.id
191
+ thrift_count_unit.count = count_unit.count
192
+ thrift_count_unit
193
+ end
194
+ thrift_counts[name] = thrift_unit_list
195
+ end
196
+ thrift_count = SimpleCount.new
197
+ thrift_count.counts = thrift_counts
198
+ thrift_count
199
+ end
200
+
201
+ def to_date_query(date_query)
202
+ if date_query.nil?
203
+ raise ArgumentError,"DateQuery is nil"
204
+ end
205
+ thrift_queries = {}
206
+ queries = date_query.queries
207
+ queries.each_pair do |key, value|
208
+ name = key
209
+ query_unit_list = value
210
+ thrift_unit_list = query_unit_list.collect do |query_unit|
211
+ thrift_query_unit = QueryDateUnit.new
212
+ thrift_query_unit.id = query_unit.id
213
+ thrift_count_unit.limit = query_unit.limit
214
+ fromDate = query_unit.fromDate
215
+ toDate = query_unit.toDate
216
+ if(toDate.nil?)
217
+ #not set fromDate and toDate, keep it -1,
218
+ #and it will transfered to null at stat count server side
219
+ thrift_count_unit.fromDate = -1
220
+ thrift_count_unit.toDate = -1
221
+ else
222
+ if(fromDate.nil?)
223
+ thrift_count_unit.fromDate = 0
224
+ elsif(toDate >= fromDate)
225
+ thrift_count_unit.fromDate = fromDate.to_f * 1000
226
+ thrift_count_unit.toDate = toDate.to_f * 1000
227
+ else
228
+ raise ArgumentError, "fromDate is less than toDate"
229
+ end
230
+ end
231
+ end
232
+ thrift_queries[name] = thrift_unit_list
233
+ end
234
+ thrift_query = DateCountQuery.new
235
+ thrift_query.queries = thrift_queries
236
+ thrift_query
237
+ end
238
+
239
+ def to_query(query)
240
+ if query.nil?
241
+ raise ArgumentError,"CountQuery is nil"
242
+ end
243
+ thrift_queries = {}
244
+ queries = query.queries
245
+ queries.each_pair do |key, value|
246
+ name = key
247
+ query_unit_list = value
248
+ thrift_unit_list = query_unit_list.map do |query_unit|
249
+ thrift_query_unit = QueryUnit.new
250
+ thrift_query_unit.id = query_unit.id
251
+ thrift_query_unit.limit = query_unit.limit
252
+ thrift_query_unit
253
+ end
254
+ thrift_queries[name] = thrift_unit_list
255
+ end
256
+ thrift_query = SimpleCountQuery.new
257
+ thrift_query.queries = thrift_queries
258
+ thrift_query
259
+ end
260
+ end
261
+ end
262
+ end
263
+ end
@@ -1,7 +1,7 @@
1
1
  module Stat
2
2
  module Count
3
3
  module Client
4
- VERSION = "0.0.1"
4
+ VERSION = "0.0.2"
5
5
  end
6
6
  end
7
7
  end
@@ -14,8 +14,7 @@ class TC_StatCountClientTest < Test::Unit::TestCase
14
14
  # Called before every test method runs. Can be used
15
15
  # to set up fixture information.
16
16
  def setup
17
- @client = StatCountClient.new({'hessian.domain'=>'http://localhost:8080/stat-count-runtime/hessian/remoteSimpleCountCollecter', 'log.file.StatCountClient'=>'D:/log/stat/stat-count-ruby.log'});
18
- puts @client
17
+ @client = StatCountClient.new();
19
18
  end
20
19
 
21
20
  # Called after every test method runs. Can be used to tear
@@ -26,95 +25,85 @@ class TC_StatCountClientTest < Test::Unit::TestCase
26
25
  end
27
26
 
28
27
  # Fake test
29
- #def test_incr
30
- # @client.set("testDay", "1", 1)
31
- # value = @client.incr("testDay", "1", 2)
32
- # assert_equal(3, value)
33
- #end
34
- #
35
- #def test_incrByCount
36
- # reset()
37
- # count = CountMother.createIncrCount
38
- # countResult = @client.incrByCount(count)
39
- # puts countResult.getResult()
40
- # assertCorrect(countResult)
41
- #end
42
- #
43
- #def test_incrByCountWithDate
44
- # resetWithDate()
45
- # dateCount = CountMother.createIncrCountWithDate
46
- # @client.incrByCountWithDate(dateCount)
47
- # dateQuery = CountMother.createCountQueryWithDate
48
- # countResult = @client.getByQuery(dateQuery)
49
- # assertIncrCorrectWithDate(countResult)
50
- #end
28
+ def test_incr
29
+ @client.set("testDay", "1", 1)
30
+ value = @client.incr("testDay", "1", 2)
31
+ assert_equal(3, value)
32
+ end
33
+
34
+ def test_incrByCount
35
+ reset()
36
+ count = CountMother.createIncrCount
37
+ countResult = @client.incrByCount(count)
38
+ puts countResult.getResult()
39
+ assertCorrect(countResult)
40
+ end
41
+
42
+ def test_incrByCountWithDate
43
+ resetWithDate()
44
+ dateCount = CountMother.createIncrCountWithDate
45
+ @client.incrByCountWithDate(dateCount)
46
+ dateQuery = CountMother.createCountQueryWithDate
47
+ countResult = @client.getByQuery(dateQuery)
48
+ assertIncrCorrectWithDate(countResult)
49
+ end
51
50
 
52
51
  def testDecr
53
- #@client.set("user.tracks.count", "1", 0)
54
- #begin
55
- # val = @client.decr("user.tracks.count", "1", 7)
56
- # assert_equal(0,val)
57
- #rescue => err
58
- # puts err
59
- #end
60
- #
61
- #getVal = @client.get("user.tracks.count", "1")
62
- #assert_equal(0,getVal)
63
-
64
- puts @client.getHello()
65
- puts @client.getHello1()
52
+ @client.set("test", "1", 10)
53
+ val = @client.decr("test", "1", 7)
54
+ assert_equal(3,val)
55
+ end
56
+
57
+ def testDecrByCount
58
+ count = CountMother.initCount
59
+ @client.setByCount(count)
60
+ count = CountMother.createDecrCount
61
+ countResult = @client.decrByCount(count)
62
+ assertDecrCorrect(countResult)
63
+ reset()
64
+ end
65
+
66
+ def test_set
67
+ @client.reset("test", "1")
68
+ @client.set("test", "1", 100000000000000000)
69
+ val = @client.get("test", "1");
70
+ assert_equal(100000000000000000, val)
66
71
  end
67
72
 
68
- #def testDecrByCount
69
- # count = CountMother.initCount
70
- # @client.setByCount(count)
71
- # count = CountMother.createDecrCount
72
- # countResult = @client.decrByCount(count)
73
- # assertDecrCorrect(countResult)
74
- # reset()
75
- #end
76
- #
77
- #def test_set
78
- # @client.reset("test", "1")
79
- # @client.set("test", "1", 100000000000000000)
80
- # val = @client.get("test", "1");
81
- # assert_equal(100000000000000000, val)
82
- #end
83
- #
84
- #def test_setByCount
85
- # reset()
86
- # count = CountMother.initCount
87
- # @client.setByCount(count)
88
- # query = CountMother.createCountQuery();
89
- # countResult = @client.getByQuery(query)
90
- # assertInitCorrect(countResult)
91
- #end
92
- #
93
- #def test_del
94
- # count = CountMother.initCount
95
- # @client.setByCount(count)
96
- # query = CountMother.createCountQuery()
97
- # @client.delByQuery(query)
98
- # countResult = @client.getByQuery(query)
99
- # assertDel(countResult)
100
- #end
101
- #
102
- #private
103
- #def resetWithDate
104
- # 1.upto(5) do |i|
105
- # @client.reset("testHour", i.to_s, 10)
106
- # end
107
- #
108
- # 1.upto(5) do |i|
109
- # @client.reset "testDay", i.to_s, 10
110
- # end
111
- #end
112
- #
113
- #def reset
114
- # @client.reset("test", "1")
115
- # @client.reset("testHour", "2")
116
- # @client.reset("testDay", "3")
117
- #end
73
+ def test_setByCount
74
+ reset()
75
+ count = CountMother.initCount
76
+ @client.setByCount(count)
77
+ query = CountMother.createCountQuery();
78
+ countResult = @client.getByQuery(query)
79
+ assertInitCorrect(countResult)
80
+ end
81
+
82
+ def test_del
83
+ count = CountMother.initCount
84
+ @client.setByCount(count)
85
+ query = CountMother.createCountQuery()
86
+ @client.delByQuery(query)
87
+ countResult = @client.getByQuery(query)
88
+ assertDel(countResult)
89
+ end
90
+
91
+ private
92
+ def resetWithDate
93
+ 1.upto(5) do |i|
94
+ @client.reset("testHour", i.to_s, 10)
95
+ end
96
+
97
+ 1.upto(5) do |i|
98
+ @client.reset "testDay", i.to_s, 10
99
+ end
100
+ end
101
+
102
+ def reset
103
+ @client.reset("test", "1")
104
+ @client.reset("testHour", "2")
105
+ @client.reset("testDay", "3")
106
+ end
118
107
 
119
108
  def assertCorrect(countResult)
120
109
  count = countResult.getResult("test", "1")