Skip to content

Commit

Permalink
Add reporter logic (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
unanoc authored Nov 23, 2021
1 parent 3ab63a5 commit 407ad8d
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 47 deletions.
10 changes: 5 additions & 5 deletions pkg/file/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,33 @@ import (
"sync"
)

type FileProvider struct {
type FileService struct {
mu *sync.RWMutex
cache map[string]*AssetFile
}

func NewFileProvider(filePaths ...string) *FileProvider {
func NewFileService(filePaths ...string) *FileService {
var filesMap = make(map[string]*AssetFile)

for _, path := range filePaths {
assetF := newAssetFile(path)
filesMap[path] = assetF
}

return &FileProvider{
return &FileService{
mu: &sync.RWMutex{},
cache: filesMap,
}
}

func (f *FileProvider) GetAssetFile(path string) (*AssetFile, error) {
func (f *FileService) GetAssetFile(path string) (*AssetFile, error) {
f.mu.RLock()
defer f.mu.RUnlock()

return f.getFile(path)
}

func (f *FileProvider) getFile(path string) (*AssetFile, error) {
func (f *FileService) getFile(path string) (*AssetFile, error) {
if file, exists := f.cache[path]; exists {
err := file.open()
if err != nil {
Expand Down
49 changes: 34 additions & 15 deletions src/processor/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,62 +5,81 @@ import (

"github.com/trustwallet/assets-go-libs/pkg/file"
"github.com/trustwallet/assets-go-libs/pkg/validation"
"github.com/trustwallet/assets-go-libs/src/reporter"
"github.com/trustwallet/assets-go-libs/src/validator"
)

const (
reportSanityCheckKey = "sanity-check"
)

type Service struct {
fileStorage *file.FileProvider
fileService *file.FileService
validatorsService *validator.Service
reporterService *reporter.Service
}

func NewService(storage *file.FileProvider, service *validator.Service) *Service {
func NewService(fs *file.FileService, vs *validator.Service, rs *reporter.Service) *Service {
return &Service{
fileStorage: storage,
validatorsService: service,
fileService: fs,
validatorsService: vs,
reporterService: rs,
}
}

func (s *Service) RunSanityCheck(paths []string) error {
report := s.reporterService.GetOrNew(reportSanityCheckKey)

for _, path := range paths {
f, err := s.fileStorage.GetAssetFile(path)
f, err := s.fileService.GetAssetFile(path)
if err != nil {
log.WithError(err).Error()
return err
}

validator := s.validatorsService.GetValidatorForFilesAndFolders(f)
report.TotalFiles += 1

validator := s.validatorsService.GetFoldersFilesValidator(f)
if validator != nil {
err = validator.Run(f)
if err != nil {
HandleError(err, f.Info, validator.ValidationName)
HandleError(err, f.Info, validator.ValidationName, report)
}
}

err = f.Close()
if err != nil {
log.WithError(err).Error()

return err
}
}

err := s.reporterService.Update(reportSanityCheckKey, report)
if err != nil {
log.WithError(err).Error()
return err
}

return nil
}

func HandleError(err error, info *file.AssetInfo, valName string) {
func HandleError(err error, info *file.AssetInfo, valName string, report *reporter.Report) {
errors := UnwrapComposite(err)

for _, err := range errors {
if warn, ok := err.(*validation.Warning); ok {
report.Warnings += 1
HandleWarning(warn, info)
continue
} else {
log.WithField("file_type", info.Type()).
WithField("file_chain", info.Chain().Handle).
WithField("file_asset", info.Asset()).
WithField("file_path", info.Path()).
WithField("validation_name", valName).
Errorf("%+v", err)
report.Errors += 1

log.WithField("type", info.Type()).
WithField("chain", info.Chain().Handle).
WithField("asset", info.Asset()).
WithField("path", info.Path()).
WithField("validation", valName).
Error(err)
}

switch err {
Expand Down
27 changes: 6 additions & 21 deletions src/reporter/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ func (s *Service) Update(key string, report *Report) error {

if r, ok := s.reports[key]; ok {
r.Fixed += report.Fixed
r.Fixed += report.Errors
r.Fixed += report.Warnings
r.Fixed += report.TotalFiles
r.Errors += report.Errors
r.Warnings += report.Warnings
r.TotalFiles += report.TotalFiles
}

s.reports["key"] = report
s.reports[key] = report

return nil
}
Expand All @@ -57,26 +57,11 @@ func (s *Service) GetReports() ReportList {
return s.reports
}

type ReportList map[string]*Report

type Report struct {
TotalFiles uint
Errors uint
Warnings uint
Fixed uint
}

type ReportList map[string]*Report

func (rl ReportList) WithTotal() map[string]*Report {
var totalReport = Report{}

for _, v := range rl {
totalReport.TotalFiles += v.TotalFiles
totalReport.Errors += v.Errors
totalReport.Warnings += v.Warnings
totalReport.Fixed += v.Fixed
}

rl["Total"] = &totalReport

return rl
}
9 changes: 5 additions & 4 deletions src/validator/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@ import (
)

type Service struct {
fileProvider *file.FileProvider
fileService *file.FileService
}

func NewService(fileProvider *file.FileProvider) (*Service, error) {
func NewService(fileProvider *file.FileService) (*Service, error) {
return &Service{
fileProvider: fileProvider,
fileService: fileProvider,
}, nil
}

// nolint:funlen
func (s *Service) GetValidatorForFilesAndFolders(f *file.AssetFile) *Validator {
func (s *Service) GetFoldersFilesValidator(f *file.AssetFile) *Validator {
fileType := f.Info.Type()

switch fileType {
case file.TypeRootFolder:
return &Validator{
Expand Down
4 changes: 2 additions & 2 deletions src/validator/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (s *Service) ValidateAssetFolder(file *file.AssetFile) error {
errLogo := validation.ValidateHasFiles(dirFiles, []string{"logo.png"})

if errLogo != nil || errInfo != nil {
infoFile, err := s.fileProvider.GetAssetFile(fmt.Sprintf("%s/info.json", file.Info.Path()))
infoFile, err := s.fileService.GetAssetFile(fmt.Sprintf("%s/info.json", file.Info.Path()))
if err != nil {
return err
}
Expand Down Expand Up @@ -264,7 +264,7 @@ func (s *Service) ValidateValidatorsListFile(file *file.AssetFile) error {
}

assetsPath := fmt.Sprintf("blockchains/%s/validators/assets", file.Info.Chain().Handle)
assetFolder, err := s.fileProvider.GetAssetFile(assetsPath)
assetFolder, err := s.fileService.GetAssetFile(assetsPath)
if err != nil {
return err
}
Expand Down

0 comments on commit 407ad8d

Please sign in to comment.