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.
- checksums.yaml +7 -0
- data/Gemfile +1 -0
- data/doc/collecter.thrift +299 -0
- data/doc/gen.txt +1 -0
- data/lib/config_loader.rb +2 -7
- data/lib/hessian_data_utils.rb +0 -7
- data/lib/logger_factory.rb +7 -12
- data/lib/stat-count-client.rb +2 -332
- data/lib/stat-count-client/config_loader.rb +13 -0
- data/lib/stat-count-client/hessian/stat_data_utils.rb +16 -0
- data/lib/stat-count-client/hessian_client.rb +179 -0
- data/lib/stat-count-client/logger_factory.rb +32 -0
- data/lib/stat-count-client/stat_count_data.rb +135 -0
- data/lib/stat-count-client/thrift/collecter_constants.rb +15 -0
- data/lib/stat-count-client/thrift/collecter_types.rb +209 -0
- data/lib/stat-count-client/thrift/remote_simple_count_collecter.rb +1766 -0
- data/lib/stat-count-client/thrift_client.rb +263 -0
- data/lib/stat-count-client/version.rb +1 -1
- data/test/stat_ccount_client_test.rb +76 -87
- data/test/stat_count_client_test.rb +181 -0
- data/test/thrift_count_client_test.rb +181 -0
- metadata +39 -15
- data/Gemfile.lock +0 -14
- data/stat-count-client.gemspec +0 -21
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 595651505fecffd5f682998e37926c9f741d3c1d
|
4
|
+
data.tar.gz: 7ebed0499b909897e1fecf87b9178ca74354a432
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 69a7038be10efbb2139e4d145815085cea14acc243d0a0db57d50d9ce6b86f36318894b82a58d540d0ee13f7844d6b5678f19cf1406e27879f9190e02a9fb4f7
|
7
|
+
data.tar.gz: 75c9141157a994a2f33474e404dfc5bcb3c3cc45db298bb1af05688be853627f7e002572ec1fa1feadeacf7837e3d4d91779c32b3ea0639a927f6699a854b1e5
|
data/Gemfile
CHANGED
@@ -0,0 +1,299 @@
|
|
1
|
+
namespace java com.ximalaya.stat.count.thrift
|
2
|
+
namespace rb stat.count.thrift
|
3
|
+
|
4
|
+
struct Record {
|
5
|
+
1:map<string, string> recordMap
|
6
|
+
}
|
7
|
+
|
8
|
+
struct CountUnit{
|
9
|
+
1:string id,
|
10
|
+
2:i32 count
|
11
|
+
}
|
12
|
+
|
13
|
+
struct SimpleCount {
|
14
|
+
1:map<string, list<CountUnit>> counts
|
15
|
+
}
|
16
|
+
|
17
|
+
struct DateCountUnit {
|
18
|
+
1:string id,
|
19
|
+
2:i32 count,
|
20
|
+
3:i64 timeMills
|
21
|
+
}
|
22
|
+
|
23
|
+
struct DateCount {
|
24
|
+
1: map<string, list<DateCountUnit>> counts
|
25
|
+
}
|
26
|
+
|
27
|
+
struct QueryDateUnit {
|
28
|
+
1:string id,
|
29
|
+
2:i32 limit,
|
30
|
+
3:i64 fromDate,
|
31
|
+
4:i64 toDate
|
32
|
+
}
|
33
|
+
|
34
|
+
struct DateCountQuery {
|
35
|
+
1:map<string, list<QueryDateUnit>> queries
|
36
|
+
}
|
37
|
+
|
38
|
+
struct QueryUnit {
|
39
|
+
1:string id,
|
40
|
+
2:i32 limit
|
41
|
+
}
|
42
|
+
|
43
|
+
struct SimpleCountQuery {
|
44
|
+
1:map<string, list<QueryUnit>> queries
|
45
|
+
}
|
46
|
+
|
47
|
+
struct CountResult {
|
48
|
+
1:map<string, map<string, i64>> countResults
|
49
|
+
}
|
50
|
+
|
51
|
+
exception DataCollectionException {
|
52
|
+
1: string message
|
53
|
+
}
|
54
|
+
|
55
|
+
service RemoteSimpleCountCollecter {
|
56
|
+
/**
|
57
|
+
* 将name服务的id对象的值增加count。如果服务配置了<code>{@link TimeUnit}</code>,就将当前时间单元的计数值增加count。
|
58
|
+
* @param name 服务名
|
59
|
+
* @param id 对象id
|
60
|
+
* @param count 增加的值
|
61
|
+
* @return 增加后的值,如果服务配有时间单元,返回的是当前时间单元的值
|
62
|
+
* @throws DataCollectionException
|
63
|
+
*/
|
64
|
+
i64 incrBy(1:string name, 2:string id, 3:i32 count) throws (1:DataCollectionException dcex),
|
65
|
+
|
66
|
+
/**
|
67
|
+
* 根据<code>{@link DateCount}</code>描述的值。对相关的记录增加指定的值。
|
68
|
+
* @param count 增加计数的参数
|
69
|
+
* @throws DataCollectionException 无法完成计数服务
|
70
|
+
*/
|
71
|
+
void incrByCountWithDate(1:DateCount dateCount) throws (1:DataCollectionException dcex),
|
72
|
+
|
73
|
+
/**
|
74
|
+
* 根据<code>{@link Count}</code>描述的值。对相关的记录增加指定的值。
|
75
|
+
* @param count 增加计数的参数
|
76
|
+
* @return 增加的结果,如果服务配有时间单元,返回的是当前时间单元的值
|
77
|
+
* @throws DataCollectionException
|
78
|
+
*/
|
79
|
+
CountResult incrByCount(1:SimpleCount count) throws (1:DataCollectionException dcex),
|
80
|
+
|
81
|
+
/**
|
82
|
+
* 将name服务的id对象值加一.
|
83
|
+
* @see #incr(String, String, int)
|
84
|
+
*/
|
85
|
+
i64 incr(1:string name, 2:string id) throws (1:DataCollectionException dcex),
|
86
|
+
|
87
|
+
/**
|
88
|
+
* 将name服务id对象在date时间计数值增加指定的数值。
|
89
|
+
* @param name 服务名
|
90
|
+
* @param id 对象ID
|
91
|
+
* @param count 增加的数目
|
92
|
+
* @param date 指定的时间
|
93
|
+
* @return 指定时间对应时间单元增加后的值
|
94
|
+
* @throws DataCollectionException
|
95
|
+
*/
|
96
|
+
i64 incrByWithDate(1:string name, 2:string id, 3:i32 count, 4:i64 timeMills) throws (1:DataCollectionException dcex),
|
97
|
+
|
98
|
+
/**
|
99
|
+
* 将name服务的id对象的减去count。如果服务配置了<code>{@link TimeUnit}</code>,就将当前时间单元的计数值增加count。
|
100
|
+
* @param name 服务名
|
101
|
+
* @param id 对象id
|
102
|
+
* @param count 减去的值
|
103
|
+
* @return 减去后的值,如果服务配有时间单元,返回的是当前时间单元的值
|
104
|
+
* @throws DataCollectionException
|
105
|
+
*/
|
106
|
+
i64 decrBy(1:string name, 2:string id, 3:i32 count) throws (1:DataCollectionException dcex),
|
107
|
+
|
108
|
+
/**
|
109
|
+
* 将name服务id对象在date时间计数值减去指定的数值。
|
110
|
+
* @param name 服务名
|
111
|
+
* @param id 对象ID
|
112
|
+
* @param count 数目
|
113
|
+
* @param date 指定的时间
|
114
|
+
* @return 指定时间对应时间单元减去后的值
|
115
|
+
* @throws DataCollectionException
|
116
|
+
*/
|
117
|
+
i64 decrByWithDate(1:string name, 2:string id, 3:i32 count, 4:i64 date) throws (1:DataCollectionException dcex),
|
118
|
+
|
119
|
+
/**
|
120
|
+
* 将name服务的id对象的减1
|
121
|
+
* @see #decr(String, String, int)
|
122
|
+
*/
|
123
|
+
i64 decr(1:string name, 2:string id) throws (1:DataCollectionException dcex);
|
124
|
+
|
125
|
+
/**
|
126
|
+
* 根据<code>{@link DateCount}</code>描述的值。对相关的记录减去指定的值。
|
127
|
+
* @param count 减去计数的参数
|
128
|
+
* @throws DataCollectionException 无法完成计数服务
|
129
|
+
*/
|
130
|
+
void decrByCountWithDate(1:DateCount dateCount) throws (1:DataCollectionException dcex),
|
131
|
+
|
132
|
+
/**
|
133
|
+
* 根据<code>{@link Count}</code>描述的值。对相关的记录减去指定的值。
|
134
|
+
* @param count 减去计数的参数
|
135
|
+
* @return 减去后的结果,如果服务配有时间单元,返回的是当前时间单元的值
|
136
|
+
* @throws DataCollectionException
|
137
|
+
*/
|
138
|
+
CountResult decrByCount(1:SimpleCount count) throws (1:DataCollectionException dcex),
|
139
|
+
|
140
|
+
/**
|
141
|
+
* 根据<code>{@link DateCount}</code>描述的值。对相关的记录设置值。
|
142
|
+
* @param count 设置的参数
|
143
|
+
* @throws DataCollectionException 无法完成计数服务
|
144
|
+
*/
|
145
|
+
void setByCountWithDate(1:DateCount count) throws (1:DataCollectionException dcex),
|
146
|
+
|
147
|
+
/**
|
148
|
+
* 根据<code>{@link Count}</code>描述的值。对相关的记录设置值。
|
149
|
+
* @param count 设置的参数
|
150
|
+
* @throws DataCollectionException 无法完成计数服务
|
151
|
+
*/
|
152
|
+
void setByCount(1:SimpleCount count) throws (1:DataCollectionException dcex),
|
153
|
+
|
154
|
+
/**
|
155
|
+
* 将name服务id对象设置成指定的数值。如果服务配置了<code>{@link TimeUnit}</code>,就将当前时间单元的计数值设置成指定的值。
|
156
|
+
* @param name 服务名
|
157
|
+
* @param id 对象ID
|
158
|
+
* @param count 数目
|
159
|
+
* @throws DataCollectionException
|
160
|
+
*/
|
161
|
+
void setValue(1:string name, 2:string id, 3:i64 value) throws (1:DataCollectionException dcex),
|
162
|
+
|
163
|
+
/**
|
164
|
+
* 将name服务id对象在对应时间的记录设置成指定的数值
|
165
|
+
* @param name 服务名
|
166
|
+
* @param id 对象ID
|
167
|
+
* @param count 数目
|
168
|
+
* @param date 指定的时间
|
169
|
+
* @throws DataCollectionException
|
170
|
+
*/
|
171
|
+
void setWithDate(1:string name, 2:string id, 3:i64 value, 4:i64 timeMills) throws (1:DataCollectionException dcex),
|
172
|
+
|
173
|
+
/**
|
174
|
+
* 重置name服务id对象的记录数值。
|
175
|
+
* @param name 服务名
|
176
|
+
* @param id 对象id
|
177
|
+
* @throws DataCollectionException
|
178
|
+
*/
|
179
|
+
void reset(1:string name, 2:string id) throws (1:DataCollectionException dcex),
|
180
|
+
|
181
|
+
/**
|
182
|
+
* 重置name服务id对象最近limit条数的记录。该服务必须配置有<code>TimeUnit</code>。
|
183
|
+
* @param name 服务名
|
184
|
+
* @param id 对象id
|
185
|
+
* @param limit 条数
|
186
|
+
* @throws DataCollectionException
|
187
|
+
*/
|
188
|
+
void resetByLimit(1:string name, 2:string id, 3:i32 limit) throws (1:DataCollectionException dcex),
|
189
|
+
|
190
|
+
/**
|
191
|
+
* 获取name服务id对象的计数值。如果有配了<code>TimeUnit</code>,则返回当前时间单元的数值。
|
192
|
+
* @param name
|
193
|
+
* @param id
|
194
|
+
* @return
|
195
|
+
* @throws DataCollectionException
|
196
|
+
*/
|
197
|
+
i64 get(1:string name, 2:string id) throws (1:DataCollectionException dcex),
|
198
|
+
|
199
|
+
/**
|
200
|
+
* 获取那么服务id对象指定条数的记录值的和。该服务必须配有<code>TimeUnit</code>。
|
201
|
+
* 获取204用户近一个星期内的发帖总数。
|
202
|
+
* <p>
|
203
|
+
* collecter.get("paste", "204", 7);
|
204
|
+
* </p>
|
205
|
+
* @param name 服务名
|
206
|
+
* @param id 对象id
|
207
|
+
* @param limit 时间单元数
|
208
|
+
* @return 计数总和
|
209
|
+
* @throws DataCollectionException
|
210
|
+
*/
|
211
|
+
i64 getByLimit(1:string name, 2:string id, 3:i32 limit) throws (1:DataCollectionException dcex),
|
212
|
+
|
213
|
+
/**
|
214
|
+
* 批量获取id对象多个服务在limit个数的时间单元内的计数总和
|
215
|
+
* @param names 多个服务名。
|
216
|
+
* @param id 对象id
|
217
|
+
* @param limit 时间单元个数
|
218
|
+
* @return 和各个服务对应的记录和,顺序和提供的服务名的顺序是一致的。
|
219
|
+
* @throws DataCollectionException
|
220
|
+
*/
|
221
|
+
list<i64> getByNamesAndLimit(1:list<string> names, 2:string id, 3:i32 limit) throws (1:DataCollectionException dcex),
|
222
|
+
|
223
|
+
/**
|
224
|
+
* 批量获取id对象多个服务的计数值。如果配有<code>TimeUnit</code>为当前时间单元的记录值。
|
225
|
+
* @param names 多个服务名
|
226
|
+
* @param id 对象id
|
227
|
+
* @return 和各个服务对应的记录值,顺序和提供的服务名的顺序是一致的。
|
228
|
+
* @throws DataCollectionException
|
229
|
+
*/
|
230
|
+
list<i64> getByNames(1:list<string> names, 2:string id) throws (1:DataCollectionException dcex),
|
231
|
+
|
232
|
+
/**
|
233
|
+
* 批量获取name服务下多个对象的计数值。如果配有<code>TimeUnit</code>为当前时间单元的记录值。
|
234
|
+
* @param name 服务名
|
235
|
+
* @param ids 多个对象id
|
236
|
+
* @return 和各对象对应的记录值。顺序和提供的对象id顺序是一致的。
|
237
|
+
* @throws DataCollectionException
|
238
|
+
*/
|
239
|
+
list<i64> getByIds(1:string name, 2:list<string> ids) throws (1:DataCollectionException dcex),
|
240
|
+
|
241
|
+
/**
|
242
|
+
* 批量获取name服务下多个对象的多条计数总和,
|
243
|
+
* @param name 服务名
|
244
|
+
* @param ids 多个对象id
|
245
|
+
* @param limit 指定条数
|
246
|
+
* @return 和各对象对应的计数总和。顺序和提供的对象id顺序是一致的。
|
247
|
+
* @throws DataCollectionException
|
248
|
+
*/
|
249
|
+
list<i64> getByIdsAndLimit(1:string name, 2:list<string> ids, 3:i32 limit) throws (1:DataCollectionException dcex),
|
250
|
+
|
251
|
+
/**
|
252
|
+
* 批量获取<code>CountQuery</code>提供的服务,id,时间的记录值之和。
|
253
|
+
* @param query
|
254
|
+
* @return 对应的计数之和。
|
255
|
+
* @throws DataCollectionException
|
256
|
+
*/
|
257
|
+
CountResult getByQuery(1:SimpleCountQuery query) throws (1:DataCollectionException dcex),
|
258
|
+
|
259
|
+
|
260
|
+
/**
|
261
|
+
* 批量获取<code>CountDateQuery</code>提供的服务,id,时间的记录值之和。
|
262
|
+
* @param query
|
263
|
+
* @return 对应的计数
|
264
|
+
* @throws DataCollectionException
|
265
|
+
*/
|
266
|
+
CountResult getByDateQuery(1:DateCountQuery query) throws (1:DataCollectionException dcex),
|
267
|
+
|
268
|
+
|
269
|
+
/**
|
270
|
+
* 根据<code>CountQuery</code>批量删除计数值。
|
271
|
+
* @param query
|
272
|
+
* @throws DataCollectionException
|
273
|
+
*/
|
274
|
+
void delByQuery(1:SimpleCountQuery query) throws (1:DataCollectionException dcex),
|
275
|
+
|
276
|
+
/**
|
277
|
+
* 根据<code>CountQuery</code>批量删除计数值。
|
278
|
+
* @param query
|
279
|
+
* @throws DataCollectionException
|
280
|
+
*/
|
281
|
+
void delByDateQuery(1:DateCountQuery query) throws (1:DataCollectionException dcex)
|
282
|
+
|
283
|
+
/**
|
284
|
+
* 添加一个记录值
|
285
|
+
* @param name 服务名
|
286
|
+
* @param id 服务对象的ID
|
287
|
+
* @param record 记录
|
288
|
+
* @throws DataCollectionException 无法完成统计服务时
|
289
|
+
*/
|
290
|
+
void setRecord(1:string name, 2:string id, 3:Record record) throws (1:DataCollectionException dcex);
|
291
|
+
|
292
|
+
/**
|
293
|
+
* 删除一个记录
|
294
|
+
* @param name 服务名
|
295
|
+
* @param id 服务对象的ID
|
296
|
+
* @throws DataCollectionException 无法完成统计服务时
|
297
|
+
*/
|
298
|
+
void delByKey(1:string name, 2:string id) throws (1:DataCollectionException dcex);
|
299
|
+
}
|
data/doc/gen.txt
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
thrift -out /workspaces/ruby/stat-count-client/lib/stat-count-client/thrift --gen rb /workspaces/ruby/stat-count-client/doc/collecter.thrift
|
data/lib/config_loader.rb
CHANGED
@@ -8,11 +8,6 @@
|
|
8
8
|
# log.level: info
|
9
9
|
|
10
10
|
module ConfigLoader
|
11
|
-
|
12
|
-
|
13
|
-
CONFIG = LOADER['stat.count']
|
14
|
-
rescue => err
|
15
|
-
CONFIG = {}
|
16
|
-
puts "can't load config file stat-count-client.yaml, you need to init statcountclient with config hash"
|
17
|
-
end
|
11
|
+
LOADER = YAML.load_file("../config/stat-count-client.yaml");
|
12
|
+
CONFIG = LOADER['stat.count']
|
18
13
|
end
|
data/lib/hessian_data_utils.rb
CHANGED
@@ -28,13 +28,6 @@ module Hessian
|
|
28
28
|
}
|
29
29
|
hash
|
30
30
|
end
|
31
|
-
|
32
|
-
def java_class_name(java_package)
|
33
|
-
index = self.class.name.rindex "::"
|
34
|
-
index = (index.nil?) ? 0 : index+2
|
35
|
-
simple_name = self.class.name[index..-1]
|
36
|
-
java_package.nil? ? simple_name : java_package + "." + simple_name
|
37
|
-
end
|
38
31
|
end
|
39
32
|
end
|
40
33
|
end
|
data/lib/logger_factory.rb
CHANGED
@@ -7,25 +7,20 @@ require "monitor"
|
|
7
7
|
class LoggerFactory
|
8
8
|
include ConfigLoader
|
9
9
|
@@logger = nil
|
10
|
-
def self.getLogger(name
|
10
|
+
def self.getLogger(name)
|
11
11
|
if @@logger.nil?
|
12
12
|
@monitor = Monitor.new
|
13
13
|
@monitor.synchronize do
|
14
14
|
if @@logger.nil?
|
15
|
+
log_path = CONFIG['log.file.'+name];
|
15
16
|
if(log_path.nil?)
|
16
|
-
|
17
|
-
if(log_path.nil?)
|
18
|
-
log_path = "../log"
|
19
|
-
end
|
17
|
+
log_path = "../log"
|
20
18
|
end
|
21
19
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
20
|
+
log_level = CONFIG['log.level.'+name]
|
21
|
+
if(log_level.nil?)
|
22
|
+
log_level = "error";
|
23
|
+
end
|
29
24
|
logger = Logger.new(log_path,'daily')
|
30
25
|
logger.level = Logger::const_get(log_level.upcase)
|
31
26
|
@@logger = logger;
|
data/lib/stat-count-client.rb
CHANGED
@@ -1,335 +1,5 @@
|
|
1
1
|
#encoding: UTF-8
|
2
2
|
|
3
3
|
require "stat-count-client/version"
|
4
|
-
require "
|
5
|
-
require "
|
6
|
-
require "logger_factory"
|
7
|
-
require "singleton"
|
8
|
-
require "hessian_data_utils"
|
9
|
-
|
10
|
-
|
11
|
-
module Stat
|
12
|
-
module Count
|
13
|
-
module Data
|
14
|
-
class QueryUnit
|
15
|
-
include Hessian::Data::Utils
|
16
|
-
include ConfigLoader
|
17
|
-
|
18
|
-
attr :id, true
|
19
|
-
attr :limit, true
|
20
|
-
|
21
|
-
def to_hash
|
22
|
-
to_hash_unit
|
23
|
-
end
|
24
|
-
|
25
|
-
def to_hessian
|
26
|
-
Hessian2::TypeWrapper.new(java_class_name("com.ximalaya.stat.count.data"), to_hash)
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
class CountUnit
|
31
|
-
include Hessian::Data::Utils
|
32
|
-
include ConfigLoader
|
33
|
-
|
34
|
-
attr :id, true
|
35
|
-
attr :count, true
|
36
|
-
|
37
|
-
def to_hash
|
38
|
-
to_hash_unit
|
39
|
-
end
|
40
|
-
|
41
|
-
def to_hessian
|
42
|
-
Hessian2::TypeWrapper.new(java_class_name("com.ximalaya.stat.count.data"), to_hash)
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
class DateQueryUnit < QueryUnit
|
47
|
-
attr :fromDate, true
|
48
|
-
attr :toDate, true
|
49
|
-
end
|
50
|
-
|
51
|
-
class DateCountUnit < CountUnit
|
52
|
-
attr :date, true
|
53
|
-
|
54
|
-
def to_hessian
|
55
|
-
Hessian2::TypeWrapper.new(java_class_name("com.ximalaya.stat.count.data"), to_hash)
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
class SimpleCount
|
60
|
-
include Hessian::Data::Utils
|
61
|
-
include ConfigLoader
|
62
|
-
|
63
|
-
attr_accessor :counts
|
64
|
-
|
65
|
-
def initialize
|
66
|
-
@counts = Hash.new
|
67
|
-
end
|
68
|
-
|
69
|
-
def addCount(name, id, count=1, countUnit=nil)
|
70
|
-
countUnitList = @counts[name]
|
71
|
-
if (countUnitList.nil?)
|
72
|
-
countUnitList = Array.new
|
73
|
-
@counts[name] = countUnitList
|
74
|
-
end
|
75
|
-
|
76
|
-
if (countUnit.nil?)
|
77
|
-
countUnit = CountUnit.new
|
78
|
-
end
|
79
|
-
countUnit.id = id
|
80
|
-
countUnit.count = count
|
81
|
-
countUnitList << countUnit
|
82
|
-
self
|
83
|
-
end
|
84
|
-
|
85
|
-
def getCounts
|
86
|
-
@counts
|
87
|
-
end
|
88
|
-
|
89
|
-
def getCountsSize
|
90
|
-
@counts.length
|
91
|
-
end
|
92
|
-
|
93
|
-
def to_hash
|
94
|
-
to_hash_map
|
95
|
-
end
|
96
|
-
|
97
|
-
def to_hessian
|
98
|
-
Hessian2::TypeWrapper.new(java_class_name("com.ximalaya.stat.count.data"), to_hash)
|
99
|
-
end
|
100
|
-
end
|
101
|
-
|
102
|
-
class DateCount < SimpleCount
|
103
|
-
def addCountWithDate(name, id, count=1, date=Time.now)
|
104
|
-
dateCountUnit = DateCountUnit.new
|
105
|
-
dateCountUnit.date = date
|
106
|
-
addCount(name, id, count, dateCountUnit)
|
107
|
-
self
|
108
|
-
end
|
109
|
-
|
110
|
-
def to_hessian
|
111
|
-
Hessian2::TypeWrapper.new(java_class_name("com.ximalaya.stat.count.data"), to_hash)
|
112
|
-
end
|
113
|
-
end
|
114
|
-
|
115
|
-
class SimpleCountQuery
|
116
|
-
include Hessian::Data::Utils
|
117
|
-
include ConfigLoader
|
118
|
-
|
119
|
-
attr_accessor :queries
|
120
|
-
|
121
|
-
def initialize
|
122
|
-
@queries = Hash.new
|
123
|
-
end
|
124
|
-
|
125
|
-
def addQuery(name, id, limit=1, queryUnit=nil)
|
126
|
-
queryUnitList = @queries[name]
|
127
|
-
|
128
|
-
if(queryUnitList.nil?)
|
129
|
-
queryUnitList = Array.new
|
130
|
-
@queries[name] = queryUnitList
|
131
|
-
end
|
132
|
-
|
133
|
-
if(queryUnit.nil?)
|
134
|
-
queryUnit = QueryUnit.new
|
135
|
-
end
|
136
|
-
|
137
|
-
queryUnit.id = id
|
138
|
-
queryUnit.limit = limit
|
139
|
-
queryUnitList << queryUnit
|
140
|
-
self
|
141
|
-
end
|
142
|
-
|
143
|
-
def getQueries
|
144
|
-
@queries
|
145
|
-
end
|
146
|
-
|
147
|
-
def to_hash
|
148
|
-
to_hash_map
|
149
|
-
end
|
150
|
-
|
151
|
-
def to_hessian
|
152
|
-
Hessian2::TypeWrapper.new(java_class_name("com.ximalaya.stat.count.data"), to_hash)
|
153
|
-
end
|
154
|
-
end
|
155
|
-
|
156
|
-
class DateCountQuery < SimpleCountQuery
|
157
|
-
def addQueryWithDate(name, id, fromDate, toDate, limit=1)
|
158
|
-
queryUnit = DateQueryUnit.new
|
159
|
-
addQuery(name, id, limit, queryUnit)
|
160
|
-
self
|
161
|
-
end
|
162
|
-
|
163
|
-
def to_hessian
|
164
|
-
Hessian2::TypeWrapper.new(java_class_name("com.ximalaya.stat.count.data"), to_hash)
|
165
|
-
end
|
166
|
-
end
|
167
|
-
|
168
|
-
class CountResult
|
169
|
-
attr_accessor :countResults
|
170
|
-
|
171
|
-
def initialize(hash)
|
172
|
-
@countResults = hash['countResults']
|
173
|
-
end
|
174
|
-
|
175
|
-
def getResult(name=nil, id=nil)
|
176
|
-
if(name.nil?)
|
177
|
-
return @countResults
|
178
|
-
end
|
179
|
-
|
180
|
-
countHash = @countResults[name]
|
181
|
-
if(countHash.nil?)
|
182
|
-
raise ArgumentError,"服务的名称: " + name + " 不存在!"
|
183
|
-
end
|
184
|
-
|
185
|
-
if (id.nil?)
|
186
|
-
return countHash
|
187
|
-
end
|
188
|
-
|
189
|
-
count = countHash[id]
|
190
|
-
if(count.nil?)
|
191
|
-
railse ArgumentError,"业务ID: "+id +" 不存在"
|
192
|
-
end
|
193
|
-
count
|
194
|
-
end
|
195
|
-
end
|
196
|
-
|
197
|
-
end
|
198
|
-
|
199
|
-
module Client
|
200
|
-
class StatCountClient
|
201
|
-
include Stat::Count::Data
|
202
|
-
include ConfigLoader
|
203
|
-
|
204
|
-
def initialize(config={})
|
205
|
-
@hessian_url = config['hessian.domain'];
|
206
|
-
if (@hessian_url.nil?)
|
207
|
-
@hessian_url ||= ENV['stat.count.domain']
|
208
|
-
@hessian_url ||= CONFIG['hessian.domain']
|
209
|
-
end
|
210
|
-
@logger = LoggerFactory.getLogger("StatCountClient", config['log.file.StatCountClient'], config['log.level.StatCountClient'])
|
211
|
-
|
212
|
-
init(@hessian_url)
|
213
|
-
end
|
214
|
-
|
215
|
-
def incrByCountWithDate(dateCount)
|
216
|
-
wrapper = dateCount.to_hessian
|
217
|
-
@client.incrByCountWithDate(wrapper)
|
218
|
-
end
|
219
|
-
|
220
|
-
def incrByCount(count)
|
221
|
-
wrapper = count.to_hessian
|
222
|
-
countResult = @client.incrByCount(wrapper)
|
223
|
-
CountResult.new(countResult)
|
224
|
-
end
|
225
|
-
|
226
|
-
def incr(name, id, count=1, date=nil)
|
227
|
-
if(date.nil?)
|
228
|
-
@client.incrBy(name,id, count)
|
229
|
-
else
|
230
|
-
@client.incrByWithDate(name, id, count, date)
|
231
|
-
end
|
232
|
-
end
|
233
|
-
|
234
|
-
def decr(name, id, count=1, date=nil)
|
235
|
-
if(date.nil?)
|
236
|
-
@client.decrBy(name, id, count)
|
237
|
-
else
|
238
|
-
@client.decrByWithDate(name, id, count, date)
|
239
|
-
end
|
240
|
-
end
|
241
|
-
|
242
|
-
def decrByCount(count)
|
243
|
-
wrapper = count.to_hessian
|
244
|
-
count_result = @client.decrByCount(wrapper)
|
245
|
-
CountResult.new(count_result)
|
246
|
-
end
|
247
|
-
|
248
|
-
def decrByCountWithDate(dateCount)
|
249
|
-
wrapper = dateCount.to_hessian
|
250
|
-
@client.decrByCountWithDate(wrapper)
|
251
|
-
end
|
252
|
-
|
253
|
-
def setByCountWithDate(dateCount)
|
254
|
-
wrapper = dateCount.to_hessian
|
255
|
-
@client.setByCountWithDate(wrapper)
|
256
|
-
end
|
257
|
-
|
258
|
-
def setByCount(count)
|
259
|
-
wrapper = count.to_hessian
|
260
|
-
@client.setByCount(wrapper)
|
261
|
-
end
|
262
|
-
|
263
|
-
def set(name,id, value, date=nil)
|
264
|
-
if(date.nil?)
|
265
|
-
@client.set(name, id, value)
|
266
|
-
else
|
267
|
-
@client.setWithDate(name,id, value,date)
|
268
|
-
end
|
269
|
-
end
|
270
|
-
|
271
|
-
def reset(name, id, limit=1)
|
272
|
-
@client.resetByLimit(name, id, limit)
|
273
|
-
end
|
274
|
-
|
275
|
-
def get(name, id, limit=1)
|
276
|
-
@client.getByLimit(name, id, limit)
|
277
|
-
end
|
278
|
-
|
279
|
-
def getByNames(name, id, limit=1)
|
280
|
-
@client.getByNamesAndLimit(name, id, limit)
|
281
|
-
end
|
282
|
-
|
283
|
-
def getByIds(name, ids, limit=1)
|
284
|
-
@client.getByIdsAndLimit(name, ids, limit)
|
285
|
-
end
|
286
|
-
|
287
|
-
def getByQuery(query)
|
288
|
-
wrapper = query.to_hessian
|
289
|
-
countResult = @client.getByQuery(wrapper)
|
290
|
-
CountResult.new(countResult)
|
291
|
-
end
|
292
|
-
|
293
|
-
def getByDateQuery(dateQuery)
|
294
|
-
wrapper = dateQuery.to_hessian
|
295
|
-
countResult = @client.getByDateQuery(wrapper)
|
296
|
-
CountResult.new(countResult)
|
297
|
-
end
|
298
|
-
|
299
|
-
def delByQuery(query)
|
300
|
-
wrapper = query.to_hessian
|
301
|
-
@client.delByQuery(wrapper)
|
302
|
-
end
|
303
|
-
|
304
|
-
def delByDateQuery(dateQuery)
|
305
|
-
wrapper = dateQuery.to_hessian
|
306
|
-
@client.delByDateQuery(wrapper)
|
307
|
-
end
|
308
|
-
|
309
|
-
def getHello
|
310
|
-
@client.getHello()
|
311
|
-
end
|
312
|
-
|
313
|
-
def getHello1
|
314
|
-
@client.getHello1()
|
315
|
-
end
|
316
|
-
|
317
|
-
private
|
318
|
-
def init(hessian_url)
|
319
|
-
if(hessian_url.nil?)
|
320
|
-
raise ArgumentError.new("hessian url is null or empty")
|
321
|
-
end
|
322
|
-
|
323
|
-
@logger.info("Init Stat count client connect to hessian url: #{hessian_url}")
|
324
|
-
begin
|
325
|
-
@client = Hessian2::HessianClient.new(@hessian_url)
|
326
|
-
rescue => err
|
327
|
-
@logger.error("init hessian service fails!", err)
|
328
|
-
raise err
|
329
|
-
end
|
330
|
-
end
|
331
|
-
|
332
|
-
end
|
333
|
-
end
|
334
|
-
end
|
335
|
-
end
|
4
|
+
require "stat-count-client/hessian_client"
|
5
|
+
require "stat-count-client/thrift_client"
|