-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathllanalyzer.go
192 lines (155 loc) · 4.12 KB
/
llanalyzer.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
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"sync"
"time"
"github.com/venturasr/ll-analyzer/analyzer"
"github.com/venturasr/ll-analyzer/config"
"github.com/venturasr/ll-analyzer/files"
"github.com/venturasr/ll-analyzer/reports"
"github.com/pkg/errors"
"github.com/venturasr/ll-analyzer/tools"
)
const workers = 20
var engineer *analyzer.Engineer
func main() {
start := time.Now()
log.Println("| [main.go][main] start=", start)
defer func() {
//report published before method returns. Everything ends here.
engineer.Publish()
fmt.Println("")
log.Println("| [main.go][main] elapsed=", time.Since(start))
}()
err := setup()
tools.ExitIfError(err)
err = starts(getCurrentPath())
if err != nil {
fmt.Printf("%+v\n", err)
}
}
func setup() (error) {
//Configuration
pathToConfig := getPathToConfig()
path, err := filepath.Abs(pathToConfig)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("can't find path %s", path))
}
c, err := config.NewConfig(path)
if err != nil {
return errors.Wrap(err, "failed to create configuration")
}
//Reports registry
r, err := reports.NewRegistry(c)
if err != nil {
return errors.Wrap(err, "failed to create registry")
}
//Engineer (engineer)
eng, err := analyzer.NewEngineer(c, r)
if err != nil {
return errors.Wrap(err, "failed to create engineer")
}
engineer = eng
fmt.Println("")
return nil
}
func starts(path string) error {
done := make(chan struct{})
numberFilesChan := make(chan int)
var totalFiles int
defer func() {
close(done)
log.Printf("| [llanalyzer.go][starts] number of files loaded: %v", totalFiles)
}()
filesChan, errChan := collectFiles(done, path)
var wg sync.WaitGroup
wg.Add(workers)
for i := 0; i < workers; i++ {
go func() {
iterateFiles(done, numberFilesChan, filesChan)
wg.Done()
}()
}
go func() {
wg.Wait()
close(numberFilesChan)
//log.Printf("| [llanalyzer.go][starts] clousing number file channel -> 4 \n") //TODO DELETE IT
}()
for nf := range numberFilesChan {
//log.Printf("| [llanalyzer.go][starts] counting files -> 2 \n") //TODO DELETE IT
totalFiles += nf
}
if err := <-errChan; err != nil {
return err
}
return nil
}
// collectFiles starts goroutines to walk the given root directory to collect log files.
func collectFiles(done <-chan struct{}, directory string) (<-chan *files.LogFile, <-chan error) {
filesChan := make(chan *files.LogFile) //It sends log files paths
errChan := make(chan error, 1) //buffered channel
go func() {
var wg sync.WaitGroup
err := filepath.Walk(directory, func(pathToFile string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.Mode().IsRegular() {
return nil
}
wg.Add(1)
go func() {
lf := files.NewLogFile(info, pathToFile)
select {
case filesChan <- lf:
case <-done:
default:
}
wg.Done()
}()
// Abort the walk if analysisDoneChan is closed.
select {
case <-done:
return nil
default:
}
return nil
})
// here the Walk function has returned, so all calls to wg.Add are done.
// it starts a goroutine to close filesChan once all the sends are done.
go func() {
wg.Wait()
close(filesChan)
}()
// No select needed here, since errChan is buffered.
errChan <- err
}()
return filesChan, errChan
}
func iterateFiles(done <-chan struct{}, numberFilesChan chan<- int, filesChan <-chan *files.LogFile) {
for lf := range filesChan {
engineer.Analyze(lf)
select {
case numberFilesChan <- 1:
//log.Printf("| [llanalyzer.go][iterateFiles] returning from engineer.Analyze() -> 1 \n") //TODO DELETE IT
case <-done:
return
}
}
}
func getCurrentPath() (currentpath string) {
currentpath, err := tools.GetCurrentDir()
tools.ExitIfError(err)
log.Printf("| [main.go][getCurrentPath] execution directory - %s", currentpath)
return currentpath
}
func getPathToConfig() (pathToConfig string) {
homeDir, err := tools.GetHomeDir()
tools.ExitIfError(err)
pathToConfig = fmt.Sprintf("%s%sconfig.toml", homeDir, tools.PathSeparator)
log.Printf("| [main.go][GetPathToConfig] config file location - %s", pathToConfig)
return pathToConfig
}