Class: JPSClient::UploadProgress

Inherits:
Object
  • Object
show all
Defined in:
lib/jpsclient/upload/upload_progress.rb

Overview

上传进度条管理类

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(upload_total_size: nil, draw_char: '>') ⇒ UploadProgress

Returns a new instance of UploadProgress.



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/jpsclient/upload/upload_progress.rb', line 15

def initialize(upload_total_size:nil, draw_char:'>')
  @upload_total_size = upload_total_size
  @draw_char = draw_char
  @last_update_time = (Time.now.to_f * 1000).to_i #毫秒

  @complete_size = 0
  @update_ing_size = {}
  @is_done = false

  # 添加互斥锁来保护进度条更新
  @mutex = Mutex.new
end

Instance Attribute Details

#complete_sizeObject

Returns the value of attribute complete_size.



9
10
11
# File 'lib/jpsclient/upload/upload_progress.rb', line 9

def complete_size
  @complete_size
end

#draw_charObject

Returns the value of attribute draw_char.



8
9
10
# File 'lib/jpsclient/upload/upload_progress.rb', line 8

def draw_char
  @draw_char
end

#is_doneObject

Returns the value of attribute is_done.



13
14
15
# File 'lib/jpsclient/upload/upload_progress.rb', line 13

def is_done
  @is_done
end

#last_update_timeObject

Returns the value of attribute last_update_time.



11
12
13
# File 'lib/jpsclient/upload/upload_progress.rb', line 11

def last_update_time
  @last_update_time
end

#update_ing_sizeObject

Returns the value of attribute update_ing_size.



12
13
14
# File 'lib/jpsclient/upload/upload_progress.rb', line 12

def update_ing_size
  @update_ing_size
end

#upload_total_sizeObject

Returns the value of attribute upload_total_size.



10
11
12
# File 'lib/jpsclient/upload/upload_progress.rb', line 10

def upload_total_size
  @upload_total_size
end

Instance Method Details

#complete_upload_index(upload_part: nil, complete_size: nil) ⇒ Object



40
41
42
43
44
45
# File 'lib/jpsclient/upload/upload_progress.rb', line 40

def complete_upload_index(upload_part:nil, complete_size:nil)
    @mutex.synchronize do
        @complete_size = @complete_size + complete_size
        @update_ing_size[upload_part] = 0
    end
end

#delete_upload_index(upload_part: nil) ⇒ Object



34
35
36
37
38
# File 'lib/jpsclient/upload/upload_progress.rb', line 34

def delete_upload_index(upload_part:nil)
    @mutex.synchronize do
        @update_ing_size[upload_part] = 0
    end
end

#update_upload_index(upload_part: nil, upload_size: nil) ⇒ Object



28
29
30
31
32
# File 'lib/jpsclient/upload/upload_progress.rb', line 28

def update_upload_index(upload_part:nil, upload_size:nil)
    @mutex.synchronize do
        @update_ing_size[upload_part] = upload_size
    end
end

#update_upload_progress(force_update: false) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/jpsclient/upload/upload_progress.rb', line 47

def update_upload_progress(force_update: false)
    time_now = (Time.now.to_f * 1000).to_i #毫秒
    # 允许强制更新或者满足时间间隔条件
    if force_update || time_now - @last_update_time > 80
        @mutex.synchronize do
            @last_update_time = time_now
            total_num = @upload_total_size
            index_num = @complete_size
            @update_ing_size.each do |key, value|
               index_num = index_num + value
            end

            # 确保不会超过100%
            index_num = [index_num, total_num].min

            progress_str = sprintf("%.2f", 100.0 * index_num / total_num )
            total_size = sprintf("%.2f", 1.00 * total_num / 1024 /1024 )
            upload_size = sprintf("%.2f", 1.00 * index_num / 1024 /1024 )
            index = 40.0 * index_num / total_num
            upload_message = "已上传:#{upload_size}MB|#{progress_str}\%【" + (@draw_char * (index/1).floor).ljust(40.0, '_') +  "】Total:#{total_size}MB"

            if index_num >= total_num && !@is_done
              @is_done = true
              Logger.instance.fancyinfo_complete(upload_message)
            else
              Logger.instance.fancyinfo_update(upload_message)
            end
        end
    end
end