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,13 @@
1
+ #encoding: UTF-8
2
+
3
+ #Example
4
+ #stat.count:
5
+ # hessian.domain: http://localhost:8080/stat-count-runtime/hessian/remoteSimpleCountCollecter
6
+ # hessian.java.package: com.ximalaya.stat.count.data
7
+ # log.file: D:/log/stat/stat-count-ruby.log
8
+ # log.level: info
9
+
10
+ module ConfigLoader
11
+ LOADER = YAML.load_file("../config/stat-count-client.yaml");
12
+ CONFIG = LOADER['stat.count']
13
+ end
@@ -0,0 +1,16 @@
1
+ #encoding: UTF-8
2
+
3
+ module Stat
4
+ module Data
5
+ module Utils
6
+
7
+
8
+ # def java_class_name(java_package)
9
+ # index = self.class.name.rindex "::"
10
+ # index = (index.nil?) ? 0 : index+2
11
+ # simple_name = self.class.name[index..-1]
12
+ # java_package.nil? ? simple_name : java_package + "." + simple_name
13
+ # end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,179 @@
1
+ #encoding: UTF-8
2
+
3
+ require "yaml"
4
+ require "stat-count-client/config_loader"
5
+ require "stat-count-client/logger_factory"
6
+ require "stat-count-client/stat_count_data"
7
+
8
+ module Stat
9
+ module Count
10
+ module Client
11
+ class StatCountClient
12
+ include Stat::Count::Data
13
+ include ConfigLoader
14
+ CONFIG = ConfigLoader::CONFIG
15
+ HESSIAN_PACKAGE = CONFIG['hessian.java.package'] || "com.ximalaya.stat.count.data"
16
+
17
+ @@logger = LoggerFactory.getLogger("StatCountHessianClient")
18
+
19
+ def initialize(hessian_url=nil)
20
+ @hessian_url = hessian_url;
21
+ if (@hessian_url.nil?)
22
+ @hessian_url ||= ENV['stat.count.domain']
23
+ @hessian_url ||= CONFIG['hessian.domain']
24
+ end
25
+ init(@hessian_url)
26
+ end
27
+
28
+ def to_hash_unit(data)
29
+ hash = {}
30
+ data.instance_variables.each {|var| hash[var.to_s.delete("@")] = data.instance_variable_get(var) }
31
+ hash
32
+ end
33
+
34
+ def to_hash_map(data)
35
+ hash = {}
36
+ data.instance_variables.each {|var|
37
+ internal_hash = {}
38
+ map = data.instance_variable_get(var)
39
+ map.each { |key, list|
40
+ newList = nil
41
+ list.each { |unit|
42
+ if (newList.nil?)
43
+ newList = Array.new
44
+ end
45
+ newList << to_hessian_unit(unit)
46
+ }
47
+ internal_hash[key] = newList
48
+ }
49
+ hash[var.to_s.delete("@")] = internal_hash
50
+ }
51
+ hash
52
+ end
53
+
54
+ def java_class_name(java_package, clazz)
55
+ index = clazz.name.rindex "::"
56
+ index = (index.nil?) ? 0 : index+2
57
+ simple_name = clazz.name[index..-1]
58
+ java_package.nil? ? simple_name : java_package + "." + simple_name
59
+ end
60
+
61
+ def to_hessian_unit(data)
62
+ clazz = data.class
63
+ Hessian2::TypeWrapper.new(java_class_name(HESSIAN_PACKAGE, clazz), to_hash_unit(data))
64
+ end
65
+
66
+ def to_hessian(data)
67
+ clazz = data.class
68
+ Hessian2::TypeWrapper.new(java_class_name(HESSIAN_PACKAGE, clazz), to_hash_map(data))
69
+ end
70
+
71
+ def incrByCountWithDate(dateCount)
72
+ wrapper = to_hessian(dateCount)
73
+ @client.incrByCountWithDate(wrapper)
74
+ end
75
+
76
+ def incrByCount(count)
77
+ wrapper = to_hessian(count)
78
+ countResult = @client.incrByCount(wrapper)
79
+ CountResult.new(countResult)
80
+ end
81
+
82
+ def incr(name, id, count=1, date=nil)
83
+ if(date.nil?)
84
+ @client.incrBy(name,id, count)
85
+ else
86
+ @client.incrByWithDate(name, id, count, date)
87
+ end
88
+ end
89
+
90
+ def decr(name, id, count=1, date=nil)
91
+ if(date.nil?)
92
+ @client.decrBy(name, id, count)
93
+ else
94
+ @client.decrByWithDate(name, id, count, date)
95
+ end
96
+ end
97
+
98
+ def decrByCount(count)
99
+ wrapper = to_hessian(count)
100
+ count_result = @client.decrByCount(wrapper)
101
+ CountResult.new(count_result)
102
+ end
103
+
104
+ def decrByCountWithDate(dateCount)
105
+ wrapper = to_hessian(dateCount)
106
+ @client.decrByCountWithDate(wrapper)
107
+ end
108
+
109
+ def setByCountWithDate(dateCount)
110
+ wrapper = to_hessian(dateCount)
111
+ @client.setByCountWithDate(wrapper)
112
+ end
113
+
114
+ def setByCount(count)
115
+ wrapper = to_hessian(count)
116
+ @client.setByCount(wrapper)
117
+ end
118
+
119
+ def set(name,id, value, date=nil)
120
+ if(date.nil?)
121
+ @client.set(name, id, value)
122
+ else
123
+ @client.setWithDate(name,id, value,date)
124
+ end
125
+ end
126
+
127
+ def reset(name, id, limit=1)
128
+ @client.resetByLimit(name, id, limit)
129
+ end
130
+
131
+ def get(name, id, limit=1)
132
+ @client.getByLimit(name, id, limit)
133
+ end
134
+
135
+ def getByNames(name, id, limit=1)
136
+ @client.getByNamesAndLimit(name, id, limit)
137
+ end
138
+
139
+ def getByIds(name, ids, limit=1)
140
+ @client.getByIdsAndLimit(name, ids, limit)
141
+ end
142
+
143
+ def getByQuery(query)
144
+ wrapper = to_hessian(query)
145
+ countResult = @client.getByQuery(wrapper)
146
+ CountResult.new(countResult)
147
+ end
148
+
149
+ def getByDateQuery(dateQuery)
150
+ wrapper = to_hessian(dateQuery)
151
+ countResult = @client.getByDateQuery(wrapper)
152
+ CountResult.new(countResult)
153
+ end
154
+
155
+ def delByQuery(query)
156
+ wrapper = to_hessian(query)
157
+ @client.delByQuery(wrapper)
158
+ end
159
+
160
+ def delByDateQuery(dateQuery)
161
+ wrapper = to_hessian(dateQuery)
162
+ @client.delByDateQuery(wrapper)
163
+ end
164
+
165
+ private
166
+ def init(hessian_url)
167
+ @@logger.info("Init Stat count client connect to hessian url: #{hessian_url}")
168
+ begin
169
+ @client = Hessian2::HessianClient.new(@hessian_url)
170
+ rescue => err
171
+ @@logger.error("init hessian service fails!, #{err}")
172
+ raise err
173
+ end
174
+ end
175
+
176
+ end
177
+ end
178
+ end
179
+ end
@@ -0,0 +1,32 @@
1
+ #encoding: UTF-8
2
+
3
+ require "logger"
4
+ require "stat-count-client/config_loader"
5
+ require "monitor"
6
+
7
+ class LoggerFactory
8
+ include ConfigLoader
9
+ @@logger = nil
10
+ def self.getLogger(name)
11
+ if @@logger.nil?
12
+ @monitor = Monitor.new
13
+ @monitor.synchronize do
14
+ if @@logger.nil?
15
+ log_path = CONFIG['log.file.'+name];
16
+ if(log_path.nil?)
17
+ log_path = "../log"
18
+ end
19
+
20
+ log_level = CONFIG['log.level.'+name]
21
+ if(log_level.nil?)
22
+ log_level = "error";
23
+ end
24
+ logger = Logger.new(log_path,'daily')
25
+ logger.level = Logger::const_get(log_level.upcase)
26
+ @@logger = logger;
27
+ end
28
+ end
29
+ end
30
+ @@logger
31
+ end
32
+ end
@@ -0,0 +1,135 @@
1
+ module Stat
2
+ module Count
3
+ module Data
4
+ class QueryUnit
5
+ attr :id, true
6
+ attr :limit, true
7
+ end
8
+
9
+ class CountUnit
10
+ attr :id, true
11
+ attr :count, true
12
+ end
13
+
14
+ class DateQueryUnit < QueryUnit
15
+ attr :fromDate, true
16
+ attr :toDate, true
17
+ end
18
+
19
+ class DateCountUnit < CountUnit
20
+ attr :date, true
21
+ end
22
+
23
+ class SimpleCount
24
+ attr_accessor :counts
25
+
26
+ def initialize
27
+ @counts = Hash.new
28
+ end
29
+
30
+ def addCount(name, id, count=1, countUnit=nil)
31
+ countUnitList = @counts[name]
32
+ if (countUnitList.nil?)
33
+ countUnitList = Array.new
34
+ @counts[name] = countUnitList
35
+ end
36
+
37
+ if (countUnit.nil?)
38
+ countUnit = CountUnit.new
39
+ end
40
+ countUnit.id = id
41
+ countUnit.count = count
42
+ countUnitList << countUnit
43
+ self
44
+ end
45
+
46
+ def getCounts
47
+ @counts
48
+ end
49
+
50
+ def getCountsSize
51
+ @counts.length
52
+ end
53
+ end
54
+
55
+ class DateCount < SimpleCount
56
+ def addCountWithDate(name, id, count=1, date=Time.now)
57
+ dateCountUnit = DateCountUnit.new
58
+ dateCountUnit.date = date
59
+ addCount(name, id, count, dateCountUnit)
60
+ self
61
+ end
62
+ end
63
+
64
+ class SimpleCountQuery
65
+
66
+ attr_accessor :queries
67
+
68
+ def initialize
69
+ @queries = Hash.new
70
+ end
71
+
72
+ def addQuery(name, id, limit=1, queryUnit=nil)
73
+ queryUnitList = @queries[name]
74
+
75
+ if(queryUnitList.nil?)
76
+ queryUnitList = Array.new
77
+ @queries[name] = queryUnitList
78
+ end
79
+
80
+ if(queryUnit.nil?)
81
+ queryUnit = QueryUnit.new
82
+ end
83
+
84
+ queryUnit.id = id
85
+ queryUnit.limit = limit
86
+ queryUnitList << queryUnit
87
+ self
88
+ end
89
+
90
+ def getQueries
91
+ @queries
92
+ end
93
+ end
94
+
95
+ class DateCountQuery < SimpleCountQuery
96
+ def addQueryWithDate(name, id, fromDate, toDate, limit=1)
97
+ queryUnit = DateQueryUnit.new
98
+ queryUnit.fromDate = fromDate
99
+ queryUnit.toDate = toDate
100
+ addQuery(name, id, limit, queryUnit)
101
+ self
102
+ end
103
+ end
104
+
105
+ class CountResult
106
+ attr_accessor :countResults
107
+
108
+ def initialize(hash)
109
+ @countResults = hash['countResults']
110
+ end
111
+
112
+ def getResult(name=nil, id=nil)
113
+ if(name.nil?)
114
+ return @countResults
115
+ end
116
+
117
+ countHash = @countResults[name]
118
+ if(countHash.nil?)
119
+ raise ArgumentError,"服务的名称: " + name + " 不存在!"
120
+ end
121
+
122
+ if (id.nil?)
123
+ return countHash
124
+ end
125
+
126
+ count = countHash[id]
127
+ if(count.nil?)
128
+ raise ArgumentError,"业务ID: "+id +" 不存在"
129
+ end
130
+ count
131
+ end
132
+ end
133
+ end
134
+ end
135
+ end
@@ -0,0 +1,15 @@
1
+ #
2
+ # Autogenerated by Thrift Compiler (0.9.0)
3
+ #
4
+ # DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
5
+ #
6
+
7
+ require 'thrift'
8
+ require 'collecter_types'
9
+
10
+ module Stat
11
+ module Count
12
+ module Thrift
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,209 @@
1
+ #
2
+ # Autogenerated by Thrift Compiler (0.9.0)
3
+ #
4
+ # DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
5
+ #
6
+
7
+ require 'thrift'
8
+
9
+ module Stat
10
+ module Count
11
+ module Thrift
12
+ class Record
13
+ include ::Thrift::Struct, ::Thrift::Struct_Union
14
+ RECORDMAP = 1
15
+
16
+ FIELDS = {
17
+ RECORDMAP => {:type => ::Thrift::Types::MAP, :name => 'recordMap', :key => {:type => ::Thrift::Types::STRING}, :value => {:type => ::Thrift::Types::STRING}}
18
+ }
19
+
20
+ def struct_fields; FIELDS; end
21
+
22
+ def validate
23
+ end
24
+
25
+ ::Thrift::Struct.generate_accessors self
26
+ end
27
+
28
+ class CountUnit
29
+ include ::Thrift::Struct, ::Thrift::Struct_Union
30
+ ID = 1
31
+ COUNT = 2
32
+
33
+ FIELDS = {
34
+ ID => {:type => ::Thrift::Types::STRING, :name => 'id'},
35
+ COUNT => {:type => ::Thrift::Types::I32, :name => 'count'}
36
+ }
37
+
38
+ def struct_fields; FIELDS; end
39
+
40
+ def validate
41
+ end
42
+
43
+ ::Thrift::Struct.generate_accessors self
44
+ end
45
+
46
+ class SimpleCount
47
+ include ::Thrift::Struct, ::Thrift::Struct_Union
48
+ COUNTS = 1
49
+
50
+ FIELDS = {
51
+ COUNTS => {:type => ::Thrift::Types::MAP, :name => 'counts', :key => {:type => ::Thrift::Types::STRING}, :value => {:type => ::Thrift::Types::LIST, :element => {:type => ::Thrift::Types::STRUCT, :class => ::Stat::Count::Thrift::CountUnit}}}
52
+ }
53
+
54
+ def struct_fields; FIELDS; end
55
+
56
+ def validate
57
+ end
58
+
59
+ ::Thrift::Struct.generate_accessors self
60
+ end
61
+
62
+ class DateCountUnit
63
+ include ::Thrift::Struct, ::Thrift::Struct_Union
64
+ ID = 1
65
+ COUNT = 2
66
+ TIMEMILLS = 3
67
+
68
+ FIELDS = {
69
+ ID => {:type => ::Thrift::Types::STRING, :name => 'id'},
70
+ COUNT => {:type => ::Thrift::Types::I32, :name => 'count'},
71
+ TIMEMILLS => {:type => ::Thrift::Types::I64, :name => 'timeMills'}
72
+ }
73
+
74
+ def struct_fields; FIELDS; end
75
+
76
+ def validate
77
+ end
78
+
79
+ ::Thrift::Struct.generate_accessors self
80
+ end
81
+
82
+ class DateCount
83
+ include ::Thrift::Struct, ::Thrift::Struct_Union
84
+ COUNTS = 1
85
+
86
+ FIELDS = {
87
+ COUNTS => {:type => ::Thrift::Types::MAP, :name => 'counts', :key => {:type => ::Thrift::Types::STRING}, :value => {:type => ::Thrift::Types::LIST, :element => {:type => ::Thrift::Types::STRUCT, :class => ::Stat::Count::Thrift::DateCountUnit}}}
88
+ }
89
+
90
+ def struct_fields; FIELDS; end
91
+
92
+ def validate
93
+ end
94
+
95
+ ::Thrift::Struct.generate_accessors self
96
+ end
97
+
98
+ class QueryDateUnit
99
+ include ::Thrift::Struct, ::Thrift::Struct_Union
100
+ ID = 1
101
+ LIMIT = 2
102
+ FROMDATE = 3
103
+ TODATE = 4
104
+
105
+ FIELDS = {
106
+ ID => {:type => ::Thrift::Types::STRING, :name => 'id'},
107
+ LIMIT => {:type => ::Thrift::Types::I32, :name => 'limit'},
108
+ FROMDATE => {:type => ::Thrift::Types::I64, :name => 'fromDate'},
109
+ TODATE => {:type => ::Thrift::Types::I64, :name => 'toDate'}
110
+ }
111
+
112
+ def struct_fields; FIELDS; end
113
+
114
+ def validate
115
+ end
116
+
117
+ ::Thrift::Struct.generate_accessors self
118
+ end
119
+
120
+ class DateCountQuery
121
+ include ::Thrift::Struct, ::Thrift::Struct_Union
122
+ QUERIES = 1
123
+
124
+ FIELDS = {
125
+ QUERIES => {:type => ::Thrift::Types::MAP, :name => 'queries', :key => {:type => ::Thrift::Types::STRING}, :value => {:type => ::Thrift::Types::LIST, :element => {:type => ::Thrift::Types::STRUCT, :class => ::Stat::Count::Thrift::QueryDateUnit}}}
126
+ }
127
+
128
+ def struct_fields; FIELDS; end
129
+
130
+ def validate
131
+ end
132
+
133
+ ::Thrift::Struct.generate_accessors self
134
+ end
135
+
136
+ class QueryUnit
137
+ include ::Thrift::Struct, ::Thrift::Struct_Union
138
+ ID = 1
139
+ LIMIT = 2
140
+
141
+ FIELDS = {
142
+ ID => {:type => ::Thrift::Types::STRING, :name => 'id'},
143
+ LIMIT => {:type => ::Thrift::Types::I32, :name => 'limit'}
144
+ }
145
+
146
+ def struct_fields; FIELDS; end
147
+
148
+ def validate
149
+ end
150
+
151
+ ::Thrift::Struct.generate_accessors self
152
+ end
153
+
154
+ class SimpleCountQuery
155
+ include ::Thrift::Struct, ::Thrift::Struct_Union
156
+ QUERIES = 1
157
+
158
+ FIELDS = {
159
+ QUERIES => {:type => ::Thrift::Types::MAP, :name => 'queries', :key => {:type => ::Thrift::Types::STRING}, :value => {:type => ::Thrift::Types::LIST, :element => {:type => ::Thrift::Types::STRUCT, :class => ::Stat::Count::Thrift::QueryUnit}}}
160
+ }
161
+
162
+ def struct_fields; FIELDS; end
163
+
164
+ def validate
165
+ end
166
+
167
+ ::Thrift::Struct.generate_accessors self
168
+ end
169
+
170
+ class CountResult
171
+ include ::Thrift::Struct, ::Thrift::Struct_Union
172
+ COUNTRESULTS = 1
173
+
174
+ FIELDS = {
175
+ COUNTRESULTS => {:type => ::Thrift::Types::MAP, :name => 'countResults', :key => {:type => ::Thrift::Types::STRING}, :value => {:type => ::Thrift::Types::MAP, :key => {:type => ::Thrift::Types::STRING}, :value => {:type => ::Thrift::Types::I64}}}
176
+ }
177
+
178
+ def struct_fields; FIELDS; end
179
+
180
+ def validate
181
+ end
182
+
183
+ ::Thrift::Struct.generate_accessors self
184
+ end
185
+
186
+ class DataCollectionException < ::Thrift::Exception
187
+ include ::Thrift::Struct, ::Thrift::Struct_Union
188
+ def initialize(message=nil)
189
+ super()
190
+ self.message = message
191
+ end
192
+
193
+ MESSAGE = 1
194
+
195
+ FIELDS = {
196
+ MESSAGE => {:type => ::Thrift::Types::STRING, :name => 'message'}
197
+ }
198
+
199
+ def struct_fields; FIELDS; end
200
+
201
+ def validate
202
+ end
203
+
204
+ ::Thrift::Struct.generate_accessors self
205
+ end
206
+
207
+ end
208
+ end
209
+ end