-
Notifications
You must be signed in to change notification settings - Fork 12
/
progress.go
102 lines (93 loc) · 2.46 KB
/
progress.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Copyright 2020 Alexey Krivonogov. All rights reserved.
// Use of this source code is governed by a MIT license
// that can be found in the LICENSE file.
package main
import (
"encoding/json"
"strconv"
"time"
"github.com/gentee/gentee"
"github.com/gentee/gentee/vm"
)
const (
ProgCounter = 100
)
type ProgressData struct {
Percent int64
Remain time.Duration
Start time.Time
Updated time.Time
}
type ProgressInfo struct {
ID uint32 `json:"id"`
Type int32 `json:"type"`
Total string `json:"total"`
Current string `json:"current"`
Source string `json:"source"`
Dest string `json:"dest"`
Percent int64 `json:"percent"`
Remain string `json:"remain"`
}
func ProgressHandle(prog *gentee.Progress) bool {
if scriptTask.Header.Console {
return true
}
switch prog.Status {
case 0:
now := time.Now()
prog.Custom = ProgressData{
Start: now,
Updated: now,
}
case 1:
custom := prog.Custom.(ProgressData)
percent := int64(100.0 * prog.Ratio)
if /*percent != progress.Percent &&*/ time.Since(custom.Updated) > 500*time.Millisecond {
/* dif := progress.Current - progress.prevTotal
speed := float64(dif) / float64(since)
progress.Remain = time.Duration(float64(progress.Total-progress.Current) / speed).Round(time.Second)*/
remain := time.Duration(float64(time.Since(custom.Start)) * (1 - prog.Ratio) /
prog.Ratio).Round(time.Second)
if percent != custom.Percent || remain != custom.Remain {
custom.Percent = percent
custom.Remain = remain
custom.Updated = time.Now()
prog.Custom = custom
chProgress <- prog
}
}
case 2:
custom := prog.Custom.(ProgressData)
if custom.Start != custom.Updated {
custom.Percent = 100
prog.Custom = custom
chProgress <- prog
}
}
return true
}
func ProgressToString(progress *gentee.Progress) (string, error) {
var remain, total, current string
custom := progress.Custom.(ProgressData)
if custom.Remain <= 24*time.Hour {
remain = custom.Remain.String()
}
if progress.Type >= ProgCounter {
total = strconv.FormatInt(progress.Total, 10)
current = strconv.FormatInt(progress.Current, 10)
} else {
total = vm.SizeToStr(progress.Total, ``)
current = vm.SizeToStr(progress.Current, ``)
}
data, err := json.Marshal(ProgressInfo{
ID: progress.ID,
Type: progress.Type,
Total: total,
Current: current,
Percent: custom.Percent,
Remain: remain,
Source: progress.Source,
Dest: progress.Dest,
})
return string(data), err
}