-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
168 lines (143 loc) · 4.22 KB
/
config.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package config
import (
"context"
"crypto/md5"
"encoding/hex"
"log"
"time"
pbd "github.com/brotherlogic/discogs/proto"
pb "github.com/brotherlogic/gramophile/proto"
"google.golang.org/protobuf/proto"
)
type Validator interface {
Validate(ctx context.Context, fields []*pbd.Field, c *pb.GramophileConfig) error
GetMoves(c *pb.GramophileConfig) []*pb.FolderMove
}
func ValidateConfig(ctx context.Context, user *pb.StoredUser, fields []*pbd.Field, c *pb.GramophileConfig) ([]*pbd.Folder, []*pb.FolderMove, error) {
var moves []*pb.FolderMove
moves = append(moves, c.GetMoves()...)
for _, validator := range []Validator{
&cleaning{},
&listen{},
&width{},
&arrived{},
&weight{},
&goalFolder{},
&sales{},
&keep{},
&org{},
&wants{},
&sleeve{}} {
err := validator.Validate(ctx, fields, c)
if err != nil {
return nil, nil, err
}
moves = append(moves, validator.GetMoves(c)...)
}
var folders []*pbd.Folder
if c.GetCreateFolders() == pb.Create_AUTOMATIC {
for _, move := range moves {
if !move.GetMoveToGoalFolder() {
folderFound := false
for _, folder := range user.GetFolders() {
if folder.GetName() == move.GetMoveFolder() {
folderFound = true
}
if !folderFound {
folders = append(folders, &pbd.Folder{Name: move.GetMoveFolder()})
}
}
}
}
for _, validation := range c.GetValidations() {
switch validation.GetValidationStrategy() {
case pb.ValidationStrategy_LISTEN_TO_VALIDATE:
folders = append(folders, &pbd.Folder{Name: "Listening Pile"})
case pb.ValidationStrategy_MOVE_TO_VALIDATE:
folders = append(folders, &pbd.Folder{Name: "Validation Pile"})
}
}
}
var rmoves []*pb.FolderMove
if c.GetCreateMoves() == pb.Create_AUTOMATIC {
for _, move := range moves {
moveFound := false
for _, umove := range user.GetMoves() {
if move.GetName() == umove.GetName() {
moveFound = true
}
}
if !moveFound {
move.Origin = pb.Create_AUTOMATIC
rmoves = append(rmoves, move)
}
}
}
log.Printf("Returning folders: %v", folders)
log.Printf("Returning moves: %v from %v", rmoves, len(moves))
return folders, rmoves, nil
}
func Hash(c *pb.GramophileConfig) string {
bytes, _ := proto.Marshal(c)
hash := md5.Sum(bytes)
return hex.EncodeToString(hash[:])
}
func setIssue(r *pb.Record, issue pb.NoncomplianceIssue, set bool) {
found := false
var newIssues []pb.NoncomplianceIssue
for _, existing := range r.GetIssues() {
if existing != issue {
newIssues = append(newIssues, existing)
found = true
}
}
if set && !found {
r.Issues = append(r.Issues, issue)
}
if !set {
r.Issues = newIssues
}
}
func Filter(filter *pb.Filter, r *pb.Record) bool {
log.Printf("Folders for exclusion: %v", filter.GetExcludeFolder())
for _, folderid := range filter.GetExcludeFolder() {
log.Printf("Exclude %v -> %v", r.GetRelease().GetFolderId(), folderid)
if r.GetRelease().GetFolderId() == folderid {
return false
}
}
for _, folderid := range filter.GetIncludeFolder() {
log.Printf("Exclude %v -> %v", r, folderid)
if r.GetRelease().GetFolderId() != folderid {
return false
}
}
for _, format := range r.GetRelease().GetFormats() {
for _, matcher := range filter.GetFormats() {
if matcher == format.GetName() {
return true
}
}
}
log.Printf("HERE: %v -> %v", filter.GetFormats(), len(filter.GetFormats()))
return len(filter.GetFormats()) == 0
}
func Apply(c *pb.GramophileConfig, r *pb.Record) error {
if c.GetCleaningConfig().GetCleaning() != pb.Mandate_NONE {
if Filter(c.GetCleaningConfig().GetAppliesTo(), r) {
needsClean := false
if c.GetCleaningConfig().GetCleaningGapInSeconds() > 0 && time.Since(time.Unix(0, r.GetLastCleanTime())) > time.Second*time.Duration(c.CleaningConfig.GetCleaningGapInSeconds()) {
needsClean = true
}
if c.GetCleaningConfig().GetCleaningGapInPlays() > 0 && r.GetNumPlays() > c.GetCleaningConfig().GetCleaningGapInPlays() {
needsClean = true
}
log.Printf("Setting for %v -> %v", r.GetRelease().GetInstanceId(), needsClean)
setIssue(r, pb.NoncomplianceIssue_NEEDS_CLEAN, needsClean)
} else {
log.Printf("Filter %v skips %v", c.GetCleaningConfig().GetAppliesTo(), r)
setIssue(r, pb.NoncomplianceIssue_NEEDS_CLEAN, false)
}
}
return nil
}