trifle-stats 1.1.2 → 1.2.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4ac85761915230026b6ab5affd1ac6d45236d36d2434a69a4128d8b08c04af12
4
- data.tar.gz: e9255ab996fc187925f67459fe3ce93e2af6428e7f1429267de4c1b874d7de4e
3
+ metadata.gz: a42d81aa9cf531df2abc462167d9b173af444c2089f15f1d53354153b1fb18df
4
+ data.tar.gz: b5237b9c28ce0779ecdc3f129d26de062084490dd9b470b05ffb975a05ecc293
5
5
  SHA512:
6
- metadata.gz: 0cd919f5670dba68d02cb477b39a95bd424811c95cef99b54d28b3557aabb738ac39d73a907ca2ce595623af23ddcdf69780403471a49f29a37c05fdf5eeb6f6
7
- data.tar.gz: 897e2860a614d65523999b7a3e07395188be479fdec837aabc57ae6f8e2cf252d28a0fed0459505bd96d771af54c1afa3ff4deb7a5c54e21116dbcca4547139e
6
+ metadata.gz: d835304df2db49efd76728e0de0bb1ae827680a5ec1a5d4a071a9ac60ca4a045441b8afa5628ca6e38e7f07c010c2cd550bdb15b91409e89f9211242bed17527
7
+ data.tar.gz: '08e5fa3dceb15fd8927c2320efe73ab543801ac4bac99ff80a478ea344e03b0c6f1f78c7ec23a21c5ac18d22912f4a10774f5a6389e2ae5a428402776e057fc5'
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- trifle-stats (1.1.2)
4
+ trifle-stats (1.2.0)
5
5
  tzinfo (~> 2.0)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -12,7 +12,7 @@ Simple analytics backed by Redis, Postgres, MongoDB, Google Analytics, Segment,
12
12
 
13
13
  ## Documentation
14
14
 
15
- You can find guides and documentation at https://trifle.io/docs/stats
15
+ You can find guides and documentation at https://trifle.io/trifle-stats
16
16
 
17
17
  ## Installation
18
18
 
data/bin/console CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  require "bundler/setup"
4
4
  require "trifle/stats"
5
+ require "byebug"
5
6
 
6
7
  # You can add fixtures and/or initialization code here to make experimenting
7
8
  # with your gem easier. You can also use a different console, if you like.
@@ -20,15 +20,13 @@ module Trifle
20
20
  keys.map do |key|
21
21
  pkey = key.join(separator)
22
22
 
23
- self.class.pack(hash: values).each do |k, c|
24
- _inc_one(key: pkey, name: k, value: c)
25
- end
23
+ _inc_all(key: pkey, data: self.class.pack(hash: values))
26
24
  end
27
25
  end
28
26
 
29
- def _inc_one(key:, name:, value:)
30
- data = { name => value }
31
- query = "INSERT INTO trifle_stats(key, data) VALUES ('#{key}', '#{data.to_json}') ON CONFLICT (key) DO UPDATE SET data = jsonb_set(to_jsonb(trifle_stats.data), '{#{name}}', (COALESCE(trifle_stats.data->>'#{name}','0')::int + #{value})::text::jsonb)" # rubocop:disable Metric/LineLength
27
+ def _inc_all(key:, data:)
28
+ query = "INSERT INTO trifle_stats(key, data) VALUES ('#{key}', '#{data.to_json}') ON CONFLICT (key) DO UPDATE SET data = " + # rubocop:disable Layout/LineLength
29
+ data.inject('to_jsonb(trifle_stats.data)') { |o, (k, v)| "jsonb_set(#{o}, '{#{k}}', (COALESCE(trifle_stats.data->>'#{k}', '0')::int + #{v})::text::jsonb)" } # rubocop:disable Layout/LineLength
32
30
 
33
31
  client.exec(query)
34
32
  end
@@ -37,37 +35,36 @@ module Trifle
37
35
  keys.map do |key|
38
36
  pkey = key.join(separator)
39
37
 
40
- _set_all(key: pkey, **values)
38
+ _set_all(key: pkey, data: self.class.pack(hash: values))
41
39
  end
42
40
  end
43
41
 
44
- def _set_all(key:, **values)
45
- data = self.class.pack(hash: values)
46
- query = "INSERT INTO trifle_stats(key, data) VALUES ('#{key}', '#{data.to_json}') ON CONFLICT (key) DO UPDATE SET data = '#{data.to_json}'" # rubocop:disable Metric/LineLength
42
+ def _set_all(key:, data:)
43
+ query = "INSERT INTO trifle_stats(key, data) VALUES ('#{key}', '#{data.to_json}') ON CONFLICT (key) DO UPDATE SET data = " + # rubocop:disable Layout/LineLength
44
+ data.inject('to_jsonb(trifle_stats.data)') { |o, (k, v)| "jsonb_set(#{o}, '{#{k}}', (#{v})::text::jsonb)" } # rubocop:disable Layout/LineLength
47
45
 
48
46
  client.exec(query)
49
47
  end
50
48
 
51
49
  def get(keys:)
52
- keys.map do |key|
53
- pkey = key.join(separator)
54
-
55
- data = _get(key: pkey)
56
- return {} if data.nil?
50
+ pkeys = keys.map { |key| key.join(separator) }
51
+ data = _get_all(keys: pkeys)
52
+ map = data.inject({}) { |o, d| o.merge(d['key'] => d['data']) }
57
53
 
58
- self.class.unpack(hash: data)
59
- end
54
+ pkeys.map { |pkey| self.class.unpack(hash: map[pkey]) || {} }
60
55
  end
61
56
 
62
- def _get(key:)
63
- result = client.exec_params(
64
- "SELECT * FROM #{table_name} WHERE key = $1 LIMIT 1;", [key]
65
- ).to_a.first
66
- return nil if result.nil?
57
+ def _get_all(keys:)
58
+ results = client.exec_params(
59
+ "SELECT * FROM #{table_name} WHERE key IN ('#{keys.join("', '")}');"
60
+ ).to_a
67
61
 
68
- JSON.parse(result['data'])
69
- rescue JSON::ParserError
70
- nil
62
+ results.map do |r|
63
+ r['data'] = JSON.parse(r['data'])
64
+ r
65
+ rescue JSON::ParserError
66
+ r
67
+ end
71
68
  end
72
69
  end
73
70
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Trifle
4
4
  module Stats
5
- VERSION = '1.1.2'
5
+ VERSION = '1.2.0'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: trifle-stats
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.2
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jozef Vaclavik
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-07-30 00:00:00.000000000 Z
11
+ date: 2022-07-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler