Skip to content

Commit

Permalink
Fix stats for start, end and execution time
Browse files Browse the repository at this point in the history
  • Loading branch information
jwillp committed Aug 19, 2024
1 parent 2424316 commit 700e35d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 48 deletions.
36 changes: 36 additions & 0 deletions linting.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,42 @@ const (
WarningSeverity LinterResultSeverity = "warning"
)

type LintingProcessor struct {
linters []SpecificationLinter
}

func NewLintingProcessor(linters ...SpecificationLinter) *LintingProcessor {
return &LintingProcessor{linters: linters}
}

func (l LintingProcessor) Name() string {
return "linting_processor"
}

func (l LintingProcessor) Process(ctx ProcessingContext) ([]ProcessingOutput, error) {
linter := CompositeSpecificationLinter(l.linters...)
ctx.Logger.Info("\nLinting specifications ...")
lr := linter.Lint(SpecificationGroup(ctx.DependencyGraph))
if lr.HasWarnings() {
for _, w := range lr.Warnings() {
ctx.Logger.Warning(fmt.Sprintf("Warning: %s\n", w.Message))
}
}

if lr.HasErrors() {
for _, e := range lr.Errors().Errors {
ctx.Logger.Error(fmt.Sprintf("Error: %s\n", e.Error()))
}
}

if !lr.HasWarnings() && !lr.HasErrors() {
ctx.Logger.Success("Specifications linted successfully.")
}

return nil, nil

}

type LinterResult struct {
Severity LinterResultSeverity
Message string
Expand Down
56 changes: 8 additions & 48 deletions specter.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ type Specter struct {
SourceLoaders []SourceLoader
Loaders []SpecificationLoader
Processors []SpecificationProcessor
Linters []SpecificationLinter
OutputProcessors []OutputProcessor
Logger Logger
ExecutionMode ExecutionMode
Expand All @@ -39,21 +38,27 @@ type Stats struct {
}

func (s Stats) ExecutionTime() time.Duration {
return s.EndedAt.Sub(s.EndedAt)
return s.EndedAt.Sub(s.StartedAt)
}

// Run the pipeline from start to finish.
func (s Specter) Run(sourceLocations []string) error {
stats := Stats{}

defer func() {
s.Logger.Info(fmt.Sprintf("\nStarted At: %s, ended at: %s, execution time: %s", stats.StartedAt, stats.EndedAt, stats.ExecutionTime()))
stats.EndedAt = time.Now()

s.Logger.Info(fmt.Sprintf("\nStarted At: %s", stats.StartedAt))
s.Logger.Info(fmt.Sprintf("Ended at: %s", stats.EndedAt))
s.Logger.Info(fmt.Sprintf("Execution time: %s", stats.ExecutionTime()))
s.Logger.Info(fmt.Sprintf("Number of source locations: %d", stats.NbSourceLocations))
s.Logger.Info(fmt.Sprintf("Number of sources: %d", stats.NbSources))
s.Logger.Info(fmt.Sprintf("Number of specifications: %d", stats.NbSpecifications))
s.Logger.Info(fmt.Sprintf("Number of outputs: %d", stats.NbOutputs))
}()

stats.StartedAt = time.Now()

// Load sources
stats.NbSourceLocations = len(sourceLocations)
sources, err := s.LoadSources(sourceLocations)
Expand Down Expand Up @@ -83,20 +88,6 @@ func (s Specter) Run(sourceLocations []string) error {
return e
}

// Lint Specifications
lr := s.LintSpecifications(deps)
if lr.HasErrors() {
errs := errors.NewGroup(errors.InternalErrorCode)
for _, e := range lr.Errors().Errors {
errs = errs.Append(e)
}
return errors.WrapWithMessage(errs, errors.InternalErrorCode, "linting errors encountered")
}
// stop here
if s.ExecutionMode == LintMode {
return nil
}

// Process Specifications
var outputs []ProcessingOutput
outputs, err = s.ProcessSpecifications(deps)
Expand Down Expand Up @@ -209,30 +200,6 @@ func (s Specter) ResolveDependencies(specifications []Specification) (ResolvedDe
return deps, nil
}

// LintSpecifications runes all Linters against a list of Specifications.
func (s Specter) LintSpecifications(specifications []Specification) LinterResultSet {
linter := CompositeSpecificationLinter(s.Linters...)
s.Logger.Info("\nLinting specifications ...")
lr := linter.Lint(specifications)
if lr.HasWarnings() {
for _, w := range lr.Warnings() {
s.Logger.Warning(fmt.Sprintf("Warning: %s\n", w.Message))
}
}

if lr.HasErrors() {
for _, e := range lr.Errors().Errors {
s.Logger.Error(fmt.Sprintf("Error: %s\n", e.Error()))
}
}

if !lr.HasWarnings() && !lr.HasErrors() {
s.Logger.Success("Specifications linted successfully.")
}

return lr
}

// ProcessSpecifications sends the specifications to processors.
func (s Specter) ProcessSpecifications(specifications ResolvedDependencies) ([]ProcessingOutput, error) {
ctx := ProcessingContext{
Expand Down Expand Up @@ -315,13 +282,6 @@ func WithLoaders(loaders ...SpecificationLoader) Option {
}
}

// WithLinters configures the SpecificationLinter of a Specter instance.
func WithLinters(linters ...SpecificationLinter) Option {
return func(s *Specter) {
s.Linters = append(s.Linters, linters...)
}
}

// WithProcessors configures the SpecProcess of a Specter instance.
func WithProcessors(processors ...SpecificationProcessor) Option {
return func(s *Specter) {
Expand Down

0 comments on commit 700e35d

Please sign in to comment.