summaryrefslogtreecommitdiff
path: root/internal/model/scan.go
blob: 675b5dd9546f212b0dbe68d043c3eac7c6b193fc (plain)
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
package model

import "sync"

// ScanProgress tracks the state of an in-progress or recently completed scan.
type ScanProgress struct {
	mu           sync.RWMutex
	Running      bool   `json:"running"`
	CurrentSet   string `json:"current_set,omitempty"`
	SetsTotal    int    `json:"sets_total"`
	SetsDone     int    `json:"sets_done"`
	FilesTotal   int    `json:"files_total"`
	FilesDone    int    `json:"files_done"`
	LastError    string `json:"last_error,omitempty"`
}

func (p *ScanProgress) Start(setsTotal int) {
	p.mu.Lock()
	defer p.mu.Unlock()
	p.Running = true
	p.SetsTotal = setsTotal
	p.SetsDone = 0
	p.FilesTotal = 0
	p.FilesDone = 0
	p.LastError = ""
}

func (p *ScanProgress) SetCurrentSet(name string) {
	p.mu.Lock()
	defer p.mu.Unlock()
	p.CurrentSet = name
}

func (p *ScanProgress) IncrementFile() {
	p.mu.Lock()
	defer p.mu.Unlock()
	p.FilesDone++
}

func (p *ScanProgress) SetFilesTotal(total int) {
	p.mu.Lock()
	defer p.mu.Unlock()
	p.FilesTotal = total
}

func (p *ScanProgress) IncrementSet() {
	p.mu.Lock()
	defer p.mu.Unlock()
	p.SetsDone++
}

func (p *ScanProgress) Done(err error) {
	p.mu.Lock()
	defer p.mu.Unlock()
	p.Running = false
	p.CurrentSet = ""
	if err != nil {
		p.LastError = err.Error()
	}
}

func (p *ScanProgress) Copy() ScanProgress {
	p.mu.RLock()
	defer p.mu.RUnlock()
	return ScanProgress{
		Running:    p.Running,
		CurrentSet: p.CurrentSet,
		SetsTotal:  p.SetsTotal,
		SetsDone:   p.SetsDone,
		FilesTotal: p.FilesTotal,
		FilesDone:  p.FilesDone,
		LastError:  p.LastError,
	}
}