Commit 3cf0f1c9 authored by Toon Claes's avatar Toon Claes Committed by Stan Hu

Add methods to filter on status

Add some methods which you can use to filter Geo::FileRegistries on
status, or check what's a particular registry has (used in the Geo
Admin panel).
parent b8cd0f6a
......@@ -5,6 +5,8 @@ class Geo::FileRegistry < Geo::BaseRegistry
scope :lfs_objects, -> { where(file_type: :lfs) }
scope :attachments, -> { where(file_type: Geo::FileService::DEFAULT_OBJECT_TYPES) }
scope :never, -> { where(retry_count: nil) }
scope :fresh, -> { order(created_at: :desc) }
self.inheritance_column = 'file_type'
......@@ -15,4 +17,30 @@ class Geo::FileRegistry < Geo::BaseRegistry
Geo::UploadRegistry
end
end
def self.with_status(status)
case status
when 'synced'
synced
when 'never'
never
when 'failed'
failed
else
self
end
end
# Returns a synchronization state based on existing attribute values
#
# It takes into account things like if a successful replication has been done
# if there are pending actions or existing errors
#
# @return [Symbol] :never, :failed:, :pending or :synced
def synchronization_state
return :synced if success?
return :never if success.nil? && retry_count.nil?
:failed
end
end
......@@ -17,11 +17,55 @@ describe Geo::FileRegistry do
end
describe '.retry_due' do
set(:retry_yesterday) { create(:geo_file_registry, retry_at: Date.yesterday) }
set(:retry_tomorrow) { create(:geo_file_registry, retry_at: Date.tomorrow) }
it 'returns registries in the synced state' do
retry_yesterday = create(:geo_file_registry, retry_at: Date.yesterday)
create(:geo_file_registry, retry_at: Date.tomorrow)
expect(described_class.retry_due).to match_ids([failed, synced, retry_yesterday])
end
end
describe '.never' do
it 'returns registries that are never synced' do
create(:geo_file_registry, retry_count: 1, success: true)
create(:geo_file_registry, retry_count: 2, success: false)
expect(described_class.never).to match_ids([failed, synced])
end
end
describe '.with_status' do
it 'finds the registries with status "synced"' do
expect(described_class).to receive(:synced)
described_class.with_status('synced')
end
it 'finds the registries with status "never"' do
expect(described_class).to receive(:never)
described_class.with_status('never')
end
it 'finds the registries with status "failed"' do
expect(described_class).to receive(:failed)
described_class.with_status('failed')
end
end
describe '#synchronization_state' do
it 'returns :synced for a successful synced registry' do
expect(synced.synchronization_state).to eq(:synced)
end
it 'returns :never for a successful registry never synced' do
never = build(:geo_file_registry, success: nil)
expect(never.synchronization_state).to eq(:never)
end
it 'returns :failed for a failed registry' do
expect(failed.synchronization_state).to eq(:failed)
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment