Don't forget to lycansubscribe

rewritten import progress estimation logic

+1 -1
app/importers/base_importer.rb
··· 38 38 import_items 39 39 40 40 @import.update!(last_completed: @import.started_from) unless requested_time_limit 41 - @import.update!(cursor: nil, started_from: nil) 41 + @import.update!(cursor: nil, started_from: nil, fetched_until: nil) 42 42 @report&.update(importers: { importer_name => { :finished => true }}) 43 43 end 44 44
+1 -1
app/importers/likes_importer.rb
··· 33 33 @report&.update(importers: { importer_name => { :oldest_date => oldest_date }}) if oldest_date 34 34 35 35 params[:cursor] = cursor 36 - @import.update!(cursor: cursor) 36 + @import.update!(cursor: cursor, fetched_until: oldest_date) 37 37 38 38 break if !cursor 39 39 break if @time_limit && oldest_date && oldest_date < @time_limit
+1 -1
app/importers/posts_importer.rb
··· 41 41 @report&.update(importers: { importer_name => { :oldest_date => oldest_date }}) if oldest_date 42 42 43 43 params[:cursor] = cursor 44 - @import.update!(cursor: cursor) 44 + @import.update!(cursor: cursor, fetched_until: oldest_date) 45 45 46 46 break if !cursor 47 47 break if @time_limit && oldest_date && oldest_date < @time_limit
+1 -1
app/importers/reposts_importer.rb
··· 33 33 @report&.update(importers: { importer_name => { :oldest_date => oldest_date }}) if oldest_date 34 34 35 35 params[:cursor] = cursor 36 - @import.update!(cursor: cursor) 36 + @import.update!(cursor: cursor, fetched_until: oldest_date) 37 37 38 38 break if !cursor 39 39 break if @time_limit && oldest_date && oldest_date < @time_limit
+26
app/models/import.rb
··· 9 9 validates_uniqueness_of :collection, scope: :user_id 10 10 11 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 12 38 end
+3 -16
app/models/user.rb
··· 50 50 end 51 51 52 52 def imported_until 53 - return nil unless self.imports.exists? 54 - 55 - oldest_imported_items = [] 56 - started = false 53 + import_positions = self.imports.map(&:imported_until) 57 54 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? 55 + if import_positions.empty? || import_positions.any? { |x| x.nil? } 69 56 nil 70 57 else 71 - :end 58 + import_positions.sort.last 72 59 end 73 60 end 74 61
+1 -1
app/server.rb
··· 162 162 else 163 163 json_response(status: 'not_started') 164 164 end 165 - when :end 165 + when Import::IMPORT_END 166 166 json_response(status: 'finished') 167 167 else 168 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 10 # 11 11 # It's strongly recommended that you check this file into your version control system. 12 12 13 - ActiveRecord::Schema[7.2].define(version: 2025_09_23_180153) do 13 + ActiveRecord::Schema[7.2].define(version: 2025_10_27_134657) do 14 14 # These are extensions that must be enabled in order to support this database 15 15 enable_extension "plpgsql" 16 16 ··· 25 25 t.datetime "started_from" 26 26 t.datetime "last_completed" 27 27 t.string "collection", limit: 20, null: false 28 + t.datetime "fetched_until" 28 29 t.index ["user_id", "collection"], name: "index_imports_on_user_id_and_collection", unique: true 29 30 end 30 31