-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprogress.go
77 lines (66 loc) · 1.89 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
package main
import (
"fmt"
"strings"
"time"
"github.com/fatih/color"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
func main() {
session, err := mgo.Dial("localhost")
if err != nil {
panic(err)
}
defer session.Close()
c := session.DB("local").C("$cmd.sys.inprog")
fmt.Println("Waiting for an index to be applied...\n ")
for true {
result := inProgress{}
c.Find(bson.M{}).One(&result)
if len(result.Inprog) > 0 {
printProgress(extractProgress(result.Inprog[0]))
} else {
fmt.Println("\033[1A\r\033[K")
}
time.Sleep(time.Duration(200) * time.Millisecond)
}
}
func extractProgress(operation operation) (collection string, name string, curr int64, total int64, duration string) {
duration = fmt.Sprint(time.Duration(operation.Duration*1000) * time.Nanosecond)
return operation.Insert.Collection, operation.Insert.Name, operation.Progress.Done, operation.Progress.Total, duration
}
func printProgress(collection string, name string, current int64, total int64, duration string) {
if total == 0 || current == 0 {
return
}
percent := int((float64(current) / float64(total)) * 100)
progress := fmt.Sprintf("%d/%d", current, total)
collectionName := fmt.Sprintf("%s/%s", collection, name)
doneParts := int((float64(current) / float64(total)) * 30)
fmt.Printf("\033[1A\r\033[K%s%s %s %s %d%% (time %s)\n",
drawBar(color.BgGreen, doneParts),
drawBar(color.BgRed, int(30-doneParts)),
collectionName, progress, percent, duration)
}
func drawBar(colour color.Attribute, width int) (result string) {
colourize := color.New(colour).SprintFunc()
return colourize(strings.Repeat(" ", width))
}
type operation struct {
Progress progress
Insert insert
Duration int64 `bson:"microsecs_running"`
}
type inProgress struct {
Inprog []operation
}
type progress struct {
Done int64
Total int64
}
type insert struct {
Name string
Background bool
Collection string `bson:"ns"`
}