Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Introduce Deprecation tracking #2056

Merged
merged 2 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .github/workflows/spec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,20 @@ jobs:
POSTGRES_DB: lago
POSTGRES_USER: lago
POSTGRES_PASSWORD: lago
redis:
image: redis
ports:
- 6379:6379
options: >-
--health-cmd "redis-cli ping"
--health-interval 10s
--health-timeout 5s
--health-retries 5

env:
RAILS_ENV: test
DATABASE_URL: "postgres://lago:lago@localhost:5432/lago"
LAGO_REDIS_CACHE_URL: "redis://localhost:6379"
RAILS_MASTER_KEY: ${{ secrets.RAILS_TEST_KEY }}
SECRET_KEY_BASE: ${{ secrets.SECRET_KEY_BASE }}
LAGO_API_URL: https://api.lago.dev
Expand All @@ -32,6 +43,7 @@ jobs:
LAGO_CLICKHOUSE_DATABASE: default
LAGO_CLICKHOUSE_USERNAME: ""
LAGO_CLICKHOUSE_PASSWORD: ""

steps:
- name: Checkout code
uses: actions/checkout@v4
Expand Down
44 changes: 44 additions & 0 deletions app/models/deprecation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# frozen_string_literal: true

require 'csv'

class Deprecation
EXPIRE_IN = 1.month

class << self
def report(feature_name, organization_id)
Rails.cache.write(cache_key(feature_name, organization_id, 'last_seen_at'), Time.current, expires_in: EXPIRE_IN)
Rails.cache.increment(cache_key(feature_name, organization_id, 'count'), 1, expires_in: EXPIRE_IN)
end

def get_all(feature_name)
Organization.pluck(:id).filter_map do |organization_id|
h = get(feature_name, organization_id)
h[:last_seen_at] ? h : nil
end
end

def get_all_as_csv(feature_name)
CSV.generate do |csv|
csv << %w[organization_id last_seen_at count]

get_all(feature_name).each do |d|
csv << [d[:organization_id], d[:last_seen_at], d[:count]]
end
end
end

def get(feature_name, organization_id)
last_seen_at = Rails.cache.read(cache_key(feature_name, organization_id, 'last_seen_at'))
count = Rails.cache.read(cache_key(feature_name, organization_id, 'count'), raw: true).to_i

{organization_id:, last_seen_at:, count:}
end

private

def cache_key(feature_name, organization_id, suffix)
"deprecation:#{feature_name}:#{organization_id}:#{suffix}"
end
end
end
4 changes: 4 additions & 0 deletions app/services/events/post_process_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ def initialize(event:)
def call
event.external_customer_id ||= customer&.external_id

unless event.external_subscription_id
Deprecation.report('event_missing_external_subscription_id', organization.id)
end

# NOTE: prevent subscription if more than 1 subscription is active
# if multiple terminated matches the timestamp, takes the most recent
if !event.external_subscription_id && subscriptions.count(&:active?) <= 1
Expand Down
5 changes: 2 additions & 3 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@
config.consider_all_requests_local = true
config.server_timing = true

config.cache_store = :redis_cache_store, {url: ENV['LAGO_REDIS_CACHE_URL'], db: '3'}

if Rails.root.join('tmp/caching-dev.txt').exist?
config.cache_store = :memory_store
config.public_file_server.headers = {
'Cache-Control' => "public, max-age=#{2.days.to_i}"
}
else
config.action_controller.perform_caching = false

config.cache_store = :null_store
end

config.active_storage.service = if ENV['LAGO_USE_AWS_S3'].present? && ENV['LAGO_USE_AWS_S3'] == 'true'
Expand Down
12 changes: 11 additions & 1 deletion config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

config.consider_all_requests_local = true
config.action_controller.perform_caching = false
config.cache_store = :null_store
config.action_dispatch.show_exceptions = false
config.action_controller.allow_forgery_protection = false
config.active_storage.service = :test
Expand All @@ -35,4 +34,15 @@

config.active_job.queue_adapter = :test
config.license_url = 'http://license.lago'

# Configure the redis cache store but always set the null_store by default
# Use `context '...', cache: :redis` to enable the redis cache store in specs
if ENV['LAGO_REDIS_CACHE_URL'].present?
redis_store_config = {
url: ENV['LAGO_REDIS_CACHE_URL'],
ssl_params: {verify_mode: OpenSSL::SSL::VERIFY_NONE}
}
config.cache_store = :redis_cache_store, redis_store_config
end
config.cache_store = :null_store
end
52 changes: 52 additions & 0 deletions spec/models/deprecation_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe Deprecation, type: :model, cache: :redis do
let(:organization) { create(:organization) }
let(:feature_name) { 'event_legacy' }

before do
Rails.cache.write("deprecation:#{feature_name}:#{organization.id}:last_seen_at", "2024-05-22T14:58:20.280Z")
Rails.cache.increment("deprecation:#{feature_name}:#{organization.id}:count", 101)
end

describe '.report' do
it 'writes to cache' do
freeze_time do
described_class.report(feature_name, organization.id)

expect(Rails.cache.read("deprecation:#{feature_name}:#{organization.id}:last_seen_at")).to eq(Time.zone.now.utc)
expect(Rails.cache.read("deprecation:#{feature_name}:#{organization.id}:count", raw: true)).to eq("102")
end
end
end

describe '.get' do
it 'returns deprecation data for an organization' do
expect(described_class.get(feature_name, organization.id)).to eq({
organization_id: organization.id,
last_seen_at: "2024-05-22T14:58:20.280Z",
count: 101
})
end
end

describe '.get_all' do
it 'returns deprecation data for all organizations' do
expect(described_class.get_all(feature_name)).to eq([{
organization_id: organization.id,
last_seen_at: "2024-05-22T14:58:20.280Z",
count: 101
}])
end
end

describe '.get_all_as_csv' do
it 'returns deprecation data for all organizations' do
csv = "organization_id,last_seen_at,count\n"
csv += "#{organization.id},2024-05-22T14:58:20.280Z,101\n"
expect(described_class.get_all_as_csv(feature_name)).to eq(csv)
end
end
end
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
ActiveSupport::Cache.lookup_store(:memory_store)
elsif example.metadata[:cache].to_sym == :null
ActiveSupport::Cache.lookup_store(:null_store)
elsif example.metadata[:cache].to_sym == :redis
ActiveSupport::Cache.lookup_store(:redis_cache_store)
else
raise "Unknown cache store: #{example.metadata[:cache]}"
end
Expand Down
Loading