aliyun-log 0.2.10 → 0.2.11

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: b0d96cc9d1e5e1a2bee7cdcbca04043f3a80e8b733b1be820aac363b31f8ec34
4
- data.tar.gz: 2769ddbdd8d54f257e02d12813d207fbb920c50659977a42527d6f714b45b818
3
+ metadata.gz: 8c29e47df03c69ea213e48e43e272714f950887b5ab8efaa60321e4506b4d4bd
4
+ data.tar.gz: fd5dcd273e40f70f93bd69e1fc9dbc392171d90c9881650503c420b18cf0cd5d
5
5
  SHA512:
6
- metadata.gz: 2a3c0526d83176a4b34e111744ebac7cd0543fc1396462f0299dcda7c43ce31b12fb7b8ebb08898a08edddaa684ea16e3288d1985add5f35eaf61cc8ca4222e8
7
- data.tar.gz: d658a42dd7aff881cb7cc9b31374af83417533185cca1c3e4b8763ec4c52bd85bd22749f4b5130dede0b42fdca0ec84663db469c5a9893d126cd9b49972fb198
6
+ metadata.gz: 6a4a990ac42f07d0ce44fe018604a5af553f5d53a74cdb0b3abfd9a6b15f891f434bc3fada83afdae815c3bbf5477d72be9520f6926609ec356fe9aa8b6098d5
7
+ data.tar.gz: 0addf200ac1e5328fd5fbd4a459cf756a52f0b8ad1bc53f775676340147af385dc8758da2b7b0d63c8285b409820e4ab8778a822d0505172d554e12865ad7567
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'version'
3
4
  require_relative 'log/common'
4
5
  require_relative 'log/client'
5
6
  require_relative 'log/config'
@@ -35,6 +35,7 @@ module Aliyun
35
35
  end
36
36
 
37
37
  def sync_index
38
+ return if field_indices.blank?
38
39
  has_index? ? update_index : create_index
39
40
  end
40
41
 
@@ -35,14 +35,28 @@ module Aliyun
35
35
  end
36
36
 
37
37
  def last(line = 1)
38
- find_offset(0, line, true)
38
+ data = find_offset(0, line, true)
39
+ line > 1 ? data.reverse : data
39
40
  end
40
41
 
41
42
  def find_offset(nth, line = 1, reverse = false)
43
+ # @opts[:select] = '*'
42
44
  @opts[:line] = line
43
45
  @opts[:offset] = nth
44
- @opts[:reverse] = reverse
45
- line <= 1 ? load[0] : load
46
+ if @opts[:order].present? && reverse
47
+ conds = []
48
+ @opts[:order].split(',').each do |field|
49
+ field.gsub!(/\s+desc|asc.*/i, '')
50
+ conds << "#{field.strip} DESC"
51
+ end
52
+ @opts[:order] = conds.join(', ')
53
+ end
54
+ @opts[:order] ||= reverse ? '__time__ DESC' : '__time__ ASC'
55
+ if @opts[:select].blank?
56
+ line <= 1 ? load[0] : load
57
+ else
58
+ line <= 1 ? result[0] : result
59
+ end
46
60
  end
47
61
 
48
62
  def scoping
@@ -78,10 +92,16 @@ module Aliyun
78
92
 
79
93
  def page(val)
80
94
  @opts[:page] = val - 1 if val >= 1
95
+ singleton_class.send(:define_method, :total_count) do
96
+ @total_count ||= count
97
+ end
98
+ singleton_class.send(:define_method, :total_pages) do
99
+ (total_count.to_f / (@opts[:line] || 100)).ceil
100
+ end
81
101
  self
82
102
  end
83
103
 
84
- def where(opts)
104
+ def api(opts)
85
105
  @opts.merge!(opts)
86
106
  self
87
107
  end
@@ -101,33 +121,76 @@ module Aliyun
101
121
  self
102
122
  end
103
123
 
124
+ def where(*statement)
125
+ if statement[0].is_a?(String)
126
+ ql = sanitize_array(*statement)
127
+ @opts[:where] = ql
128
+ else
129
+ ql = statement_ql(*statement)
130
+ @opts[:search] = ql
131
+ end
132
+ self
133
+ end
134
+
135
+ def select(*fields)
136
+ @opts[:select] = fields.join(', ')
137
+ self
138
+ end
139
+
140
+ def group(*fields)
141
+ @opts[:group] = fields.join(', ')
142
+ self
143
+ end
144
+
145
+ def order(*fields)
146
+ if fields[0].is_a?(Hash)
147
+ @opts[:order] = fields[0].map do |k, v|
148
+ unless %w[asc desc].include?(v.to_s)
149
+ raise ArgumentError, "Direction \"#{v}\" is invalid. Valid directions are: [:asc, :desc, :ASC, :DESC]"
150
+ end
151
+ "#{k} #{v}"
152
+ end.join(', ')
153
+ else
154
+ @opts[:order] = fields.join(', ')
155
+ end
156
+ self
157
+ end
158
+
104
159
  def query(opts = {})
105
160
  @opts[:query] = opts
106
161
  self
107
162
  end
108
163
 
109
164
  def count
165
+ # @opts[:select] = 'COUNT(*) as count'
166
+ _sql = to_sql
167
+ sql = "SELECT COUNT(*) as count"
168
+ sql += " FROM(#{_sql})" if _sql.present?
110
169
  query = @opts.dup
111
- if query[:query].blank?
112
- where_cond = query[:sql].split(/where /i)[1] if query[:sql].present?
113
- query[:query] = "#{query[:search]}|SELECT COUNT(*) as count"
114
- query[:query] = "#{query[:query]} WHERE #{where_cond}" if where_cond.present?
115
- end
116
- res = Log.record_connection.get_logs(@klass.project_name, @klass.logstore_name, query)
117
- res = JSON.parse(res.body)
118
- res[0]['count'].to_i
170
+ query[:query] = "#{query[:search] || '*'}|#{sql}"
171
+ res = execute(query)
172
+ res.dig(0, 'count').to_i
173
+ end
174
+
175
+ def sum(field)
176
+ @opts[:select] = "SUM(#{field}) as sum"
177
+ query = @opts.dup
178
+ query[:query] = "#{query[:search] || '*'}|#{to_sql}"
179
+ res = execute(query)
180
+ res.dig(0, 'sum').to_f
119
181
  end
120
182
 
121
183
  def result
122
184
  query = @opts.dup
123
- if query[:page]
124
- query[:line] ||= 100
125
- query[:offset] = query[:page] * query[:line]
126
- end
127
185
  query[:query] = query[:search] || '*'
128
- query[:query] = "#{query[:query]}|#{query[:sql]}" if query[:sql].present?
129
- res = Log.record_connection.get_logs(@klass.project_name, @klass.logstore_name, query)
130
- JSON.parse(res)
186
+ sql = to_sql
187
+ if sql.present?
188
+ query[:query] += "|#{sql}"
189
+ @opts[:line] = nil
190
+ @opts[:offset] = nil
191
+ @opts[:page] = nil
192
+ end
193
+ execute(query)
131
194
  end
132
195
 
133
196
  def load
@@ -140,8 +203,37 @@ module Aliyun
140
203
  end
141
204
  end
142
205
 
206
+ def to_sql
207
+ opts = @opts.dup
208
+ sql_query = []
209
+ sql_query << "WHERE #{opts[:where]}" if opts[:where].present?
210
+ sql_query << "GROUP BY #{opts[:group]}" if opts[:group].present?
211
+ sql_query << "ORDER BY #{opts[:order]}" if opts[:order].present?
212
+ if sql_query.present? || opts[:select].present?
213
+ sql_query.insert(0, "SELECT #{opts[:select] || '*'}")
214
+ sql_query.insert(1, 'FROM log')
215
+ if opts[:line] || opts[:page] || opts[:offset]
216
+ parse_page
217
+ sql_query << "LIMIT #{@opts[:offset]},#{@opts[:line]}"
218
+ end
219
+ end
220
+ "#{opts[:query]}#{sql_query.join(' ')}"
221
+ end
222
+
143
223
  private
144
224
 
225
+ def execute(query)
226
+ puts query.slice(:from, :to, :search, :query).to_json
227
+ res = Log.record_connection.get_logs(@klass.project_name, @klass.logstore_name, query)
228
+ JSON.parse(res)
229
+ end
230
+
231
+ def parse_page
232
+ @opts[:line] ||= 100
233
+ @opts[:page] ||= 0
234
+ @opts[:offset] = @opts[:page] * @opts[:line]
235
+ end
236
+
145
237
  def statement_ql(*statement)
146
238
  if statement.size == 1
147
239
  sanitize_hash(statement.first)
@@ -225,7 +317,7 @@ module Aliyun
225
317
  end
226
318
 
227
319
  def _quote(type, value)
228
- v = TypeCasting.dump_field(value, cast_type: type || :string)
320
+ v = TypeCasting.cast_field(value, cast_type: type || :string)
229
321
  case type
230
322
  when :string, nil then "'#{v.to_s}'"
231
323
  when :bigdecimal then v.to_s("F")
@@ -239,7 +331,7 @@ module Aliyun
239
331
  def _quote_type_value(value)
240
332
  case value.class.name
241
333
  when 'String' then "'#{value.to_s}'"
242
- when 'BigDecimal' then value.to_s("F")
334
+ when 'BigDecimal', 'Float' then value.to_s("F")
243
335
  when 'Date', 'DateTime', 'Time' then "'#{value.iso8601}'"
244
336
  else
245
337
  value
@@ -10,9 +10,10 @@ module Aliyun
10
10
  extend ActiveSupport::Concern
11
11
 
12
12
  module ClassMethods
13
- delegate :load, :result, :count, to: :all
14
- delegate :where, :query, :search, :sql, :from, :to,
15
- :page, :line, :limit, :offset, to: :all
13
+ delegate :load, :result, :count, :sum, to: :all
14
+ delegate :where, :query, :search, :sql, :from, :to, :api,
15
+ :page, :line, :limit, :offset, :select, :group,
16
+ :order, :to_sql, to: :all
16
17
  delegate :first, :last, :second, :third, :fourth, :fifth, :find_offset, to: :all
17
18
 
18
19
  def current_scope
@@ -9,7 +9,7 @@ module Aliyun
9
9
  TYPE_MAPPING = {
10
10
  text: :string,
11
11
  long: :integer,
12
- double: :bigdecimal,
12
+ double: :float,
13
13
  json: :json
14
14
  }.freeze
15
15
 
@@ -148,6 +148,26 @@ module Aliyun
148
148
  end
149
149
  end
150
150
 
151
+ class FloatType < Value
152
+ def cast(value)
153
+ if value == true
154
+ 1
155
+ elsif value == false
156
+ 0
157
+ elsif value.is_a?(Symbol)
158
+ value.to_s.to_f
159
+ elsif value.is_a?(String) && value.blank?
160
+ nil
161
+ elsif value.is_a?(Float) && !value.finite?
162
+ nil
163
+ elsif !value.respond_to?(:to_f)
164
+ nil
165
+ else
166
+ value.to_f
167
+ end
168
+ end
169
+ end
170
+
151
171
  class JsonType < Value
152
172
  def cast(value)
153
173
  return value unless value.is_a?(String)
@@ -183,6 +203,7 @@ module Aliyun
183
203
  register(:datetime, DateTimeType)
184
204
  register(:bigdecimal, BigDecimalType)
185
205
  register(:integer, IntegerType)
206
+ register(:float, FloatType)
186
207
  register(:json, JsonType)
187
208
  end
188
209
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Aliyun
4
4
  module Log
5
- VERSION = '0.2.10'
5
+ VERSION = '0.2.11'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aliyun-log
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.10
4
+ version: 0.2.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yingce Liu
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-06-29 00:00:00.000000000 Z
11
+ date: 2020-07-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel