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(deprecation): Add reset methods #2262

Merged
merged 2 commits into from
Jul 8, 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
21 changes: 18 additions & 3 deletions app/models/deprecation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@ def get_all(feature_name)
end

def get_all_as_csv(feature_name)
lines = get_all(feature_name)
orgs = Organization.find(lines.pluck(:organization_id)).index_by(&:id)

CSV.generate do |csv|
csv << %w[organization_id last_seen_at count]
csv << %w[org_id org_name org_email last_event_sent_at count]

get_all(feature_name).each do |d|
csv << [d[:organization_id], d[:last_seen_at], d[:count]]
lines.each do |d|
o = orgs[d[:organization_id]]
csv << [d[:organization_id], o.name, o.email, d[:last_seen_at], d[:count]]
end
end
end
Expand All @@ -35,6 +39,17 @@ def get(feature_name, organization_id)
{organization_id:, last_seen_at:, count:}
end

def reset_all(feature_name)
Organization.pluck(:id).each do |organization_id|
reset(feature_name, organization_id)
end
end

def reset(feature_name, organization_id)
Rails.cache.delete(cache_key(feature_name, organization_id, 'count'))
Rails.cache.delete(cache_key(feature_name, organization_id, 'last_seen_at'))
end

private

def cache_key(feature_name, organization_id, suffix)
Expand Down
22 changes: 20 additions & 2 deletions spec/models/deprecation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,27 @@

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"
csv = "org_id,org_name,org_email,last_event_sent_at,count\n"
csv += "#{organization.id},#{organization.name},#{organization.email},2024-05-22T14:58:20.280Z,101\n"
expect(described_class.get_all_as_csv(feature_name)).to eq(csv)
end
end

describe '.reset' do
it 'deletes deprecation data for an organization' do
described_class.reset(feature_name, organization.id)

expect(Rails.cache.read("deprecation:#{feature_name}:#{organization.id}:last_seen_at")).to be_nil
expect(Rails.cache.read("deprecation:#{feature_name}:#{organization.id}:count")).to be_nil
end
end

describe '.reset_all' do
it 'deletes deprecation data for all organizations' do
described_class.reset_all(feature_name)

expect(Rails.cache.read("deprecation:#{feature_name}:#{organization.id}:last_seen_at")).to be_nil
expect(Rails.cache.read("deprecation:#{feature_name}:#{organization.id}:count")).to be_nil
end
end
end