-
Notifications
You must be signed in to change notification settings - Fork 0
/
analyze_project.go
264 lines (230 loc) · 10 KB
/
analyze_project.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
258
259
260
261
262
263
264
package usecase
import (
"context"
"errors"
"fmt"
"time"
"github.com/eroatta/src-reader/entity"
"github.com/eroatta/src-reader/repository"
"github.com/eroatta/src-reader/usecase/step"
"github.com/google/uuid"
log "github.com/sirupsen/logrus"
)
var (
// ErrProjectNotFound indicates that the requested project is not accessible.
ErrProjectNotFound = errors.New("unable to retrieve requested Project")
// ErrPreviousAnalysisFound indicates there is an existing previous analysis.
ErrPreviousAnalysisFound = errors.New("existing previous analysis for project")
// ErrUnableToBuildASTs indicates that an error occurred while trying to read or parse the source code files to
// build the required Abstract Syntax Trees.
ErrUnableToBuildASTs = errors.New("unable to create ASTs from input")
// ErrUnableToMineASTs indicates that an error occurred while trying to create or apply the miners specified
// during the import process.
ErrUnableToMineASTs = errors.New("unable to apply one or more miners to the ASTs")
// ErrUnableToCreateProcessors indicates that an error occurred while trying to create or apply the splitters or
// expanders during the import process.
ErrUnableToCreateProcessors = errors.New("unable to create splitting or expansion algorithms")
// ErrUnableToSaveIdentifiers indicates that an error occurred while trying to save an already processed identifier.
ErrUnableToSaveIdentifiers = errors.New("unable to save extracted and processed indentifiers")
// ErrUnableToSaveAnalysis indicates that an error occurred while trying to store the results for an import process.
ErrUnableToSaveAnalysis = errors.New("unable to save analysis results after completed processing")
// ErrUnexpected indicates that an unexpected error ocurring while analyzing the project.
ErrUnexpected = errors.New("unexpected error")
)
// AnalyzeProjectUsecase defines the contract for the use case related to the analysis process of a project.
type AnalyzeProjectUsecase interface {
// Process performs the splitting and expansion process on the source code belonging to the Project.
Process(ctx context.Context, projecID uuid.UUID) (entity.AnalysisResults, error)
}
// NewAnalyzeProjectUsecase initializes a new AnalyzeProjectUsecase handler.
func NewAnalyzeProjectUsecase(pr repository.ProjectRepository, scr repository.SourceCodeRepository,
ir repository.IdentifierRepository, ar repository.AnalysisRepository, config *entity.AnalysisConfig) AnalyzeProjectUsecase {
return &analyzeProjectUsecase{
projectRepository: pr,
sourceCodeRepository: scr,
identifierRepository: ir,
analysisRepository: ar,
defaultConfig: config,
}
}
type analyzeProjectUsecase struct {
projectRepository repository.ProjectRepository
sourceCodeRepository repository.SourceCodeRepository
identifierRepository repository.IdentifierRepository
analysisRepository repository.AnalysisRepository
defaultConfig *entity.AnalysisConfig
}
// Process processes the given Project, based on the configuration provided by the AnalysisConfig.
// The process reads the source code, applies the given miners, splitters and expanders and then stores the results.
// It's the default implementation for the use case.
func (uc analyzeProjectUsecase) Process(ctx context.Context, projectID uuid.UUID) (entity.AnalysisResults, error) {
project, err := uc.projectRepository.Get(ctx, projectID)
switch err {
case nil:
// do nothing
case repository.ErrProjectNoResults:
return entity.AnalysisResults{}, ErrProjectNotFound
default:
log.WithError(err).Errorf("unable to retrieve project %s", projectID.String())
return entity.AnalysisResults{}, ErrUnexpected
}
previousAnalysis, err := uc.analysisRepository.GetByProjectID(ctx, projectID)
switch err {
case repository.ErrAnalysisNoResults:
// do nothing
case nil:
return previousAnalysis, ErrPreviousAnalysisFound
default:
log.WithError(err).Errorf("unable to check for previous analysis on project %s", projectID.String())
return entity.AnalysisResults{}, ErrUnexpected
}
analysisID, _ := uuid.NewUUID()
analysisResults := entity.AnalysisResults{
ID: analysisID,
DateCreated: time.Now(),
ProjectID: project.ID,
ProjectName: project.Metadata.Fullname,
PipelineMiners: make([]string, 0),
PipelineSplitters: make([]string, 0),
PipelineExpanders: make([]string, 0),
}
// read and parse files
filesc := step.Read(ctx, uc.sourceCodeRepository, project.SourceCode.Location, project.SourceCode.Files)
parsed := step.Parse(filesc)
files := step.Merge(parsed)
valid := make([]entity.File, 0)
fileErrorSamples := make([]string, 0)
for _, file := range files {
if file.Error != nil {
if len(fileErrorSamples) < 10 {
fileErrorSamples = append(fileErrorSamples, file.Error.Error())
}
log.WithError(file.Error).Warnf("unable to read or parse file %s at %s", file.Name, project.SourceCode.Location)
continue
}
valid = append(valid, file)
}
analysisResults.FilesTotal = len(files)
analysisResults.FilesValid = len(valid)
analysisResults.FilesError = len(files) - len(valid)
analysisResults.FilesErrorSamples = fileErrorSamples
// if every file can't be parsed, then fail
if len(valid) == 0 {
log.Errorf("unable to read or parse any file on %s for project %s",
project.SourceCode.Location, project.Reference)
return entity.AnalysisResults{}, ErrUnableToBuildASTs
}
// apply the pre-process step (mine them)
miners := buildMiners(uc.defaultConfig)
for _, miner := range miners {
analysisResults.PipelineMiners = append(analysisResults.PipelineMiners, miner.Name())
}
miningResults := step.Mine(valid, miners...)
// make the splitters from input and mining results
splitters := buildSplittersFromMiningResults(uc.defaultConfig, miningResults)
if len(splitters) == 0 {
log.WithField("desired", uc.defaultConfig.Splitters).Error("unable to create any splitter")
return entity.AnalysisResults{}, ErrUnableToCreateProcessors
}
for _, splitter := range splitters {
analysisResults.PipelineSplitters = append(analysisResults.PipelineSplitters, splitter.Name())
}
// make the expanders from input and mining results
expanders := buildExpandersFromMiningResults(uc.defaultConfig, miningResults)
if len(expanders) == 0 {
log.WithField("desired", uc.defaultConfig.Expanders).Error("unable to create any expander")
return entity.AnalysisResults{}, ErrUnableToCreateProcessors
}
for _, expander := range expanders {
analysisResults.PipelineExpanders = append(analysisResults.PipelineExpanders, expander.Name())
}
// analyze each identifier
identc := step.Extract(valid, uc.defaultConfig.ExtractorFactory)
splittedc := step.Split(identc, splitters...)
expandedc := step.Expand(splittedc, expanders...)
normalizedc := step.Normalize(expandedc)
identErrorSamples := make([]string, 0)
for ident := range normalizedc {
analysisResults.IdentifiersTotal++
if ident.Error != nil {
if len(identErrorSamples) < 10 {
identErrorSamples = append(identErrorSamples, ident.Error.Error())
}
log.WithFields(log.Fields{
"project": project.Metadata.Fullname,
"filename": ident.File,
"identifier": ident.Name,
"position": ident.Position,
}).Warn("an error occurred during the splitting the expansion for the identifier")
analysisResults.IdentifiersError++
}
err := uc.identifierRepository.Add(ctx, analysisResults, ident)
if err != nil {
log.WithError(err).Error(fmt.Sprintf("unable to save identifier %s, on file %s for project %s",
ident.Name, ident.File, analysisResults.ProjectName))
return entity.AnalysisResults{}, ErrUnableToSaveIdentifiers
}
}
analysisResults.IdentifiersValid = analysisResults.IdentifiersTotal - analysisResults.IdentifiersError
analysisResults.IdentifiersErrorSamples = identErrorSamples
err = uc.analysisRepository.Add(ctx, analysisResults)
if err != nil {
log.WithError(err).Errorf("unable to save analysis results for project %s", project.Reference)
return entity.AnalysisResults{}, ErrUnableToSaveAnalysis
}
return analysisResults, nil
}
// buildMiners initializes a set of miners, making them exclusives on current process.
func buildMiners(config *entity.AnalysisConfig) []entity.Miner {
miners := make([]entity.Miner, 0)
for _, name := range config.Miners {
factory, err := config.MinerAlgorithmFactory.Get(name)
if err != nil {
log.WithError(err).Error(fmt.Sprintf("unable to get mining factory for %s", name))
continue
}
miner, err := factory.Make()
if err != nil {
log.WithError(err).Error(fmt.Sprintf("unable to make mining algorithm for %s", name))
continue
}
miners = append(miners, miner)
}
return miners
}
// buildSplittersFromMiningResults initializes a set of splitters from the mining results, making them exclusives on current process.
func buildSplittersFromMiningResults(config *entity.AnalysisConfig, miningResults map[string]entity.Miner) []entity.Splitter {
splitters := make([]entity.Splitter, 0)
for _, name := range config.Splitters {
factory, err := config.SplittingAlgorithmFactory.Get(name)
if err != nil {
log.WithError(err).Error(fmt.Sprintf("unable to get splitting factory for %s", name))
continue
}
splitter, err := factory.Make(miningResults)
if err != nil {
log.WithError(err).Error(fmt.Sprintf("unable to make splitting algorithm for %s", name))
continue
}
splitters = append(splitters, splitter)
}
return splitters
}
// buildExpandersFromMiningResults initializes a set of expanders from the mining results, making them exclusives on current process.
func buildExpandersFromMiningResults(config *entity.AnalysisConfig, miningResults map[string]entity.Miner) []entity.Expander {
expanders := make([]entity.Expander, 0)
for _, name := range config.Expanders {
factory, err := config.ExpansionAlgorithmFactory.Get(name)
if err != nil {
log.WithError(err).Error(fmt.Sprintf("unable to get expasions factory for %s", name))
continue
}
expander, err := factory.Make(miningResults)
if err != nil {
log.WithError(err).Error(fmt.Sprintf("unable to make expansion algorithm for %s", name))
continue
}
expanders = append(expanders, expander)
}
return expanders
}