aliyun-log 0.2.2 → 0.2.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5b17c3b8727ec3dc44742d220f22f26014a201334889f888d858a0cbc33b9c39
4
- data.tar.gz: df953904cf39fea58a11d99fab15ca115e3187667d062dfacec0a6f1301b2510
3
+ metadata.gz: ed78b39497d6a671405b2d815292ca1e1e11ee301d816395264add76a9bc1235
4
+ data.tar.gz: 1b2f92c27745b27be61ca5f8fbb41470eaffcd710b22979671604173646c5450
5
5
  SHA512:
6
- metadata.gz: dc5e4072953b2e687f7153230d23f3f6c6d68ac80abb52278819531cb6219eec1c0c9abf593e93d2b05dc697d74f6be75d94c522314088c416bb108631290bd7
7
- data.tar.gz: 96b65f80b1fba4284d4472e524d8e424d58fff86ebac92cb281481af51134f8a75bb624d8339f67735038d3d36252bb4a6aa66bb3bc117c8706622a80eccefc1
6
+ metadata.gz: 19e6a18d2c168882054e14875da1baa18bab32217b2626a33e209e25c867b011deb5cba7d0b6de2f4af40a67ff4c66fb12ff3908881d9e28ced5ca8020c152b4
7
+ data.tar.gz: 7cfdddb1c6d804b4f62ee34cac200d4eb869ff32c70cf5bda5a4fd10947d06c5df61ba273662aff908a57e685a8ce2c586d1e3efed8d71e1dc2c8a59b649e117
@@ -38,7 +38,7 @@ module Aliyun
38
38
  def self.logger
39
39
  unless @logger
40
40
  @logger = Logger.new(
41
- @log_file ||= Config.log_file, MAX_NUM_LOG, ROTATE_SIZE
41
+ @log_file ||= Config.log_file || IO::NULL, MAX_NUM_LOG, ROTATE_SIZE
42
42
  )
43
43
  @logger.level = Logging.logger_level
44
44
  end
@@ -7,7 +7,6 @@ module Aliyun
7
7
  @endpoint = 'https://cn-beijing.log.aliyuncs.com'
8
8
  @open_timeout = 10
9
9
  @read_timeout = 120
10
- @log_file = 'aliyun_log.log'
11
10
  @log_level = Logger::DEBUG
12
11
  @timestamps = true
13
12
  class << self
@@ -108,7 +108,7 @@ module Aliyun
108
108
  def build_log_pb(attrs, time = Time.now.to_i)
109
109
  logs = attrs.is_a?(Array) ? attrs : [attrs]
110
110
  logs.map do |log_attr|
111
- contents = log_attr.compact.map { |k, v| { key: k, value: v.to_s } }
111
+ contents = log_attr.map { |k, v| { key: k, value: v.to_s } }
112
112
  Protobuf::Log.new(time: time, contents: contents)
113
113
  end
114
114
  end
@@ -182,7 +182,7 @@ module Aliyun
182
182
  keys: {}
183
183
  }
184
184
  fields.each do |k, v|
185
- v[:token] = INDEX_DEFAULT_TOKEN if %w[text json].include?(v[:type].to_s) && v[:token].blank?
185
+ v[:token] = INDEX_DEFAULT_TOKEN if %w[text json].include?(v[:type].to_s) && v[:token].nil?
186
186
  body[:keys][k] = v
187
187
  end
188
188
  @http.post({ project: project_name, logstore: logstore_name, action: 'index' }, body.to_json)
@@ -196,7 +196,7 @@ module Aliyun
196
196
  keys: {}
197
197
  }
198
198
  fields.each do |k, v|
199
- v[:token] = INDEX_DEFAULT_TOKEN if %w[text json].include?(v[:type].to_s) && v[:token].blank?
199
+ v[:token] = INDEX_DEFAULT_TOKEN if %w[text json].include?(v[:type].to_s) && v[:token].nil?
200
200
  body[:keys][k] = v
201
201
  end
202
202
  @http.put({ project: project_name, logstore: logstore_name, action: 'index' }, body.to_json)
@@ -10,6 +10,7 @@ require_relative 'record/exception'
10
10
  require_relative 'record/field'
11
11
  require_relative 'record/persistence'
12
12
  require_relative 'record/relation'
13
+ require_relative 'record/scope_registry'
13
14
 
14
15
  module Aliyun
15
16
  module Log
@@ -31,7 +32,7 @@ module Aliyun
31
32
 
32
33
  define_model_callbacks :save, :create, :initialize
33
34
 
34
- after_initialize :set_created_at
35
+ before_save :set_created_at
35
36
  end
36
37
 
37
38
  include Field
@@ -41,27 +42,45 @@ module Aliyun
41
42
 
42
43
  module ClassMethods
43
44
  def logstore(options = {})
44
- if options[:timestamps] && !Config.timestamps
45
+ opt = options.dup
46
+ if opt[:timestamps] && !Config.timestamps
45
47
  field :created_at, :text
46
- elsif options[:timestamps] == false && Config.timestamps
48
+ elsif opt[:timestamps] == false && Config.timestamps
47
49
  remove_field :created_at
48
50
  end
49
- self._schema_load = true if options[:auto_sync] == false
50
- self.options = options
51
+ self._schema_load = true if opt[:auto_sync] == false
52
+ opt[:field_doc_value] = opt[:field_doc_value] != false
53
+ self.options = opt
51
54
  end
52
55
 
53
- Relation.instance_methods(false).each do |method|
54
- define_method(method) do |args|
55
- relation.public_send(method, args)
56
- end
56
+ delegate :load, :result, :count, to: :all
57
+ delegate :where, :query, :search, :sql, :from, :to, :page, :line, :limit, :offset, to: :all
58
+ delegate :first, :last, :second, :third, :fourth, :fifth, :find_offset, to: :all
59
+
60
+ def current_scope
61
+ ScopeRegistry.value_for(:current_scope, self)
62
+ end
63
+
64
+ def current_scope=(scope)
65
+ ScopeRegistry.set_value_for(:current_scope, self, scope)
57
66
  end
58
67
 
59
- %i[count load result].each do |method|
60
- define_method(method) do
61
- relation.public_send(method)
68
+ def scope(name, body)
69
+ raise ArgumentError, 'The scope body needs to be callable.' unless body.respond_to?(:call)
70
+
71
+ singleton_class.send(:define_method, name) do |*args|
72
+ scope = all
73
+ scope = scope.scoping { body.call(*args) }
74
+ scope
62
75
  end
63
76
  end
64
77
 
78
+ def all
79
+ scope = current_scope
80
+ scope ||= relation.from(0).to(Time.now.to_i)
81
+ scope
82
+ end
83
+
65
84
  private
66
85
 
67
86
  def relation
@@ -101,7 +120,7 @@ module Aliyun
101
120
  end.compact.join(', ')
102
121
  else
103
122
  'not initialized'
104
- end
123
+ end
105
124
 
106
125
  "#<#{self.class} #{inspection}>"
107
126
  end
@@ -1,8 +1,4 @@
1
1
  # frozen_string_literal: true
2
- require "active_support"
3
- require "active_support/time"
4
- require "active_support/core_ext"
5
- require "active_model"
6
2
 
7
3
  module Aliyun
8
4
  module Log
@@ -29,6 +25,7 @@ module Aliyun
29
25
  unless PERMITTED_KEY_TYPES.include?(type)
30
26
  raise ArgumentError, "Field #{name} type(#{type}) error, key type only support text/long/double/json"
31
27
  end
28
+
32
29
  named = name.to_s
33
30
  self.attributes = attributes.merge(name => { type: type }.merge(options))
34
31
 
@@ -8,7 +8,8 @@ module Aliyun
8
8
 
9
9
  module ClassMethods
10
10
  def logstore_name
11
- @logstore_name ||= options[:name] || base_class.name.split('::').last.downcase.pluralize
11
+ @logstore_name ||= options[:name] ||
12
+ base_class.name.split('::').last.underscore.pluralize
12
13
  end
13
14
 
14
15
  def logstore_name=(value)
@@ -79,11 +80,17 @@ module Aliyun
79
80
  end
80
81
 
81
82
  def field_indices
82
- if options[:field_index] == true
83
- attributes.reject { |_, value| value[:index] == false }
84
- else
85
- attributes.select { |_, value| value[:index] == true }
83
+ indices = if options[:field_index] == false
84
+ attributes.select { |_, value| value[:index] == true }
85
+ else
86
+ attributes.reject { |_, value| value[:index] == false }
87
+ end
88
+ indices.each do |_, v|
89
+ next unless v[:doc_value].nil?
90
+
91
+ v[:doc_value] = options[:field_doc_value] != false
86
92
  end
93
+ indices
87
94
  end
88
95
 
89
96
  def create_index
@@ -11,6 +11,49 @@ module Aliyun
11
11
  @opts[:search] ||= '*'
12
12
  end
13
13
 
14
+ def inspect
15
+ "#<#{self.class}>"
16
+ end
17
+
18
+ def first(line = 1)
19
+ find_offset(0, line, false)
20
+ end
21
+
22
+ def second
23
+ find_offset(1)
24
+ end
25
+
26
+ def third
27
+ find_offset(2)
28
+ end
29
+
30
+ def fourth
31
+ find_offset(3)
32
+ end
33
+
34
+ def fifth
35
+ find_offset(4)
36
+ end
37
+
38
+ def last(line = 1)
39
+ find_offset(0, line, true)
40
+ end
41
+
42
+ def find_offset(nth, line = 1, reverse = false)
43
+ @opts[:line] = line
44
+ @opts[:offset] = nth
45
+ @opts[:reverse] = reverse
46
+ line <= 1 ? load[0] : load
47
+ end
48
+
49
+ def scoping
50
+ previous = @klass.current_scope
51
+ @klass.current_scope = self
52
+ yield
53
+ ensure
54
+ @klass.current_scope = previous
55
+ end
56
+
14
57
  def from(from)
15
58
  ts = from.is_a?(Integer) ? from : from.to_time.to_i
16
59
  @opts[:from] = ts
@@ -92,6 +135,16 @@ module Aliyun
92
135
  @klass.new(attrs)
93
136
  end
94
137
  end
138
+
139
+ private
140
+
141
+ def method_missing(method, *args, &block)
142
+ if @klass.respond_to?(method)
143
+ scoping { @klass.public_send(method, *args, &block) }
144
+ else
145
+ super
146
+ end
147
+ end
95
148
  end
96
149
  end
97
150
  end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Aliyun
4
+ module Log
5
+ module PerThreadRegistry
6
+ def self.extended(object)
7
+ object.instance_variable_set '@per_thread_registry_key', object.name.freeze
8
+ end
9
+
10
+ def instance
11
+ Thread.current[@per_thread_registry_key] ||= new
12
+ end
13
+
14
+ private
15
+
16
+ def method_missing(name, *args, &block)
17
+ singleton_class.delegate name, to: :instance
18
+
19
+ send(name, *args, &block)
20
+ end
21
+ end
22
+
23
+ class ScopeRegistry
24
+ extend PerThreadRegistry
25
+
26
+ def initialize
27
+ @registry = Hash.new { |hash, key| hash[key] = {} }
28
+ end
29
+
30
+ def value_for(scope_type, model)
31
+ @registry[scope_type][model.name]
32
+ end
33
+
34
+ # Sets the +value+ for a given +scope_type+ and +model+.
35
+ def set_value_for(scope_type, model, value)
36
+ @registry[scope_type][model.name] = value
37
+ end
38
+ end
39
+ end
40
+ end
@@ -83,7 +83,7 @@ module Aliyun
83
83
  end
84
84
 
85
85
  logger.debug("Received HTTP response, code: #{response.code}, headers: " \
86
- "#{response.headers}, body: #{response.body}")
86
+ "#{response.headers}, body: #{response.body.force_encoding('UTF-8')}")
87
87
 
88
88
  response
89
89
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Aliyun
4
4
  module Log
5
- VERSION = '0.2.2'
5
+ VERSION = '0.2.6'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aliyun-log
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yingce Liu
@@ -136,6 +136,7 @@ files:
136
136
  - lib/aliyun/log/record/field.rb
137
137
  - lib/aliyun/log/record/persistence.rb
138
138
  - lib/aliyun/log/record/relation.rb
139
+ - lib/aliyun/log/record/scope_registry.rb
139
140
  - lib/aliyun/log/request.rb
140
141
  - lib/aliyun/log/server_error.rb
141
142
  - lib/aliyun/version.rb