This repository has been archived by the owner on Apr 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
progress.go
138 lines (122 loc) · 3.78 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package ortfomk
import (
"encoding/json"
"io/ioutil"
"math"
)
var spinner Spinner = DummySpinner{}
var currentWorkID string = ""
type BuildStep string
const (
StepDeadLinks BuildStep = "dead links"
StepBuildPage BuildStep = "build page"
StepGeneratePDF BuildStep = "pdf generation"
StepLoadWorks BuildStep = "load works"
StepLoadTechnologies BuildStep = "load technologies"
StepLoadExternalSites BuildStep = "load external sites"
StepLoadCollections BuildStep = "load collections"
StepLoadTags BuildStep = "load tags"
StepLoadTranslations BuildStep = "load translations"
)
// ProgressFile holds the data that gets written to the progress file as JSON.
type ProgressFile struct {
Total int `json:"total"`
Processed int `json:"processed"`
Percent int `json:"percent"`
Current struct {
ID string `json:"id"`
Step BuildStep `json:"step"`
// The resolution of the thumbnail being generated. 0 when step is not "thumbnails"
Resolution int `json:"resolution"`
// The file being processed:
//
// - original media when making thumbnails or during media analysis,
//
// - media the colors are being extracted from, or
//
// - the description.md file when parsing description
File string `json:"file"`
Language string `json:"language"`
Output string `json:"output"`
} `json:"current"`
}
type ProgressDetails struct {
Resolution int
File string
Language string
OutFile string
}
// Status updates the current progress and writes the progress to a file if --write-progress is set.
func Status(step BuildStep, details ProgressDetails) {
g.mu.Lock()
defer g.mu.Unlock()
g.Progress.Step = step
g.Progress.Resolution = details.Resolution
g.Progress.File = details.File
if details.Language != "" {
g.CurrentLanguage = details.Language
}
g.CurrentOutputFile = details.OutFile
UpdateSpinner()
err := WriteProgressFile()
if err != nil {
LogError("Couldn't write to progress file: %s", err)
}
}
// IncrementProgress increments the number of processed works and writes the progress to a file if --write-progress is set.
func IncrementProgress() error {
g.mu.Lock()
defer g.mu.Unlock()
g.Progress.Current++
UpdateSpinner()
return WriteProgressFile()
}
// WriteProgressFile writes the progress to a file if --write-progress is set.
func WriteProgressFile() error {
if g.Flags.ProgressFile == "" {
return nil
}
progressDataJSON, err := json.Marshal(g.ProgressFileData())
if err != nil {
return err
}
return ioutil.WriteFile(g.Flags.ProgressFile, progressDataJSON, 0644)
}
// ProgressPercent returns the current progress as a percentage.
func (g *GlobalData) ProgressPercent() int {
if g.Progress.Total == 0 {
return 0
}
return int(math.Floor(float64(g.Progress.Current) / float64(g.Progress.Total) * 100))
}
// ProgressFileData returns a ProgressData struct ready to be marshalled to JSON for --write-progress.
func (g *GlobalData) ProgressFileData() ProgressFile {
return ProgressFile{
Total: g.Progress.Total,
Processed: g.Progress.Current,
Percent: g.ProgressPercent(),
Current: struct {
ID string `json:"id"`
Step BuildStep `json:"step"`
Resolution int `json:"resolution"`
File string `json:"file"`
Language string `json:"language"`
Output string `json:"output"`
}{
ID: g.CurrentObjectID,
Step: g.Progress.Step,
Resolution: g.Progress.Resolution,
File: g.Progress.File,
Language: g.CurrentLanguage,
Output: g.CurrentOutputFile,
},
}
}
// SetCurrentObjectID sets the current object ID and updates the spinner.
func SetCurrentObjectID(objectID string) {
g.mu.Lock()
g.CurrentObjectID = objectID
g.mu.Unlock()
// UpdateSpinner is thread-safe, as yacspin has its own mutex.
UpdateSpinner()
}