~singpolyma/sgx-jmp

bc13638e01b9d1c96b3d61028f01a8f9043af986 — Stephen Paul Weber 2 years ago 51121d1
Store customer outbound messages/day for 1 year in redis

Storage is a sorted set, with dates as the values and message counts as the
scores.  Use zincrby to increment the message count by 1 on each new message.
Use zremrangebylex to remove all items older than 1 year so the set does not
grow unboundedly.

Dates with known message counts can be found using zrangebylex. Scores can be
had one at a time with zscore.  In redis 6.2+ zrange bylex withscores or zmscore
may also be used, but that is only in Debian experimental at time of writing.
2 files changed, 19 insertions(+), 1 deletions(-)

M .rubocop.yml
M sgx_jmp.rb
M .rubocop.yml => .rubocop.yml +3 -0
@@ 71,3 71,6 @@ Layout/IndentArray:

Style/FormatString:
  EnforcedStyle: percent

Style/FormatStringToken:
  EnforcedStyle: unannotated

M sgx_jmp.rb => sgx_jmp.rb +16 -1
@@ 5,6 5,7 @@ require "bigdecimal"
require "blather/client/dsl" # Require this first to not auto-include
require "blather/client"
require "braintree"
require "date"
require "dhall"
require "em-hiredis"
require "em_promise"


@@ 127,7 128,21 @@ end

message do |m|
	Customer.for_jid(m.from.stripped).then { |customer|
		customer.stanza_from(m)
		today = Time.now.utc.to_date
		EMPromise.all([
			REDIS.zremrangebylex(
				"jmp_customer_outbound_messages-#{customer.customer_id}",
				"-",
				# Store message counts per day for 1 year
				"[#{(today << 12).strftime('%Y%m%d')}"
			),
			REDIS.zincrby(
				"jmp_customer_outbound_messages-#{customer.customer_id}",
				1,
				today.strftime("%Y%m%d")
			),
			customer.stanza_from(m)
		])
	}.catch(&method(:panic))
end