/
stats.go
71 lines (61 loc) · 1.87 KB
/
stats.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
package main
import (
"fmt"
"sync"
)
var gStats = NewGlobalStats()
// GlobalStats keeps track of various statistics during a download process.
type GlobalStats struct {
filesDownloaded uint64
filesFound uint64
alreadyExists uint64
hardlinked uint64
// bytesDownloaded only counts bytes from files.
bytesDownloaded uint64
// bytesOverhead counts bytes from the json scraping.
bytesOverhead uint64
// bytesSaved indicated space saved due to hardlinking instead of downloading.
bytesSaved uint64
// NowScraping is used to show which blogs are being scraped.
nowScraping Tracker
}
// Tracker is a record-keeping structure that tracks which users are still
// in the scraping/downloading phase.
type Tracker struct {
sync.RWMutex
Blog map[*User]bool
}
// NewGlobalStats does the initialization for a new set of global stats.
func NewGlobalStats() *GlobalStats {
return &GlobalStats{
nowScraping: Tracker{
Blog: make(map[*User]bool),
},
}
}
// PrintStatus prints the current status of each active user.
//
// It currently prints active (scraping and downloading) blogs.
// Not sure if it should be changed to also include finished blogs.
func (g *GlobalStats) PrintStatus() {
g.nowScraping.RLock()
defer g.nowScraping.RUnlock()
fmt.Println()
// XXX: Optimize this if necessary.
for k, v := range g.nowScraping.Blog {
if v {
fmt.Println(k.GetStatus())
}
}
fmt.Println()
fmt.Println(g.filesDownloaded, "/", g.filesFound-g.alreadyExists, "files downloaded.")
if g.alreadyExists != 0 {
fmt.Println(g.alreadyExists, "previously downloaded.")
}
if g.hardlinked != 0 {
fmt.Println(g.hardlinked, "new hardlinks.")
}
fmt.Println(byteSize(g.bytesDownloaded), "of files downloaded during this session.")
fmt.Println(byteSize(g.bytesOverhead), "of data downloaded as JSON overhead.")
fmt.Println(byteSize(g.bytesSaved), "of bandwidth saved due to hardlinking.")
}