fluent-plugin-fluent-plugin-json-size-limit 0.1.26 → 0.1.27
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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fc2d0b868e3e3879068c4c4adb44caf832379ad4d927f09e72f02223cc9efe9b
|
4
|
+
data.tar.gz: cfa85d22a6b21b41ff7541f6040ca22c4ed5fbf20b13f818392928e1f5d5624a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 18f99e8b8e7841be35f024b481676e7cb0da6b9f045d4ff67479c6cb6325a8ca74452f469f8d3631878c3bf2dbaa179af6dd4ab76392e912656cd44a70c9fc23
|
7
|
+
data.tar.gz: b3eae5c689ed71874327ba01d30803321b19f5848f8977f8e2b57862650adf1504deb263181ad13232ee97dcc6fa1a5c396c5014cb30a0257ddd8865d9b759bc
|
@@ -3,7 +3,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
3
3
|
|
4
4
|
Gem::Specification.new do |spec|
|
5
5
|
spec.name = "fluent-plugin-fluent-plugin-json-size-limit"
|
6
|
-
spec.version = "0.1.
|
6
|
+
spec.version = "0.1.27"
|
7
7
|
spec.authors = ["Mykola Panin"]
|
8
8
|
spec.email = ["mykola.panin@creatoriq.com"]
|
9
9
|
|
@@ -41,41 +41,36 @@ module Fluent
|
|
41
41
|
def reduce_size(obj, target_size)
|
42
42
|
attempts = 0
|
43
43
|
while obj.to_json.bytesize > target_size && attempts < 100
|
44
|
-
|
44
|
+
excess_size = obj.to_json.bytesize - target_size
|
45
|
+
perform_reduction(obj, excess_size)
|
45
46
|
attempts += 1
|
46
47
|
end
|
47
48
|
end
|
48
49
|
|
49
|
-
def perform_reduction(obj,
|
50
|
-
excess_size = obj.to_json.bytesize - target_size
|
51
|
-
|
50
|
+
def perform_reduction(obj, excess_size)
|
52
51
|
if obj.is_a?(Hash)
|
53
|
-
|
54
|
-
|
55
|
-
reduce_array(obj, target_size)
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
def reduce_hash(hash_obj, excess_size)
|
60
|
-
hash_obj.each do |key, value|
|
61
|
-
if value.is_a?(String)
|
62
|
-
reduce_string(hash_obj, key, value, excess_size)
|
63
|
-
elsif value.is_a?(Hash) || value.is_a?(Array)
|
64
|
-
reduce_size(value, excess_size)
|
52
|
+
obj.each do |key, value|
|
53
|
+
apply_reduction(value, excess_size)
|
65
54
|
end
|
55
|
+
elsif obj.is_a?(Array)
|
56
|
+
obj.each { |element| apply_reduction(element, excess_size) }
|
57
|
+
elsif obj.is_a?(String)
|
58
|
+
reduce_string(obj, excess_size)
|
66
59
|
end
|
67
60
|
end
|
68
61
|
|
69
|
-
def
|
70
|
-
|
71
|
-
|
62
|
+
def apply_reduction(element, excess_size)
|
63
|
+
if element.is_a?(String)
|
64
|
+
reduce_string(element, excess_size)
|
65
|
+
elsif element.is_a?(Hash) || element.is_a?(Array)
|
66
|
+
reduce_size(element, element.to_json.bytesize - excess_size)
|
72
67
|
end
|
73
68
|
end
|
74
69
|
|
75
|
-
def reduce_string(
|
76
|
-
reduction_amount = (
|
77
|
-
new_length = [
|
78
|
-
|
70
|
+
def reduce_string(str, excess_size)
|
71
|
+
reduction_amount = (str.length.to_f / str.to_json.bytesize) * excess_size
|
72
|
+
new_length = [str.length - reduction_amount, 0].max
|
73
|
+
str.replace(str[0...new_length])
|
79
74
|
end
|
80
75
|
end
|
81
76
|
end
|