-
Notifications
You must be signed in to change notification settings - Fork 27
/
statistics.go
64 lines (56 loc) · 1.54 KB
/
statistics.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
/*
* knoxite
* Copyright (c) 2016-2017, Christian Muehlhaeuser <muesli@gmail.com>
*
* For license see LICENSE
*/
package knoxite
import (
"fmt"
)
// Stats contains a bunch of Stats counters.
type Stats struct {
Files uint64 `json:"files"`
Dirs uint64 `json:"dirs"`
SymLinks uint64 `json:"symlinks"`
Size uint64 `json:"size"`
StorageSize uint64 `json:"stored_size"`
Transferred uint64 `json:"transferred"`
Errors uint64 `json:"errors"`
}
// Add accumulates other into s.
func (s *Stats) Add(other Stats) {
s.Files += other.Files
s.Dirs += other.Dirs
s.SymLinks += other.SymLinks
s.Size += other.Size
s.StorageSize += other.StorageSize
s.Transferred += other.Transferred
s.Errors += other.Errors
}
// SizeToString prettifies sizes.
func SizeToString(size uint64) (str string) {
b := float64(size)
switch {
case size >= 1<<60:
str = fmt.Sprintf("%.2f EiB", b/(1<<60))
case size >= 1<<50:
str = fmt.Sprintf("%.2f PiB", b/(1<<50))
case size >= 1<<40:
str = fmt.Sprintf("%.2f TiB", b/(1<<40))
case size >= 1<<30:
str = fmt.Sprintf("%.2f GiB", b/(1<<30))
case size >= 1<<20:
str = fmt.Sprintf("%.2f MiB", b/(1<<20))
case size >= 1<<10:
str = fmt.Sprintf("%.2f KiB", b/(1<<10))
default:
str = fmt.Sprintf("%dB", size)
}
return
}
// String returns human-readable Stats.
func (s Stats) String() string {
return fmt.Sprintf("%d files, %d dirs, %d symlinks, %d errors, %v Original Size, %v Storage Size",
s.Files, s.Dirs, s.SymLinks, s.Errors, SizeToString(s.Size), SizeToString(s.StorageSize))
}