From bc13638e01b9d1c96b3d61028f01a8f9043af986 Mon Sep 17 00:00:00 2001 From: Stephen Paul Weber Date: Tue, 1 Jun 2021 22:40:25 -0500 Subject: [PATCH] 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. --- .rubocop.yml | 3 +++ sgx_jmp.rb | 17 ++++++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/.rubocop.yml b/.rubocop.yml index 996f457..cdd6878 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -71,3 +71,6 @@ Layout/IndentArray: Style/FormatString: EnforcedStyle: percent + +Style/FormatStringToken: + EnforcedStyle: unannotated diff --git a/sgx_jmp.rb b/sgx_jmp.rb index 3008d54..c6e5a90 100644 --- a/sgx_jmp.rb +++ b/sgx_jmp.rb @@ -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 -- 2.34.5