Don't forget to lycansubscribe

rewritten import progress estimation logic

+1 -1
app/importers/base_importer.rb
··· 38 import_items 39 40 @import.update!(last_completed: @import.started_from) unless requested_time_limit 41 - @import.update!(cursor: nil, started_from: nil) 42 @report&.update(importers: { importer_name => { :finished => true }}) 43 end 44
··· 38 import_items 39 40 @import.update!(last_completed: @import.started_from) unless requested_time_limit 41 + @import.update!(cursor: nil, started_from: nil, fetched_until: nil) 42 @report&.update(importers: { importer_name => { :finished => true }}) 43 end 44
+1 -1
app/importers/likes_importer.rb
··· 33 @report&.update(importers: { importer_name => { :oldest_date => oldest_date }}) if oldest_date 34 35 params[:cursor] = cursor 36 - @import.update!(cursor: cursor) 37 38 break if !cursor 39 break if @time_limit && oldest_date && oldest_date < @time_limit
··· 33 @report&.update(importers: { importer_name => { :oldest_date => oldest_date }}) if oldest_date 34 35 params[:cursor] = cursor 36 + @import.update!(cursor: cursor, fetched_until: oldest_date) 37 38 break if !cursor 39 break if @time_limit && oldest_date && oldest_date < @time_limit
+1 -1
app/importers/posts_importer.rb
··· 41 @report&.update(importers: { importer_name => { :oldest_date => oldest_date }}) if oldest_date 42 43 params[:cursor] = cursor 44 - @import.update!(cursor: cursor) 45 46 break if !cursor 47 break if @time_limit && oldest_date && oldest_date < @time_limit
··· 41 @report&.update(importers: { importer_name => { :oldest_date => oldest_date }}) if oldest_date 42 43 params[:cursor] = cursor 44 + @import.update!(cursor: cursor, fetched_until: oldest_date) 45 46 break if !cursor 47 break if @time_limit && oldest_date && oldest_date < @time_limit
+1 -1
app/importers/reposts_importer.rb
··· 33 @report&.update(importers: { importer_name => { :oldest_date => oldest_date }}) if oldest_date 34 35 params[:cursor] = cursor 36 - @import.update!(cursor: cursor) 37 38 break if !cursor 39 break if @time_limit && oldest_date && oldest_date < @time_limit
··· 33 @report&.update(importers: { importer_name => { :oldest_date => oldest_date }}) if oldest_date 34 35 params[:cursor] = cursor 36 + @import.update!(cursor: cursor, fetched_until: oldest_date) 37 38 break if !cursor 39 break if @time_limit && oldest_date && oldest_date < @time_limit
+26
app/models/import.rb
··· 9 validates_uniqueness_of :collection, scope: :user_id 10 11 scope :unfinished, -> { where('(started_from IS NOT NULL) OR (last_completed IS NULL)') } 12 end
··· 9 validates_uniqueness_of :collection, scope: :user_id 10 11 scope :unfinished, -> { where('(started_from IS NOT NULL) OR (last_completed IS NULL)') } 12 + 13 + IMPORT_END = Time.at(0) 14 + 15 + def imported_until 16 + return nil if cursor.nil? && last_completed.nil? 17 + 18 + groups = case collection 19 + when 'likes' 20 + [:likes] 21 + when 'reposts' 22 + [:reposts] 23 + when 'posts' 24 + [:pins, :quotes] 25 + end 26 + 27 + newest_queued_items = groups.map { |g| user.send(g).where(queue: :import).order(:time).last } 28 + newest_queued = newest_queued_items.compact.sort_by(&:time).last 29 + 30 + if newest_queued 31 + newest_queued.time 32 + elsif fetched_until 33 + fetched_until 34 + else 35 + IMPORT_END 36 + end 37 + end 38 end
+3 -16
app/models/user.rb
··· 50 end 51 52 def imported_until 53 - return nil unless self.imports.exists? 54 - 55 - oldest_imported_items = [] 56 - started = false 57 58 - [:likes, :reposts, :pins, :quotes].each do |group| 59 - if self.send(group).where(queue: :import).exists? 60 - oldest_imported_items << self.send(group).where(queue: nil).order(:time).first 61 - end 62 - end 63 - 64 - earliest_oldest = oldest_imported_items.compact.sort_by(&:time).last 65 - 66 - if earliest_oldest 67 - earliest_oldest.time 68 - elsif self.imports.merge(Import.unfinished).exists? 69 nil 70 else 71 - :end 72 end 73 end 74
··· 50 end 51 52 def imported_until 53 + import_positions = self.imports.map(&:imported_until) 54 55 + if import_positions.empty? || import_positions.any? { |x| x.nil? } 56 nil 57 else 58 + import_positions.sort.last 59 end 60 end 61
+1 -1
app/server.rb
··· 162 else 163 json_response(status: 'not_started') 164 end 165 - when :end 166 json_response(status: 'finished') 167 else 168 progress = 1 - (until_date - user.registered_at) / (Time.now - user.registered_at)
··· 162 else 163 json_response(status: 'not_started') 164 end 165 + when Import::IMPORT_END 166 json_response(status: 'finished') 167 else 168 progress = 1 - (until_date - user.registered_at) / (Time.now - user.registered_at)
+5
db/migrate/20251027134657_add_fetched_until_to_imports.rb
···
··· 1 + class AddFetchedUntilToImports < ActiveRecord::Migration[7.2] 2 + def change 3 + add_column :imports, :fetched_until, :datetime 4 + end 5 + end
+2 -1
db/schema.rb
··· 10 # 11 # It's strongly recommended that you check this file into your version control system. 12 13 - ActiveRecord::Schema[7.2].define(version: 2025_09_23_180153) do 14 # These are extensions that must be enabled in order to support this database 15 enable_extension "plpgsql" 16 ··· 25 t.datetime "started_from" 26 t.datetime "last_completed" 27 t.string "collection", limit: 20, null: false 28 t.index ["user_id", "collection"], name: "index_imports_on_user_id_and_collection", unique: true 29 end 30
··· 10 # 11 # It's strongly recommended that you check this file into your version control system. 12 13 + ActiveRecord::Schema[7.2].define(version: 2025_10_27_134657) do 14 # These are extensions that must be enabled in order to support this database 15 enable_extension "plpgsql" 16 ··· 25 t.datetime "started_from" 26 t.datetime "last_completed" 27 t.string "collection", limit: 20, null: false 28 + t.datetime "fetched_until" 29 t.index ["user_id", "collection"], name: "index_imports_on_user_id_and_collection", unique: true 30 end 31