forked from openshift/ci-search
/
pathindex.go
257 lines (218 loc) · 5.62 KB
/
pathindex.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package main
import (
"fmt"
"net/url"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
"sync"
"time"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/klog"
"github.com/weshayutin/ci-search/walk"
)
type PathAccessor interface {
// Stats returns aggregate statistics for the indexed paths.
Stats() PathIndexStats
// LastModified returns the time the requested path reported a failure at
// or the zero time.
LastModified(path string) time.Time
}
type PathIndexStats struct {
Entries int
Size int64
}
type PathResolver interface {
// MetadataFor returns metadata for the slash-separated path
// resolved relative to the index base.
MetadataFor(path string) (Result, error)
}
type pathIndex struct {
base string
baseURI *url.URL
maxAge time.Duration
lock sync.Mutex
ordered []pathAge
stats PathIndexStats
pathIndex map[string]int
}
type pathAge struct {
path string
index string
age time.Time
}
func (index *pathIndex) parseJobPath(path string) (*Result, error) {
var result Result
parts := strings.SplitN(path, "/", 8)
last := len(parts) - 1
result.URI = index.baseURI.ResolveReference(&url.URL{Path: strings.Join(parts[:last], "/")})
//Note: WES: I've removed these options from the UI, defaults to all in webui
// default file to search for is now *.log
switch parts[last] {
case "build-log.txt":
result.FileType = "build-log"
case "junit.failures":
result.FileType = "junit"
default:
result.FileType = parts[last]
}
var err error
result.Number, err = strconv.Atoi(parts[last-1])
if err != nil {
return nil, err
}
if last < 3 {
return nil, fmt.Errorf("not enough parts (%d < 3)", last)
}
result.Name = parts[last-2]
switch parts[1] {
case "logs":
result.Trigger = "build"
case "pr-logs":
result.Trigger = "pull"
default:
result.Trigger = parts[1]
}
return &result, nil
}
func (index *pathIndex) LastModified(path string) time.Time {
index.lock.Lock()
defer index.lock.Unlock()
if position, ok := index.pathIndex[path]; ok {
return index.ordered[position].age
}
return time.Time{}
}
func (index *pathIndex) Notify(paths []string) {
}
func (index *pathIndex) Load() error {
ordered := make([]pathAge, 0, 1024)
var err error
start := time.Now()
defer func() {
klog.Infof("Refreshed path index in %s, loaded %d: %v", time.Now().Sub(start).Truncate(time.Millisecond), len(ordered), err)
}()
mustExpire := index.maxAge != 0
expiredAt := start.Add(-index.maxAge)
stats := PathIndexStats{}
err = walk.Walk(index.base, func(path string, info os.FileInfo, err error) error {
if err != nil {
if os.IsNotExist(err) {
return nil
}
return err
}
if mustExpire && expiredAt.After(info.ModTime()) {
os.RemoveAll(path)
return nil
}
if info.IsDir() {
return nil
}
// WES: ovrridden in ui to all, file to search for is *.log
var indexName string
switch name := info.Name(); {
case strings.HasPrefix(name, "build-log.txt"):
indexName = "build-log.txt"
case strings.HasPrefix(name, "junit.failures"):
indexName = "junit.failures"
default:
return nil
}
stats.Entries++
stats.Size += info.Size()
relPath, err := filepath.Rel(index.base, path)
if err != nil {
return err
}
relPath = filepath.ToSlash(relPath)
ordered = append(ordered, pathAge{index: indexName, path: relPath, age: info.ModTime()})
return nil
})
if err != nil {
return err
}
sort.Slice(ordered, func(i, j int) bool {
if ordered[i].age.Before(ordered[j].age) {
return false
}
return true
})
pathIndex := make(map[string]int, len(ordered))
for i, item := range ordered {
path := strings.TrimPrefix(item.path, index.base)
pathIndex[path] = i
}
index.lock.Lock()
defer index.lock.Unlock()
index.ordered = ordered
index.pathIndex = pathIndex
index.stats = stats
return nil
}
func (i *pathIndex) FilenamesForSearchType(searchType string) []string {
switch searchType {
case "", "bug+junit", "junit":
return []string{"junit.failures"}
case "build-log":
return []string{"build-log.txt"}
case "all":
return []string{"junit.failures", "build-log.txt"}
default:
return nil
}
}
func (i *pathIndex) Stats() PathIndexStats {
i.lock.Lock()
defer i.lock.Unlock()
return i.stats
}
func (i *pathIndex) SearchPaths(index *Index, jobNames sets.String) ([]string, error) {
// if there are no search targets return nil
names := i.FilenamesForSearchType(index.SearchType)
if len(names) == 0 {
return nil, nil
}
var paths []pathAge
i.lock.Lock()
paths = i.ordered
i.lock.Unlock()
// search all if we haven't built an index yet, or if the number of paths is above the max
if l := len(paths); l == 0 {
return nil, nil
}
// grow the map to the desired size up front
copied := make([]string, 0, len(paths))
var oldest time.Time
if index.MaxAge > 0 {
oldest = time.Now().Add(-index.MaxAge)
}
for _, path := range paths {
if path.age.Before(oldest) {
klog.V(2).Infof("Stopped path index at %s because it is before %s", path.path, oldest)
break
}
if index.JobFilter != nil {
// Paths should be .../job/build/file - isolate the job and verify it matches the job regex
if i := strings.LastIndex(path.path, string(filepath.Separator)); i != -1 {
if j := strings.LastIndex(path.path[:i], string(filepath.Separator)); j != -1 {
if k := strings.LastIndex(path.path[:j], string(filepath.Separator)); k != -1 {
jobName := path.path[k+1 : j]
if !index.JobFilter(jobName) {
continue
}
if jobNames != nil {
jobNames.Insert(jobName)
}
}
}
}
}
if contains(names, path.index) {
copied = append(copied, filepath.Join(i.base, filepath.FromSlash(path.path)))
}
}
return copied, nil
}